Skip to content

Bedrock aws-sdk auth: ExpiredTokenException after credential file refresh (regression in v2026.5.3) #77551

Closed

Description

@shipitsteven

Description

After upgrading to v2026.5.3-1, the gateway no longer picks up refreshed AWS credentials from ~/.aws/credentials during its lifetime. Every credential rotation (e.g. mwinit / ada credentials update) causes ExpiredTokenException until the gateway is manually restarted.

This did not happen on the previous version — the gateway would seamlessly use fresh credentials after file refresh.

Config

{
  "models": {
    "providers": {
      "amazon-bedrock": {
        "baseUrl": "https://bedrock-runtime.us-west-2.amazonaws.com",
        "api": "bedrock-converse-stream",
        "auth": "aws-sdk",
        "models": [...]
      }
    }
  }
}

Systemd env: AWS_PROFILE=default, AWS_REGION=us-west-2, AWS_EC2_METADATA_DISABLED=true

Observed behavior

  • Gateway starts fine with valid credentials
  • After credentials rotate (new session token written to ~/.aws/credentials), all Bedrock calls fail:
    ExpiredTokenException: The security token included in the request is expired
    
  • Failover decision: surface_error reason=auth
  • Only fix is restarting the gateway service

Expected behavior

The gateway should re-read ~/.aws/credentials when the current token expires or fails with an auth error, matching AWS SDK v3's default credential provider behavior.

Possible root cause

The ~/.aws/credentials file has no expiration field (common with mwinit/ada tooling). Without an expiration timestamp, the SDK's fromIni() provider assumes credentials are permanent and never triggers a refresh. If v2026.5.3 moved to a singleton BedrockClient (vs. per-request instantiation in prior versions), the cached credential provider would hold stale tokens indefinitely.

Environment

  • OpenClaw: v2026.5.3-1
  • Platform: Amazon Linux 2 (Cloud Desktop)
  • Node: 22.18.0
  • Auth: shared credentials file (~/.aws/credentials) via AWS_PROFILE=default
  • Credential source: mwinit / Midway (writes temp STS tokens without expiration field)

Workaround

Restart the gateway after every credential refresh.

