diff options
| author | chrislu <chris.lu@gmail.com> | 2025-08-30 11:18:03 -0700 |
|---|---|---|
| committer | chrislu <chris.lu@gmail.com> | 2025-08-30 11:18:03 -0700 |
| commit | 87021a146027f83f911619f71b9c27bd51e9d55a (patch) | |
| tree | c7720f1c285683ce19d28931bd7c11b5475a2844 /weed/iam/utils/arn_utils.go | |
| parent | 0748214c8e2f497a84b9392d2d7d4ec976bc84eb (diff) | |
| parent | 879d512b552d834136cfb746a239e6168e5c4ffb (diff) | |
| download | seaweedfs-origin/add-ec-vacuum.tar.xz seaweedfs-origin/add-ec-vacuum.zip | |
Merge branch 'master' into add-ec-vacuumorigin/add-ec-vacuum
Diffstat (limited to 'weed/iam/utils/arn_utils.go')
| -rw-r--r-- | weed/iam/utils/arn_utils.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/weed/iam/utils/arn_utils.go b/weed/iam/utils/arn_utils.go new file mode 100644 index 000000000..f4c05dab1 --- /dev/null +++ b/weed/iam/utils/arn_utils.go @@ -0,0 +1,39 @@ +package utils + +import "strings" + +// ExtractRoleNameFromPrincipal extracts role name from principal ARN +// Handles both STS assumed role and IAM role formats +func ExtractRoleNameFromPrincipal(principal string) string { + // Handle STS assumed role format: arn:seaweed:sts::assumed-role/RoleName/SessionName + stsPrefix := "arn:seaweed:sts::assumed-role/" + if strings.HasPrefix(principal, stsPrefix) { + remainder := principal[len(stsPrefix):] + // Split on first '/' to get role name + if slashIndex := strings.Index(remainder, "/"); slashIndex != -1 { + return remainder[:slashIndex] + } + // If no slash found, return the remainder (edge case) + return remainder + } + + // Handle IAM role format: arn:seaweed:iam::role/RoleName + iamPrefix := "arn:seaweed:iam::role/" + if strings.HasPrefix(principal, iamPrefix) { + return principal[len(iamPrefix):] + } + + // Return empty string to signal invalid ARN format + // This allows callers to handle the error explicitly instead of masking it + return "" +} + +// ExtractRoleNameFromArn extracts role name from an IAM role ARN +// Specifically handles: arn:seaweed:iam::role/RoleName +func ExtractRoleNameFromArn(roleArn string) string { + prefix := "arn:seaweed:iam::role/" + if strings.HasPrefix(roleArn, prefix) && len(roleArn) > len(prefix) { + return roleArn[len(prefix):] + } + return "" +} |
