-
Notifications
You must be signed in to change notification settings - Fork 2k
aws: support AWS_EC2_METADATA_SERVICE_ENDPOINT for custom IMDS endpoints #12151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4369431
a981de4
8601506
15e973f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -232,6 +232,14 @@ struct flb_aws_provider *flb_ec2_provider_create(struct flb_config *config, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct flb_aws_provider_ec2 *implementation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct flb_aws_provider *provider; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct flb_upstream *upstream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *endpoint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_t host = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_t port_str = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_t protocol = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_t path = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const char *use_host; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int use_port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int ret; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider = flb_calloc(1, sizeof(struct flb_aws_provider)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -253,17 +261,57 @@ struct flb_aws_provider *flb_ec2_provider_create(struct flb_config *config, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider->provider_vtable = &ec2_provider_vtable; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider->implementation = implementation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream = flb_upstream_create(config, FLB_AWS_IMDS_HOST, FLB_AWS_IMDS_PORT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FLB_IO_TCP, NULL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Check for custom IMDS endpoint */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_host = FLB_AWS_IMDS_HOST; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_port = FLB_AWS_IMDS_PORT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int use_custom_endpoint = FLB_FALSE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = getenv(AWS_EC2_METADATA_SERVICE_ENDPOINT_ENV); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (endpoint && strlen(endpoint) > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ret = flb_utils_url_split_sds(endpoint, &protocol, &host, &port_str, &path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ret >= 0 && host) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_host = host; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_custom_endpoint = FLB_TRUE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (port_str) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_port = atoi(port_str); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (use_port <= 0 || use_port > 65535) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_port = FLB_AWS_IMDS_PORT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_info("[aws_credentials] Using custom IMDS endpoint: %s:%d", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_host, use_port); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_destroy(protocol); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_destroy(port_str); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_destroy(path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+264
to
+287
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Silent fallback when the custom endpoint URL fails to parse. If 🛠️ Proposed fix to log on parse failure endpoint = getenv(AWS_EC2_METADATA_SERVICE_ENDPOINT_ENV);
if (endpoint && strlen(endpoint) > 0) {
ret = flb_utils_url_split_sds(endpoint, &protocol, &host, &port_str, &path);
if (ret >= 0 && host) {
use_host = host;
use_custom_endpoint = FLB_TRUE;
if (port_str) {
use_port = atoi(port_str);
if (use_port <= 0 || use_port > 65535) {
use_port = FLB_AWS_IMDS_PORT;
}
}
flb_info("[aws_credentials] Using custom IMDS endpoint: %s:%d",
use_host, use_port);
}
+ else {
+ flb_error("[aws_credentials] invalid %s value: %s, using default IMDS endpoint",
+ AWS_EC2_METADATA_SERVICE_ENDPOINT_ENV, endpoint);
+ }
flb_sds_destroy(protocol);
flb_sds_destroy(port_str);
flb_sds_destroy(path);
}📝 Committable suggestion
Suggested change
🧰 Tools🪛 ast-grep (0.44.1)[warning] 275-275: Avoid (atoi-no-error-detection-c) 🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream = flb_upstream_create(config, use_host, use_port, FLB_IO_TCP, NULL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+264
to
+289
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🌐 Web query:
💡 Result: Yes, the flb_upstream_create_url function in Fluent Bit correctly handles the https scheme and automatically sets the FLB_IO_TLS flag if it is not already present [1][2]. When flb_upstream_create_url parses a URL, it examines the protocol component [1][2]. If the protocol is identified as https and no port is explicitly specified, the function defaults the port to 443 [1][2]. During this process, it checks the provided flags; if the FLB_IO_TLS bit is not set, it performs a bitwise OR operation to add FLB_IO_TLS to the flags [1][2]. This ensures that the upstream context is initialized with the appropriate transport security settings for an HTTPS connection [1][2]. Citations:
🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== candidate files =="
git ls-files | rg '(^|/)flb_aws_credentials_ec2\.c$|(^|/)flb_upstream\.c$|(^|/)flb_upstream\.h$|(^|/)aws.*credentials.*ec2.*\.(c|h)$' || true
echo "== target section =="
sed -n '1,340p' src/aws/flb_aws_credentials_ec2.c | nl -ba | sed -n '220,310p'
echo "== upstream create/url symbols =="
rg -n "flb_upstream_create\(|flb_upstream_create_url|AWSCONFIG_HTTPS|AWS_EC2_METADATA_SERVICE_ENDPOINT|FLB_IO_TLS|FLB_AWS_IMDS" src/aws/flb_aws_credentials_ec2.c src/flb_upstream.c src/include/flb_upstream.h 2>/dev/null || true
echo "== flb_upstream_create_url implementation slice =="
rg -n "flb_upstream_create_url" src/flb_upstream.c src -g '*upstream*'
sed -n '1,420p' src/flb_upstream.c | nl -ba | sed -n '1,520p' | rg -C 25 "flb_upstream_create_url|AWSCONFIG_HTTPS|protocol" || trueRepository: fluent/fluent-bit Length of output: 362 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== repository files =="
git ls-files | grep -E '(^|/)flb_aws_credentials_ec2\.c$|(^|/)flb_upstream\.c$|(^|/)flb_upstream\.h$|(^|/)tests/internal/aws_credentials_ec2\.c$' || true
echo "== target section =="
awk '{printf "%d\t%s\n", NR, $0}' src/aws/flb_aws_credentials_ec2.c | sed -n '230,305p'
echo "== upstream symbols in target/context files =="
grep -n "flb_upstream_create\|flb_upstream_create_url\|AWS_EC2_METADATA_SERVICE_ENDPOINT\|FLB_AWS_IMDS\|FLB_IO_TLS\|AWSCONFIG_HTTPS\|AWS_EC2_METADATA_SERVICE_ENDPOINT_TLS" src/aws/flb_aws_credentials_ec2.c src/flb_upstream.c include/fluent-bit/flb_upstream.h tests/internal/aws_credentials_ec2.c
echo "== flb_upstream_create_url implementation =="
awk '{printf "%d\t%s\n", NR, $0}' src/flb_upstream.c | sed -n '1,380p' | grep -C 25 "flb_upstream_create_url\|AWSCONFIG_HTTPS\|protocol\|https\|FLB_IO_TLS"
echo "== AWS cred create helper declarations/usages around credentials env =="
awk '{printf "%d\t%s\n", NR, $0}' src/aws/flb_aws_credentials_ec2.c | sed -n '20,140p'Repository: fluent/fluent-bit Length of output: 12203 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== flb_upstream_create_url implementation =="
awk '{printf "%d\t%s\n", NR, $0}' src/flb_upstream.c | sed -n '410,470p'
echo "== headers around constants =="
awk '{printf "%d\t%s\n", NR, $0}' include/fluent-bit/aws/flb_aws_imds.h | sed -n '1,160p'
awk '{printf "%d\t%s\n", NR, $0}' include/fluent-bit/flb_http_client.h | sed -n '1,80p'
echo "== credential tests around HTTPS custom endpoint =="
awk '{printf "%d\t%s\n", NR, $0}' tests/internal/aws_credentials_ec2.c | sed -n '1000,1080p'
grep -Rn "AWS_EC2_METADATA_SERVICE_ENDPOINT.*https\|https://" tests include src/aws | head -n 80Repository: fluent/fluent-bit Length of output: 22267 Propagate the IMDS URL protocol into the upstream flags.
🧰 Tools🪛 ast-grep (0.44.1)[warning] 275-275: Avoid (atoi-no-error-detection-c) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (host) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_sds_destroy(host); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!upstream) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_aws_provider_destroy(provider); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_debug("[aws_credentials] unable to connect to EC2 IMDS."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* IMDSv2 token request will timeout if hops = 1 and running within container */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream->base.net.connect_timeout = FLB_AWS_IMDS_TIMEOUT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream->base.net.io_timeout = FLB_AWS_IMDS_TIMEOUT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Set timeout based on endpoint type: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * - Standard AWS IMDS: 1 second (fast local endpoint) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * - Custom IMDS endpoints (e.g., IAM Roles Anywhere): 10 seconds (certificate auth takes longer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (use_custom_endpoint) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream->base.net.connect_timeout = FLB_AWS_IMDS_TIMEOUT_CUSTOM; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream->base.net.io_timeout = FLB_AWS_IMDS_TIMEOUT_CUSTOM; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+306
to
+308
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flb_info("[aws_credentials] Using extended timeout (%d seconds) for custom IMDS endpoint", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FLB_AWS_IMDS_TIMEOUT_CUSTOM); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream->base.net.connect_timeout = FLB_AWS_IMDS_TIMEOUT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream->base.net.io_timeout = FLB_AWS_IMDS_TIMEOUT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upstream->base.net.keepalive = FLB_FALSE; /* On timeout, the connection is broken */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client = generator->create(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -278,7 +326,7 @@ struct flb_aws_provider *flb_ec2_provider_create(struct flb_config *config, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->provider = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->region = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->service = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->port = 80; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->port = use_port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->flags = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->proxy = NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implementation->client->upstream = upstream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,17 +80,9 @@ struct flb_aws_imds *flb_aws_imds_create(const struct flb_aws_imds_config *imds_ | |
| flb_aws_imds_destroy(ctx); | ||
| return NULL; | ||
| } | ||
| if (0 != strncmp(ec2_imds_client->upstream->tcp_host, FLB_AWS_IMDS_HOST, | ||
| FLB_AWS_IMDS_HOST_LEN)) { | ||
| flb_debug("[imds] ec2_imds_client tcp host must be set to %s", FLB_AWS_IMDS_HOST); | ||
| flb_aws_imds_destroy(ctx); | ||
| return NULL; | ||
| } | ||
| if (ec2_imds_client->upstream->tcp_port != FLB_AWS_IMDS_PORT) { | ||
| flb_debug("[imds] ec2_imds_client tcp port must be set to %i", FLB_AWS_IMDS_PORT); | ||
| flb_aws_imds_destroy(ctx); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* Allow custom IMDS endpoints via AWS_EC2_METADATA_SERVICE_ENDPOINT */ | ||
| /* The hardcoded host/port checks have been removed to support custom endpoints */ | ||
|
Comment on lines
+84
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Removing the host/port rejection makes Useful? React with 👍 / 👎. |
||
|
|
||
| /* Connect client */ | ||
| ctx->ec2_imds_client = ec2_imds_client; | ||
|
|
@@ -250,7 +242,6 @@ static int get_imds_version(struct flb_aws_imds *ctx) | |
| { | ||
| int ret; | ||
| struct flb_aws_client *client = ctx->ec2_imds_client; | ||
| struct flb_aws_header invalid_token_header; | ||
| struct flb_http_client *c = NULL; | ||
|
|
||
| if (ctx->imds_version != FLB_AWS_IMDS_VERSION_EVALUATE) { | ||
|
|
@@ -259,65 +250,37 @@ static int get_imds_version(struct flb_aws_imds *ctx) | |
|
|
||
| /* | ||
| * Evaluate version | ||
| * To evaluate wether IMDSv2 is available, send an invalid token | ||
| * in IMDS request. If response status is 'Unauthorized', then IMDSv2 | ||
| * is available. | ||
| * Try to get an IMDSv2 token first. If that fails, fall back to IMDSv1. | ||
| * This approach is more compatible with custom IMDS implementations like IAM Roles Anywhere. | ||
| */ | ||
| invalid_token_header = imds_v2_token_token_header_template; | ||
| invalid_token_header.val = "INVALID"; | ||
| invalid_token_header.val_len = 7; | ||
| c = client->client_vtable->request(client, FLB_HTTP_GET, FLB_AWS_IMDS_ROOT, NULL, 0, | ||
| &invalid_token_header, 1); | ||
|
|
||
| ctx->imds_version = FLB_AWS_IMDS_VERSION_2; | ||
| ret = refresh_imds_v2_token(ctx); | ||
| if (ret == 0) { | ||
|
Comment on lines
+256
to
+258
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This changes version detection so the first mocked request is now Useful? React with 👍 / 👎. |
||
| /* Successfully got IMDSv2 token */ | ||
| flb_info("[imds] using IMDSv2"); | ||
| return FLB_AWS_IMDS_VERSION_2; | ||
| } | ||
|
|
||
| /* IMDSv2 token request failed, try IMDSv1 */ | ||
| flb_debug("[imds] IMDSv2 token request failed, testing IMDSv1"); | ||
| ctx->imds_version = FLB_AWS_IMDS_VERSION_EVALUATE; | ||
| c = client->client_vtable->request(client, FLB_HTTP_GET, FLB_AWS_IMDS_ROOT, | ||
| NULL, 0, NULL, 0); | ||
|
|
||
| if (!c) { | ||
| flb_debug("[imds] imds endpoint unavailable"); | ||
| return FLB_AWS_IMDS_VERSION_EVALUATE; | ||
| } | ||
|
|
||
| /* Unauthorized response means that IMDS version 2 is in use */ | ||
| if (c->resp.status == 401) { | ||
| ctx->imds_version = FLB_AWS_IMDS_VERSION_2; | ||
| ret = refresh_imds_v2_token(ctx); | ||
| if (ret == -1) { | ||
| /* | ||
| * Token cannot be refreshed, test IMDSv1 | ||
| * If IMDSv1 cannot be used, response will be status 401 | ||
| */ | ||
| flb_http_client_destroy(c); | ||
| ctx->imds_version = FLB_AWS_IMDS_VERSION_EVALUATE; | ||
| c = client->client_vtable->request(client, FLB_HTTP_GET, FLB_AWS_IMDS_ROOT, | ||
| NULL, 0, NULL, 0); | ||
| if (!c) { | ||
| flb_debug("[imds] imds v1 attempt, endpoint unavailable"); | ||
| return FLB_AWS_IMDS_VERSION_EVALUATE; | ||
| } | ||
|
|
||
| if (c->resp.status == 200) { | ||
| flb_info("[imds] to use IMDSv2, set --http-put-response-hop-limit to 2"); | ||
| } | ||
| else { | ||
| /* IMDSv1 unavailable. IMDSv2 beyond network hop count */ | ||
| flb_warn("[imds] failed to retrieve IMDSv2 token and IMDSv1 unavailable. " | ||
| "This is likely due to instance-metadata-options " | ||
| "--http-put-response-hop-limit being set to 1 and --http-tokens " | ||
| "set to required. " | ||
| "To use IMDSv2, please set --http-put-response-hop-limit to 2 as " | ||
| "described https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/" | ||
| "configuring-instance-metadata-options.html"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Success means that IMDS version 1 is in use | ||
| */ | ||
|
|
||
| if (c->resp.status == 200) { | ||
| flb_warn("[imds] falling back on IMDSv1"); | ||
| flb_info("[imds] falling back to IMDSv1"); | ||
| ctx->imds_version = FLB_AWS_IMDS_VERSION_1; | ||
| flb_http_client_destroy(c); | ||
| return FLB_AWS_IMDS_VERSION_1; | ||
| } | ||
|
|
||
| flb_http_client_destroy(c); | ||
| return ctx->imds_version; | ||
| return FLB_AWS_IMDS_VERSION_EVALUATE; | ||
| } | ||
|
|
||
| /* | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Mid-function variable declaration.
int use_custom_endpoint = FLB_FALSE;is declared mid-block instead of alongside the other locals at the top of the function. As per coding guidelines, "Declare variables at the start of functions rather than mid-block."🛠️ Proposed fix
const char *use_host; int use_port; int ret; + int use_custom_endpoint; provider = flb_calloc(1, sizeof(struct flb_aws_provider)); @@ use_host = FLB_AWS_IMDS_HOST; use_port = FLB_AWS_IMDS_PORT; - int use_custom_endpoint = FLB_FALSE; - + use_custom_endpoint = FLB_FALSE;📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Coding guidelines