mirror of
https://github.com/coder/coder.git
synced 2026-09-21 20:51:01 +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
75 lines
2.4 KiB
Go
75 lines
2.4 KiB
Go
package aibridge
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/aibridge/config"
|
|
aibcontext "github.com/coder/coder/v2/aibridge/context"
|
|
"github.com/coder/coder/v2/aibridge/metrics"
|
|
"github.com/coder/coder/v2/aibridge/provider"
|
|
"github.com/coder/coder/v2/aibridge/recorder"
|
|
)
|
|
|
|
// Const + Type + function aliases for backwards compatibility.
|
|
const (
|
|
ProviderAnthropic = config.ProviderAnthropic
|
|
ProviderOpenAI = config.ProviderOpenAI
|
|
ProviderCopilot = config.ProviderCopilot
|
|
)
|
|
|
|
type (
|
|
Metrics = metrics.Metrics
|
|
|
|
Provider = provider.Provider
|
|
|
|
InterceptionRecord = recorder.InterceptionRecord
|
|
InterceptionRecordEnded = recorder.InterceptionRecordEnded
|
|
TokenUsageRecord = recorder.TokenUsageRecord
|
|
PromptUsageRecord = recorder.PromptUsageRecord
|
|
ToolUsageRecord = recorder.ToolUsageRecord
|
|
ModelThoughtRecord = recorder.ModelThoughtRecord
|
|
Recorder = recorder.Recorder
|
|
Metadata = recorder.Metadata
|
|
|
|
AnthropicConfig = config.Anthropic
|
|
AWSBedrockConfig = config.AWSBedrock
|
|
OpenAIConfig = config.OpenAI
|
|
CopilotConfig = config.Copilot
|
|
)
|
|
|
|
func AsActor(ctx context.Context, actorID string, metadata recorder.Metadata) context.Context {
|
|
return aibcontext.AsActor(ctx, actorID, metadata)
|
|
}
|
|
|
|
func NewAnthropicProvider(ctx context.Context, cfg config.Anthropic, bedrockCfg *config.AWSBedrock) (provider.Provider, error) {
|
|
return provider.NewAnthropic(ctx, cfg, bedrockCfg)
|
|
}
|
|
|
|
func NewOpenAIProvider(cfg config.OpenAI) provider.Provider {
|
|
return provider.NewOpenAI(cfg)
|
|
}
|
|
|
|
func NewCopilotProvider(cfg config.Copilot) provider.Provider {
|
|
return provider.NewCopilot(cfg)
|
|
}
|
|
|
|
// NewDisabledProviderStub returns a Provider that reports Enabled() ==
|
|
// false and has no-op implementations for all other methods. Use this
|
|
// instead of constructing a concrete provider for disabled rows so that
|
|
// adding a new provider type does not require updating a switch here.
|
|
func NewDisabledProviderStub(name, providerType string) provider.Provider {
|
|
return provider.NewDisabledStub(name, providerType)
|
|
}
|
|
|
|
func NewMetrics(reg prometheus.Registerer) *metrics.Metrics {
|
|
return metrics.NewMetrics(reg)
|
|
}
|
|
|
|
func NewRecorder(logger slog.Logger, tracer trace.Tracer, clientFn func() (Recorder, error)) Recorder {
|
|
return recorder.NewWrappedRecorder(logger, tracer, clientFn)
|
|
}
|