forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_role.go
58 lines (45 loc) · 1.15 KB
/
aws_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 saml2aws
import (
"fmt"
"strings"
)
// AWSRole aws role attributes
type AWSRole struct {
RoleARN string
PrincipalARN string
Name string
}
// ParseAWSRoles parses and splits the roles while also validating the contents
func ParseAWSRoles(roles []string) ([]*AWSRole, error) {
awsRoles := make([]*AWSRole, len(roles))
for i, role := range roles {
awsRole, err := parseRole(role)
if err != nil {
return nil, err
}
awsRoles[i] = awsRole
}
return awsRoles, nil
}
func parseRole(role string) (*AWSRole, error) {
tokens := strings.Split(role, ",")
if len(tokens) != 2 {
return nil, fmt.Errorf("Invalid role string only %d tokens", len(tokens))
}
awsRole := &AWSRole{}
for _, token := range tokens {
if strings.Contains(token, ":saml-provider") {
awsRole.PrincipalARN = strings.TrimSpace(token)
}
if strings.Contains(token, ":role") {
awsRole.RoleARN = strings.TrimSpace(token)
}
}
if awsRole.PrincipalARN == "" {
return nil, fmt.Errorf("Unable to locate PrincipalARN in: %s", role)
}
if awsRole.RoleARN == "" {
return nil, fmt.Errorf("Unable to locate RoleARN in: %s", role)
}
return awsRole, nil
}