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

Function generateSessionName

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

generateSessionName creates a descriptive session name for AWS CloudTrail auditing. Format: bytebase-{instance-id}-{timestamp} AWS constraints: 2-64 characters, matching pattern [\w+=,.@-]*

(instanceID string)

Source from the content-addressed store, hash-verified

61// Format: bytebase-{instance-id}-{timestamp}
62// AWS constraints: 2-64 characters, matching pattern [\w+=,.@-]*
63func generateSessionName(instanceID string) string {
64 // Sanitize instance ID to ensure valid session name (alphanumeric, =,.@-)
65 sanitizedID := "unknown"
66 if instanceID != "" {
67 sanitizedID = strings.Map(func(r rune) rune {
68 if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '-' {
69 return r
70 }
71 return '-'
72 }, instanceID)
73 }
74
75 // Generate timestamp
76 timestamp := fmt.Sprintf("%d", time.Now().Unix())
77
78 // Calculate max length for instance ID to stay within 64 char limit
79 // Format: "bytebase-" (9) + instanceID + "-" (1) + timestamp (10) = 20 overhead
80 // So instanceID can be at most 44 characters (64 - 20)
81 maxInstanceIDLength := 44
82 if len(sanitizedID) > maxInstanceIDLength {
83 // Truncate but keep the end part which is usually more unique
84 sanitizedID = sanitizedID[len(sanitizedID)-maxInstanceIDLength:]
85 }
86
87 sessionName := fmt.Sprintf("bytebase-%s-%s", sanitizedID, timestamp)
88
89 // Final safety check (should never happen with our math above)
90 if len(sessionName) > 64 {
91 sessionName = sessionName[:64]
92 }
93
94 return sessionName
95}
96
97// handleAssumeRoleError provides context-specific error messages for role assumption failures.
98func handleAssumeRoleError(err error, roleArn string, externalID string) error {

Callers 1

AssumeRoleIfNeededFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected