Skip to content

Commit 77f5c56

Browse files
author
Viktor Vergunov
committed
Add support for profile selection in clusterawsadm bootstrap credentials encode-as-profile
1 parent 7b09356 commit 77f5c56

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

cmd/clusterawsadm/cmd/bootstrap/credentials/credentials.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,13 @@ func generateAWSDefaultProfileWithChain() *cobra.Command {
131131
region = backupAWSRegion
132132
}
133133

134-
awsCreds, err := creds.NewAWSCredentialFromDefaultChain(region)
134+
profile := flags.GetProfile(c)
135+
if profile == "" {
136+
fmt.Fprintf(os.Stderr, "Could not resolve AWS profile, defaulting to default.\n\n")
137+
profile = "default"
138+
}
139+
140+
awsCreds, err := creds.NewAWSCredentialFromDefaultChain(region, profile)
135141
if err != nil {
136142
return flags.ResolveAWSError(err)
137143
}
@@ -156,5 +162,6 @@ func generateAWSDefaultProfileWithChain() *cobra.Command {
156162

157163
newCmd.Flags().String("output", string(base64SharedConfig), "Output for credential configuration (rawSharedConfig, base64SharedConfig)")
158164
flags.AddRegionFlag(newCmd)
165+
flags.AddProfileFlag(newCmd)
159166
return newCmd
160167
}

cmd/clusterawsadm/cmd/flags/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,13 @@ func MarkAlphaDeprecated(c *cobra.Command) {
6767
func CredentialWarning(c *cobra.Command) {
6868
fmt.Fprintf(os.Stderr, "\nWARNING: `%s` should only be used for bootstrapping.\n\n", c.Name())
6969
}
70+
71+
// AddProfileFlag will add a profile flag to the cli.
72+
func AddProfileFlag(c *cobra.Command) {
73+
c.Flags().String("profile", "", "The AWS profile to use for authentication")
74+
}
75+
76+
// GetProfile will return the AWS profile to use.
77+
func GetProfile(c *cobra.Command) string {
78+
return c.Flags().Lookup("profile").Value.String()
79+
}

cmd/clusterawsadm/credentials/credentials.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ type AWSCredentials struct {
5858

5959
// NewAWSCredentialFromDefaultChain will create a new credential provider chain from the
6060
// default chain.
61-
func NewAWSCredentialFromDefaultChain(region string) (*AWSCredentials, error) {
61+
func NewAWSCredentialFromDefaultChain(region, profile string) (*AWSCredentials, error) {
6262
creds := AWSCredentials{}
6363
conf := aws.NewConfig()
6464
conf.CredentialsChainVerboseErrors = aws.Bool(true)
6565
sess, err := session.NewSessionWithOptions(session.Options{
6666
SharedConfigState: session.SharedConfigEnable,
67+
Profile: profile,
6768
Config: *conf,
6869
})
6970
if err != nil {

0 commit comments

Comments
 (0)