1package aws
2
3import (
4	"net/http"
5	"time"
6
7	"github.com/aws/aws-sdk-go/aws/credentials"
8	"github.com/aws/aws-sdk-go/aws/endpoints"
9)
10
11// UseServiceDefaultRetries instructs the config to use the service's own
12// default number of retries. This will be the default action if
13// Config.MaxRetries is nil also.
14const UseServiceDefaultRetries = -1
15
16// RequestRetryer is an alias for a type that implements the request.Retryer
17// interface.
18type RequestRetryer interface{}
19
20// A Config provides service configuration for service clients. By default,
21// all clients will use the defaults.DefaultConfig structure.
22//
23//     // Create Session with MaxRetry configuration to be shared by multiple
24//     // service clients.
25//     sess := session.Must(session.NewSession(&aws.Config{
26//         MaxRetries: aws.Int(3),
27//     }))
28//
29//     // Create S3 service client with a specific Region.
30//     svc := s3.New(sess, &aws.Config{
31//         Region: aws.String("us-west-2"),
32//     })
33type Config struct {
34	// Enables verbose error printing of all credential chain errors.
35	// Should be used when wanting to see all errors while attempting to
36	// retrieve credentials.
37	CredentialsChainVerboseErrors *bool
38
39	// The credentials object to use when signing requests. Defaults to a
40	// chain of credential providers to search for credentials in environment
41	// variables, shared credential file, and EC2 Instance Roles.
42	Credentials *credentials.Credentials
43
44	// An optional endpoint URL (hostname only or fully qualified URI)
45	// that overrides the default generated endpoint for a client. Set this
46	// to `""` to use the default generated endpoint.
47	//
48	// Note: You must still provide a `Region` value when specifying an
49	// endpoint for a client.
50	Endpoint *string
51
52	// The resolver to use for looking up endpoints for AWS service clients
53	// to use based on region.
54	EndpointResolver endpoints.Resolver
55
56	// EnforceShouldRetryCheck is used in the AfterRetryHandler to always call
57	// ShouldRetry regardless of whether or not if request.Retryable is set.
58	// This will utilize ShouldRetry method of custom retryers. If EnforceShouldRetryCheck
59	// is not set, then ShouldRetry will only be called if request.Retryable is nil.
60	// Proper handling of the request.Retryable field is important when setting this field.
61	EnforceShouldRetryCheck *bool
62
63	// The region to send requests to. This parameter is required and must
64	// be configured globally or on a per-client basis unless otherwise
65	// noted. A full list of regions is found in the "Regions and Endpoints"
66	// document.
67	//
68	// See http://docs.aws.amazon.com/general/latest/gr/rande.html for AWS
69	// Regions and Endpoints.
70	Region *string
71
72	// Set this to `true` to disable SSL when sending requests. Defaults
73	// to `false`.
74	DisableSSL *bool
75
76	// The HTTP client to use when sending requests. Defaults to
77	// `http.DefaultClient`.
78	HTTPClient *http.Client
79
80	// An integer value representing the logging level. The default log level
81	// is zero (LogOff), which represents no logging. To enable logging set
82	// to a LogLevel Value.
83	LogLevel *LogLevelType
84
85	// The logger writer interface to write logging messages to. Defaults to
86	// standard out.
87	Logger Logger
88
89	// The maximum number of times that a request will be retried for failures.
90	// Defaults to -1, which defers the max retry setting to the service
91	// specific configuration.
92	MaxRetries *int
93
94	// Retryer guides how HTTP requests should be retried in case of
95	// recoverable failures.
96	//
97	// When nil or the value does not implement the request.Retryer interface,
98	// the client.DefaultRetryer will be used.
99	//
100	// When both Retryer and MaxRetries are non-nil, the former is used and
101	// the latter ignored.
102	//
103	// To set the Retryer field in a type-safe manner and with chaining, use
104	// the request.WithRetryer helper function:
105	//
106	//   cfg := request.WithRetryer(aws.NewConfig(), myRetryer)
107	//
108	Retryer RequestRetryer
109
110	// Disables semantic parameter validation, which validates input for
111	// missing required fields and/or other semantic request input errors.
112	DisableParamValidation *bool
113
114	// Disables the computation of request and response checksums, e.g.,
115	// CRC32 checksums in Amazon DynamoDB.
116	DisableComputeChecksums *bool
117
118	// Set this to `true` to force the request to use path-style addressing,
119	// i.e., `http://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client
120	// will use virtual hosted bucket addressing when possible
121	// (`http://BUCKET.s3.amazonaws.com/KEY`).
122	//
123	// Note: This configuration option is specific to the Amazon S3 service.
124	//
125	// See http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
126	// for Amazon S3: Virtual Hosting of Buckets
127	S3ForcePathStyle *bool
128
129	// Set this to `true` to disable the SDK adding the `Expect: 100-Continue`
130	// header to PUT requests over 2MB of content. 100-Continue instructs the
131	// HTTP client not to send the body until the service responds with a
132	// `continue` status. This is useful to prevent sending the request body
133	// until after the request is authenticated, and validated.
134	//
135	// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
136	//
137	// 100-Continue is only enabled for Go 1.6 and above. See `http.Transport`'s
138	// `ExpectContinueTimeout` for information on adjusting the continue wait
139	// timeout. https://golang.org/pkg/net/http/#Transport
140	//
141	// You should use this flag to disble 100-Continue if you experience issues
142	// with proxies or third party S3 compatible services.
143	S3Disable100Continue *bool
144
145	// Set this to `true` to enable S3 Accelerate feature. For all operations
146	// compatible with S3 Accelerate will use the accelerate endpoint for
147	// requests. Requests not compatible will fall back to normal S3 requests.
148	//
149	// The bucket must be enable for accelerate to be used with S3 client with
150	// accelerate enabled. If the bucket is not enabled for accelerate an error
151	// will be returned. The bucket name must be DNS compatible to also work
152	// with accelerate.
153	S3UseAccelerate *bool
154
155	// S3DisableContentMD5Validation config option is temporarily disabled,
156	// For S3 GetObject API calls, #1837.
157	//
158	// Set this to `true` to disable the S3 service client from automatically
159	// adding the ContentMD5 to S3 Object Put and Upload API calls. This option
160	// will also disable the SDK from performing object ContentMD5 validation
161	// on GetObject API calls.
162	S3DisableContentMD5Validation *bool
163
164	// Set this to `true` to disable the EC2Metadata client from overriding the
165	// default http.Client's Timeout. This is helpful if you do not want the
166	// EC2Metadata client to create a new http.Client. This options is only
167	// meaningful if you're not already using a custom HTTP client with the
168	// SDK. Enabled by default.
169	//
170	// Must be set and provided to the session.NewSession() in order to disable
171	// the EC2Metadata overriding the timeout for default credentials chain.
172	//
173	// Example:
174	//    sess := session.Must(session.NewSession(aws.NewConfig()
175	//       .WithEC2MetadataDiableTimeoutOverride(true)))
176	//
177	//    svc := s3.New(sess)
178	//
179	EC2MetadataDisableTimeoutOverride *bool
180
181	// Instructs the endpoint to be generated for a service client to
182	// be the dual stack endpoint. The dual stack endpoint will support
183	// both IPv4 and IPv6 addressing.
184	//
185	// Setting this for a service which does not support dual stack will fail
186	// to make requets. It is not recommended to set this value on the session
187	// as it will apply to all service clients created with the session. Even
188	// services which don't support dual stack endpoints.
189	//
190	// If the Endpoint config value is also provided the UseDualStack flag
191	// will be ignored.
192	//
193	// Only supported with.
194	//
195	//     sess := session.Must(session.NewSession())
196	//
197	//     svc := s3.New(sess, &aws.Config{
198	//         UseDualStack: aws.Bool(true),
199	//     })
200	UseDualStack *bool
201
202	// SleepDelay is an override for the func the SDK will call when sleeping
203	// during the lifecycle of a request. Specifically this will be used for
204	// request delays. This value should only be used for testing. To adjust
205	// the delay of a request see the aws/client.DefaultRetryer and
206	// aws/request.Retryer.
207	//
208	// SleepDelay will prevent any Context from being used for canceling retry
209	// delay of an API operation. It is recommended to not use SleepDelay at all
210	// and specify a Retryer instead.
211	SleepDelay func(time.Duration)
212
213	// DisableRestProtocolURICleaning will not clean the URL path when making rest protocol requests.
214	// Will default to false. This would only be used for empty directory names in s3 requests.
215	//
216	// Example:
217	//    sess := session.Must(session.NewSession(&aws.Config{
218	//         DisableRestProtocolURICleaning: aws.Bool(true),
219	//    }))
220	//
221	//    svc := s3.New(sess)
222	//    out, err := svc.GetObject(&s3.GetObjectInput {
223	//    	Bucket: aws.String("bucketname"),
224	//    	Key: aws.String("//foo//bar//moo"),
225	//    })
226	DisableRestProtocolURICleaning *bool
227
228	// EnableEndpointDiscovery will allow for endpoint discovery on operations that
229	// have the definition in its model. By default, endpoint discovery is off.
230	//
231	// Example:
232	//    sess := session.Must(session.NewSession(&aws.Config{
233	//         EnableEndpointDiscovery: aws.Bool(true),
234	//    }))
235	//
236	//    svc := s3.New(sess)
237	//    out, err := svc.GetObject(&s3.GetObjectInput {
238	//    	Bucket: aws.String("bucketname"),
239	//    	Key: aws.String("/foo/bar/moo"),
240	//    })
241	EnableEndpointDiscovery *bool
242
243	// DisableEndpointHostPrefix will disable the SDK's behavior of prefixing
244	// request endpoint hosts with modeled information.
245	//
246	// Disabling this feature is useful when you want to use local endpoints
247	// for testing that do not support the modeled host prefix pattern.
248	DisableEndpointHostPrefix *bool
249}
250
251// NewConfig returns a new Config pointer that can be chained with builder
252// methods to set multiple configuration values inline without using pointers.
253//
254//     // Create Session with MaxRetry configuration to be shared by multiple
255//     // service clients.
256//     sess := session.Must(session.NewSession(aws.NewConfig().
257//         WithMaxRetries(3),
258//     ))
259//
260//     // Create S3 service client with a specific Region.
261//     svc := s3.New(sess, aws.NewConfig().
262//         WithRegion("us-west-2"),
263//     )
264func NewConfig() *Config {
265	return &Config{}
266}
267
268// WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
269// a Config pointer.
270func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
271	c.CredentialsChainVerboseErrors = &verboseErrs
272	return c
273}
274
275// WithCredentials sets a config Credentials value returning a Config pointer
276// for chaining.
277func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
278	c.Credentials = creds
279	return c
280}
281
282// WithEndpoint sets a config Endpoint value returning a Config pointer for
283// chaining.
284func (c *Config) WithEndpoint(endpoint string) *Config {
285	c.Endpoint = &endpoint
286	return c
287}
288
289// WithEndpointResolver sets a config EndpointResolver value returning a
290// Config pointer for chaining.
291func (c *Config) WithEndpointResolver(resolver endpoints.Resolver) *Config {
292	c.EndpointResolver = resolver
293	return c
294}
295
296// WithRegion sets a config Region value returning a Config pointer for
297// chaining.
298func (c *Config) WithRegion(region string) *Config {
299	c.Region = &region
300	return c
301}
302
303// WithDisableSSL sets a config DisableSSL value returning a Config pointer
304// for chaining.
305func (c *Config) WithDisableSSL(disable bool) *Config {
306	c.DisableSSL = &disable
307	return c
308}
309
310// WithHTTPClient sets a config HTTPClient value returning a Config pointer
311// for chaining.
312func (c *Config) WithHTTPClient(client *http.Client) *Config {
313	c.HTTPClient = client
314	return c
315}
316
317// WithMaxRetries sets a config MaxRetries value returning a Config pointer
318// for chaining.
319func (c *Config) WithMaxRetries(max int) *Config {
320	c.MaxRetries = &max
321	return c
322}
323
324// WithDisableParamValidation sets a config DisableParamValidation value
325// returning a Config pointer for chaining.
326func (c *Config) WithDisableParamValidation(disable bool) *Config {
327	c.DisableParamValidation = &disable
328	return c
329}
330
331// WithDisableComputeChecksums sets a config DisableComputeChecksums value
332// returning a Config pointer for chaining.
333func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
334	c.DisableComputeChecksums = &disable
335	return c
336}
337
338// WithLogLevel sets a config LogLevel value returning a Config pointer for
339// chaining.
340func (c *Config) WithLogLevel(level LogLevelType) *Config {
341	c.LogLevel = &level
342	return c
343}
344
345// WithLogger sets a config Logger value returning a Config pointer for
346// chaining.
347func (c *Config) WithLogger(logger Logger) *Config {
348	c.Logger = logger
349	return c
350}
351
352// WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
353// pointer for chaining.
354func (c *Config) WithS3ForcePathStyle(force bool) *Config {
355	c.S3ForcePathStyle = &force
356	return c
357}
358
359// WithS3Disable100Continue sets a config S3Disable100Continue value returning
360// a Config pointer for chaining.
361func (c *Config) WithS3Disable100Continue(disable bool) *Config {
362	c.S3Disable100Continue = &disable
363	return c
364}
365
366// WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
367// pointer for chaining.
368func (c *Config) WithS3UseAccelerate(enable bool) *Config {
369	c.S3UseAccelerate = &enable
370	return c
371
372}
373
374// WithS3DisableContentMD5Validation sets a config
375// S3DisableContentMD5Validation value returning a Config pointer for chaining.
376func (c *Config) WithS3DisableContentMD5Validation(enable bool) *Config {
377	c.S3DisableContentMD5Validation = &enable
378	return c
379
380}
381
382// WithUseDualStack sets a config UseDualStack value returning a Config
383// pointer for chaining.
384func (c *Config) WithUseDualStack(enable bool) *Config {
385	c.UseDualStack = &enable
386	return c
387}
388
389// WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
390// returning a Config pointer for chaining.
391func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
392	c.EC2MetadataDisableTimeoutOverride = &enable
393	return c
394}
395
396// WithSleepDelay overrides the function used to sleep while waiting for the
397// next retry. Defaults to time.Sleep.
398func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
399	c.SleepDelay = fn
400	return c
401}
402
403// WithEndpointDiscovery will set whether or not to use endpoint discovery.
404func (c *Config) WithEndpointDiscovery(t bool) *Config {
405	c.EnableEndpointDiscovery = &t
406	return c
407}
408
409// WithDisableEndpointHostPrefix will set whether or not to use modeled host prefix
410// when making requests.
411func (c *Config) WithDisableEndpointHostPrefix(t bool) *Config {
412	c.DisableEndpointHostPrefix = &t
413	return c
414}
415
416// MergeIn merges the passed in configs into the existing config object.
417func (c *Config) MergeIn(cfgs ...*Config) {
418	for _, other := range cfgs {
419		mergeInConfig(c, other)
420	}
421}
422
423func mergeInConfig(dst *Config, other *Config) {
424	if other == nil {
425		return
426	}
427
428	if other.CredentialsChainVerboseErrors != nil {
429		dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
430	}
431
432	if other.Credentials != nil {
433		dst.Credentials = other.Credentials
434	}
435
436	if other.Endpoint != nil {
437		dst.Endpoint = other.Endpoint
438	}
439
440	if other.EndpointResolver != nil {
441		dst.EndpointResolver = other.EndpointResolver
442	}
443
444	if other.Region != nil {
445		dst.Region = other.Region
446	}
447
448	if other.DisableSSL != nil {
449		dst.DisableSSL = other.DisableSSL
450	}
451
452	if other.HTTPClient != nil {
453		dst.HTTPClient = other.HTTPClient
454	}
455
456	if other.LogLevel != nil {
457		dst.LogLevel = other.LogLevel
458	}
459
460	if other.Logger != nil {
461		dst.Logger = other.Logger
462	}
463
464	if other.MaxRetries != nil {
465		dst.MaxRetries = other.MaxRetries
466	}
467
468	if other.Retryer != nil {
469		dst.Retryer = other.Retryer
470	}
471
472	if other.DisableParamValidation != nil {
473		dst.DisableParamValidation = other.DisableParamValidation
474	}
475
476	if other.DisableComputeChecksums != nil {
477		dst.DisableComputeChecksums = other.DisableComputeChecksums
478	}
479
480	if other.S3ForcePathStyle != nil {
481		dst.S3ForcePathStyle = other.S3ForcePathStyle
482	}
483
484	if other.S3Disable100Continue != nil {
485		dst.S3Disable100Continue = other.S3Disable100Continue
486	}
487
488	if other.S3UseAccelerate != nil {
489		dst.S3UseAccelerate = other.S3UseAccelerate
490	}
491
492	if other.S3DisableContentMD5Validation != nil {
493		dst.S3DisableContentMD5Validation = other.S3DisableContentMD5Validation
494	}
495
496	if other.UseDualStack != nil {
497		dst.UseDualStack = other.UseDualStack
498	}
499
500	if other.EC2MetadataDisableTimeoutOverride != nil {
501		dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
502	}
503
504	if other.SleepDelay != nil {
505		dst.SleepDelay = other.SleepDelay
506	}
507
508	if other.DisableRestProtocolURICleaning != nil {
509		dst.DisableRestProtocolURICleaning = other.DisableRestProtocolURICleaning
510	}
511
512	if other.EnforceShouldRetryCheck != nil {
513		dst.EnforceShouldRetryCheck = other.EnforceShouldRetryCheck
514	}
515
516	if other.EnableEndpointDiscovery != nil {
517		dst.EnableEndpointDiscovery = other.EnableEndpointDiscovery
518	}
519
520	if other.DisableEndpointHostPrefix != nil {
521		dst.DisableEndpointHostPrefix = other.DisableEndpointHostPrefix
522	}
523}
524
525// Copy will return a shallow copy of the Config object. If any additional
526// configurations are provided they will be merged into the new config returned.
527func (c *Config) Copy(cfgs ...*Config) *Config {
528	dst := &Config{}
529	dst.MergeIn(c)
530
531	for _, cfg := range cfgs {
532		dst.MergeIn(cfg)
533	}
534
535	return dst
536}
537