Skip to content

Commit

Permalink
fix(cli)!: use default creds when deploying "test" env with "copilot …
Browse files Browse the repository at this point in the history
…init" (#1242)

Removes the --profile flag from the "init" command as
it's most likely not used + this change is the expected behavior from
customers.

Fixes #1068 #1104

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
  • Loading branch information
efekarakus authored Aug 10, 2020
1 parent b9cb3e4 commit 65d115c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
17 changes: 13 additions & 4 deletions internal/pkg/aws/sessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"time"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/copilot-cli/internal/pkg/version"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -23,7 +24,7 @@ import (
const (
userAgentHeader = "User-Agent"

credsTimeout = 10 * time.Second
credsTimeout = 10 * time.Second
clientTimeout = 30 * time.Second
)

Expand Down Expand Up @@ -65,7 +66,7 @@ func (p *Provider) Default() (*session.Session, error) {
// DefaultWithRegion returns a session configured against the "default" AWS profile and the input region.
func (p *Provider) DefaultWithRegion(region string) (*session.Session, error) {
sess, err := session.NewSessionWithOptions(session.Options{
Config: *newConfig().WithRegion(region),
Config: *newConfig().WithRegion(region),
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
Expand Down Expand Up @@ -112,14 +113,22 @@ func (p *Provider) FromRole(roleARN string, region string) (*session.Session, er
// AreCredsFromEnvVars returns true if the session's credentials provider is environment variables, false otherwise.
// An error is returned if the credentials are invalid or the request times out.
func AreCredsFromEnvVars(sess *session.Session) (bool, error) {
v, err := creds(sess)
if err != nil {
return false, err
}
return v.ProviderName == session.EnvProviderName, nil
}

func creds(sess *session.Session) (credentials.Value, error) {
ctx, cancel := context.WithTimeout(context.Background(), credsTimeout)
defer cancel()

v, err := sess.Config.Credentials.GetWithContext(ctx)
if err != nil {
return false, fmt.Errorf("get credentials of session: %w", err)
return credentials.Value{}, fmt.Errorf("get credentials of session: %w", err)
}
return v.ProviderName == session.EnvProviderName, nil
return v, nil
}

// newConfig returns a config with an end-to-end request timeout and verbose credentials errors.
Expand Down
12 changes: 6 additions & 6 deletions internal/pkg/aws/sessions/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// mockProvider implements the AWS SDK's credentials.Provider interface.
type mockProvider struct {
value credentials.Value
err error
err error
}

func (m mockProvider) Retrieve() (credentials.Value, error) {
Expand All @@ -34,17 +34,17 @@ func TestAreCredsFromEnvVars(t *testing.T) {
testCases := map[string]struct {
inSess *session.Session

wantedOk bool
wantedOk bool
wantedErr error
} {
}{
"returns true if the credentials come from environment variables": {
inSess: &session.Session{
Config: &aws.Config{
Credentials: credentials.NewCredentials(mockProvider{
value: credentials.Value{
ProviderName: session.EnvProviderName,
},
err: nil,
err: nil,
}),
},
},
Expand All @@ -57,7 +57,7 @@ func TestAreCredsFromEnvVars(t *testing.T) {
value: credentials.Value{
ProviderName: credentials.SharedCredsProviderName,
},
err: nil,
err: nil,
}),
},
},
Expand All @@ -81,7 +81,7 @@ func TestAreCredsFromEnvVars(t *testing.T) {
ok, err := AreCredsFromEnvVars(tc.inSess)

if tc.wantedErr != nil {
require.EqualError(t, tc.wantedErr, err.Error())
require.EqualError(t, err, tc.wantedErr.Error())
} else {
require.Equal(t, tc.wantedOk, ok)
}
Expand Down
14 changes: 12 additions & 2 deletions internal/pkg/cli/env_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type initEnvOpts struct {
configureRuntimeClients func(*initEnvOpts) error
}

func configureInitEnvClients(o *initEnvOpts) error {
func configureInitEnvFromFlags(o *initEnvOpts) error {
var sess *session.Session
if o.Profile != "" {
profileSess, err := sessions.NewProvider().FromProfile(o.Profile)
Expand All @@ -150,6 +150,16 @@ func configureInitEnvClients(o *initEnvOpts) error {
return nil
}

func configureInitEnvFromDefaultSess(o *initEnvOpts) error {
sess, err := sessions.NewProvider().Default()
if err != nil {
return fmt.Errorf("create default session: %w", err)
}
o.envIdentity = identity.New(sess)
o.envDeployer = deploycfn.New(sess)
return nil
}

func newInitEnvOpts(vars initEnvVars) (*initEnvOpts, error) {
store, err := config.NewStore()
if err != nil {
Expand All @@ -171,7 +181,7 @@ func newInitEnvOpts(vars initEnvVars) (*initEnvOpts, error) {
identity: identity.New(defaultSession),
profileConfig: cfg,
prog: termprogress.NewSpinner(),
configureRuntimeClients: configureInitEnvClients,
configureRuntimeClients: configureInitEnvFromFlags,
}, nil
}

Expand Down
11 changes: 4 additions & 7 deletions internal/pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type initVars struct {
svcType string
svcName string
dockerfilePath string
profile string
imageTag string
port uint16
}
Expand Down Expand Up @@ -81,14 +80,14 @@ func newInitOpts(vars initVars) (*initOpts, error) {
return nil, err
}
sessProvider := sessions.NewProvider()
sess, err := sessProvider.Default()
defaultSess, err := sessProvider.Default()
if err != nil {
return nil, err
}
prompt := prompt.New()
spin := termprogress.NewSpinner()
id := identity.New(sess)
deployer := cloudformation.New(sess)
id := identity.New(defaultSess)
deployer := cloudformation.New(defaultSess)
cfg, err := profile.NewConfig()
if err != nil {
return nil, err
Expand Down Expand Up @@ -126,7 +125,6 @@ func newInitOpts(vars initVars) (*initOpts, error) {
initEnvVars: initEnvVars{
GlobalOpts: NewGlobalOpts(),
Name: defaultEnvironmentName,
Profile: vars.profile,
IsProduction: false,
},
store: ssm,
Expand All @@ -135,7 +133,7 @@ func newInitOpts(vars initVars) (*initOpts, error) {
prog: spin,
identity: id,

configureRuntimeClients: configureInitEnvClients,
configureRuntimeClients: configureInitEnvFromDefaultSess,
}

deploySvcCmd := &deploySvcOpts{
Expand Down Expand Up @@ -293,7 +291,6 @@ func BuildInitCmd() *cobra.Command {
return nil
}),
}
cmd.Flags().StringVar(&vars.profile, profileFlag, defaultEnvironmentProfile, profileFlagDescription)
cmd.Flags().StringVarP(&vars.appName, appFlag, appFlagShort, "", appFlagDescription)
cmd.Flags().StringVarP(&vars.svcName, svcFlag, svcFlagShort, "", svcFlagDescription)
cmd.Flags().StringVarP(&vars.svcType, svcTypeFlag, svcTypeFlagShort, "", svcTypeFlagDescription)
Expand Down

0 comments on commit 65d115c

Please sign in to comment.