forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_account.go
More file actions
119 lines (98 loc) · 3.37 KB
/
aws_account.go
File metadata and controls
119 lines (98 loc) · 3.37 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package saml2aws
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/url"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
)
// AWSAccount holds the AWS account name and roles
type AWSAccount struct {
Name string
Roles []*AWSRole
}
// ParseAWSAccounts extract the aws accounts from the saml assertion
func ParseAWSAccounts(audience string, samlAssertion string) ([]*AWSAccount, error) {
// log.Println("=== DEBUG ParseAWSAccounts ===")
// log.Printf("Posting to audience: %s", audience)
// log.Printf("SAML assertion length: %d", len(samlAssertion))
res, err := http.PostForm(audience, url.Values{"SAMLResponse": {samlAssertion}})
if err != nil {
log.Printf("ERROR: http.PostForm failed: %v", err)
return nil, errors.Wrap(err, "error retrieving AWS login form")
}
defer res.Body.Close()
log.Printf("Response status: %s", res.Status)
log.Printf("Response headers: %v", res.Header)
data, err := io.ReadAll(res.Body)
if err != nil {
log.Printf("ERROR: reading response body failed: %v", err)
return nil, errors.Wrap(err, "error retrieving AWS login body")
}
// log.Printf("Response body length: %d bytes", len(data))
// log.Println(string(data))
// log.Println("First 500 chars of response:")
// if len(data) > 500 {
// log.Println(string(data[:500]))
// } else {
// log.Println(string(data))
// }
log.Println("=== END DEBUG ParseAWSAccounts ===")
return ExtractAWSAccounts(data)
}
// ExtractAWSAccounts extract the accounts from the AWS html page
func ExtractAWSAccounts(data []byte) ([]*AWSAccount, error) {
log.Println("=== DEBUG ExtractAWSAccounts ===")
accounts := []*AWSAccount{}
doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(data))
if err != nil {
log.Printf("ERROR: failed to build document: %v", err)
return nil, errors.Wrap(err, "failed to build document from response")
}
log.Println("Searching for fieldset > div.saml-account elements...")
foundCount := 0
doc.Find("fieldset > div.saml-account").Each(func(i int, s *goquery.Selection) {
foundCount++
account := new(AWSAccount)
account.Name = s.Find("div.saml-account-name").Text()
log.Printf("Found account %d: %s", i, account.Name)
roleCount := 0
s.Find("label").Each(func(i int, s *goquery.Selection) {
role := new(AWSRole)
role.Name = s.Text()
role.RoleARN, _ = s.Attr("for")
log.Printf(" Role %d: %s (ARN: %s)", i, role.Name, role.RoleARN)
account.Roles = append(account.Roles, role)
roleCount++
})
log.Printf("Account has %d roles", roleCount)
accounts = append(accounts, account)
})
log.Printf("Found %d total accounts with saml-account class", foundCount)
log.Println("=== END DEBUG ExtractAWSAccounts ===")
return accounts, nil
}
// AssignPrincipals assign principal from roles
func AssignPrincipals(awsRoles []*AWSRole, awsAccounts []*AWSAccount) {
awsPrincipalARNs := make(map[string]string)
for _, awsRole := range awsRoles {
awsPrincipalARNs[awsRole.RoleARN] = awsRole.PrincipalARN
}
for _, awsAccount := range awsAccounts {
for _, awsRole := range awsAccount.Roles {
awsRole.PrincipalARN = awsPrincipalARNs[awsRole.RoleARN]
}
}
}
// LocateRole locate role by name
func LocateRole(awsRoles []*AWSRole, roleName string) (*AWSRole, error) {
for _, awsRole := range awsRoles {
if awsRole.RoleARN == roleName {
return awsRole, nil
}
}
return nil, fmt.Errorf("Supplied RoleArn not found in saml assertion: %s", roleName)
}