Skip to content

fix(amplify-provider-awscloudformation): mfa role assumption fails with cached credentials#14627

Open
danrivett wants to merge 2 commits intoaws-amplify:devfrom
danrivett:provider-awscloudformation/fix-mfa-credential-caching
Open

fix(amplify-provider-awscloudformation): mfa role assumption fails with cached credentials#14627
danrivett wants to merge 2 commits intoaws-amplify:devfrom
danrivett:provider-awscloudformation/fix-mfa-credential-caching

Conversation

@danrivett
Copy link

Description

Fixes three cascading bugs in the MFA role assumption credential caching logic introduced by commit 04f7bcfc24 (PR #14315, fixing #14290). These bugs cause MFA-based role assumption to fail with Resolved credential object is not valid.

Bug 1 — MFA prompt never appears: getCachedRoleCredentials() always returned { credentials: {} } even when no valid cached credentials existed, so the STS AssumeRole call was never executed.

Bug 2 — Cache validation always fails: Credentials were cached in nested format { credentials: { accessKeyId, ... } } but validateCachedCredentials() expected flat format { accessKeyId, ... }.

Bug 3 — expiration.getTime is not a function: Cached Date is deserialized as a string from JSON, but the AWS SDK calls expiration.getTime(). PR #14315 only fixed this in getConfiguredAWSClientConfig(), not in getProfiledAwsConfig().

Issue

Closes #14626

How did you test these changes?

  • Added unit tests for getCachedRoleCredentials and cacheRoleCredentials covering:
    • Returns undefined when cache file does not exist
    • Returns undefined when roleArn not in cache
    • Returns undefined when cached credentials are expired
    • Returns valid cached credentials with expiration converted to Date
    • Caches credentials in flat format (not nested)
  • Manual testing with an AWS profile configured with role_arn + source_profile + mfa_serial

Checklist

  • If this is a change to a feature that has existing integration tests, I updated the existing integration tests
  • My change meets the acceptance criteria set in the issue
  • This change does not directly reference any internal resources

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…th cached credentials (aws-amplify#14626)

Fixes three related bugs in credential caching for MFA-based role
assumption introduced in commit 04f7bcf (PR aws-amplify#14315).

Bug 1 - MFA prompt never appears: getCachedRoleCredentials() always
returned { credentials: {} } even with no valid cache, so the STS
AssumeRole call was never executed.

Fix: return undefined when no valid cached credentials exist.

Bug 2 - Cache validation always fails: credentials were cached in
nested format { credentials: { accessKeyId, ... } } but
validateCachedCredentials() expected flat format.

Fix: cache the flat credentials object (roleCredentials.credentials).

Bug 3 - expiration.getTime error: cached Date is deserialized as a
string, but the AWS SDK calls expiration.getTime(). The fix in
PR aws-amplify#14315 only addressed this in getConfiguredAWSClientConfig(), not
in getProfiledAwsConfig().

Fix: convert expiration to Date when returning cached credentials.
Add tests for the credential caching logic used in MFA role assumption.
These tests verify the bug fixes made to address three issues:

1. getCachedRoleCredentials returns undefined when no cache exists
   - Test: should return undefined when cache file does not exist
   - Test: should return undefined when roleArn not in cache

2. getCachedRoleCredentials returns undefined for expired credentials
   - Test: should return undefined when cached credentials are expired

3. Cached credentials use flat format and expiration is converted to Date
   - Test: should return valid cached credentials with Date expiration
   - Test: should cache credentials in flat format (not nested)

The tests verify that:
- STS is called when no valid cache exists
- STS is NOT called when valid cached credentials exist
- Cached expiration string is converted to Date object on read
- Credentials are cached in flat format (accessKeyId at top level,
  not nested under credentials property)
@danrivett danrivett requested a review from a team as a code owner February 27, 2026 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MFA role assumption fails with "Resolved credential object is not valid" after v14.2.2

1 participant