Activity

  1. clawsweeper commented on May 4, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve.

    Summary
    Keep open. The report matches a real Bedrock auth surface on current main: docs promise AWS SDK default-chain auth for Bedrock, but the aws-sdk sentinel path leaves no refreshable runtime-auth state, so OpenClaw's auth-error refresh hook cannot help when the underlying shared credentials go stale.

    Reproducibility: yes. for the OpenClaw-side refresh gap, from source inspection: aws-sdk sentinel auth clears runtimeAuthState, and auth-error refresh is gated on that state. A live AWS rotation repro was not run, so the full ExpiredTokenException path remains source-reproducible rather than live-reproduced.

    Next step
    This is a narrow, provider-owned regression candidate with clear source paths, but the repair should first validate AWS SDK v3 credential-provider behavior with rotated shared credentials.

    Review details

    Best possible solution:

    Keep the AWS SDK default-chain contract, but make the Amazon Bedrock provider/runtime path invalidate or recreate stale shared-credential resolution on auth-class failures without adding a user-facing config knob.

    Do we have a high-confidence way to reproduce the issue?

    Yes for the OpenClaw-side refresh gap, from source inspection: aws-sdk sentinel auth clears runtimeAuthState, and auth-error refresh is gated on that state. A live AWS rotation repro was not run, so the full ExpiredTokenException path remains source-reproducible rather than live-reproduced.

    Is this the best way to solve the issue?

    No current-main fix is present. The narrowest maintainable direction is provider-owned Bedrock credential refresh/recreation on auth failures, with regression coverage for shared credentials that rotate without an expiration field.

    Acceptance criteria:

    • pnpm test src/agents/pi-embedded-runner/run/auth-controller.test.ts extensions/amazon-bedrock/index.test.ts extensions/amazon-bedrock/embedding-provider.test.ts
    • pnpm test extensions/amazon-bedrock
    • pnpm check:changed

    What I checked:

    • Current docs define the expected auth contract: The Bedrock provider docs say Bedrock auth uses the AWS SDK default credential chain, not an API key, and list shared config/env/instance-role credentials as supported. Public docs: docs/providers/bedrock.md. (docs/providers/bedrock.md:9, d0cae0d9500e)
    • AWS SDK auth uses the sentinel path without refresh state: When no explicit key is resolved and auth mode is aws-sdk, the runner falls back to __aws_sdk_auth__ and sets runtimeAuthState to null. (src/agents/pi-embedded-runner/run/auth-controller.ts:365, d0cae0d9500e)
    • Auth-error refresh cannot run for sentinel-only Bedrock auth: maybeRefreshRuntimeAuthForAuthError immediately returns false when runtimeAuthState is missing, which is the current sentinel fallback state for Bedrock aws-sdk auth without a provider runtime-auth hook. (src/agents/pi-embedded-runner/run/auth-controller.ts:544, d0cae0d9500e)
    • Bedrock plugin registers stream wrapping but no runtime-auth hook: The Amazon Bedrock plugin registers provider metadata, config API key resolution, stream wrapping, failover classification, and thinking profiles; the inspected registration does not provide prepareRuntimeAuth for refreshable AWS SDK credentials. (extensions/amazon-bedrock/register.sync.runtime.ts:432, d0cae0d9500e)
    • Dependency surface relevant to the regression: The current main dependency set pins @mariozechner/pi-ai at 0.71.1 and Bedrock runtime dependencies are plugin/root-owned; the fix needs validation against that provider transport and AWS SDK v3 credential behavior. (package.json:1678, d0cae0d9500e)
    • Likely owner history sampled: Current-line blame points the Bedrock docs/plugin/auth-controller content at the grafted current snapshot by Vincent Koc; available local history also shows Peter Steinberger split the embedded run auth controller and later mirrored Bedrock runtime dependencies. (src/agents/pi-embedded-runner/run/auth-controller.ts:349, d0cae0d9500e)

    Likely related people:

    • Vincent Koc: Current-line blame on the Bedrock provider registration, Bedrock docs, and aws-sdk sentinel tests points to the grafted current snapshot authored by Vincent; local history also shows prior Bedrock docs work under this name. (role: recent maintainer; confidence: medium; commits: 841eb81baf37, dcf2c6d7f14d; files: extensions/amazon-bedrock/register.sync.runtime.ts, docs/providers/bedrock.md, src/agents/pi-embedded-runner/run/auth-controller.test.ts)
    • Peter Steinberger: Available local history shows Peter split the embedded run auth controller and later made a Bedrock runtime dependency fix, both adjacent to the reported auth/runtime boundary. (role: adjacent owner; confidence: medium; commits: 18dc98b00e43, d74a12264aa5; files: src/agents/pi-embedded-runner/run/auth-controller.ts, extensions/amazon-bedrock/package.json, package.json)

    Remaining risk / open question:

    • The exact failing AWS credential-cache behavior still needs dependency-level validation with rotated shared-credentials files that omit expiration metadata.
    • A robust fix may need either a provider-owned Bedrock runtime-auth seam or a focused upstream/provider-transport change rather than a small auth-controller-only patch.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against d0cae0d9500e.

  2. jtatum commented on May 6, 2026

    @jtatum
    SponsorContributor

    This breaks IMDS tokens as well

  3. steipete commented on May 7, 2026

    @steipete
    Contributor

    Maintainer repro note: I can get enough locally to prove the SDK/cache side of this without secrets.

    Local evidence:

    • ~/.profile provides AWS_PROFILE and AWS_REGION; aws sts get-caller-identity resolves the profile.
    • Bedrock control-plane proof is blocked for this local identity: aws bedrock list-foundation-models returns AccessDeniedException for bedrock:ListFoundationModels, so I cannot do a real Bedrock invoke/list proof from this account.
    • A temp-file AWS SDK probe reproduces the reported stale-read class: after rewriting AWS_SHARED_CREDENTIALS_FILE, a fresh defaultProvider() still returned the first access key; defaultProvider({ ignoreCache: true }) returned the rewritten access key.
    • Current @mariozechner/pi-ai@0.73.0 creates BedrockRuntimeClient without an ignoreCache credential provider hook, and its public Bedrock options do not expose a credential-provider override.

    So this stays open. The likely fix is provider-owned Bedrock runtime wiring that forces shared INI reloads for aws-sdk auth, or an upstream pi-ai option to pass credentialDefaultProvider/defaultProvider({ ignoreCache: true }) into BedrockRuntimeClient. I did not patch pi-ai directly here because dependency patches need maintainer approval.

  4. steipete commented on May 7, 2026

    @steipete
    Contributor

    Fixed on main in c018d84 / 4721ca8.

    What changed:

    • Bedrock model streaming now refreshes Smithy's shared AWS profile/config cache immediately before the AWS SDK sends the request.
    • Bedrock discovery and memory embedding requests also refresh the shared profile/config cache before constructing/sending with SDK clients.
    • Embeddings no longer keep a long-lived BedrockRuntimeClient that can hold stale no-expiration profile credentials.

    End-to-end proof from the same long-lived Node process using a scratch AWS_SHARED_CREDENTIALS_FILE:

    • first call with a bad default profile failed with AccessDeniedException for account 631860342038
    • rewrote the same credentials file to the working default profile
    • second OpenClaw-wrapped Bedrock stream in the same process returned OK without restart

    Local verification:

    • pnpm test extensions/amazon-bedrock/index.test.ts extensions/amazon-bedrock/discovery.test.ts extensions/amazon-bedrock/embedding-provider.test.ts
    • pnpm exec oxfmt --check --threads=1 CHANGELOG.md extensions/amazon-bedrock/aws-credential-refresh.ts extensions/amazon-bedrock/register.sync.runtime.ts extensions/amazon-bedrock/discovery.ts extensions/amazon-bedrock/embedding-provider.ts extensions/amazon-bedrock/index.test.ts extensions/amazon-bedrock/package.json
  5. steipete commented on May 7, 2026

    @steipete
    Contributor

    Fixed on current main.

    Proof:

    • c018d84 adds provider-owned live refresh for Bedrock AWS SDK/profile credentials.
    • 4721ca8 carries the lockfile follow-up.
    • Live Bedrock proof used a long-lived OpenClaw-wrapped Bedrock process with a scratch AWS shared credentials file: first call failed with the intentionally bad profile, the same credentials file was rewritten to the working profile, and the next Bedrock stream returned OK without restarting the process.
    • Targeted verification passed: pnpm test extensions/amazon-bedrock/index.test.ts extensions/amazon-bedrock/discovery.test.ts extensions/amazon-bedrock/embedding-provider.test.ts.

    Closing this as fixed by main.

  6. steipete commented on May 7, 2026

    @steipete
    Contributor

    Fixed on current main by c018d84.

    That patch moved Bedrock AWS SDK auth refresh into the Amazon Bedrock plugin itself:

    • extensions/amazon-bedrock/aws-credential-refresh.ts now reloads the AWS shared config/credentials files with ignoreCache: true unless static AWS env credentials or Bedrock bearer-token auth are in use.
    • extensions/amazon-bedrock/register.sync.runtime.ts refreshes before Bedrock Converse payload construction/control-plane calls.
    • extensions/amazon-bedrock/embedding-provider.ts creates a fresh BedrockRuntimeClient per invoke and destroys it after use, so refreshed shared-file credentials are picked up without a gateway restart.

    The auth-profile follow-up in 8e17910 also treats aws-sdk profiles as config routing metadata, not persisted credentials, and keeps doctor migration/docs aligned.

    Closing as fixed on main.

  7. locked as resolved and limited conversation to collaborators on May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions