Allow S3 bucket used for BlueSky storage to be specified.
[bluesky.git] / bluesky / store-s3.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky-private.h"
15 #include "libs3.h"
16
17 /* Interface to Amazon S3 storage. */
18
19 typedef struct {
20     GThreadPool *thread_pool;
21     S3BucketContext bucket;
22     uint8_t encryption_key[CRYPTO_KEY_SIZE];
23 } S3Store;
24
25 typedef struct {
26     enum { S3_GET, S3_PUT } op;
27     gchar *key;
28     BlueSkyRCStr *data;
29 } S3Op;
30
31 struct get_info {
32     int success;
33     GString *buf;
34 };
35
36 struct put_info {
37     int success;
38     BlueSkyRCStr *val;
39     gint offset;
40 };
41
42 struct list_info {
43     int success;
44     char *last_entry;
45 };
46
47 static S3Status s3store_get_handler(int bufferSize, const char *buffer,
48                                     void *callbackData)
49 {
50     struct get_info *info = (struct get_info *)callbackData;
51     g_string_append_len(info->buf, buffer, bufferSize);
52     return S3StatusOK;
53 }
54
55 static int s3store_put_handler(int bufferSize, char *buffer,
56                                void *callbackData)
57 {
58     struct put_info *info = (struct put_info *)callbackData;
59     gint bytes = MIN(bufferSize, (int)(info->val->len - info->offset));
60     memcpy(buffer, (char *)info->val->data + info->offset, bytes);
61     info->offset += bytes;
62     return bytes;
63 }
64
65 static S3Status s3store_properties_callback(const S3ResponseProperties *properties,
66                                      void *callbackData)
67 {
68     return S3StatusOK;
69 }
70
71 static void s3store_response_callback(S3Status status,
72                                const S3ErrorDetails *errorDetails,
73                                void *callbackData)
74 {
75     struct get_info *info = (struct get_info *)callbackData;
76
77     if (status == 0) {
78         info->success = 1;
79     }
80
81     if (errorDetails != NULL && errorDetails->message != NULL) {
82         g_print("  Error message: %s\n", errorDetails->message);
83     }
84 }
85
86 static void s3store_task(gpointer a, gpointer s)
87 {
88     BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
89     S3Store *store = (S3Store *)s;
90
91     async->status = ASYNC_RUNNING;
92     async->exec_time = bluesky_now_hires();
93
94     if (async->op == STORE_OP_GET) {
95         struct get_info info;
96         info.buf = g_string_new("");
97         info.success = 0;
98
99         struct S3GetObjectHandler handler;
100         handler.responseHandler.propertiesCallback = s3store_properties_callback;
101         handler.responseHandler.completeCallback = s3store_response_callback;
102         handler.getObjectDataCallback = s3store_get_handler;
103
104         S3_get_object(&store->bucket, async->key, NULL, 0, 0, NULL,
105                       &handler, &info);
106
107         if (info.success) {
108             async->data = bluesky_string_new_from_gstring(info.buf);
109             async->result = 0;
110         } else {
111             g_string_free(info.buf, TRUE);
112         }
113
114     } else if (async->op == STORE_OP_PUT) {
115         struct put_info info;
116         info.val = async->data;
117         info.offset = 0;
118
119         struct S3PutObjectHandler handler;
120         handler.responseHandler.propertiesCallback
121             = s3store_properties_callback;
122         handler.responseHandler.completeCallback = s3store_response_callback;
123         handler.putObjectDataCallback = s3store_put_handler;
124
125         S3_put_object(&store->bucket, async->key, async->data->len, NULL, NULL,
126                       &handler, &info);
127
128         async->result = 0;
129     }
130
131     bluesky_store_async_mark_complete(async);
132     bluesky_store_async_unref(async);
133 }
134
135 static S3Status s3store_list_handler(int isTruncated,
136                                      const char *nextMarker,
137                                      int contentsCount,
138                                      const S3ListBucketContent *contents,
139                                      int commonPrefixesCount,
140                                      const char **commonPrefixes,
141                                      void *callbackData)
142 {
143     struct list_info *info = (struct list_info *)callbackData;
144     if (contentsCount > 0) {
145         g_free(info->last_entry);
146         info->last_entry = g_strdup(contents[contentsCount - 1].key);
147     }
148     return S3StatusOK;
149 }
150
151 static char *s3store_lookup_last(gpointer s, const char *prefix)
152 {
153     S3Store *store = (S3Store *)s;
154     struct list_info info = {0, NULL};
155
156     struct S3ListBucketHandler handler;
157     handler.responseHandler.propertiesCallback
158         = s3store_properties_callback;
159     handler.responseHandler.completeCallback = s3store_response_callback;
160     handler.listBucketCallback = s3store_list_handler;
161
162     char *marker = NULL;
163
164     S3_list_bucket(&store->bucket, prefix, marker, NULL, 1024, NULL, &handler, &info);
165
166     g_print("Last key: %s\n", info.last_entry);
167
168     return info.last_entry;
169 }
170
171 static gpointer s3store_new(const gchar *path)
172 {
173     S3Store *store = g_new(S3Store, 1);
174     store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
175                                            NULL);
176     if (path == NULL || strlen(path) == 0)
177         store->bucket.bucketName = "mvrable-bluesky";
178     else
179         store->bucket.bucketName = g_strdup(path);
180     store->bucket.protocol = S3ProtocolHTTP;
181     store->bucket.uriStyle = S3UriStylePath;
182     store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
183     store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
184
185     const char *key = getenv("BLUESKY_KEY");
186     if (key == NULL) {
187         g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
188         exit(1);
189     }
190
191     bluesky_crypt_hash_key(key, store->encryption_key);
192
193     g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
194             store->bucket.bucketName, store->bucket.accessKeyId, key);
195
196     return store;
197 }
198
199 static void s3store_destroy(gpointer store)
200 {
201     g_free(store);
202 }
203
204 static void s3store_submit(gpointer s, BlueSkyStoreAsync *async)
205 {
206     S3Store *store = (S3Store *)s;
207     g_return_if_fail(async->status == ASYNC_NEW);
208     g_return_if_fail(async->op != STORE_OP_NONE);
209
210     switch (async->op) {
211     case STORE_OP_GET:
212     case STORE_OP_PUT:
213         async->status = ASYNC_PENDING;
214         bluesky_store_async_ref(async);
215         g_thread_pool_push(store->thread_pool, async, NULL);
216         break;
217
218     default:
219         g_warning("Uknown operation type for S3Store: %d\n", async->op);
220         bluesky_store_async_mark_complete(async);
221         break;
222     }
223 }
224
225 static void s3store_cleanup(gpointer store, BlueSkyStoreAsync *async)
226 {
227     GString *buf = (GString *)async->store_private;
228
229     if (buf != NULL) {
230         g_string_free(buf, TRUE);
231         async->store_private = NULL;
232     }
233 }
234
235 static BlueSkyStoreImplementation store_impl = {
236     .create = s3store_new,
237     .destroy = s3store_destroy,
238     .submit = s3store_submit,
239     .cleanup = s3store_cleanup,
240     .lookup_last = s3store_lookup_last,
241 };
242
243 void bluesky_store_init_s3(void)
244 {
245     S3_initialize(NULL, S3_INIT_ALL);
246     bluesky_store_register(&store_impl, "s3");
247 }