Skip to content

Commit 205e6fc

Browse files
authored
Improve gp idp login aws (#18601)
* [idp aws login] Make AWS profile configurable * Use workspaceID as session name
1 parent bf3d548 commit 205e6fc

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"fmt"
1111
"os"
1212
"os/exec"
13-
"path/filepath"
1413
"time"
1514

15+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
1616
"github.com/spf13/cobra"
1717
)
1818

@@ -21,8 +21,8 @@ const (
2121
)
2222

2323
var idpLoginAwsOpts struct {
24-
RoleARN string
25-
CredentialsFile string
24+
RoleARN string
25+
Profile string
2626
}
2727

2828
var idpLoginAwsCmd = &cobra.Command{
@@ -42,7 +42,12 @@ var idpLoginAwsCmd = &cobra.Command{
4242
return err
4343
}
4444

45-
awsCmd := exec.Command("aws", "sts", "assume-role-with-web-identity", "--role-arn", idpLoginAwsOpts.RoleARN, "--role-session-name", fmt.Sprintf("gitpod-%d", time.Now().Unix()), "--web-identity-token", tkn)
45+
wsInfo, err := gitpod.GetWSInfo(ctx)
46+
if err != nil {
47+
return err
48+
}
49+
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)
4651
out, err := awsCmd.CombinedOutput()
4752
if err != nil {
4853
return fmt.Errorf("%w: %s", err, string(out))
@@ -60,15 +65,17 @@ var idpLoginAwsCmd = &cobra.Command{
6065
return err
6166
}
6267

63-
credentials := "[default]\n"
64-
credentials += fmt.Sprintf("aws_access_key_id=%s\n", result.Credentials.AccessKeyId)
65-
credentials += fmt.Sprintf("aws_secret_access_key=%s\n", result.Credentials.SecretAccessKey)
66-
credentials += fmt.Sprintf("aws_session_token=%s\n", result.Credentials.SessionToken)
67-
68-
_ = os.MkdirAll(filepath.Dir(idpLoginAwsOpts.CredentialsFile), 0755)
69-
err = os.WriteFile(idpLoginAwsOpts.CredentialsFile, []byte(credentials), 0600)
70-
if err != nil {
71-
return err
68+
vars := map[string]string{
69+
"aws_access_key_id": result.Credentials.AccessKeyId,
70+
"aws_secret_access_key": result.Credentials.SecretAccessKey,
71+
"aws_session_token": result.Credentials.SessionToken,
72+
}
73+
for k, v := range vars {
74+
awsCmd := exec.Command("aws", "configure", "set", "--profile", idpLoginAwsOpts.Profile, k, v)
75+
out, err := awsCmd.CombinedOutput()
76+
if err != nil {
77+
return fmt.Errorf("%w: %s", err, string(out))
78+
}
7279
}
7380

7481
return nil
@@ -79,11 +86,6 @@ func init() {
7986
idpLoginCmd.AddCommand(idpLoginAwsCmd)
8087

8188
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)")
82-
83-
home, err := os.UserHomeDir()
84-
if err != nil {
85-
panic(err)
86-
}
87-
idpLoginAwsCmd.Flags().StringVar(&idpLoginAwsOpts.CredentialsFile, "credentials-file", filepath.Join(home, ".aws", "credentials"), "path to the AWS credentials file")
88-
_ = idpLoginAwsCmd.MarkFlagFilename("credentials-file")
89+
idpLoginAwsCmd.Flags().StringVarP(&idpLoginAwsOpts.Profile, "profile", "p", "default", "AWS profile to configure")
90+
_ = idpLoginAwsCmd.MarkFlagFilename("profile")
8991
}

0 commit comments

Comments
 (0)