-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
stackdriver.c
3228 lines (2810 loc) · 107 KB
/
stackdriver.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- 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_http_client.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_oauth2.h>
#include <fluent-bit/flb_regex.h>
#include <fluent-bit/flb_pthread.h>
#include <fluent-bit/flb_crypto.h>
#include <fluent-bit/flb_hash.h>
#include <fluent-bit/flb_base64.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_ra_key.h>
#include <fluent-bit/flb_record_accessor.h>
#include <fluent-bit/flb_log_event_decoder.h>
#include <fluent-bit/flb_gzip.h>
#include <msgpack.h>
#include "gce_metadata.h"
#include "stackdriver.h"
#include "stackdriver_conf.h"
#include "stackdriver_operation.h"
#include "stackdriver_source_location.h"
#include "stackdriver_http_request.h"
#include "stackdriver_timestamp.h"
#include "stackdriver_helper.h"
#include "stackdriver_resource_types.h"
pthread_key_t oauth2_type;
pthread_key_t oauth2_token;
pthread_key_t oauth2_token_expires;
static void oauth2_cache_exit(void *ptr)
{
if (ptr) {
flb_sds_destroy(ptr);
}
}
static void oauth2_cache_free_expiration(void *ptr)
{
if (ptr) {
flb_free(ptr);
}
}
static void oauth2_cache_init()
{
/* oauth2 pthread key */
pthread_key_create(&oauth2_type, oauth2_cache_exit);
pthread_key_create(&oauth2_token, oauth2_cache_exit);
pthread_key_create(&oauth2_token_expires, oauth2_cache_free_expiration);
}
/* Set oauth2 type and token in pthread keys */
static void oauth2_cache_set(char *type, char *token, time_t expires)
{
flb_sds_t tmp;
time_t *tmp_expires;
/* oauth2 type */
tmp = pthread_getspecific(oauth2_type);
if (tmp) {
flb_sds_destroy(tmp);
}
tmp = flb_sds_create(type);
pthread_setspecific(oauth2_type, tmp);
/* oauth2 access token */
tmp = pthread_getspecific(oauth2_token);
if (tmp) {
flb_sds_destroy(tmp);
}
tmp = flb_sds_create(token);
pthread_setspecific(oauth2_token, tmp);
/* oauth2 access token expiration */
tmp_expires = pthread_getspecific(oauth2_token_expires);
if (tmp_expires) {
flb_free(tmp_expires);
}
tmp_expires = flb_calloc(1, sizeof(time_t));
if (!tmp_expires) {
flb_errno();
return;
}
*tmp_expires = expires;
pthread_setspecific(oauth2_token_expires, tmp_expires);
}
/* By using pthread keys cached values, compose the authorizatoin token */
static time_t oauth2_cache_get_expiration()
{
time_t *expires = pthread_getspecific(oauth2_token_expires);
if (expires) {
return *expires;
}
return 0;
}
/* By using pthread keys cached values, compose the authorizatoin token */
static flb_sds_t oauth2_cache_to_token()
{
flb_sds_t type;
flb_sds_t token;
flb_sds_t output;
type = pthread_getspecific(oauth2_type);
if (!type) {
return NULL;
}
output = flb_sds_create(type);
if (!output) {
return NULL;
}
token = pthread_getspecific(oauth2_token);
flb_sds_printf(&output, " %s", token);
return output;
}
/*
* Base64 Encoding in JWT must:
*
* - remove any trailing padding '=' character
* - replace '+' with '-'
* - replace '/' with '_'
*
* ref: https://www.rfc-editor.org/rfc/rfc7515.txt Appendix C
*/
int jwt_base64_url_encode(unsigned char *out_buf, size_t out_size,
unsigned char *in_buf, size_t in_size,
size_t *olen)
{
int i;
size_t len;
int result;
/* do normal base64 encoding */
result = flb_base64_encode((unsigned char *) out_buf, out_size - 1,
&len, in_buf, in_size);
if (result != 0) {
return -1;
}
/* Replace '+' and '/' characters */
for (i = 0; i < len && out_buf[i] != '='; i++) {
if (out_buf[i] == '+') {
out_buf[i] = '-';
}
else if (out_buf[i] == '/') {
out_buf[i] = '_';
}
}
/* Now 'i' becomes the new length */
*olen = i;
return 0;
}
static int jwt_encode(char *payload, char *secret,
char **out_signature, size_t *out_size,
struct flb_stackdriver *ctx)
{
int ret;
int len;
int buf_size;
size_t olen;
char *buf;
char *sigd;
char *headers = "{\"alg\": \"RS256\", \"typ\": \"JWT\"}";
unsigned char sha256_buf[32] = {0};
flb_sds_t out;
unsigned char sig[256] = {0};
size_t sig_len;
buf_size = (strlen(payload) + strlen(secret)) * 2;
buf = flb_malloc(buf_size);
if (!buf) {
flb_errno();
return -1;
}
/* Encode header */
len = strlen(headers);
ret = flb_base64_encode((unsigned char *) buf, buf_size - 1,
&olen, (unsigned char *) headers, len);
if (ret != 0) {
flb_free(buf);
return ret;
}
/* Create buffer to store JWT */
out = flb_sds_create_size(2048);
if (!out) {
flb_errno();
flb_free(buf);
return -1;
}
/* Append header */
flb_sds_cat(out, buf, olen);
flb_sds_cat(out, ".", 1);
/* Encode Payload */
len = strlen(payload);
jwt_base64_url_encode((unsigned char *) buf, buf_size,
(unsigned char *) payload, len, &olen);
/* Append Payload */
flb_sds_cat(out, buf, olen);
/* do sha256() of base64(header).base64(payload) */
ret = flb_hash_simple(FLB_HASH_SHA256,
(unsigned char *) out, flb_sds_len(out),
sha256_buf, sizeof(sha256_buf));
if (ret != FLB_CRYPTO_SUCCESS) {
flb_plg_error(ctx->ins, "error hashing token");
flb_free(buf);
flb_sds_destroy(out);
return -1;
}
len = strlen(secret);
sig_len = sizeof(sig);
ret = flb_crypto_sign_simple(FLB_CRYPTO_PRIVATE_KEY,
FLB_CRYPTO_PADDING_PKCS1,
FLB_HASH_SHA256,
(unsigned char *) secret, len,
sha256_buf, sizeof(sha256_buf),
sig, &sig_len);
if (ret != FLB_CRYPTO_SUCCESS) {
flb_plg_error(ctx->ins, "error creating RSA context");
flb_free(buf);
flb_sds_destroy(out);
return -1;
}
sigd = flb_malloc(2048);
if (!sigd) {
flb_errno();
flb_free(buf);
flb_sds_destroy(out);
return -1;
}
jwt_base64_url_encode((unsigned char *) sigd, 2048, sig, 256, &olen);
flb_sds_cat(out, ".", 1);
flb_sds_cat(out, sigd, olen);
*out_signature = out;
*out_size = flb_sds_len(out);
flb_free(buf);
flb_free(sigd);
return 0;
}
/* Create a new oauth2 context and get a oauth2 token */
static int get_oauth2_token(struct flb_stackdriver *ctx)
{
int ret;
char *token;
char *sig_data;
size_t sig_size;
time_t issued;
time_t expires;
char payload[1024];
flb_oauth2_payload_clear(ctx->o);
/* In case of using metadata server, fetch token from there */
if (ctx->metadata_server_auth) {
return gce_metadata_read_token(ctx);
}
/* JWT encode for oauth2 */
issued = time(NULL);
expires = issued + FLB_STD_TOKEN_REFRESH;
snprintf(payload, sizeof(payload) - 1,
"{\"iss\": \"%s\", \"scope\": \"%s\", "
"\"aud\": \"%s\", \"exp\": %lu, \"iat\": %lu}",
ctx->client_email, FLB_STD_SCOPE,
FLB_STD_AUTH_URL,
expires, issued);
/* Compose JWT signature */
ret = jwt_encode(payload, ctx->private_key, &sig_data, &sig_size, ctx);
if (ret != 0) {
flb_plg_error(ctx->ins, "JWT signature generation failed");
return -1;
}
flb_plg_debug(ctx->ins, "JWT signature:\n%s", sig_data);
ret = flb_oauth2_payload_append(ctx->o,
"grant_type", -1,
"urn%3Aietf%3Aparams%3Aoauth%3A"
"grant-type%3Ajwt-bearer", -1);
if (ret == -1) {
flb_plg_error(ctx->ins, "error appending oauth2 params");
flb_sds_destroy(sig_data);
return -1;
}
ret = flb_oauth2_payload_append(ctx->o,
"assertion", -1,
sig_data, sig_size);
if (ret == -1) {
flb_plg_error(ctx->ins, "error appending oauth2 params");
flb_sds_destroy(sig_data);
return -1;
}
flb_sds_destroy(sig_data);
/* Retrieve access token */
token = flb_oauth2_token_get(ctx->o);
if (!token) {
flb_plg_error(ctx->ins, "error retrieving oauth2 access token");
return -1;
}
return 0;
}
static flb_sds_t get_google_token(struct flb_stackdriver *ctx)
{
int ret = 0;
flb_sds_t output = NULL;
time_t cached_expiration = 0;
ret = pthread_mutex_trylock(&ctx->token_mutex);
if (ret == EBUSY) {
/*
* If the routine is locked we just use our pre-cached values and
* compose the expected authorization value.
*
* If the routine fails it will return NULL and the caller will just
* issue a FLB_RETRY.
*/
output = oauth2_cache_to_token();
cached_expiration = oauth2_cache_get_expiration();
if (time(NULL) >= cached_expiration) {
return output;
} else {
/*
* Cached token is expired. Wait on lock to use up-to-date token
* by either waiting for it to be refreshed or refresh it ourselves.
*/
flb_plg_info(ctx->ins, "Cached token is expired. Waiting on lock.");
ret = pthread_mutex_lock(&ctx->token_mutex);
}
}
if (ret != 0) {
flb_plg_error(ctx->ins, "error locking mutex");
return NULL;
}
if (flb_oauth2_token_expired(ctx->o) == FLB_TRUE) {
ret = get_oauth2_token(ctx);
}
/* Copy string to prevent race conditions (get_oauth2 can free the string) */
if (ret == 0) {
/* Update pthread keys cached values */
oauth2_cache_set(ctx->o->token_type, ctx->o->access_token, ctx->o->expires);
/* Compose outgoing buffer using cached values */
output = oauth2_cache_to_token();
}
if (pthread_mutex_unlock(&ctx->token_mutex)){
flb_plg_error(ctx->ins, "error unlocking mutex");
if (output) {
flb_sds_destroy(output);
}
return NULL;
}
return output;
}
void replace_prefix_dot(flb_sds_t s, int tag_prefix_len)
{
int i;
int str_len;
char c;
if (!s) {
return;
}
str_len = flb_sds_len(s);
if (tag_prefix_len > str_len) {
flb_error("[output] tag_prefix shouldn't be longer than local_resource_id");
return;
}
for (i = 0; i < tag_prefix_len; i++) {
c = s[i];
if (c == '.') {
s[i] = '_';
}
}
}
static int extract_msgpack_obj_from_msgpack_map(msgpack_object_map *root,
char *name, int size,
msgpack_object_type object_type,
msgpack_object *val)
{
int i;
msgpack_object key;
if (root == NULL) {
return -1;
}
for (i = 0; i < root->size; i++) {
key = root->ptr[i].key;
if (key.type != MSGPACK_OBJECT_STR) {
continue;
}
if (key.via.str.size == size
&& strncmp(key.via.str.ptr, name, size) == 0) {
*val = root->ptr[i].val;
if (val->type != object_type) {
return -1;
}
return 0;
}
}
return -1;
}
static flb_sds_t get_str_value_from_msgpack_map(msgpack_object_map map,
const char *key, int key_size)
{
int ret;
msgpack_object v;
flb_sds_t ptr = NULL;
/* convert msgpack_object_map to msgpack_object */
ret = extract_msgpack_obj_from_msgpack_map(&map, (char*) key, key_size,
MSGPACK_OBJECT_STR, &v);
if (ret == 0) {
ptr = flb_sds_create_len(v.via.str.ptr, v.via.str.size);
}
return ptr;
}
/* parse_monitored_resource is to extract the monitoired resource labels
* from "logging.googleapis.com/monitored_resource" in log data
* and append to 'resource'/'labels' in log entry.
* Monitored resource type is already read from resource field in stackdriver
* output plugin configuration parameters.
*
* The structure of monitored_resource is:
* {
* "logging.googleapis.com/monitored_resource": {
* "labels": {
* "resource_label": <label_value>,
* }
* }
* }
* See https://cloud.google.com/logging/docs/api/v2/resource-list#resource-types
* for required labels for each monitored resource.
*/
static int parse_monitored_resource(struct flb_stackdriver *ctx, const void *data, size_t bytes, msgpack_packer *mp_pck)
{
int ret = -1;
msgpack_object *obj;
struct flb_log_event_decoder log_decoder;
struct flb_log_event log_event;
ret = flb_log_event_decoder_init(&log_decoder, (char *) data, bytes);
if (ret != FLB_EVENT_DECODER_SUCCESS) {
flb_plg_error(ctx->ins,
"Log event decoder initialization error : %d", ret);
return -1;
}
while ((ret = flb_log_event_decoder_next(
&log_decoder,
&log_event)) == FLB_EVENT_DECODER_SUCCESS) {
obj = log_event.body;
msgpack_object_kv *kv = obj->via.map.ptr;
msgpack_object_kv *const kvend = obj->via.map.ptr + obj->via.map.size;
for (; kv < kvend; ++kv) {
if (kv->val.type == MSGPACK_OBJECT_MAP && kv->key.type == MSGPACK_OBJECT_STR
&& strncmp (MONITORED_RESOURCE_KEY, kv->key.via.str.ptr, kv->key.via.str.size) == 0) {
msgpack_object subobj = kv->val;
msgpack_object_kv *p = subobj.via.map.ptr;
msgpack_object_kv *pend = subobj.via.map.ptr + subobj.via.map.size;
for (; p < pend; ++p) {
if (p->key.type != MSGPACK_OBJECT_STR || p->val.type != MSGPACK_OBJECT_MAP) {
continue;
}
if (strncmp("labels", p->key.via.str.ptr, p->key.via.str.size) == 0) {
msgpack_object labels = p->val;
msgpack_object_kv *q = labels.via.map.ptr;
msgpack_object_kv *qend = labels.via.map.ptr + labels.via.map.size;
int fields = 0;
for (; q < qend; ++q) {
if (q->key.type != MSGPACK_OBJECT_STR || q->val.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "Key and value should be string in the %s/labels", MONITORED_RESOURCE_KEY);
}
++fields;
}
if (fields > 0) {
msgpack_pack_map(mp_pck, fields);
q = labels.via.map.ptr;
for (; q < qend; ++q) {
if (q->key.type != MSGPACK_OBJECT_STR || q->val.type != MSGPACK_OBJECT_STR) {
continue;
}
flb_plg_debug(ctx->ins, "[%s] found in the payload", MONITORED_RESOURCE_KEY);
msgpack_pack_str(mp_pck, q->key.via.str.size);
msgpack_pack_str_body(mp_pck, q->key.via.str.ptr, q->key.via.str.size);
msgpack_pack_str(mp_pck, q->val.via.str.size);
msgpack_pack_str_body(mp_pck, q->val.via.str.ptr, q->val.via.str.size);
}
flb_log_event_decoder_destroy(&log_decoder);
return 0;
}
}
}
}
}
}
flb_log_event_decoder_destroy(&log_decoder);
flb_plg_debug(ctx->ins, "[%s] not found in the payload", MONITORED_RESOURCE_KEY);
return ret;
}
/*
* Given a local_resource_id, split the content using the proper separator generating
* a linked list to store the spliited string
*/
static struct mk_list *parse_local_resource_id_to_list(char *local_resource_id, char *type)
{
int ret = -1;
int max_split = -1;
int len_k8s_container;
int len_k8s_node;
int len_k8s_pod;
struct mk_list *list;
len_k8s_container = sizeof(K8S_CONTAINER) - 1;
len_k8s_node = sizeof(K8S_NODE) - 1;
len_k8s_pod = sizeof(K8S_POD) - 1;
/* Allocate list head */
list = flb_malloc(sizeof(struct mk_list));
if (!list) {
flb_errno();
return NULL;
}
mk_list_init(list);
/* Determinate the max split value based on type */
if (strncmp(type, K8S_CONTAINER, len_k8s_container) == 0) {
/* including the prefix of tag */
max_split = 4;
}
else if (strncmp(type, K8S_NODE, len_k8s_node) == 0) {
max_split = 2;
}
else if (strncmp(type, K8S_POD, len_k8s_pod) == 0) {
max_split = 3;
}
/* The local_resource_id is splitted by '.' */
ret = flb_slist_split_string(list, local_resource_id, '.', max_split);
if (ret == -1 || mk_list_size(list) != max_split) {
flb_error("error parsing local_resource_id [%s] for type %s", local_resource_id, type);
flb_slist_destroy(list);
flb_free(list);
return NULL;
}
return list;
}
/*
* extract_local_resource_id():
* - extract the value from "logging.googleapis.com/local_resource_id" field
* - if local_resource_id is missing from the payLoad, use the tag of the log
*/
static int extract_local_resource_id(const void *data, size_t bytes,
struct flb_stackdriver *ctx, const char *tag) {
msgpack_object_map map;
flb_sds_t local_resource_id;
struct flb_log_event_decoder log_decoder;
struct flb_log_event log_event;
int ret;
ret = flb_log_event_decoder_init(&log_decoder, (char *) data, bytes);
if (ret != FLB_EVENT_DECODER_SUCCESS) {
flb_plg_error(ctx->ins,
"Log event decoder initialization error : %d", ret);
return -1;
}
if ((ret = flb_log_event_decoder_next(
&log_decoder,
&log_event)) == FLB_EVENT_DECODER_SUCCESS) {
map = log_event.body->via.map;
local_resource_id = get_str_value_from_msgpack_map(map, LOCAL_RESOURCE_ID_KEY,
LEN_LOCAL_RESOURCE_ID_KEY);
if (local_resource_id == NULL) {
/* if local_resource_id is not found, use the tag of the log */
flb_plg_debug(ctx->ins, "local_resource_id not found, "
"tag [%s] is assigned for local_resource_id", tag);
local_resource_id = flb_sds_create(tag);
}
/* we need to create up the local_resource_id from previous log */
if (ctx->local_resource_id) {
flb_sds_destroy(ctx->local_resource_id);
}
ctx->local_resource_id = flb_sds_create(local_resource_id);
flb_sds_destroy(local_resource_id);
ret = 0;
}
else {
flb_plg_error(ctx->ins, "failed to unpack data");
ret = -1;
}
flb_log_event_decoder_destroy(&log_decoder);
return ret;
}
/*
* set_monitored_resource_labels():
* - use the extracted local_resource_id to assign the label keys for different
* resource types that are specified in the configuration of stackdriver_out plugin
*/
static int set_monitored_resource_labels(struct flb_stackdriver *ctx, char *type)
{
int ret = -1;
int first = FLB_TRUE;
int counter = 0;
int len_k8s_container;
int len_k8s_node;
int len_k8s_pod;
size_t prefix_len = 0;
struct local_resource_id_list *ptr;
struct mk_list *list = NULL;
struct mk_list *head;
flb_sds_t new_local_resource_id;
if (!ctx->local_resource_id) {
flb_plg_error(ctx->ins, "local_resource_is is not assigned");
return -1;
}
len_k8s_container = sizeof(K8S_CONTAINER) - 1;
len_k8s_node = sizeof(K8S_NODE) - 1;
len_k8s_pod = sizeof(K8S_POD) - 1;
prefix_len = flb_sds_len(ctx->tag_prefix);
if (flb_sds_casecmp(ctx->tag_prefix, ctx->local_resource_id, prefix_len) != 0) {
flb_plg_error(ctx->ins, "tag_prefix [%s] doesn't match the prefix of"
" local_resource_id [%s]", ctx->tag_prefix,
ctx->local_resource_id);
return -1;
}
new_local_resource_id = flb_sds_create_len(ctx->local_resource_id,
flb_sds_len(ctx->local_resource_id));
replace_prefix_dot(new_local_resource_id, prefix_len - 1);
if (strncmp(type, K8S_CONTAINER, len_k8s_container) == 0) {
list = parse_local_resource_id_to_list(new_local_resource_id, K8S_CONTAINER);
if (!list) {
goto error;
}
/* iterate through the list */
mk_list_foreach(head, list) {
ptr = mk_list_entry(head, struct local_resource_id_list, _head);
if (first) {
first = FLB_FALSE;
continue;
}
/* Follow the order of fields in local_resource_id */
if (counter == 0) {
if (ctx->namespace_name) {
flb_sds_destroy(ctx->namespace_name);
}
ctx->namespace_name = flb_sds_create(ptr->val);
}
else if (counter == 1) {
if (ctx->pod_name) {
flb_sds_destroy(ctx->pod_name);
}
ctx->pod_name = flb_sds_create(ptr->val);
}
else if (counter == 2) {
if (ctx->container_name) {
flb_sds_destroy(ctx->container_name);
}
ctx->container_name = flb_sds_create(ptr->val);
}
counter++;
}
if (!ctx->namespace_name || !ctx->pod_name || !ctx->container_name) {
goto error;
}
}
else if (strncmp(type, K8S_NODE, len_k8s_node) == 0) {
list = parse_local_resource_id_to_list(new_local_resource_id, K8S_NODE);
if (!list) {
goto error;
}
mk_list_foreach(head, list) {
ptr = mk_list_entry(head, struct local_resource_id_list, _head);
if (first) {
first = FLB_FALSE;
continue;
}
if (ptr != NULL) {
if (ctx->node_name) {
flb_sds_destroy(ctx->node_name);
}
ctx->node_name = flb_sds_create(ptr->val);
}
}
if (!ctx->node_name) {
goto error;
}
}
else if (strncmp(type, K8S_POD, len_k8s_pod) == 0) {
list = parse_local_resource_id_to_list(new_local_resource_id, K8S_POD);
if (!list) {
goto error;
}
mk_list_foreach(head, list) {
ptr = mk_list_entry(head, struct local_resource_id_list, _head);
if (first) {
first = FLB_FALSE;
continue;
}
/* Follow the order of fields in local_resource_id */
if (counter == 0) {
if (ctx->namespace_name) {
flb_sds_destroy(ctx->namespace_name);
}
ctx->namespace_name = flb_sds_create(ptr->val);
}
else if (counter == 1) {
if (ctx->pod_name) {
flb_sds_destroy(ctx->pod_name);
}
ctx->pod_name = flb_sds_create(ptr->val);
}
counter++;
}
if (!ctx->namespace_name || !ctx->pod_name) {
goto error;
}
}
ret = 0;
if (list) {
flb_slist_destroy(list);
flb_free(list);
}
flb_sds_destroy(new_local_resource_id);
return ret;
error:
if (list) {
flb_slist_destroy(list);
flb_free(list);
}
if (strncmp(type, K8S_CONTAINER, len_k8s_container) == 0) {
if (ctx->namespace_name) {
flb_sds_destroy(ctx->namespace_name);
}
if (ctx->pod_name) {
flb_sds_destroy(ctx->pod_name);
}
if (ctx->container_name) {
flb_sds_destroy(ctx->container_name);
}
}
else if (strncmp(type, K8S_NODE, len_k8s_node) == 0) {
if (ctx->node_name) {
flb_sds_destroy(ctx->node_name);
}
}
else if (strncmp(type, K8S_POD, len_k8s_pod) == 0) {
if (ctx->namespace_name) {
flb_sds_destroy(ctx->namespace_name);
}
if (ctx->pod_name) {
flb_sds_destroy(ctx->pod_name);
}
}
flb_sds_destroy(new_local_resource_id);
return -1;
}
static int is_tag_match_regex(struct flb_stackdriver *ctx,
const char *tag, int tag_len)
{
int ret;
int tag_prefix_len;
int len_to_be_matched;
const char *tag_str_to_be_matcheds;
tag_prefix_len = flb_sds_len(ctx->tag_prefix);
if (tag_len > tag_prefix_len &&
flb_sds_cmp(ctx->tag_prefix, tag, tag_prefix_len) != 0) {
return 0;
}
tag_str_to_be_matcheds = tag + tag_prefix_len;
len_to_be_matched = tag_len - tag_prefix_len;
ret = flb_regex_match(ctx->regex,
(unsigned char *) tag_str_to_be_matcheds,
len_to_be_matched);
/* 1 -> match; 0 -> doesn't match; < 0 -> error */
return ret;
}
static int is_local_resource_id_match_regex(struct flb_stackdriver *ctx)
{
int ret;
int prefix_len;
int len_to_be_matched;
const char *str_to_be_matcheds;
if (!ctx->local_resource_id) {
flb_plg_warn(ctx->ins, "local_resource_id not found in the payload");
return -1;
}
prefix_len = flb_sds_len(ctx->tag_prefix);
str_to_be_matcheds = ctx->local_resource_id + prefix_len;
len_to_be_matched = flb_sds_len(ctx->local_resource_id) - prefix_len;
ret = flb_regex_match(ctx->regex,
(unsigned char *) str_to_be_matcheds,
len_to_be_matched);
/* 1 -> match; 0 -> doesn't match; < 0 -> error */
return ret;
}
static void cb_results(const char *name, const char *value,
size_t vlen, void *data);
/*
* extract_resource_labels_from_regex(4) will only be called if the
* tag or local_resource_id field matches the regex rule
*/
static int extract_resource_labels_from_regex(struct flb_stackdriver *ctx,
const char *tag, int tag_len,
int from_tag)
{
int ret = 1;
int prefix_len;
int len_to_be_matched;
int local_resource_id_len;
const char *str_to_be_matcheds;
struct flb_regex_search result;
prefix_len = flb_sds_len(ctx->tag_prefix);
if (from_tag == FLB_TRUE) {
local_resource_id_len = tag_len;
str_to_be_matcheds = tag + prefix_len;
}
else {
// this will be called only if the payload contains local_resource_id
local_resource_id_len = flb_sds_len(ctx->local_resource_id);
str_to_be_matcheds = ctx->local_resource_id + prefix_len;
}
len_to_be_matched = local_resource_id_len - prefix_len;
ret = flb_regex_do(ctx->regex, str_to_be_matcheds, len_to_be_matched, &result);
if (ret <= 0) {
flb_plg_warn(ctx->ins, "invalid pattern for given value %s when"
" extracting resource labels", str_to_be_matcheds);
return -1;
}
flb_regex_parse(ctx->regex, &result, cb_results, ctx);
return ret;
}
static int process_local_resource_id(struct flb_stackdriver *ctx,
const char *tag, int tag_len, char *type)
{
int ret;
// parsing local_resource_id from tag takes higher priority
if (is_tag_match_regex(ctx, tag, tag_len) > 0) {
ret = extract_resource_labels_from_regex(ctx, tag, tag_len, FLB_TRUE);
}
else if (is_local_resource_id_match_regex(ctx) > 0) {
ret = extract_resource_labels_from_regex(ctx, tag, tag_len, FLB_FALSE);
}
else {
ret = set_monitored_resource_labels(ctx, type);
}
return ret;
}
/*
* get_payload_labels
* - Iterate throught the original payload (obj) and find out the entry that matches
* the labels_key
* - Used to convert all labels under labels_key to root-level `labels` field
*/
static msgpack_object *get_payload_labels(struct flb_stackdriver *ctx, msgpack_object *obj)
{
int i;
int len;
msgpack_object_kv *kv = NULL;
if (!obj || obj->type != MSGPACK_OBJECT_MAP) {
return NULL;
}
len = flb_sds_len(ctx->labels_key);
for (i = 0; i < obj->via.map.size; i++) {
kv = &obj->via.map.ptr[i];
if (flb_sds_casecmp(ctx->labels_key, kv->key.via.str.ptr, len) == 0) {