@@ -53,9 +53,9 @@ static int http_credentials_request(struct flb_aws_provider_http
5353 * implementation );
5454
5555
56- /*
57- If the resolved URI’s scheme is HTTPS, its hostname may be used in the request.
58- Otherwise, implementations MUST fail to resolve when the URI hostname
56+ /*
57+ If the resolved URI’s scheme is HTTPS, its hostname may be used in the request.
58+ Otherwise, implementations MUST fail to resolve when the URI hostname
5959does not satisfy any of the following conditions:
6060
6161is within the loopback CIDR (IPv4 127.0.0.0/8, IPv6 ::1/128)
@@ -67,7 +67,7 @@ static int validate_http_credential_uri(flb_sds_t protocol, flb_sds_t host)
6767 return 0 ;
6868 } else if (strncmp (host , "127." , 4 ) == 0 ||
6969 strncmp (host , ECS_CREDENTIALS_HOST , ECS_CREDENTIALS_HOST_LEN ) == 0 ||
70- strncmp (host , EKS_CREDENTIALS_HOST , EKS_CREDENTIALS_HOST_LEN ) == 0 ||
70+ strncmp (host , EKS_CREDENTIALS_HOST , EKS_CREDENTIALS_HOST_LEN ) == 0 ||
7171 strstr (host , "::1" ) != NULL ||
7272 strstr (host , "fd00:ec2::23" ) != NULL ||
7373 strstr (host , "fe80:" ) != NULL ) {
@@ -263,7 +263,6 @@ struct flb_aws_provider *flb_endpoint_provider_create(struct flb_config *config,
263263 host , path );
264264
265265 provider = flb_calloc (1 , sizeof (struct flb_aws_provider ));
266-
267266 if (!provider ) {
268267 flb_errno ();
269268 return NULL ;
@@ -322,9 +321,7 @@ struct flb_aws_provider *flb_endpoint_provider_create(struct flb_config *config,
322321 * with the ECS credentials endpoint.
323322 */
324323struct flb_aws_provider * flb_http_provider_create (struct flb_config * config ,
325- struct
326- flb_aws_client_generator
327- * generator )
324+ struct flb_aws_client_generator * generator )
328325{
329326 flb_sds_t path = NULL ;
330327 flb_sds_t protocol = NULL ;
@@ -351,24 +348,27 @@ struct flb_aws_provider *flb_http_provider_create(struct flb_config *config,
351348 flb_free (host );
352349 return NULL ;
353350 }
354- } else if (full_uri && strlen (full_uri ) > 0 ) {
351+ }
352+ else if (full_uri && strlen (full_uri ) > 0 ) {
355353 ret = flb_utils_url_split_sds (full_uri , & protocol , & host , & port_sds , & path );
356354 if (ret < 0 ) {
357355 return NULL ;
358356 }
357+
359358 insecure = strncmp (protocol , "http" , 4 ) == 0 ? FLB_TRUE : FLB_FALSE ;
360359 ret = validate_http_credential_uri (protocol , host );
361360 if (ret < 0 ) {
362361 flb_error ("[aws credentials] %s must be set to an https:// address or a link local IP address."
363- " Found protocol=%s, host=%s, port=%s, path=%s" ,
362+ " Found protocol=%s, host=%s, port=%s, path=%s" ,
364363 AWS_CREDENTIALS_FULL_URI , protocol , host , port_sds , path );
365364 flb_sds_destroy (protocol );
366365 flb_sds_destroy (host );
367366 flb_sds_destroy (port_sds );
368367 flb_sds_destroy (path );
369368 return NULL ;
370369 }
371- } else {
370+ }
371+ else {
372372 flb_debug ("[aws_credentials] Not initializing ECS/EKS HTTP Provider because"
373373 " %s and %s is not set" , AWS_CREDENTIALS_PATH , AWS_CREDENTIALS_FULL_URI );
374374 return NULL ;
@@ -378,7 +378,7 @@ struct flb_aws_provider *flb_http_provider_create(struct flb_config *config,
378378 port = atoi (port_sds );
379379 if (port == 0 ) {
380380 flb_error ("[aws credentials] invalid port: %s must be set to an https:// address or a link local IP address."
381- " Found protocol=%s, host=%s, port=%s, path=%s" ,
381+ " Found protocol=%s, host=%s, port=%s, path=%s" ,
382382 AWS_CREDENTIALS_FULL_URI , protocol , host , port_sds , path );
383383 flb_sds_destroy (protocol );
384384 flb_sds_destroy (host );
@@ -388,6 +388,9 @@ struct flb_aws_provider *flb_http_provider_create(struct flb_config *config,
388388 }
389389 }
390390
391+ flb_sds_destroy (port_sds );
392+ flb_sds_destroy (protocol );
393+
391394 return flb_endpoint_provider_create (config , host , path , port , insecure , generator );
392395
393396}
@@ -412,14 +415,30 @@ static int http_credentials_request(struct flb_aws_provider_http
412415 struct flb_aws_client * client = implementation -> client ;
413416 struct flb_http_client * c = NULL ;
414417 int ret ;
418+ char * tmp ;
415419 char * auth_token = NULL ;
416420 size_t auth_token_size = 0 ;
417421 char * auth_token_path = NULL ;
418422
419423 auth_token_path = getenv (AUTH_TOKEN_FILE_ENV_VAR );
420- auth_token = getenv (AUTH_TOKEN_ENV_VAR );
424+ tmp = getenv (AUTH_TOKEN_ENV_VAR );
425+ if (tmp ) {
426+ auth_token = flb_malloc (strlen (tmp ) + 1 );
427+ if (!auth_token ) {
428+ flb_errno ();
429+ return -1 ;
430+ }
431+ strcpy (auth_token , tmp );
432+ }
433+
421434 if (auth_token_path != NULL && strlen (auth_token_path ) > 0 ) {
422435 flb_debug ("[aws] reading authorization token from %s" , auth_token_path );
436+
437+ if (auth_token ) {
438+ flb_free (auth_token );
439+ auth_token = NULL ;
440+ }
441+
423442 ret = flb_read_file (auth_token_path , & auth_token ,
424443 & auth_token_size );
425444 if (ret < 0 ) {
@@ -441,6 +460,11 @@ static int http_credentials_request(struct flb_aws_provider_http
441460 NULL , 0 );
442461 }
443462
463+ if (auth_token ) {
464+ flb_free (auth_token );
465+ auth_token = NULL ;
466+ }
467+
444468 if (!c || c -> resp .status != 200 ) {
445469 flb_debug ("[aws_credentials] http credentials request failed" );
446470 if (c ) {
@@ -450,15 +474,22 @@ static int http_credentials_request(struct flb_aws_provider_http
450474 }
451475 flb_http_client_destroy (c );
452476 }
477+ if (auth_token ) {
478+ flb_free (auth_token );
479+ }
453480 return -1 ;
454481 }
455482
483+
456484 response = c -> resp .payload ;
457485 response_len = c -> resp .payload_size ;
458486
459487 creds = flb_parse_http_credentials (response , response_len , & expiration );
460488 if (!creds ) {
461489 flb_http_client_destroy (c );
490+ if (auth_token ) {
491+ flb_free (auth_token );
492+ }
462493 return -1 ;
463494 }
464495
@@ -469,6 +500,7 @@ static int http_credentials_request(struct flb_aws_provider_http
469500 implementation -> creds = creds ;
470501 implementation -> next_refresh = expiration - FLB_AWS_REFRESH_WINDOW ;
471502 flb_http_client_destroy (c );
503+
472504 return 0 ;
473505}
474506
0 commit comments