-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
stackdriver_conf.c
677 lines (598 loc) · 22.3 KB
/
stackdriver_conf.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2024 The Fluent Bit Authors
*
* 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.
*/
#include <fluent-bit/flb_output_plugin.h>
#include <fluent-bit/flb_compat.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_unescape.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_jsmn.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_kv.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "gce_metadata.h"
#include "stackdriver.h"
#include "stackdriver_conf.h"
#include "stackdriver_resource_types.h"
static inline int key_cmp(const char *str, int len, const char *cmp) {
if (strlen(cmp) != len) {
return -1;
}
return strncasecmp(str, cmp, len);
}
static int read_credentials_file(const char *cred_file, struct flb_stackdriver *ctx)
{
int i;
int ret;
int key_len;
int val_len;
int tok_size = 32;
char *buf;
char *key;
char *val;
flb_sds_t tmp;
struct stat st;
jsmn_parser parser;
jsmntok_t *t;
jsmntok_t *tokens;
/* Validate credentials path */
ret = stat(cred_file, &st);
if (ret == -1) {
flb_errno();
flb_plg_error(ctx->ins, "cannot open credentials file: %s",
cred_file);
return -1;
}
if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
flb_plg_error(ctx->ins, "credentials file "
"is not a valid file: %s", cred_file);
return -1;
}
/* Read file content */
buf = mk_file_to_buffer(cred_file);
if (!buf) {
flb_plg_error(ctx->ins, "error reading credentials file: %s",
cred_file);
return -1;
}
/* Parse content */
jsmn_init(&parser);
tokens = flb_calloc(1, sizeof(jsmntok_t) * tok_size);
if (!tokens) {
flb_errno();
flb_free(buf);
return -1;
}
ret = jsmn_parse(&parser, buf, st.st_size, tokens, tok_size);
if (ret <= 0) {
flb_plg_error(ctx->ins, "invalid JSON credentials file: %s",
cred_file);
flb_free(buf);
flb_free(tokens);
return -1;
}
t = &tokens[0];
if (t->type != JSMN_OBJECT) {
flb_plg_error(ctx->ins, "invalid JSON map on file: %s",
cred_file);
flb_free(buf);
flb_free(tokens);
return -1;
}
/* Parse JSON tokens */
for (i = 1; i < ret; i++) {
t = &tokens[i];
if (t->type != JSMN_STRING) {
continue;
}
if (t->start == -1 || t->end == -1 || (t->start == 0 && t->end == 0)){
break;
}
/* Key */
key = buf + t->start;
key_len = (t->end - t->start);
/* Value */
i++;
t = &tokens[i];
val = buf + t->start;
val_len = (t->end - t->start);
if (key_cmp(key, key_len, "type") == 0) {
ctx->creds->type = flb_sds_create_len(val, val_len);
}
else if (key_cmp(key, key_len, "project_id") == 0) {
ctx->project_id = flb_sds_create_len(val, val_len);
}
else if (key_cmp(key, key_len, "private_key_id") == 0) {
ctx->creds->private_key_id = flb_sds_create_len(val, val_len);
}
else if (key_cmp(key, key_len, "private_key") == 0) {
tmp = flb_sds_create_len(val, val_len);
if (tmp) {
/* Unescape private key */
ctx->creds->private_key = flb_sds_create_size(val_len);
flb_unescape_string(tmp, flb_sds_len(tmp),
&ctx->creds->private_key);
flb_sds_destroy(tmp);
}
}
else if (key_cmp(key, key_len, "client_email") == 0) {
ctx->creds->client_email = flb_sds_create_len(val, val_len);
}
else if (key_cmp(key, key_len, "client_id") == 0) {
ctx->creds->client_id = flb_sds_create_len(val, val_len);
}
else if (key_cmp(key, key_len, "auth_uri") == 0) {
ctx->creds->auth_uri = flb_sds_create_len(val, val_len);
}
else if (key_cmp(key, key_len, "token_uri") == 0) {
ctx->creds->token_uri = flb_sds_create_len(val, val_len);
}
}
flb_free(buf);
flb_free(tokens);
return 0;
}
/*
* parse_key_value_list():
* - Parses an origin list of comma seperated string specifying key=value.
* - Appends the parsed key value pairs into the destination list.
* - Returns the length of the destination list.
*/
static int parse_key_value_list(struct flb_stackdriver *ctx,
struct mk_list *origin,
struct mk_list *dest,
int shouldTrim)
{
char *p;
flb_sds_t key;
flb_sds_t val;
struct flb_kv *kv;
struct mk_list *head;
struct flb_slist_entry *entry;
if (origin) {
mk_list_foreach(head, origin) {
entry = mk_list_entry(head, struct flb_slist_entry, _head);
p = strchr(entry->str, '=');
if (!p) {
flb_plg_error(ctx->ins, "invalid key value pair on '%s'",
entry->str);
return -1;
}
key = flb_sds_create_size((p - entry->str) + 1);
flb_sds_cat(key, entry->str, p - entry->str);
val = flb_sds_create(p + 1);
if (shouldTrim) {
flb_sds_trim(key);
flb_sds_trim(val);
}
if (!key || flb_sds_len(key) == 0) {
flb_plg_error(ctx->ins,
"invalid key value pair on '%s'",
entry->str);
return -1;
}
if (!val || flb_sds_len(val) == 0) {
flb_plg_error(ctx->ins,
"invalid key value pair on '%s'",
entry->str);
flb_sds_destroy(key);
return -1;
}
kv = flb_kv_item_create(dest, key, val);
flb_sds_destroy(key);
flb_sds_destroy(val);
if (!kv) {
return -1;
}
}
}
return mk_list_size(dest);
}
/*
* parse_configuration_labels
* - Parse labels set in configuration
* - Returns the number of configuration labels
*/
static int parse_configuration_labels(struct flb_stackdriver *ctx)
{
return parse_key_value_list(ctx, ctx->labels,
&ctx->config_labels, FLB_FALSE);
}
/*
* parse_resource_labels():
* - Parses resource labels set in configuration.
* - Returns the number of resource label mappings.
*/
static int parse_resource_labels(struct flb_stackdriver *ctx)
{
return parse_key_value_list(ctx, ctx->resource_labels,
&ctx->resource_labels_kvs, FLB_TRUE);
}
struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *ins,
struct flb_config *config)
{
int ret;
const char *tmp;
const char *backwards_compatible_env_var;
struct flb_stackdriver *ctx;
size_t http_request_key_size;
struct cmt_histogram_buckets *buckets;
/* Allocate config context */
ctx = flb_calloc(1, sizeof(struct flb_stackdriver));
if (!ctx) {
flb_errno();
return NULL;
}
ctx->ins = ins;
ctx->config = config;
ret = flb_output_config_map_set(ins, (void *)ctx);
if (ret == -1) {
flb_plg_error(ins, "unable to load configuration");
flb_free(ctx);
return NULL;
}
/* Compress (gzip) */
tmp = flb_output_get_property("compress", ins);
ctx->compress_gzip = FLB_FALSE;
if (tmp && strcasecmp(tmp, "gzip") == 0) {
ctx->compress_gzip = FLB_TRUE;
}
/* labels */
flb_kv_init(&ctx->config_labels);
ret = parse_configuration_labels((void *)ctx);
if (ret == -1) {
flb_plg_error(ins, "unable to parse configuration labels");
flb_kv_release(&ctx->config_labels);
flb_free(ctx);
return NULL;
}
/* resource labels */
flb_kv_init(&ctx->resource_labels_kvs);
ret = parse_resource_labels((void *)ctx);
if (ret == -1) {
flb_plg_error(ins, "unable to parse resource label list");
flb_kv_release(&ctx->resource_labels_kvs);
flb_free(ctx);
return NULL;
}
/* Lookup metadata server URL */
ctx->metadata_server = NULL;
tmp = flb_output_get_property("metadata_server", ins);
if (tmp == NULL) {
tmp = getenv("METADATA_SERVER");
if(tmp) {
if (ctx->env == NULL) {
ctx->env = flb_calloc(1, sizeof(struct flb_stackdriver_env));
if (ctx->env == NULL) {
flb_plg_error(ins, "unable to allocate env variables");
flb_free(ctx);
return NULL;
}
}
ctx->env->metadata_server = flb_sds_create(tmp);
ctx->metadata_server = ctx->env->metadata_server;
}
else {
ctx->metadata_server = flb_sds_create(FLB_STD_METADATA_SERVER);
}
}
else {
ctx->metadata_server = flb_sds_create(tmp);
}
flb_plg_info(ctx->ins, "metadata_server set to %s", ctx->metadata_server);
/* Lookup credentials file */
if (ctx->credentials_file == NULL) {
/*
* Use GOOGLE_APPLICATION_CREDENTIALS to fetch the credentials.
* GOOGLE_SERVICE_CREDENTIALS is checked for backwards compatibility.
*/
tmp = getenv("GOOGLE_APPLICATION_CREDENTIALS");
backwards_compatible_env_var = getenv("GOOGLE_SERVICE_CREDENTIALS");
if (tmp && backwards_compatible_env_var) {
flb_plg_warn(ctx->ins, "GOOGLE_APPLICATION_CREDENTIALS and "
"GOOGLE_SERVICE_CREDENTIALS are both defined. "
"Defaulting to GOOGLE_APPLICATION_CREDENTIALS");
}
if ((tmp || backwards_compatible_env_var) && (ctx->env == NULL)) {
ctx->env = flb_calloc(1, sizeof(struct flb_stackdriver_env));
if (ctx->env == NULL) {
flb_plg_error(ins, "unable to allocate env variables");
flb_free(ctx);
return NULL;
}
}
if (tmp) {
ctx->env->creds_file = flb_sds_create(tmp);
ctx->credentials_file = ctx->env->creds_file;
}
else if (backwards_compatible_env_var) {
ctx->env->creds_file = flb_sds_create(backwards_compatible_env_var);
ctx->credentials_file = ctx->env->creds_file;
}
}
if (ctx->credentials_file) {
ctx->creds = flb_calloc(1, sizeof(struct flb_stackdriver_oauth_credentials));
if (ctx->creds == NULL) {
flb_plg_error(ctx->ins, "unable to allocate credentials");
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
ret = read_credentials_file(ctx->credentials_file, ctx);
if (ret != 0) {
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
ctx->type = ctx->creds->type;
ctx->private_key_id = ctx->creds->private_key_id;
ctx->private_key = ctx->creds->private_key;
ctx->client_email = ctx->creds->client_email;
ctx->client_id = ctx->creds->client_id;
ctx->auth_uri = ctx->creds->auth_uri;
ctx->token_uri = ctx->creds->token_uri;
}
else {
/*
* If no credentials file has been defined, do manual lookup of the
* client email and the private key
*/
ctx->creds = flb_calloc(1, sizeof(struct flb_stackdriver_oauth_credentials));
if (ctx->creds == NULL) {
flb_plg_error(ctx->ins, "unable to allocate credentials");
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
/* Service Account Email */
if (ctx->client_email == NULL) {
tmp = getenv("SERVICE_ACCOUNT_EMAIL");
if (tmp) {
ctx->creds->client_email = flb_sds_create(tmp);
ctx->client_email = ctx->creds->client_email;
}
}
/* Service Account Secret */
if (ctx->private_key == NULL) {
tmp = getenv("SERVICE_ACCOUNT_SECRET");
if (tmp) {
ctx->creds->private_key = flb_sds_create(tmp);
ctx->private_key = ctx->creds->private_key;
}
}
}
/*
* If only client email has been provided, fetch token from
* the GCE metadata server.
*
* If no credentials have been provided, fetch token from the GCE
* metadata server for default account.
*/
if (!ctx->client_email && ctx->private_key) {
flb_plg_error(ctx->ins, "client_email is not defined");
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
if (!ctx->client_email) {
flb_plg_warn(ctx->ins, "client_email is not defined, using "
"a default one");
if (ctx->creds == NULL) {
ctx->creds = flb_calloc(1, sizeof(struct flb_stackdriver_oauth_credentials));
if (ctx->creds == NULL) {
flb_plg_error(ctx->ins, "unable to allocate credentials");
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
}
ctx->creds->client_email = flb_sds_create("default");
ctx->client_email = ctx->creds->client_email;
}
if (!ctx->private_key) {
flb_plg_warn(ctx->ins, "private_key is not defined, fetching "
"it from metadata server");
ctx->metadata_server_auth = true;
}
if (ctx->http_request_key) {
http_request_key_size = flb_sds_len(ctx->http_request_key);
if (http_request_key_size >= INT_MAX) {
flb_plg_error(ctx->ins, "http_request_key is too long");
flb_sds_destroy(ctx->http_request_key);
ctx->http_request_key = NULL;
ctx->http_request_key_size = 0;
} else {
ctx->http_request_key_size = http_request_key_size;
}
}
set_resource_type(ctx);
if (resource_api_has_required_labels(ctx) == FLB_FALSE) {
if (ctx->resource_type == RESOURCE_TYPE_K8S) {
if (!ctx->cluster_name || !ctx->cluster_location) {
flb_plg_error(ctx->ins, "missing k8s_cluster_name "
"or k8s_cluster_location in configuration");
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
}
else if (ctx->resource_type == RESOURCE_TYPE_GENERIC_NODE
|| ctx->resource_type == RESOURCE_TYPE_GENERIC_TASK) {
if (ctx->location == NULL) {
flb_plg_error(ctx->ins, "missing generic resource's location");
}
if (ctx->namespace_id == NULL) {
flb_plg_error(ctx->ins, "missing generic resource's namespace");
}
if (ctx->resource_type == RESOURCE_TYPE_GENERIC_NODE) {
if (ctx->node_id == NULL) {
flb_plg_error(ctx->ins, "missing generic_node's node_id");
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
}
else {
if (ctx->job == NULL) {
flb_plg_error(ctx->ins, "missing generic_task's job");
}
if (ctx->task_id == NULL) {
flb_plg_error(ctx->ins, "missing generic_task's task_id");
}
if (!ctx->job || !ctx->task_id) {
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
}
if (!ctx->location || !ctx->namespace_id) {
flb_stackdriver_conf_destroy(ctx);
return NULL;
}
}
}
if (ctx->tag_prefix == NULL && ctx->resource_type == RESOURCE_TYPE_K8S) {
/* allocate the flb_sds_t to tag_prefix_k8s so we can safely deallocate it */
ctx->tag_prefix_k8s = flb_sds_create(ctx->resource);
ctx->tag_prefix_k8s = flb_sds_cat(ctx->tag_prefix_k8s, ".", 1);
ctx->tag_prefix = ctx->tag_prefix_k8s;
}
/* Register metrics */
#ifdef FLB_HAVE_METRICS
ctx->cmt_successful_requests = cmt_counter_create(ins->cmt,
"fluentbit",
"stackdriver",
"successful_requests",
"Total number of successful "
"requests.",
1, (char *[]) {"name"});
ctx->cmt_failed_requests = cmt_counter_create(ins->cmt,
"fluentbit",
"stackdriver",
"failed_requests",
"Total number of failed "
"requests.",
1, (char *[]) {"name"});
ctx->cmt_requests_total = cmt_counter_create(ins->cmt,
"fluentbit",
"stackdriver",
"requests_total",
"Total number of requests.",
2, (char *[]) {"status", "name"});
ctx->cmt_proc_records_total = cmt_counter_create(ins->cmt,
"fluentbit",
"stackdriver",
"proc_records_total",
"Total number of processed records.",
3, (char *[]) {"grpc_code" ,"status", "name"});
ctx->cmt_retried_records_total = cmt_counter_create(ins->cmt,
"fluentbit",
"stackdriver",
"retried_records_total",
"Total number of retried records.",
2, (char *[]) {"status", "name"});
buckets = cmt_histogram_buckets_create(7, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0);
ctx->cmt_write_entries_latency = cmt_histogram_create(ins->cmt,
"fluentbit",
"stackdriver",
"write_entries_latency",
"Latency of Cloud Logging WriteLogEntries HTTP request.",
buckets,
1, (char *[]) {"name"});
/* OLD api */
flb_metrics_add(FLB_STACKDRIVER_SUCCESSFUL_REQUESTS,
"stackdriver_successful_requests", ctx->ins->metrics);
flb_metrics_add(FLB_STACKDRIVER_FAILED_REQUESTS,
"stackdriver_failed_requests", ctx->ins->metrics);
#endif
return ctx;
}
int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx)
{
if (!ctx) {
return -1;
}
if (ctx->creds) {
if (ctx->creds->type) {
flb_sds_destroy(ctx->creds->type);
}
if (ctx->creds->private_key_id) {
flb_sds_destroy(ctx->creds->private_key_id);
}
if (ctx->creds->private_key) {
flb_sds_destroy(ctx->creds->private_key);
}
if (ctx->creds->client_email) {
flb_sds_destroy(ctx->creds->client_email);
}
if (ctx->creds->client_id) {
flb_sds_destroy(ctx->creds->client_id);
}
if (ctx->creds->auth_uri) {
flb_sds_destroy(ctx->creds->auth_uri);
}
if (ctx->creds->token_uri) {
flb_sds_destroy(ctx->creds->token_uri);
}
flb_free(ctx->creds);
}
if (ctx->env) {
if (ctx->env->creds_file) {
flb_sds_destroy(ctx->env->creds_file);
}
if (ctx->env->metadata_server) {
flb_sds_destroy(ctx->env->metadata_server);
/*
* If ctx->env is not NULL,
* ctx->metadata_server points ctx->env->metadata_server.
*
* We set ctx->metadata_server to NULL to prevent double free.
*/
ctx->metadata_server = NULL;
}
flb_free(ctx->env);
}
if (ctx->metadata_server) {
flb_sds_destroy(ctx->metadata_server);
}
if (ctx->resource_type == RESOURCE_TYPE_K8S){
flb_sds_destroy(ctx->namespace_name);
flb_sds_destroy(ctx->pod_name);
flb_sds_destroy(ctx->container_name);
flb_sds_destroy(ctx->node_name);
flb_sds_destroy(ctx->local_resource_id);
}
if (ctx->metadata_server_auth) {
flb_sds_destroy(ctx->zone);
flb_sds_destroy(ctx->instance_id);
}
if (ctx->metadata_u) {
flb_upstream_destroy(ctx->metadata_u);
}
if (ctx->u) {
flb_upstream_destroy(ctx->u);
}
if (ctx->o) {
flb_oauth2_destroy(ctx->o);
}
if (ctx->regex) {
flb_regex_destroy(ctx->regex);
}
if (ctx->project_id) {
flb_sds_destroy(ctx->project_id);
}
if (ctx->tag_prefix_k8s) {
flb_sds_destroy(ctx->tag_prefix_k8s);
}
flb_kv_release(&ctx->config_labels);
flb_kv_release(&ctx->resource_labels_kvs);
flb_free(ctx);
return 0;
}