arn_utils.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utils
  2. import "strings"
  3. // ExtractRoleNameFromPrincipal extracts role name from principal ARN
  4. // Handles both STS assumed role and IAM role formats
  5. func ExtractRoleNameFromPrincipal(principal string) string {
  6. // Handle STS assumed role format: arn:seaweed:sts::assumed-role/RoleName/SessionName
  7. stsPrefix := "arn:seaweed:sts::assumed-role/"
  8. if strings.HasPrefix(principal, stsPrefix) {
  9. remainder := principal[len(stsPrefix):]
  10. // Split on first '/' to get role name
  11. if slashIndex := strings.Index(remainder, "/"); slashIndex != -1 {
  12. return remainder[:slashIndex]
  13. }
  14. // If no slash found, return the remainder (edge case)
  15. return remainder
  16. }
  17. // Handle IAM role format: arn:seaweed:iam::role/RoleName
  18. iamPrefix := "arn:seaweed:iam::role/"
  19. if strings.HasPrefix(principal, iamPrefix) {
  20. return principal[len(iamPrefix):]
  21. }
  22. // Return empty string to signal invalid ARN format
  23. // This allows callers to handle the error explicitly instead of masking it
  24. return ""
  25. }
  26. // ExtractRoleNameFromArn extracts role name from an IAM role ARN
  27. // Specifically handles: arn:seaweed:iam::role/RoleName
  28. func ExtractRoleNameFromArn(roleArn string) string {
  29. prefix := "arn:seaweed:iam::role/"
  30. if strings.HasPrefix(roleArn, prefix) && len(roleArn) > len(prefix) {
  31. return roleArn[len(prefix):]
  32. }
  33. return ""
  34. }