mirror of
https://github.com/coder/coder.git
synced 2026-09-22 13:10:21 +08:00
# 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
215 lines
6.6 KiB
Go
215 lines
6.6 KiB
Go
package codersdk_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/util/ptr"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func TestAIProviderSettings_Marshal(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("EmptyEmitsNull", func(t *testing.T) {
|
|
t.Parallel()
|
|
got, err := json.Marshal(codersdk.AIProviderSettings{})
|
|
require.NoError(t, err)
|
|
require.JSONEq(t, `null`, string(got))
|
|
})
|
|
|
|
t.Run("BedrockEmitsDiscriminator", func(t *testing.T) {
|
|
t.Parallel()
|
|
got, err := json.Marshal(codersdk.AIProviderSettings{
|
|
Bedrock: &codersdk.AIProviderBedrockSettings{
|
|
Region: "us-east-1",
|
|
Model: "anthropic.claude-3-5-sonnet",
|
|
SmallFastModel: "anthropic.claude-3-5-haiku",
|
|
AccessKey: ptr.Ref("AKIA-test"), //nolint:gosec // fixture
|
|
AccessKeySecret: ptr.Ref("secret"),
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
require.JSONEq(t, `{
|
|
"_type": "bedrock",
|
|
"_version": 1,
|
|
"region": "us-east-1",
|
|
"model": "anthropic.claude-3-5-sonnet",
|
|
"small_fast_model": "anthropic.claude-3-5-haiku",
|
|
"access_key": "AKIA-test",
|
|
"access_key_secret": "secret"
|
|
}`, string(got))
|
|
})
|
|
|
|
t.Run("BedrockOmitsEmptyFields", func(t *testing.T) {
|
|
t.Parallel()
|
|
got, err := json.Marshal(codersdk.AIProviderSettings{
|
|
Bedrock: &codersdk.AIProviderBedrockSettings{Region: "us-east-1"},
|
|
})
|
|
require.NoError(t, err)
|
|
require.JSONEq(t, `{
|
|
"_type": "bedrock",
|
|
"_version": 1,
|
|
"region": "us-east-1"
|
|
}`, string(got))
|
|
})
|
|
}
|
|
|
|
func TestAIProviderSettings_Unmarshal(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("EmptyInputZeroes", func(t *testing.T) {
|
|
t.Parallel()
|
|
// encoding/json never invokes UnmarshalJSON with an empty
|
|
// payload, but the method must still tolerate it for callers
|
|
// (e.g. row decoders) that hand it raw column bytes.
|
|
var s codersdk.AIProviderSettings
|
|
require.NoError(t, s.UnmarshalJSON(nil))
|
|
require.True(t, s.IsZero())
|
|
require.NoError(t, s.UnmarshalJSON([]byte("")))
|
|
require.True(t, s.IsZero())
|
|
})
|
|
|
|
t.Run("NullZeroes", func(t *testing.T) {
|
|
t.Parallel()
|
|
var s codersdk.AIProviderSettings
|
|
require.NoError(t, json.Unmarshal([]byte(`null`), &s))
|
|
require.True(t, s.IsZero())
|
|
})
|
|
|
|
t.Run("BedrockSupportedVersion", func(t *testing.T) {
|
|
t.Parallel()
|
|
var s codersdk.AIProviderSettings
|
|
require.NoError(t, json.Unmarshal([]byte(`{
|
|
"_type": "bedrock",
|
|
"_version": 1,
|
|
"region": "us-east-1",
|
|
"model": "anthropic.claude-3-5-sonnet"
|
|
}`), &s))
|
|
require.NotNil(t, s.Bedrock)
|
|
require.Equal(t, "us-east-1", s.Bedrock.Region)
|
|
require.Equal(t, "anthropic.claude-3-5-sonnet", s.Bedrock.Model)
|
|
})
|
|
|
|
t.Run("MissingTypeDiscriminator", func(t *testing.T) {
|
|
t.Parallel()
|
|
var s codersdk.AIProviderSettings
|
|
err := json.Unmarshal([]byte(`{"_version":1,"region":"us-east-1"}`), &s)
|
|
require.ErrorContains(t, err, "missing _type discriminator")
|
|
})
|
|
|
|
t.Run("UnsupportedVersion", func(t *testing.T) {
|
|
t.Parallel()
|
|
var s codersdk.AIProviderSettings
|
|
err := json.Unmarshal([]byte(`{"_type":"bedrock","_version":99}`), &s)
|
|
require.ErrorContains(t, err, `unsupported "bedrock" settings version 99`)
|
|
require.ErrorContains(t, err, "expected 1")
|
|
})
|
|
|
|
t.Run("UnknownType", func(t *testing.T) {
|
|
t.Parallel()
|
|
var s codersdk.AIProviderSettings
|
|
err := json.Unmarshal([]byte(`{"_type":"copilot","_version":1}`), &s)
|
|
require.ErrorContains(t, err, `unknown settings type "copilot"`)
|
|
})
|
|
|
|
t.Run("MalformedHeader", func(t *testing.T) {
|
|
t.Parallel()
|
|
// _type must be a string; passing a number triggers the
|
|
// header decode path before any discriminator routing.
|
|
var s codersdk.AIProviderSettings
|
|
err := json.Unmarshal([]byte(`{"_type": 1}`), &s)
|
|
require.ErrorContains(t, err, "decode settings header")
|
|
require.ErrorContains(t, err, "_type")
|
|
})
|
|
|
|
t.Run("ResetsBetweenCalls", func(t *testing.T) {
|
|
t.Parallel()
|
|
// A non-zero value passed to Unmarshal should be reset when
|
|
// the payload decodes to null, so callers can reuse the
|
|
// variable without leaking stale state.
|
|
s := codersdk.AIProviderSettings{
|
|
Bedrock: &codersdk.AIProviderBedrockSettings{Region: "us-east-1"},
|
|
}
|
|
require.NoError(t, json.Unmarshal([]byte(`null`), &s))
|
|
require.True(t, s.IsZero())
|
|
})
|
|
}
|
|
|
|
func TestAIProviderSettings_Roundtrip(t *testing.T) {
|
|
t.Parallel()
|
|
orig := codersdk.AIProviderSettings{
|
|
Bedrock: &codersdk.AIProviderBedrockSettings{
|
|
Region: "us-west-2",
|
|
Model: "anthropic.claude-sonnet-4-5",
|
|
SmallFastModel: "anthropic.claude-haiku-4-5",
|
|
AccessKey: ptr.Ref("AKIA-roundtrip"), //nolint:gosec // fixture
|
|
AccessKeySecret: ptr.Ref("secret-roundtrip"),
|
|
},
|
|
}
|
|
encoded, err := json.Marshal(orig)
|
|
require.NoError(t, err)
|
|
// Sanity: discriminator is part of the on-wire shape.
|
|
require.True(t, strings.Contains(string(encoded), `"_type":"bedrock"`))
|
|
|
|
var got codersdk.AIProviderSettings
|
|
require.NoError(t, json.Unmarshal(encoded, &got))
|
|
require.Equal(t, orig, got)
|
|
}
|
|
|
|
func TestAIProviderRequest_ValidateRoleARN(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
roleARN string
|
|
wantErr bool
|
|
}{
|
|
{name: "empty is allowed", roleARN: "", wantErr: false},
|
|
{name: "standard role arn", roleARN: "arn:aws:iam::743809215448:role/bedrock-role", wantErr: false},
|
|
{name: "govcloud partition", roleARN: "arn:aws-us-gov:iam::123456789012:role/bedrock-role", wantErr: false},
|
|
{name: "china partition", roleARN: "arn:aws-cn:iam::123456789012:role/bedrock-role", wantErr: false},
|
|
{name: "role path", roleARN: "arn:aws:iam::123456789012:role/team/bedrock-role", wantErr: false},
|
|
{name: "not an arn", roleARN: "bedrock-role", wantErr: true},
|
|
{name: "wrong resource type", roleARN: "arn:aws:iam::123456789012:user/dave", wantErr: true},
|
|
{name: "wrong service", roleARN: "arn:aws:s3:::my-bucket", wantErr: true},
|
|
{name: "truncated arn", roleARN: "arn:aws:iam::123456789012", wantErr: true},
|
|
}
|
|
|
|
hasRoleARNError := func(vs []codersdk.ValidationError) bool {
|
|
for _, v := range vs {
|
|
if v.Field == "settings.role_arn" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
settings := codersdk.AIProviderSettings{
|
|
Bedrock: &codersdk.AIProviderBedrockSettings{
|
|
Region: "us-east-1",
|
|
RoleARN: tc.roleARN,
|
|
},
|
|
}
|
|
|
|
create := codersdk.CreateAIProviderRequest{
|
|
Type: codersdk.AIProviderTypeBedrock,
|
|
Name: "bedrock",
|
|
BaseURL: "https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
Settings: settings,
|
|
}
|
|
require.Equal(t, tc.wantErr, hasRoleARNError(create.Validate()))
|
|
|
|
update := codersdk.UpdateAIProviderRequest{Settings: &settings}
|
|
require.Equal(t, tc.wantErr, hasRoleARNError(update.Validate()))
|
|
})
|
|
}
|
|
}
|