Skip to content

Commit 44d77dd

Browse files
committed
utils: handle ipv6 hosts when splitting URLs
Closes #10699 Signed-off-by: Daniel Moran <[email protected]>
1 parent 70494d7 commit 44d77dd

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

src/flb_utils.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ static char *flb_utils_copy_host_sds(const char *string, int pos_init, int pos_e
11541154
if (string[pos_end-1] != ']') {
11551155
return NULL;
11561156
}
1157-
return flb_sds_create_len(string + pos_init + 1, pos_end - 1);
1157+
return flb_sds_create_len(string + pos_init + 1, pos_end - 2);
11581158
}
11591159
else {
11601160
return flb_sds_create_len(string + pos_init, pos_end);
@@ -1171,6 +1171,7 @@ int flb_utils_url_split(const char *in_url, char **out_protocol,
11711171
char *p;
11721172
char *tmp;
11731173
char *sep;
1174+
char *bracket = NULL;
11741175

11751176
/* Protocol */
11761177
p = strstr(in_url, "://");
@@ -1192,7 +1193,14 @@ int flb_utils_url_split(const char *in_url, char **out_protocol,
11921193

11931194
/* Check for first '/' */
11941195
sep = strchr(p, '/');
1195-
tmp = strchr(p, ':');
1196+
if (p[0] == '[') {
1197+
bracket = strchr(p, ']');
1198+
}
1199+
if (bracket) {
1200+
tmp = strchr(bracket, ':');
1201+
} else {
1202+
tmp = strchr(p, ':');
1203+
}
11961204

11971205
/* Validate port separator is found before the first slash */
11981206
if (sep && tmp) {
@@ -1267,6 +1275,7 @@ int flb_utils_url_split_sds(const flb_sds_t in_url, flb_sds_t *out_protocol,
12671275
char *p = NULL;
12681276
char *tmp = NULL;
12691277
char *sep = NULL;
1278+
char *bracket = NULL;
12701279

12711280
/* Protocol */
12721281
p = strstr(in_url, "://");
@@ -1288,7 +1297,14 @@ int flb_utils_url_split_sds(const flb_sds_t in_url, flb_sds_t *out_protocol,
12881297

12891298
/* Check for first '/' */
12901299
sep = strchr(p, '/');
1291-
tmp = strchr(p, ':');
1300+
if (p[0] == '[') {
1301+
bracket = strchr(p, ']');
1302+
}
1303+
if (bracket) {
1304+
tmp = strchr(bracket, ':');
1305+
} else {
1306+
tmp = strchr(p, ':');
1307+
}
12921308

12931309
/* Validate port separator is found before the first slash */
12941310
if (sep && tmp) {

tests/internal/aws_credentials_http.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,80 @@ static void test_http_provider_eks_with_token_file()
589589
cleanup_test(provider, config);
590590
}
591591

592+
static void test_http_provider_eks_ipv6()
593+
{
594+
struct flb_aws_provider *provider;
595+
struct flb_aws_credentials *creds;
596+
struct flb_config *config;
597+
int ret;
598+
599+
/* tests validation of valid ipv6 local loopback IP */
600+
setenv("AWS_CONTAINER_CREDENTIALS_FULL_URI", "http://[fd00:ec2::23]/iam_credentials/pod1", 1);
601+
setenv("AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE", TEST_AUTHORIZATION_TOKEN_FILE, 1);
602+
603+
setup_test(FLB_AWS_CLIENT_MOCK(
604+
response(
605+
expect(URI, "/iam_credentials/pod1"),
606+
expect(METHOD, FLB_HTTP_GET),
607+
expect(HEADER, "Authorization", "local-http-credential-server-authorization-token"),
608+
set(STATUS, 200),
609+
set(PAYLOAD, "{\n \"Code\" : \"Success\",\n \"LastUpdated\" : \"2021-09-16T18:29:09Z\",\n"
610+
" \"Type\" : \"AWS-HMAC\",\n \"AccessKeyId\" : \"XACCESSEKSXXX\",\n \"SecretAccessKey\""
611+
" : \"XSECRETEKSXXXXXXXXXXXXXX\",\n \"Token\" : \"XTOKENEKSXXXXXXXXXXXXXXX==\",\n"
612+
" \"Expiration\" : \"3021-09-17T00:41:00Z\"\n}"),
613+
set(PAYLOAD_SIZE, 257)
614+
),
615+
response(
616+
expect(URI, "/iam_credentials/pod1"),
617+
expect(METHOD, FLB_HTTP_GET),
618+
expect(HEADER, "Authorization", "local-http-credential-server-authorization-token"),
619+
set(STATUS, 200),
620+
set(PAYLOAD, "{\n \"Code\" : \"Success\",\n \"LastUpdated\" : \"2021-09-16T18:29:09Z\",\n"
621+
" \"Type\" : \"AWS-HMAC\",\n \"AccessKeyId\" : \"YACCESSEKSXXX\",\n \"SecretAccessKey\""
622+
" : \"YSECRETEKSXXXXXXXXXXXXXX\",\n \"Token\" : \"YTOKENEKSXXXXXXXXXXXXXXX==\",\n"
623+
" \"Expiration\" : \"3021-09-17T00:41:00Z\"\n}"),
624+
set(PAYLOAD_SIZE, 257)
625+
)
626+
), &provider, &config);
627+
628+
flb_time_msleep(1000);
629+
630+
/* Repeated calls to get credentials should return the same set */
631+
creds = provider->provider_vtable->get_credentials(provider);
632+
TEST_ASSERT(creds != NULL);
633+
TEST_CHECK(strcmp("XACCESSEKSXXX", creds->access_key_id) == 0);
634+
TEST_CHECK(strcmp("XSECRETEKSXXXXXXXXXXXXXX", creds->secret_access_key) == 0);
635+
TEST_CHECK(strcmp("XTOKENEKSXXXXXXXXXXXXXXX==", creds->session_token) == 0);
636+
637+
flb_aws_credentials_destroy(creds);
638+
639+
/* Retrieve from cache */
640+
creds = provider->provider_vtable->get_credentials(provider);
641+
TEST_ASSERT(creds != NULL);
642+
TEST_CHECK(strcmp("XACCESSEKSXXX", creds->access_key_id) == 0);
643+
TEST_CHECK(strcmp("XSECRETEKSXXXXXXXXXXXXXX", creds->secret_access_key) == 0);
644+
TEST_CHECK(strcmp("XTOKENEKSXXXXXXXXXXXXXXX==", creds->session_token) == 0);
645+
646+
flb_aws_credentials_destroy(creds);
647+
648+
/* refresh should return 0 (success) */
649+
ret = provider->provider_vtable->refresh(provider);
650+
TEST_CHECK(ret == 0);
651+
652+
/* Retrieve refreshed credentials from cache */
653+
creds = provider->provider_vtable->get_credentials(provider);
654+
TEST_ASSERT(creds != NULL);
655+
TEST_CHECK(strcmp("YACCESSEKSXXX", creds->access_key_id) == 0);
656+
TEST_CHECK(strcmp("YSECRETEKSXXXXXXXXXXXXXX", creds->secret_access_key) == 0);
657+
TEST_CHECK(strcmp("YTOKENEKSXXXXXXXXXXXXXXX==", creds->session_token) == 0);
658+
659+
flb_aws_credentials_destroy(creds);
660+
661+
/* Check we have exhausted our response list */
662+
TEST_CHECK(flb_aws_client_mock_generator_count_unused_requests() == 0);
663+
664+
cleanup_test(provider, config);
665+
}
592666

593667
static void test_http_provider_https_endpoint()
594668
{
@@ -766,5 +840,6 @@ TEST_LIST = {
766840
{ "test_http_provider_server_failure", test_http_provider_server_failure},
767841
{ "test_http_validator_invalid_host", test_http_validator_invalid_host},
768842
{ "test_http_validator_invalid_port", test_http_validator_invalid_port},
843+
{ "test_http_provider_eks_ipv6", test_http_provider_eks_ipv6},
769844
{ 0 }
770845
};

tests/internal/utils.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct url_check url_checks[] = {
3636
{0, "https://fluentbit.io:1234/", "https", "fluentbit.io", "1234", "/"},
3737
{0, "https://fluentbit.io:1234/v", "https", "fluentbit.io", "1234", "/v"},
3838
{-1, "://", NULL, NULL, NULL, NULL},
39+
{0, "http://[fd00:ec2::23]/v1/credentials", "http", "fd00:ec2::23", "80", "/v1/credentials"},
40+
{0, "https://[::192.9.5.5]:1234/v", "https", "::192.9.5.5", "1234", "/v"}
3941
};
4042

4143
void test_url_split_sds()

0 commit comments

Comments
 (0)