Skip to content

Commit fff69c9

Browse files
authored
[cli] Support gp idp login aws --duration-seconds (#18797)
1 parent 2565a1c commit fff69c9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

components/gitpod-cli/cmd/idp-login-aws.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,23 @@ const (
2121
)
2222

2323
var idpLoginAwsOpts struct {
24-
RoleARN string
25-
Profile string
24+
RoleARN string
25+
Profile string
26+
DurationSeconds int
2627
}
2728

2829
var idpLoginAwsCmd = &cobra.Command{
2930
Use: "aws",
3031
Short: "Login to AWS",
32+
Long: "Obtains credentials to access AWS. The command delegates to `aws sts assume-role-with-web-identity`, see https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role-with-web-identity.html for more details.",
3133
RunE: func(cmd *cobra.Command, args []string) error {
3234
cmd.SilenceUsage = true
3335
if idpLoginAwsOpts.RoleARN == "" {
3436
return fmt.Errorf("missing --role-arn or IDP_AWS_ROLE_ARN env var")
3537
}
38+
if idpLoginAwsOpts.DurationSeconds <= 0 {
39+
return fmt.Errorf("invalid --duration-seconds: %d, must be a positive integer", idpLoginAwsOpts.DurationSeconds)
40+
}
3641

3742
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
3843
defer cancel()
@@ -47,7 +52,12 @@ var idpLoginAwsCmd = &cobra.Command{
4752
return err
4853
}
4954

50-
awsCmd := exec.Command("aws", "sts", "assume-role-with-web-identity", "--role-arn", idpLoginAwsOpts.RoleARN, "--role-session-name", fmt.Sprintf("%s-%d", wsInfo.WorkspaceId, time.Now().Unix()), "--web-identity-token", tkn)
55+
awsCmd := exec.Command("aws", "sts", "assume-role-with-web-identity",
56+
"--role-arn", idpLoginAwsOpts.RoleARN,
57+
"--role-session-name", fmt.Sprintf("%s-%d", wsInfo.WorkspaceId, time.Now().Unix()),
58+
"--web-identity-token", tkn,
59+
"--duration-seconds", fmt.Sprintf("%d", idpLoginAwsOpts.DurationSeconds),
60+
)
5161
out, err := awsCmd.CombinedOutput()
5262
if err != nil {
5363
return fmt.Errorf("%w: %s", err, string(out))
@@ -87,5 +97,6 @@ func init() {
8797

8898
idpLoginAwsCmd.Flags().StringVar(&idpLoginAwsOpts.RoleARN, "role-arn", os.Getenv("IDP_AWS_ROLE_ARN"), "AWS role to assume (defaults to IDP_AWS_ROLE_ARN env var)")
8999
idpLoginAwsCmd.Flags().StringVarP(&idpLoginAwsOpts.Profile, "profile", "p", "default", "AWS profile to configure")
100+
idpLoginAwsCmd.Flags().IntVarP(&idpLoginAwsOpts.DurationSeconds, "duration-seconds", "d", 3600, "Duration in seconds for which the credentials will be valid (defaults to 3600), upper bound is controlled by the AWS maximum session duration. See https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role-with-web-identity.html")
90101
_ = idpLoginAwsCmd.MarkFlagFilename("profile")
91102
}

0 commit comments

Comments
 (0)