@@ -10,9 +10,9 @@ import (
10
10
"fmt"
11
11
"os"
12
12
"os/exec"
13
- "path/filepath"
14
13
"time"
15
14
15
+ "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
16
16
"github.com/spf13/cobra"
17
17
)
18
18
@@ -21,8 +21,8 @@ const (
21
21
)
22
22
23
23
var idpLoginAwsOpts struct {
24
- RoleARN string
25
- CredentialsFile string
24
+ RoleARN string
25
+ Profile string
26
26
}
27
27
28
28
var idpLoginAwsCmd = & cobra.Command {
@@ -42,7 +42,12 @@ var idpLoginAwsCmd = &cobra.Command{
42
42
return err
43
43
}
44
44
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 )
46
51
out , err := awsCmd .CombinedOutput ()
47
52
if err != nil {
48
53
return fmt .Errorf ("%w: %s" , err , string (out ))
@@ -60,15 +65,17 @@ var idpLoginAwsCmd = &cobra.Command{
60
65
return err
61
66
}
62
67
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
+ }
72
79
}
73
80
74
81
return nil
@@ -79,11 +86,6 @@ func init() {
79
86
idpLoginCmd .AddCommand (idpLoginAwsCmd )
80
87
81
88
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" )
89
91
}
0 commit comments