Skip to content

Commit 13496a8

Browse files
authored
add identity endpoint & unsafe url options (#104)
Issue #, if available: aws-controllers-k8s/community#1498 Description of changes: https://github.com/aws-controllers-k8s/runtime/blob/ae946aa1e0dd309db0064c2c347b724074725c60/pkg/config/config.go#L146-L154 As the comment says so perfectly, a temporary session is set up to get the account info from STS. This PR adds options to: 1. configure the endpoint of this identity call via `--aws-identity-endpoint-url`. 2. configure whether both `--aws-identity-endpoint-url` & `--aws-endpoint-url` should be checked for unsafe URL schema (aka http) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent ae946aa commit 13496a8

File tree

1 file changed

+69
-19
lines changed

1 file changed

+69
-19
lines changed

pkg/config/config.go

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"net/url"
2020

21+
"github.com/aws/aws-sdk-go/aws"
2122
"github.com/aws/aws-sdk-go/aws/session"
2223
"github.com/aws/aws-sdk-go/service/sts"
2324
"github.com/jaypipes/envutil"
@@ -30,17 +31,19 @@ import (
3031
)
3132

3233
const (
33-
flagEnableLeaderElection = "enable-leader-election"
34-
flagMetricAddr = "metrics-addr"
35-
flagEnableDevLogging = "enable-development-logging"
36-
flagAWSRegion = "aws-region"
37-
flagAWSEndpointURL = "aws-endpoint-url"
38-
flagLogLevel = "log-level"
39-
flagResourceTags = "resource-tags"
40-
flagWatchNamespace = "watch-namespace"
41-
flagEnableWebhookServer = "enable-webhook-server"
42-
flagWebhookServerAddr = "webhook-server-addr"
43-
envVarAWSRegion = "AWS_REGION"
34+
flagEnableLeaderElection = "enable-leader-election"
35+
flagMetricAddr = "metrics-addr"
36+
flagEnableDevLogging = "enable-development-logging"
37+
flagAWSRegion = "aws-region"
38+
flagAWSEndpointURL = "aws-endpoint-url"
39+
flagAWSIdentityEndpointURL = "aws-identity-endpoint-url"
40+
flagUnsafeAWSEndpointURLs = "allow-unsafe-aws-endpoint-urls"
41+
flagLogLevel = "log-level"
42+
flagResourceTags = "resource-tags"
43+
flagWatchNamespace = "watch-namespace"
44+
flagEnableWebhookServer = "enable-webhook-server"
45+
flagWebhookServerAddr = "webhook-server-addr"
46+
envVarAWSRegion = "AWS_REGION"
4447
)
4548

4649
var (
@@ -56,14 +59,16 @@ var (
5659
defaultLogLevel = zapcore.InfoLevel
5760
)
5861

59-
// Config contains configuration otpions for ACK service controllers
62+
// Config contains configuration options for ACK service controllers
6063
type Config struct {
6164
MetricsAddr string
6265
EnableLeaderElection bool
6366
EnableDevelopmentLogging bool
6467
AccountID string
6568
Region string
69+
IdentityEndpointURL string
6670
EndpointURL string
71+
AllowUnsafeEndpointURL bool
6772
LogLevel string
6873
ResourceTags []string
6974
WatchNamespace string
@@ -112,6 +117,18 @@ func (cfg *Config) BindFlags() {
112117
" flag that can be used to override the default behaviour of aws-sdk-go that constructs endpoint URLs"+
113118
" automatically based on service and region",
114119
)
120+
flag.StringVar(
121+
&cfg.IdentityEndpointURL, flagAWSIdentityEndpointURL,
122+
"",
123+
"The AWS endpoint URL the service controller will use to gather information from STS. This is an optional"+
124+
" flag that can be used to override the default behaviour of aws-sdk-go that constructs endpoint URLs"+
125+
" automatically based on service and region",
126+
)
127+
flag.BoolVar(
128+
&cfg.AllowUnsafeEndpointURL, flagUnsafeAWSEndpointURLs,
129+
false,
130+
"Allow an unsafe AWS endpoint URL over http",
131+
)
115132
flag.StringVar(
116133
&cfg.LogLevel, flagLogLevel,
117134
"info",
@@ -146,8 +163,14 @@ func (cfg *Config) SetupLogger() {
146163
// SetAWSAccountID uses sts GetCallerIdentity API to find AWS AccountId and set
147164
// in Config
148165
func (cfg *Config) SetAWSAccountID() error {
166+
167+
awsCfg := aws.Config{}
168+
if cfg.IdentityEndpointURL != "" {
169+
awsCfg.Endpoint = aws.String(cfg.IdentityEndpointURL)
170+
}
171+
149172
// use sts to find AWS AccountId
150-
session, err := session.NewSession()
173+
session, err := session.NewSession(&awsCfg)
151174
if err != nil {
152175
return fmt.Errorf("unable to create session: %v", err)
153176
}
@@ -162,24 +185,51 @@ func (cfg *Config) SetAWSAccountID() error {
162185

163186
// Validate ensures the options are valid
164187
func (cfg *Config) Validate() error {
165-
if err := cfg.SetAWSAccountID(); err != nil {
166-
return fmt.Errorf("unable to determine account ID: %v", err)
167-
}
168-
169188
if cfg.Region == "" {
170189
return errors.New("unable to start service controller as AWS region is missing. Please pass --aws-region flag or set AWS_REGION environment variable")
171190
}
172191

173192
if cfg.EndpointURL != "" {
174-
endpoint, err := url.Parse(cfg.EndpointURL)
175-
if err != nil || endpoint.Scheme != "https" && endpoint.Host != "" {
193+
serviceEndpoint, err := url.Parse(cfg.EndpointURL)
194+
if err != nil {
176195
return errors.New("invalid service endpoint. Please refer to " +
177196
"https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html for more details")
178197
}
198+
199+
// Throw an error if URL is unsafe and config.AllowUnsafeEndpointURL is not set accordingly
200+
if err := cfg.checkUnsafeEndpoint(serviceEndpoint); err != nil {
201+
return err
202+
}
203+
}
204+
205+
if cfg.IdentityEndpointURL != "" {
206+
identityEndpoint, err := url.Parse(cfg.IdentityEndpointURL)
207+
if err != nil {
208+
return errors.New("invalid identity endpoint. Please refer to " +
209+
"https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html for more details")
210+
}
211+
212+
// Throw an error if URL is unsafe and config.AllowUnsafeEndpointURL is not set accordingly
213+
if err := cfg.checkUnsafeEndpoint(identityEndpoint); err != nil {
214+
return err
215+
}
216+
}
217+
218+
if err := cfg.SetAWSAccountID(); err != nil {
219+
return fmt.Errorf("unable to determine account ID: %v", err)
179220
}
180221

181222
if cfg.EnableWebhookServer && cfg.WebhookServerAddr == "" {
182223
return errors.New("empty webhook server address")
183224
}
184225
return nil
185226
}
227+
228+
func (cfg *Config) checkUnsafeEndpoint(endpoint *url.URL) error {
229+
if !cfg.AllowUnsafeEndpointURL {
230+
if endpoint.Scheme != "https" && endpoint.Host != "" {
231+
return errors.New("using an unsafe endpoint is not allowed. Please review the controller configuration")
232+
}
233+
}
234+
return nil
235+
}

0 commit comments

Comments
 (0)