Skip to content

Commit 9566f0c

Browse files
committed
Implement assume-time policy limiting
1 parent 17ef4e6 commit 9566f0c

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

cmd/saml2aws/commands/login.go

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
b64 "encoding/base64"
55
"encoding/json"
66
"fmt"
7+
"io/ioutil"
78
"log"
89
"os"
910
"strings"
@@ -365,6 +366,22 @@ func loginToStsUsingRole(account *cfg.IDPAccount, role *saml2aws.AWSRole, samlAs
365366
DurationSeconds: aws.Int64(int64(account.SessionDuration)),
366367
}
367368

369+
if account.PolicyFile != "" {
370+
policy, err := ioutil.ReadFile(account.PolicyFile)
371+
if err != nil {
372+
return nil, errors.Wrap(err, fmt.Sprintf("Failed to load supplimental policy file: %s", account.PolicyFile))
373+
}
374+
params.Policy = aws.String(string(policy))
375+
}
376+
377+
if account.PolicyARNs != "" {
378+
var arns []*sts.PolicyDescriptorType
379+
for _, arn := range strings.Split(account.PolicyARNs, ",") {
380+
arns = append(arns, &sts.PolicyDescriptorType{Arn: aws.String(arn)})
381+
}
382+
params.PolicyArns = arns
383+
}
384+
368385
log.Println("Requesting AWS credentials using SAML assertion.")
369386

370387
resp, err := svc.AssumeRoleWithSAML(params)

cmd/saml2aws/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func main() {
8383
app.Flag("password", "The password used to login. (env: SAML2AWS_PASSWORD)").Envar("SAML2AWS_PASSWORD").StringVar(&commonFlags.Password)
8484
app.Flag("mfa-token", "The current MFA token (supported in Keycloak, ADFS, GoogleApps). (env: SAML2AWS_MFA_TOKEN)").Envar("SAML2AWS_MFA_TOKEN").StringVar(&commonFlags.MFAToken)
8585
app.Flag("role", "The ARN of the role to assume. (env: SAML2AWS_ROLE)").Envar("SAML2AWS_ROLE").StringVar(&commonFlags.RoleArn)
86+
app.Flag("policyfile", "The file containing the supplemental AssumeRole policy. (env: SAML2AWS_POLICY_FILE)").Envar("SAML2AWS_POLICY_FILE").StringVar(&commonFlags.PolicyFile)
87+
app.Flag("policyarns", "The ARN of supplemental policies to restrict the token. (env: SAML2AWS_POLICY_ARNS)").Envar("SAML2AWS_POLICY_ARNS").StringVar(&commonFlags.PolicyARNs)
8688
app.Flag("aws-urn", "The URN used by SAML when you login. (env: SAML2AWS_AWS_URN)").Envar("SAML2AWS_AWS_URN").StringVar(&commonFlags.AmazonWebservicesURN)
8789
app.Flag("skip-prompt", "Skip prompting for parameters during login.").BoolVar(&commonFlags.SkipPrompt)
8890
app.Flag("session-duration", "The duration of your AWS Session. (env: SAML2AWS_SESSION_DURATION)").Envar("SAML2AWS_SESSION_DURATION").IntVar(&commonFlags.SessionDuration)

pkg/cfg/cfg.go

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type IDPAccount struct {
5252
ResourceID string `ini:"resource_id"` // used by F5APM
5353
Subdomain string `ini:"subdomain"` // used by OneLogin
5454
RoleARN string `ini:"role_arn"`
55+
PolicyFile string `ini:"policy_file"`
56+
PolicyARNs string `ini:"policy_arn_list"`
5557
Region string `ini:"region"`
5658
HttpAttemptsCount string `ini:"http_attempts_count"`
5759
HttpRetryDelay string `ini:"http_retry_delay"`

pkg/flags/flags.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type CommonFlags struct {
2222
Username string
2323
Password string
2424
RoleArn string
25+
PolicyFile string
26+
PolicyARNs string
2527
AmazonWebservicesURN string
2628
SessionDuration int
2729
SkipPrompt bool
@@ -115,6 +117,12 @@ func ApplyFlagOverrides(commonFlags *CommonFlags, account *cfg.IDPAccount) {
115117
if commonFlags.RoleArn != "" {
116118
account.RoleARN = commonFlags.RoleArn
117119
}
120+
if commonFlags.PolicyFile != "" {
121+
account.PolicyFile = commonFlags.PolicyFile
122+
}
123+
if commonFlags.PolicyARNs != "" {
124+
account.PolicyARNs = commonFlags.PolicyARNs
125+
}
118126
if commonFlags.ResourceID != "" {
119127
account.ResourceID = commonFlags.ResourceID
120128
}

0 commit comments

Comments
 (0)