-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.c
438 lines (413 loc) · 10.8 KB
/
s3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* Support for Anansi S3/RADOS caches
*
* Author: Mo McRoberts <[email protected]>
*
* Copyright (c) 2014-2016 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_anansi-plugin.h"
static AWSS3BUCKET *get_bucket(const char *name);
static AWSS3BUCKET *add_bucket(struct bucketinfo_struct *info, const char *name);
static int urldecode(char *dest, const char *src, size_t len);
static size_t ingest_write(char *ptr, size_t size, size_t nemb, void *userdata);
static struct bucketinfo_struct bucketinfo[8];
static size_t maxbuckets = 8;
int
anansi_s3_init(struct anansi_context_struct *context, URI_INFO *info)
{
char *p;
const char *t;
if(!info->host)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": S3 cache URI does not include a bucket name\n");
return -1;
}
if(info->query)
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": configured S3 cache URI includes a query-string which will be ignored\n");
}
if(info->fragment)
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": configured S3 cache URI includes a fragment which will be ignored\n");
}
context->bucket = anansi_s3_bucket_create(context, info);
if(!context->bucket)
{
return -1;
}
if(info->path)
{
aws_s3_set_basepath(context->bucket, info->path);
twine_logf(LOG_NOTICE, PLUGIN_NAME ": cache URI is <s3://%s%s>\n", info->host, info->path);
}
else
{
twine_logf(LOG_NOTICE, PLUGIN_NAME ": cache URI is <s3://%s/>\n", info->host);
}
return 0;
}
/* Fetch an object from either an explicit <s3://...> URI, or from an
* <anansi:///...> URI where we already know that the configured cache URI is
* on S3.
*/
TWINEGRAPH *
anansi_s3_fetch(struct anansi_context_struct *context, const char *uristr, URI_INFO *info)
{
AWSS3BUCKET *bucket;
TWINEGRAPH *graph;
json_t *dict, *loc, *headers;
const char *location;
int r;
if(!info->host || !info->path)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": <%s> is not a valid S3 resource URL\n", uristr);
return NULL;
}
dict = NULL;
if(!strcasecmp(info->scheme, "s3"))
{
bucket = anansi_s3_bucket(context, info);
if(!bucket)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to obtain S3 bucket object for <%s>\n", uristr);
return NULL;
}
}
else
{
bucket = context->bucket;
}
r = anansi_s3_ingest_info_bucket(context, bucket, info->path, &dict);
if(r || !dict || json_typeof(dict) != JSON_OBJECT)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to fetch cache information for <%s>\n", uristr);
if(dict)
{
json_decref(dict);
}
return NULL;
}
location = NULL;
loc = json_object_get(dict, "content_location");
if(loc)
{
location = json_string_value(loc);
}
if(!loc || !location)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": object has no Content-Location\n");
json_decref(dict);
return NULL;
}
graph = twine_graph_create(context->twine, location);
if(!graph)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": failed to create new Twine graph object\n");
json_decref(dict);
return NULL;
}
r = anansi_s3_ingest_payload_bucket(context, graph, bucket, info->path, location);
if(r)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to ingest payload for <%s>\n", uristr);
twine_graph_destroy(graph);
json_decref(dict);
return NULL;
}
headers = json_object_get(dict, "headers");
if(headers && json_typeof(headers) == JSON_OBJECT)
{
if(anansi_handler_process_headers(context, graph, headers, location))
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to process headers\n");
twine_graph_destroy(graph);
json_decref(dict);
return NULL;
}
}
json_decref(dict);
return graph;
}
/* Create a new AWSS3BUCKET without adding it to the bucket MRU list */
AWSS3BUCKET *
anansi_s3_bucket_create(struct anansi_context_struct *context, URI_INFO *info)
{
AWSS3BUCKET *bucket;
const char *t;
char *p;
bucket = aws_s3_create(info->host);
if(!bucket)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": failed to create bucket object for <s3://%s>\n", info->host);
return NULL;
}
aws_s3_set_logger(bucket, twine_vlogf);
if(context->s3endpoint)
{
aws_s3_set_endpoint(bucket, context->s3endpoint);
}
if(info->auth && strlen(info->auth))
{
t = strchr(info->auth, ':');
if(t)
{
p = (char *) calloc(1, t - info->auth + 1);
if(!p)
{
aws_s3_destroy(bucket);
return NULL;
}
urldecode(p, info->auth, t - info->auth);
p[t - info->auth] = 0;
aws_s3_set_access(bucket, p);
free(p);
t++;
p = (char *) calloc(1, strlen(t) + 1);
if(!p)
{
aws_s3_destroy(bucket);
return NULL;
}
urldecode(p, t, strlen(t));
aws_s3_set_secret(bucket, p);
free(p);
}
}
else
{
if(context->s3access)
{
aws_s3_set_access(bucket, context->s3access);
}
else
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": no S3 access key has been provided in the configuration\n");
}
if(context->s3secret)
{
aws_s3_set_secret(bucket, context->s3secret);
}
else
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": no S3 secret key has been provided in the configuration\n");
}
}
return bucket;
}
/* Create or locate an AWSS3BUCKET object for the supplied URI */
AWSS3BUCKET *
anansi_s3_bucket(struct anansi_context_struct *context, URI_INFO *info)
{
size_t c;
for(c = 0; c < maxbuckets; c++)
{
if(bucketinfo[c].name && !strcasecmp(bucketinfo[c].name, info->host))
{
return bucketinfo[c].bucket;
}
}
for(c = 0; c < maxbuckets; c++)
{
if(!bucketinfo[c].name)
{
return anansi_s3_bucket_add(context, &(bucketinfo[c]), info);
}
}
/* Recycle the oldest entry */
free(bucketinfo[0].name);
aws_s3_destroy(bucketinfo[0].bucket);
memmove(&(bucketinfo[0]), &(bucketinfo[1]), sizeof(struct bucketinfo_struct) * maxbuckets - 1);
return anansi_s3_bucket_add(context, &(bucketinfo[maxbuckets - 1]), info);
}
/* Create a new AWSS3BUCKET, populate a bucketinfo_struct with the information,
* and return it.
*/
AWSS3BUCKET *
anansi_s3_bucket_add(struct anansi_context_struct *context, struct bucketinfo_struct *bucketinfo, URI_INFO *info)
{
char *t;
memset(bucketinfo, 0, sizeof(struct bucketinfo_struct));
bucketinfo->name = strdup(info->host);
if(!bucketinfo->name)
{
return NULL;
}
bucketinfo->bucket = anansi_s3_bucket_create(context, info);
if(!bucketinfo->bucket)
{
free(bucketinfo->name);
bucketinfo->name = NULL;
return NULL;
}
return bucketinfo->bucket;
}
int
anansi_s3_ingest_info_bucket(struct anansi_context_struct *context, AWSS3BUCKET *bucket, const char *resource, json_t **dict)
{
AWSREQUEST *req;
CURL *ch;
struct ingestinfo_struct info;
long status;
int r, e;
char *urlbuf;
json_error_t err;
(void) context;
r = 0;
urlbuf = (char *) malloc(strlen(resource) + 6);
if(!urlbuf)
{
return -1;
}
strcpy(urlbuf, resource);
strcat(urlbuf, ".json");
memset(&info, 0, sizeof(struct ingestinfo_struct));
req = aws_s3_request_create(bucket, urlbuf, "GET");
ch = aws_request_curl(req);
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, ingest_write);
curl_easy_setopt(ch, CURLOPT_WRITEDATA, (void *) &info);
curl_easy_setopt(ch, CURLOPT_VERBOSE, context->s3verbose);
if((e = aws_request_perform(req)) || !info.buf)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to request resource <%s>: %s\n", urlbuf, curl_easy_strerror(e));
free(urlbuf);
free(info.buf);
aws_request_destroy(req);
return -1;
}
status = 0;
curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &status);
if(status != 200)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to request resource <%s> with status %ld\n", urlbuf, status);
free(urlbuf);
free(info.buf);
aws_request_destroy(req);
return -1;
}
*dict = json_loads(info.buf, 0, &err);
if(!*dict)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to parse '%s.json': %s at (%d, %d)\n", resource, err.text, err.line, err.column);
r = -1;
}
else if(json_typeof(*dict) != JSON_OBJECT)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": '%s.json': not a JSON object\n", resource);
json_decref(*dict);
r = -1;
}
free(urlbuf);
free(info.buf);
aws_request_destroy(req);
return r;
}
int
anansi_s3_ingest_payload_bucket(struct anansi_context_struct *context, TWINEGRAPH *graph, AWSS3BUCKET *bucket, const char *resource, const char *location)
{
AWSREQUEST *req;
CURL *ch;
struct ingestinfo_struct info;
long status;
int r, e;
char *type;
memset(&info, 0, sizeof(struct ingestinfo_struct));
req = aws_s3_request_create(bucket, resource, "GET");
ch = aws_request_curl(req);
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, ingest_write);
curl_easy_setopt(ch, CURLOPT_WRITEDATA, (void *) &info);
curl_easy_setopt(ch, CURLOPT_VERBOSE, context->s3verbose);
if((e = aws_request_perform(req)) || !info.buf)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to request resource <%s>: %s\n", resource, curl_easy_strerror(e));
free(info.buf);
aws_request_destroy(req);
return -1;
}
status = 0;
curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &status);
if(status != 200)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to request resource <%s> with status %ld\n", resource, status);
free(info.buf);
aws_request_destroy(req);
return -1;
}
type = NULL;
curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &type);
if(!type)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to request resource '%s': no Content-Type in response\n", resource, status);
free(info.buf);
aws_request_destroy(req);
return -1;
}
r = anansi_handler_process_payload(context, graph, info.buf, info.pos, type, location);
free(info.buf);
aws_request_destroy(req);
return r;
}
static size_t
ingest_write(char *ptr, size_t size, size_t nemb, void *userdata)
{
struct ingestinfo_struct *info;
char *p;
info = (struct ingestinfo_struct *) userdata;
size *= nemb;
if(size >= (info->size - info->pos))
{
p = (char *) realloc(info->buf, info->size + size + 1);
if(!p)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": failed to reallocate buffer to %lu bytes\n", (unsigned long) (info->size + size + 1));
return 0;
}
info->buf = p;
info->size += size;
}
memcpy(&(info->buf[info->pos]), ptr, size);
info->pos += size;
info->buf[info->pos] = 0;
return size;
}
static int
urldecode(char *dest, const char *src, size_t len)
{
long l;
char hbuf[3];
while(*src && len)
{
if(*src == '%' && len >= 3 && isxdigit(src[1]) && isxdigit(src[2]))
{
hbuf[0] = src[1];
hbuf[1] = src[2];
hbuf[2] = 0;
l = strtol(hbuf, NULL, 16);
*dest = (char) ((unsigned char) l);
dest++;
src += 3;
len -= 3;
}
else
{
*dest = *src;
dest++;
src++;
len--;
}
}
return 0;
}