Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit e41b4a2

Browse files
thisisaaronlandsfomuseumbot
andauthored
Update Cognito/STS code to support optional session policies (#3)
* snapshot: block out tool to derive creds from cognito * snapshot: derive credentials, not sure anything works though * rename aws-sts-credentials as aws-cognito-credentials * snapshot: add cmd/aws-credentials-json-to-ini * add docs for cognito and credentials json-to-ini stuff * snapshot: add hooks to assign session policies * update cognito/sts code to support optional session policies --------- Co-authored-by: sfomuseumbot <sfomuseumbot@localhost>
1 parent 9e77da2 commit e41b4a2

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Usage of ./bin/aws-cognito-credentials:
3636
A valid AWS IAM role ARN to assign to STS credentials.
3737
-role-session-name string
3838
An identifier for the assumed role session.
39+
-session-policy value
40+
Zero or more IAM ARNs to use as session policies to supplement the default role ARN.
3941
```
4042

4143
For example:

cmd/aws-cognito-credentials/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ func main() {
2222
var duration int
2323

2424
var kv_logins multi.KeyValueString
25-
25+
var session_policies multi.MultiString
26+
2627
flag.StringVar(&aws_config_uri, "aws-config-uri", "", "A valid github.com/aaronland/go-aws-auth.Config URI.")
2728

2829
flag.StringVar(&identity_pool_id, "identity-pool-id", "", "A valid AWS Cognito Identity Pool ID.")
2930
flag.StringVar(&role_arn, "role-arn", "", "A valid AWS IAM role ARN to assign to STS credentials.")
3031
flag.StringVar(&role_session_name, "role-session-name", "", "An identifier for the assumed role session.")
3132
flag.IntVar(&duration, "duration", 900, "The duration, in seconds, of the role session. Can not be less than 900.") // Note: Can not be less than 900
3233
flag.Var(&kv_logins, "login", "One or more key=value strings mapping to AWS Cognito authentication providers.")
33-
34+
flag.Var(&session_policies, "session-policy", "Zero or more IAM ARNs to use as session policies to supplement the default role ARN.")
35+
3436
flag.Parse()
3537

3638
ctx := context.Background()
@@ -53,6 +55,7 @@ func main() {
5355
Duration: int32(duration),
5456
IdentityPoolId: identity_pool_id,
5557
Logins: logins,
58+
Policies: session_policies,
5659
}
5760

5861
creds, err := auth.STSCredentialsForDeveloperIdentity(ctx, cfg, opts)

cognito.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type STSCredentialsForDeveloperIdentityOptions struct {
2222
RoleSessionName string
2323
// The duration, in seconds, of the role session.
2424
Duration int32
25+
// An optional list of Amazon Resource Names (ARNs) that you want to use as managed session policies.
26+
Policies []string
2527
}
2628

2729
// STSCredentialsForDeveloperIdentity generate temporary STS (AWS) credentials for a developer identity.
@@ -52,6 +54,25 @@ func STSCredentialsForDeveloperIdentity(ctx context.Context, aws_cfg aws.Config,
5254
DurationSeconds: aws.Int32(opts.Duration),
5355
}
5456

57+
// https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/sts#AssumeRoleWithWebIdentityInput
58+
// https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_session
59+
60+
if len(opts.Policies) > 0 {
61+
62+
session_policies := make([]types.PolicyDescriptorType, len(opts.Policies))
63+
64+
for idx, arn := range opts.Policies {
65+
66+
session_policies[idx] = types.PolicyDescriptorType{
67+
Arn: aws.String(arn),
68+
}
69+
}
70+
71+
creds_opts.PolicyArns = session_policies
72+
}
73+
74+
// https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html
75+
5576
creds_rsp, err := sts_client.AssumeRoleWithWebIdentity(ctx, creds_opts)
5677

5778
if err != nil {

0 commit comments

Comments
 (0)