Skip to content

Commit 6080101

Browse files
committed
Only use sts GetCallerIdentity to find AWS AccountID
1 parent 20deb45 commit 6080101

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

pkg/config/config.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package config
1515

1616
import (
1717
"errors"
18+
"fmt"
1819
"net/url"
1920

2021
"github.com/aws/aws-sdk-go/aws/session"
@@ -30,14 +31,14 @@ const (
3031
flagEnableLeaderElection = "enable-leader-election"
3132
flagMetricAddr = "metrics-addr"
3233
flagEnableDevLogging = "enable-development-logging"
33-
flagAWSAccountID = "aws-account-id"
3434
flagAWSRegion = "aws-region"
3535
flagAWSEndpointURL = "aws-endpoint-url"
3636
flagLogLevel = "log-level"
3737
flagResourceTags = "resource-tags"
3838
flagWatchNamespace = "watch-namespace"
3939
flagEnableWebhookServer = "enable-webhook-server"
4040
flagWebhookServerAddr = "webhook-server-addr"
41+
envVarAWSRegion = "AWS_REGION"
4142
)
4243

4344
// Config contains configuration otpions for ACK service controllers
@@ -84,14 +85,9 @@ func (cfg *Config) BindFlags() {
8485
"Configures the logger to use a Zap development config (encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn, no sampling), "+
8586
"otherwise a Zap production config will be used (encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error), sampling).",
8687
)
87-
flag.StringVar(
88-
&cfg.AccountID, flagAWSAccountID,
89-
envutil.WithDefault("AWS_ACCOUNT_ID", ""),
90-
"The AWS Account ID in which the service controller will create resources",
91-
)
9288
flag.StringVar(
9389
&cfg.Region, flagAWSRegion,
94-
envutil.WithDefault("AWS_REGION", ""),
90+
envutil.WithDefault(envVarAWSRegion, ""),
9591
"The AWS Region in which the service controller will create its resources",
9692
)
9793
flag.StringVar(
@@ -137,27 +133,29 @@ func (cfg *Config) SetupLogger() {
137133
ctrlrt.SetLogger(zap.New(zap.UseFlagOptions(&zapOptions)))
138134
}
139135

140-
// PopulateAccountIdIfMissing uses sts GetCallerIdentity API to find
141-
// AWS AccountId when Config.AccountId is empty
142-
func (cfg *Config) PopulateAccountIdIfMissing() error {
143-
if cfg.AccountID == "" {
144-
// use sts to find AWS AccountId
145-
session := session.Must(session.NewSession())
146-
client := sts.New(session)
147-
res, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})
148-
if err == nil {
149-
cfg.AccountID = *res.Account
150-
}
151-
return err
136+
// SetAWSAccountID uses sts GetCallerIdentity API to find AWS AccountId and set
137+
// in Config
138+
func (cfg *Config) SetAWSAccountID() error {
139+
// use sts to find AWS AccountId
140+
session, err := session.NewSession()
141+
if err != nil {
142+
return fmt.Errorf("unable to create session: %v", err)
152143
}
144+
client := sts.New(session)
145+
res, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})
146+
if err != nil {
147+
return fmt.Errorf("unable to get caller identity: %v", err)
148+
}
149+
cfg.AccountID = *res.Account
153150
return nil
154151
}
155152

156153
// Validate ensures the options are valid
157154
func (cfg *Config) Validate() error {
158-
if cfg.AccountID == "" {
159-
return errors.New("unable to start service controller as account ID is missing. Please pass --aws-account-id flag or set AWS_ACCOUNT_ID environment variable")
155+
if err := cfg.SetAWSAccountID(); err != nil {
156+
return errors.New("unable to determine account ID. Please make sure AWS credentials are setup in controller pod")
160157
}
158+
161159
if cfg.Region == "" {
162160
return errors.New("unable to start service controller as AWS region is missing. Please pass --aws-region flag or set AWS_REGION environment variable")
163161
}

0 commit comments

Comments
 (0)