Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Usage of ./bin/aws-cognito-credentials:
A valid AWS IAM role ARN to assign to STS credentials.
-role-session-name string
An identifier for the assumed role session.
-session-policy value
Zero or more IAM ARNs to use as session policies to supplement the default role ARN.
```

For example:
Expand Down
7 changes: 5 additions & 2 deletions cmd/aws-cognito-credentials/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ func main() {
var duration int

var kv_logins multi.KeyValueString

var session_policies multi.MultiString

flag.StringVar(&aws_config_uri, "aws-config-uri", "", "A valid github.com/aaronland/go-aws-auth.Config URI.")

flag.StringVar(&identity_pool_id, "identity-pool-id", "", "A valid AWS Cognito Identity Pool ID.")
flag.StringVar(&role_arn, "role-arn", "", "A valid AWS IAM role ARN to assign to STS credentials.")
flag.StringVar(&role_session_name, "role-session-name", "", "An identifier for the assumed role session.")
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
flag.Var(&kv_logins, "login", "One or more key=value strings mapping to AWS Cognito authentication providers.")

flag.Var(&session_policies, "session-policy", "Zero or more IAM ARNs to use as session policies to supplement the default role ARN.")

flag.Parse()

ctx := context.Background()
Expand All @@ -53,6 +55,7 @@ func main() {
Duration: int32(duration),
IdentityPoolId: identity_pool_id,
Logins: logins,
Policies: session_policies,
}

creds, err := auth.STSCredentialsForDeveloperIdentity(ctx, cfg, opts)
Expand Down
21 changes: 21 additions & 0 deletions cognito.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type STSCredentialsForDeveloperIdentityOptions struct {
RoleSessionName string
// The duration, in seconds, of the role session.
Duration int32
// An optional list of Amazon Resource Names (ARNs) that you want to use as managed session policies.
Policies []string
}

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

// https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/sts#AssumeRoleWithWebIdentityInput
// https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_session

if len(opts.Policies) > 0 {

session_policies := make([]types.PolicyDescriptorType, len(opts.Policies))

for idx, arn := range opts.Policies {

session_policies[idx] = types.PolicyDescriptorType{
Arn: aws.String(arn),
}
}

creds_opts.PolicyArns = session_policies
}

// https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html

creds_rsp, err := sts_client.AssumeRoleWithWebIdentity(ctx, creds_opts)

if err != nil {
Expand Down