Files
coder/aibridge/intercept/credential_test.go
T
Yevhenii Shcherbina 8bf6f43016 feat: support cross-account Bedrock AssumeRole in AI Bridge (#26527)
# Support IAM role assumption for AWS Bedrock in AI Bridge

## Summary

Implements
https://linear.app/codercom/issue/AIGOV-371/support-dynamic-bedrock-assumerole-across-aws-accounts-for-ai-gateway

A Bedrock provider can now be configured with an IAM role to assume.
Before calling Bedrock, the gateway assumes that role via STS and signs
requests with the resulting temporary credentials. Whether the role
lives in the same account or another one is entirely a matter of the
role's trust policy.

## Problem

Many organizations prohibit long-lived AWS access keys and expect
workloads to authenticate through assumed IAM roles instead. A common
case is an organization that runs Bedrock across several AWS accounts,
one per business unit, and needs each unit's usage billed to its own
account by assuming a role there. AI Bridge previously authenticated a
Bedrock provider only with static keys or the gateway's own ambient AWS
identity, which is shared by every provider, with no way to assume a
role. These deployments had no clean path.

## How it works

When a provider is configured with a role ARN, the gateway uses its base
identity to assume that role via STS and signs Bedrock requests with the
temporary credentials it returns. The base identity is whatever the AWS
default credential chain resolves, IRSA, EKS Pod Identity, EC2 Instance
Profile, or static keys.

Credentials are resolved once when the provider is set up and are then
cached and rotated, so individual requests are served from the cache
rather than triggering a new STS call. A deployment that needs several
roles configures several providers, each pointing at its own role.

## Configuration

The role ARN is part of the Bedrock provider settings and is set through
the AI provider API. It is optional: a provider with no role ARN behaves
exactly as before.

## Scope and trade-offs

- This PR is backend only. The settings UI for the role ARN ships in a
follow-up.
- Configuration is not exposed through environment variables.
Environment-based provider configuration is being phased out in favor of
database-managed providers, so the role ARN is intentionally database
and API only.

Follow-up PR: https://github.com/coder/coder/pull/26578
2026-06-24 12:03:27 -04:00

144 lines
5.1 KiB
Go

package intercept_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/aibridge/config"
"github.com/coder/coder/v2/aibridge/intercept"
"github.com/coder/coder/v2/aibridge/keypool"
"github.com/coder/quartz"
)
// TestCredential covers the public surface of the Credential interface and its
// three implementations (BYOK, Bedrock, CentralizedPool), plus the
// AsBYOK/AsCentralizedPool helpers interceptors use to route. Only
// CentralizedPool fails over, so AsCentralizedPool must be true only for it.
func TestCredential(t *testing.T) {
t.Parallel()
// Matches the VARCHAR(15) DB constraint.
const maxCredentialHintLength = 15
tests := []struct {
name string
newCred func(t *testing.T) intercept.Credential
expectKind intercept.CredentialKind
expectAuthHeader string
expectHint string
expectLength int
expectAsBYOK bool
expectAsCentralizedPool bool
}{
{
name: "byok_authorization",
newCred: func(*testing.T) intercept.Credential {
return intercept.BYOK{Secret: "user-bearer-token", Header: intercept.AuthHeaderAuthorization}
},
expectKind: intercept.CredentialKindBYOK,
expectAuthHeader: intercept.AuthHeaderAuthorization,
expectHint: "us...en",
expectLength: len("user-bearer-token"),
expectAsBYOK: true,
},
{
name: "byok_xapikey",
newCred: func(*testing.T) intercept.Credential {
return intercept.BYOK{Secret: "user-api-key", Header: intercept.AuthHeaderXAPIKey}
},
expectKind: intercept.CredentialKindBYOK,
expectAuthHeader: intercept.AuthHeaderXAPIKey,
expectHint: "us...ey",
expectLength: len("user-api-key"),
expectAsBYOK: true,
},
{
// Bedrock with static AWS credentials: the access key ID is
// masked. AWS signs the request, so there is no auth header.
name: "centralized_bedrock_static",
newCred: func(*testing.T) intercept.Credential {
return intercept.Bedrock{AccessKey: "AKIAIOSFODNN7EXAMPLE"}
},
expectKind: intercept.CredentialKindCentralized,
expectAuthHeader: "",
expectHint: "AKIA...MPLE",
expectLength: len("AKIAIOSFODNN7EXAMPLE"),
},
{
// Bedrock with dynamic credentials (AWS default credential chain):
// no static key to mask, so the hint is a descriptive placeholder.
name: "centralized_bedrock_dynamic",
newCred: func(*testing.T) intercept.Credential {
return intercept.Bedrock{AccessKey: ""}
},
expectKind: intercept.CredentialKindCentralized,
expectAuthHeader: "",
expectHint: "<aws chain>",
expectLength: 0,
},
{
// Pool before failover selects a key: the hint is a placeholder
// until NextKey hands one out.
name: "centralized_pool_before_key",
newCred: func(t *testing.T) intercept.Credential {
pool, err := keypool.New(config.ProviderAnthropic, []string{"k0-pool-key"}, quartz.NewMock(t), nil)
require.NoError(t, err)
return &intercept.CentralizedPool{Pool: pool, Header: intercept.AuthHeaderXAPIKey}
},
expectKind: intercept.CredentialKindCentralized,
expectAuthHeader: intercept.AuthHeaderXAPIKey,
expectHint: "<failover key>",
expectLength: 0,
expectAsCentralizedPool: true,
},
{
// Pool after NextKey: Hint/Length reflect the selected key.
name: "centralized_pool_after_next_key",
newCred: func(t *testing.T) intercept.Credential {
pool, err := keypool.New(config.ProviderAnthropic, []string{"k0-pool-key"}, quartz.NewMock(t), nil)
require.NoError(t, err)
cp := &intercept.CentralizedPool{Pool: pool, Header: intercept.AuthHeaderXAPIKey}
_, keyErr := cp.NextKey(cp.Pool.Walker())
require.Nil(t, keyErr)
return cp
},
expectKind: intercept.CredentialKindCentralized,
expectAuthHeader: intercept.AuthHeaderXAPIKey,
expectHint: "k0...ey",
expectLength: len("k0-pool-key"),
expectAsCentralizedPool: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
cred := tc.newCred(t)
assert.Equal(t, tc.expectKind, cred.Kind(), "Kind")
assert.Equal(t, tc.expectAuthHeader, cred.AuthHeader(), "AuthHeader")
assert.Equal(t, tc.expectHint, cred.Hint(), "Hint")
assert.LessOrEqual(t, len(cred.Hint()), maxCredentialHintLength,
"Hint must fit the credential_hint column")
assert.Equal(t, tc.expectLength, cred.Length(), "Length")
credBYOK, credBYOKOK := intercept.AsBYOK(cred)
assert.Equal(t, tc.expectAsBYOK, credBYOKOK, "AsBYOK ok")
if tc.expectAsBYOK {
assert.Equal(t, cred, credBYOK, "AsBYOK returns the credential")
}
credPool, credPoolOK := intercept.AsCentralizedPool(cred)
assert.Equal(t, tc.expectAsCentralizedPool, credPoolOK, "AsCentralizedPool ok")
if tc.expectAsCentralizedPool {
assert.Same(t, cred, credPool, "AsCentralizedPool returns the same pointer")
} else {
assert.Nil(t, credPool, "AsCentralizedPool returns nil when not a pool")
}
})
}
}