Skip to content

Commit 4c19da4

Browse files
committed
fix(aws): force credential refresh in provider refresh functions
- Add force refresh logic to EC2, STS, and EKS credential providers - Set next_refresh to 0 in refresh functions to ensure immediate credential update - Fixes MSK IAM authentication failures after ~1 hour due to stale credentials - Aligns with AWS SDK behavior where refresh() means force refresh This resolves the issue where OAuth token refresh (every ~15 minutes) would not actually refresh AWS credentials until next_refresh time was reached (typically 1 hour later), causing MSK connection failures with 'Access denied' errors. The fix ensures that every OAuth callback will fetch fresh credentials from AWS, matching the behavior of official AWS SDKs (Python, Java). Signed-off-by: Arbin <[email protected]>
1 parent ca35c22 commit 4c19da4

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

src/aws/flb_aws_credentials_ec2.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ int refresh_fn_ec2(struct flb_aws_provider *provider) {
130130
int ret = -1;
131131

132132
flb_debug("[aws_credentials] Refresh called on the EC2 IMDS provider");
133+
134+
/* Force credential refresh by marking as expired */
135+
implementation->next_refresh = 0;
136+
133137
if (try_lock_provider(provider)) {
134138
ret = get_creds_ec2(implementation);
135139
unlock_provider(provider);

src/aws/flb_aws_credentials_profile.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ static int get_shared_credentials(char* credentials_path,
663663

664664
if (flb_read_file(credentials_path, &buf, &size) < 0) {
665665
if (errno == ENOENT) {
666-
AWS_CREDS_ERROR_OR_DEBUG(debug_only, "Shared credentials file %s does not exist",
667-
credentials_path);
666+
AWS_CREDS_DEBUG("Shared credentials file %s does not exist", credentials_path);
668667
} else {
669668
flb_errno();
670669
AWS_CREDS_ERROR_OR_DEBUG(debug_only, "Could not read shared credentials file %s",

src/aws/flb_aws_credentials_sts.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ int refresh_fn_sts(struct flb_aws_provider *provider) {
175175
struct flb_aws_provider_sts *implementation = provider->implementation;
176176

177177
flb_debug("[aws_credentials] Refresh called on the STS provider");
178+
179+
/* Force credential refresh by marking as expired */
180+
implementation->next_refresh = 0;
178181

179182
if (try_lock_provider(provider)) {
180183
ret = sts_assume_role_request(implementation->sts_client,
@@ -480,6 +483,10 @@ int refresh_fn_eks(struct flb_aws_provider *provider) {
480483
struct flb_aws_provider_eks *implementation = provider->implementation;
481484

482485
flb_debug("[aws_credentials] Refresh called on the EKS provider");
486+
487+
/* Force credential refresh by marking as expired */
488+
implementation->next_refresh = 0;
489+
483490
if (try_lock_provider(provider)) {
484491
ret = assume_with_web_identity(implementation);
485492
unlock_provider(provider);

src/aws/flb_aws_msk_iam.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,17 @@ static flb_sds_t build_msk_iam_payload(struct flb_aws_msk_iam *config,
216216
return NULL;
217217
}
218218

219-
flb_info("[aws_msk_iam] build_msk_iam_payload_with_creds: generating payload for host: %s, region: %s",
220-
host, config->region);
219+
flb_debug("[aws_msk_iam] build_msk_iam_payload: generating payload for host: %s, region: %s",
220+
host, config->region);
221221

222222
/* Validate credentials */
223223
if (!creds) {
224-
flb_error("[aws_msk_iam] build_msk_iam_payload_with_creds: credentials are NULL");
224+
flb_error("[aws_msk_iam] build_msk_iam_payload: credentials are NULL");
225225
return NULL;
226226
}
227227

228228
if (!creds->access_key_id || !creds->secret_access_key) {
229-
flb_error("[aws_msk_iam] build_msk_iam_payload_with_creds: incomplete credentials");
229+
flb_error("[aws_msk_iam] build_msk_iam_payload: incomplete credentials");
230230
return NULL;
231231
}
232232

@@ -635,12 +635,19 @@ static void oauthbearer_token_refresh_cb(rd_kafka_t *rk,
635635
flb_debug("[aws_msk_iam] using MSK generic endpoint: %s", host);
636636
}
637637

638-
flb_info("[aws_msk_iam] requesting MSK IAM payload for region: %s, host: %s", config->region, host);
638+
flb_debug("[aws_msk_iam] requesting MSK IAM payload for region: %s, host: %s", config->region, host);
639639

640640
/*
641-
* Get credentials from provider. The provider handles caching and expiration internally.
642-
* The provider automatically manages credential refresh when needed.
641+
* Refresh credentials before generating OAuth token.
642+
* This is necessary because provider's passive refresh only triggers when
643+
* get_credentials is called and detects expiration. However, OAuth tokens
644+
* are refreshed every ~15 minutes while IAM credentials expire after ~1 hour.
645+
* If OAuth callbacks are spaced far apart, the passive refresh may not trigger
646+
* before credentials expire, causing authentication failures.
643647
*/
648+
config->provider->provider_vtable->refresh(config->provider);
649+
650+
/* Get credentials from provider */
644651
creds = config->provider->provider_vtable->get_credentials(config->provider);
645652
if (!creds) {
646653
flb_error("[aws_msk_iam] failed to get AWS credentials from provider");

0 commit comments

Comments
 (0)