Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support saml2tencentcloud #1

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 0 additions & 84 deletions aws_account.go

This file was deleted.

60 changes: 0 additions & 60 deletions aws_role.go

This file was deleted.

32 changes: 0 additions & 32 deletions aws_role_test.go

This file was deleted.

136 changes: 136 additions & 0 deletions cloud_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package saml2aws

import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"

"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"github.com/versent/saml2aws/v2/pkg/cloud"
)

// CloudAccount holds the AWS account name and roles
type CloudAccount struct {
Name string
Roles []*CloudRole
}

func AssignAWSAccounts(awsRoles []*CloudRole, samlXml []byte, samlAssertion string) ([]*CloudRole, error) {
roleArnMap := make(map[string]*CloudRole)
for _, role := range awsRoles {
roleArnMap[role.RoleARN] = role
}

aud, err := ExtractDestinationURL(samlXml)
if err != nil {
return nil, errors.Wrap(err, "Error parsing destination URL.")
}

res, err := http.PostForm(aud, url.Values{"SAMLResponse": {samlAssertion}})
if err != nil {
return nil, errors.Wrap(err, "Error retrieving cloud SAML login form.")
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Unexpected status code: %d", res.StatusCode)
}

data, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "Error retrieving AWS login body.")
}

doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(data))
if err != nil {
return nil, errors.Wrap(err, "failed to build document from response")
}

doc.Find("fieldset > div.saml-account").Each(func(i int, s *goquery.Selection) {
name := s.Find("div.saml-account-name").Text()
s.Find("label").Each(func(i int, s *goquery.Selection) {
arn, _ := s.Attr("for")
roleArnMap[arn].Account = name
// log.Println("Marked role", arn, "as belonging to account", name)
})
})

return awsRoles, nil
}

// ParseCloudAccounts extract the aws accounts from the saml assertion
func ParseCloudAccounts(provider cloud.Provider, audience string, samlAssertion string) ([]*CloudAccount, error) {

switch provider {
case cloud.AWS:
res, err := http.PostForm(audience, url.Values{"SAMLResponse": {samlAssertion}})
if err != nil {
return nil, errors.Wrap(err, "error retrieving cloud SAML login form")
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}

data, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "error retrieving AWS login body")
}
return ExtractAWSAccounts(data)
case cloud.TencentCloud:
return nil, nil
default:
return nil, fmt.Errorf("unsupported cloud provider: %s", provider)
}
}

// ExtractAWSAccounts extract the accounts from the AWS html page
func ExtractAWSAccounts(data []byte) ([]*CloudAccount, error) {
accounts := make([]*CloudAccount, 0)

doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(data))
if err != nil {
return nil, errors.Wrap(err, "failed to build document from response")
}

doc.Find("fieldset > div.saml-account").Each(func(i int, s *goquery.Selection) {
account := new(CloudAccount)
account.Name = s.Find("div.saml-account-name").Text()
s.Find("label").Each(func(i int, s *goquery.Selection) {
role := new(CloudRole)
role.Name = s.Text()
role.RoleARN, _ = s.Attr("for")
account.Roles = append(account.Roles, role)
})
accounts = append(accounts, account)
})

return accounts, nil
}

// AssignPrincipals assign principal from roles
func AssignPrincipals(awsRoles []*CloudRole, cloudAccounts []*CloudAccount) {

awsPrincipalARNs := make(map[string]string)
for _, awsRole := range awsRoles {
awsPrincipalARNs[awsRole.RoleARN] = awsRole.PrincipalARN
}

for _, awsAccount := range cloudAccounts {
for _, awsRole := range awsAccount.Roles {
awsRole.PrincipalARN = awsPrincipalARNs[awsRole.RoleARN]
}
}

}

// LocateRole locate role by name
func LocateRole(awsRoles []*CloudRole, roleName string) (*CloudRole, 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)
}
8 changes: 4 additions & 4 deletions aws_account_test.go → cloud_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ func TestExtractAWSAccounts(t *testing.T) {
}

func TestAssignPrincipals(t *testing.T) {
awsRoles := []*AWSRole{
awsRoles := []*CloudRole{
{
PrincipalARN: "arn:aws:iam::000000000001:saml-provider/test-idp",
RoleARN: "arn:aws:iam::000000000001:role/Development",
},
}

awsAccounts := []*AWSAccount{
awsAccounts := []*CloudAccount{
{
Roles: []*AWSRole{
Roles: []*CloudRole{
{
RoleARN: "arn:aws:iam::000000000001:role/Development",
},
Expand All @@ -59,7 +59,7 @@ func TestAssignPrincipals(t *testing.T) {
}

func TestLocateRole(t *testing.T) {
awsRoles := []*AWSRole{
awsRoles := []*CloudRole{
{
PrincipalARN: "arn:aws:iam::000000000001:saml-provider/test-idp",
RoleARN: "arn:aws:iam::000000000001:role/Development",
Expand Down
Loading