forked from aliyun/saml2alibabacloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathram_role.go
58 lines (45 loc) · 1.17 KB
/
ram_role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package saml2alibabacloud
import (
"fmt"
"strings"
)
// RamRole AlibabaCloud RAM role attributes
type RamRole struct {
RoleARN string
PrincipalARN string
Name string
}
// ParseRamRoles parses and splits the roles while also validating the contents
func ParseRamRoles(roles []string) ([]*RamRole, error) {
ramRoles := make([]*RamRole, len(roles))
for i, role := range roles {
ramRole, err := parseRole(role)
if err != nil {
return nil, err
}
ramRoles[i] = ramRole
}
return ramRoles, nil
}
func parseRole(role string) (*RamRole, error) {
tokens := strings.Split(role, ",")
if len(tokens) != 2 {
return nil, fmt.Errorf("invalid role string only %d tokens", len(tokens))
}
ramRole := &RamRole{}
for _, token := range tokens {
if strings.Contains(token, ":saml-provider") {
ramRole.PrincipalARN = strings.TrimSpace(token)
}
if strings.Contains(token, ":role") {
ramRole.RoleARN = strings.TrimSpace(token)
}
}
if ramRole.PrincipalARN == "" {
return nil, fmt.Errorf("unable to locate `PrincipalARN` in: %s", role)
}
if ramRole.RoleARN == "" {
return nil, fmt.Errorf("unable to locate `RoleARN` in: %s", role)
}
return ramRole, nil
}