@@ -483,7 +483,7 @@ static ssize_t parse_hec_payload_json(struct flb_splunk *ctx, flb_sds_t tag,
483
483
return 0 ;
484
484
}
485
485
486
- static int validate_auth_header (struct flb_splunk * ctx , struct mk_http_request * request )
486
+ static int validate_auth_header (struct flb_splunk * ctx , struct mk_http_request * request , struct flb_splunk_tokens * * matched_token_out )
487
487
{
488
488
int ret = 0 ;
489
489
struct mk_list * head ;
@@ -517,6 +517,7 @@ static int validate_auth_header(struct flb_splunk *ctx, struct mk_http_request *
517
517
authorization ,
518
518
splunk_token -> length ) == 0 ) {
519
519
flb_sds_destroy (authorization );
520
+ * matched_token_out = splunk_token ;
520
521
521
522
return SPLUNK_AUTH_SUCCESS ;
522
523
}
@@ -716,6 +717,8 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
716
717
off_t diff ;
717
718
flb_sds_t tag ;
718
719
struct mk_http_header * header ;
720
+ struct flb_splunk_tokens * matched_token = NULL ;
721
+ flb_sds_t tag_from_token = NULL ;
719
722
720
723
if (request -> uri .data [0 ] != '/' ) {
721
724
send_response (conn , 400 , "error: invalid request\n" );
@@ -818,7 +821,7 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
818
821
819
822
/* Under services/collector endpoints are required for
820
823
* authentication if provided splunk_token */
821
- ret = validate_auth_header (ctx , request );
824
+ ret = validate_auth_header (ctx , request , & matched_token );
822
825
if (ret < 0 ){
823
826
send_response (conn , 401 , "error: unauthorized\n" );
824
827
if (ret == SPLUNK_AUTH_MISSING_CRED ) {
@@ -834,6 +837,12 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
834
837
return -1 ;
835
838
}
836
839
840
+ /* Tokens can be configured to map to a particular tag */
841
+ if (matched_token != NULL && matched_token -> map_to_tag != NULL ) {
842
+ tag_from_token = matched_token -> map_to_tag ;
843
+ tag = tag_from_token ;
844
+ }
845
+
837
846
/* If the request contains chunked transfer encoded data, decode it */ \
838
847
if (mk_http_parser_is_content_chunked (& session -> parser )) {
839
848
ret = mk_http_parser_chunked_decode (& session -> parser ,
@@ -845,7 +854,10 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
845
854
flb_plg_error (ctx -> ins , "failed to decode chunked data" );
846
855
send_response (conn , 400 , "error: invalid chunked data\n" );
847
856
848
- flb_sds_destroy (tag );
857
+ /* Free the tag only if it was a temporarily-allocated/calculated one */
858
+ if (tag_from_token == NULL ) {
859
+ flb_sds_destroy (tag );
860
+ }
849
861
mk_mem_free (uri );
850
862
851
863
return -1 ;
@@ -879,7 +891,10 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
879
891
880
892
ret = process_hec_payload (ctx , conn , tag , session , request );
881
893
if (ret == -2 ) {
882
- flb_sds_destroy (tag );
894
+ /* Free the tag only if it was a temporarily-allocated/calculated one */
895
+ if (tag_from_token == NULL ) {
896
+ flb_sds_destroy (tag );
897
+ }
883
898
mk_mem_free (uri );
884
899
885
900
if (out_chunked ) {
@@ -899,7 +914,10 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
899
914
else {
900
915
send_response (conn , 400 , "error: invalid HTTP endpoint\n" );
901
916
902
- flb_sds_destroy (tag );
917
+ /* Free the tag only if it was a temporarily-allocated/calculated one */
918
+ if (tag_from_token == NULL ) {
919
+ flb_sds_destroy (tag );
920
+ }
903
921
mk_mem_free (uri );
904
922
905
923
if (out_chunked ) {
@@ -914,7 +932,10 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
914
932
else {
915
933
/* HEAD, PUT, PATCH, and DELETE methods are prohibited to use.*/
916
934
917
- flb_sds_destroy (tag );
935
+ /* Free the tag only if it was a temporarily-allocated/calculated one */
936
+ if (tag_from_token == NULL ) {
937
+ flb_sds_destroy (tag );
938
+ }
918
939
mk_mem_free (uri );
919
940
920
941
if (out_chunked ) {
@@ -927,7 +948,10 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
927
948
return -1 ;
928
949
}
929
950
930
- flb_sds_destroy (tag );
951
+ /* Free the tag only if it was a temporarily-allocated/calculated one */
952
+ if (tag_from_token == NULL ) {
953
+ flb_sds_destroy (tag );
954
+ }
931
955
mk_mem_free (uri );
932
956
933
957
if (out_chunked ) {
@@ -1022,7 +1046,7 @@ static int send_json_message_response_ng(struct flb_http_response *response,
1022
1046
return 0 ;
1023
1047
}
1024
1048
1025
- static int validate_auth_header_ng (struct flb_splunk * ctx , struct flb_http_request * request )
1049
+ static int validate_auth_header_ng (struct flb_splunk * ctx , struct flb_http_request * request , struct flb_splunk_tokens * * matched_token_out )
1026
1050
{
1027
1051
struct mk_list * tmp ;
1028
1052
struct mk_list * head ;
@@ -1049,6 +1073,7 @@ static int validate_auth_header_ng(struct flb_splunk *ctx, struct flb_http_reque
1049
1073
if (strncasecmp (splunk_token -> header ,
1050
1074
auth_header ,
1051
1075
splunk_token -> length ) == 0 ) {
1076
+ * matched_token_out = splunk_token ;
1052
1077
return SPLUNK_AUTH_SUCCESS ;
1053
1078
}
1054
1079
}
@@ -1147,6 +1172,8 @@ int splunk_prot_handle_ng(struct flb_http_request *request,
1147
1172
struct flb_splunk * context ;
1148
1173
int ret = -1 ;
1149
1174
flb_sds_t tag ;
1175
+ struct flb_splunk_tokens * matched_token = NULL ;
1176
+ flb_sds_t tag_from_token = NULL ;
1150
1177
1151
1178
context = (struct flb_splunk * ) response -> stream -> user_data ;
1152
1179
@@ -1176,7 +1203,7 @@ int splunk_prot_handle_ng(struct flb_http_request *request,
1176
1203
1177
1204
/* Under services/collector endpoints are required for
1178
1205
* authentication if provided splunk_token */
1179
- ret = validate_auth_header_ng (context , request );
1206
+ ret = validate_auth_header_ng (context , request , & matched_token );
1180
1207
1181
1208
if (ret < 0 ) {
1182
1209
send_response_ng (response , 401 , "error: unauthorized\n" );
@@ -1201,10 +1228,16 @@ int splunk_prot_handle_ng(struct flb_http_request *request,
1201
1228
return -1 ;
1202
1229
}
1203
1230
1204
- tag = flb_sds_create (context -> ins -> tag );
1205
-
1206
- if (tag == NULL ) {
1207
- return -1 ;
1231
+ /* Tokens can be configured to map to a particular tag */
1232
+ if (matched_token != NULL && matched_token -> map_to_tag != NULL ) {
1233
+ tag_from_token = matched_token -> map_to_tag ;
1234
+ tag = tag_from_token ;
1235
+ }
1236
+ else {
1237
+ tag = flb_sds_create (context -> ins -> tag );
1238
+ if (tag == NULL ) {
1239
+ return -1 ;
1240
+ }
1208
1241
}
1209
1242
1210
1243
if (strcasecmp (request -> path , "/services/collector/raw/1.0" ) == 0 ||
@@ -1236,7 +1269,9 @@ int splunk_prot_handle_ng(struct flb_http_request *request,
1236
1269
send_response_ng (response , 400 , "error: invalid HTTP endpoint\n" );
1237
1270
ret = -1 ;
1238
1271
}
1239
-
1240
- flb_sds_destroy (tag );
1272
+ /* Free the tag only if it was a temporarily-allocated/calculated one */
1273
+ if (tag_from_token == NULL ) {
1274
+ flb_sds_destroy (tag );
1275
+ }
1241
1276
return ret ;
1242
1277
}
0 commit comments