MCPcopy Create free account
hub / github.com/bytebase/bytebase / AssumeRoleIfNeeded

Function AssumeRoleIfNeeded

backend/plugin/db/util/aws.go:22–58  ·  view source on GitHub ↗

AssumeRoleIfNeeded checks if role assumption is configured and updates the AWS config with assumed role credentials. This is a shared utility used by multiple AWS service drivers (Elasticsearch, RDS, etc.). Returns an error if role assumption fails.

(ctx context.Context, awsCfg *aws.Config, connectionCtx db.ConnectionContext, awsCredential *storepb.DataSource_AWSCredential)

Source from the content-addressed store, hash-verified

20// This is a shared utility used by multiple AWS service drivers (Elasticsearch, RDS, etc.).
21// Returns an error if role assumption fails.
22func AssumeRoleIfNeeded(ctx context.Context, awsCfg *aws.Config, connectionCtx db.ConnectionContext, awsCredential *storepb.DataSource_AWSCredential) error {
23 // If no role ARN is provided, no assumption needed
24 if awsCredential == nil || awsCredential.RoleArn == "" {
25 return nil
26 }
27
28 roleArn := awsCredential.RoleArn
29
30 // Create STS client with base credentials
31 stsClient := sts.NewFromConfig(*awsCfg)
32
33 // Generate descriptive session name for CloudTrail auditing
34 sessionName := generateSessionName(connectionCtx.InstanceID)
35
36 // Configure assume role provider
37 assumeRoleProvider := stscreds.NewAssumeRoleProvider(stsClient, roleArn,
38 func(o *stscreds.AssumeRoleOptions) {
39 o.RoleSessionName = sessionName
40 o.Duration = 1 * time.Hour // Temporary credentials valid for 1 hour
41
42 // Add external ID if provided for additional security
43 if externalID := awsCredential.ExternalId; externalID != "" {
44 o.ExternalID = &externalID
45 }
46 })
47
48 // Update config with assumed role credentials
49 awsCfg.Credentials = assumeRoleProvider
50
51 // Test credentials retrieval and provide context-specific error messages
52 _, err := awsCfg.Credentials.Retrieve(ctx)
53 if err != nil {
54 return handleAssumeRoleError(err, roleArn, awsCredential.ExternalId)
55 }
56
57 return nil
58}
59
60// generateSessionName creates a descriptive session name for AWS CloudTrail auditing.
61// Format: bytebase-{instance-id}-{timestamp}

Callers 3

getRDSConnectionMethod · 0.92
getRDSConnectionPasswordFunction · 0.92
openWithOpenSearchClientFunction · 0.92

Calls 2

generateSessionNameFunction · 0.85
handleAssumeRoleErrorFunction · 0.85

Tested by

no test coverage detected