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 tructure.
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
69	//   AWS 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	// @see http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
125	//   Amazon S3: Virtual Hosting of Buckets
126	S3ForcePathStyle *bool
127
128	// Set this to `true` to disable the SDK adding the `Expect: 100-Continue`
129	// header to PUT requests over 2MB of content. 100-Continue instructs the
130	// HTTP client not to send the body until the service responds with a
131	// `continue` status. This is useful to prevent sending the request body
132	// until after the request is authenticated, and validated.
133	//
134	// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
135	//
136	// 100-Continue is only enabled for Go 1.6 and above. See `http.Transport`'s
137	// `ExpectContinueTimeout` for information on adjusting the continue wait
138	// timeout. https://golang.org/pkg/net/http/#Transport
139	//
140	// You should use this flag to disble 100-Continue if you experience issues
141	// with proxies or third party S3 compatible services.
142	S3Disable100Continue *bool
143
144	// Set this to `true` to enable S3 Accelerate feature. For all operations
145	// compatible with S3 Accelerate will use the accelerate endpoint for
146	// requests. Requests not compatible will fall back to normal S3 requests.
147	//
148	// The bucket must be enable for accelerate to be used with S3 client with
149	// accelerate enabled. If the bucket is not enabled for accelerate an error
150	// will be returned. The bucket name must be DNS compatible to also work
151	// with accelerate.
152	S3UseAccelerate *bool
153
154	// S3DisableContentMD5Validation config option is temporarily disabled,
155	// For S3 GetObject API calls, #1837.
156	//
157	// Set this to `true` to disable the S3 service client from automatically
158	// adding the ContentMD5 to S3 Object Put and Upload API calls. This option
159	// will also disable the SDK from performing object ContentMD5 validation
160	// on GetObject API calls.
161	S3DisableContentMD5Validation *bool
162
163	// Set this to `true` to disable the EC2Metadata client from overriding the
164	// default http.Client's Timeout. This is helpful if you do not want the
165	// EC2Metadata client to create a new http.Client. This options is only
166	// meaningful if you're not already using a custom HTTP client with the
167	// SDK. Enabled by default.
168	//
169	// Must be set and provided to the session.NewSession() in order to disable
170	// the EC2Metadata overriding the timeout for default credentials chain.
171	//
172	// Example:
173	//    sess := session.Must(session.NewSession(aws.NewConfig()
174	//       .WithEC2MetadataDiableTimeoutOverride(true)))
175	//
176	//    svc := s3.New(sess)
177	//
178	EC2MetadataDisableTimeoutOverride *bool
179
180	// Instructs the endpoint to be generated for a service client to
181	// be the dual stack endpoint. The dual stack endpoint will support
182	// both IPv4 and IPv6 addressing.
183	//
184	// Setting this for a service which does not support dual stack will fail
185	// to make requets. It is not recommended to set this value on the session
186	// as it will apply to all service clients created with the session. Even
187	// services which don't support dual stack endpoints.
188	//
189	// If the Endpoint config value is also provided the UseDualStack flag
190	// will be ignored.
191	//
192	// Only supported with.
193	//
194	//     sess := session.Must(session.NewSession())
195	//
196	//     svc := s3.New(sess, &aws.Config{
197	//         UseDualStack: aws.Bool(true),
198	//     })
199	UseDualStack *bool
200
201	// SleepDelay is an override for the func the SDK will call when sleeping
202	// during the lifecycle of a request. Specifically this will be used for
203	// request delays. This value should only be used for testing. To adjust
204	// the delay of a request see the aws/client.DefaultRetryer and
205	// aws/request.Retryer.
206	//
207	// SleepDelay will prevent any Context from being used for canceling retry
208	// delay of an API operation. It is recommended to not use SleepDelay at all
209	// and specify a Retryer instead.
210	SleepDelay func(time.Duration)
211
212	// DisableRestProtocolURICleaning will not clean the URL path when making rest protocol requests.
213	// Will default to false. This would only be used for empty directory names in s3 requests.
214	//
215	// Example:
216	//    sess := session.Must(session.NewSession(&aws.Config{
217	//         DisableRestProtocolURICleaning: aws.Bool(true),
218	//    }))
219	//
220	//    svc := s3.New(sess)
221	//    out, err := svc.GetObject(&s3.GetObjectInput {
222	//    	Bucket: aws.String("bucketname"),
223	//    	Key: aws.String("//foo//bar//moo"),
224	//    })
225	DisableRestProtocolURICleaning *bool
226}
227
228// NewConfig returns a new Config pointer that can be chained with builder
229// methods to set multiple configuration values inline without using pointers.
230//
231//     // Create Session with MaxRetry configuration to be shared by multiple
232//     // service clients.
233//     sess := session.Must(session.NewSession(aws.NewConfig().
234//         WithMaxRetries(3),
235//     ))
236//
237//     // Create S3 service client with a specific Region.
238//     svc := s3.New(sess, aws.NewConfig().
239//         WithRegion("us-west-2"),
240//     )
241func NewConfig() *Config {
242	return &Config{}
243}
244
245// WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
246// a Config pointer.
247func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
248	c.CredentialsChainVerboseErrors = &verboseErrs
249	return c
250}
251
252// WithCredentials sets a config Credentials value returning a Config pointer
253// for chaining.
254func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
255	c.Credentials = creds
256	return c
257}
258
259// WithEndpoint sets a config Endpoint value returning a Config pointer for
260// chaining.
261func (c *Config) WithEndpoint(endpoint string) *Config {
262	c.Endpoint = &endpoint
263	return c
264}
265
266// WithEndpointResolver sets a config EndpointResolver value returning a
267// Config pointer for chaining.
268func (c *Config) WithEndpointResolver(resolver endpoints.Resolver) *Config {
269	c.EndpointResolver = resolver
270	return c
271}
272
273// WithRegion sets a config Region value returning a Config pointer for
274// chaining.
275func (c *Config) WithRegion(region string) *Config {
276	c.Region = &region
277	return c
278}
279
280// WithDisableSSL sets a config DisableSSL value returning a Config pointer
281// for chaining.
282func (c *Config) WithDisableSSL(disable bool) *Config {
283	c.DisableSSL = &disable
284	return c
285}
286
287// WithHTTPClient sets a config HTTPClient value returning a Config pointer
288// for chaining.
289func (c *Config) WithHTTPClient(client *http.Client) *Config {
290	c.HTTPClient = client
291	return c
292}
293
294// WithMaxRetries sets a config MaxRetries value returning a Config pointer
295// for chaining.
296func (c *Config) WithMaxRetries(max int) *Config {
297	c.MaxRetries = &max
298	return c
299}
300
301// WithDisableParamValidation sets a config DisableParamValidation value
302// returning a Config pointer for chaining.
303func (c *Config) WithDisableParamValidation(disable bool) *Config {
304	c.DisableParamValidation = &disable
305	return c
306}
307
308// WithDisableComputeChecksums sets a config DisableComputeChecksums value
309// returning a Config pointer for chaining.
310func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
311	c.DisableComputeChecksums = &disable
312	return c
313}
314
315// WithLogLevel sets a config LogLevel value returning a Config pointer for
316// chaining.
317func (c *Config) WithLogLevel(level LogLevelType) *Config {
318	c.LogLevel = &level
319	return c
320}
321
322// WithLogger sets a config Logger value returning a Config pointer for
323// chaining.
324func (c *Config) WithLogger(logger Logger) *Config {
325	c.Logger = logger
326	return c
327}
328
329// WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
330// pointer for chaining.
331func (c *Config) WithS3ForcePathStyle(force bool) *Config {
332	c.S3ForcePathStyle = &force
333	return c
334}
335
336// WithS3Disable100Continue sets a config S3Disable100Continue value returning
337// a Config pointer for chaining.
338func (c *Config) WithS3Disable100Continue(disable bool) *Config {
339	c.S3Disable100Continue = &disable
340	return c
341}
342
343// WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
344// pointer for chaining.
345func (c *Config) WithS3UseAccelerate(enable bool) *Config {
346	c.S3UseAccelerate = &enable
347	return c
348
349}
350
351// WithS3DisableContentMD5Validation sets a config
352// S3DisableContentMD5Validation value returning a Config pointer for chaining.
353func (c *Config) WithS3DisableContentMD5Validation(enable bool) *Config {
354	c.S3DisableContentMD5Validation = &enable
355	return c
356
357}
358
359// WithUseDualStack sets a config UseDualStack value returning a Config
360// pointer for chaining.
361func (c *Config) WithUseDualStack(enable bool) *Config {
362	c.UseDualStack = &enable
363	return c
364}
365
366// WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
367// returning a Config pointer for chaining.
368func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
369	c.EC2MetadataDisableTimeoutOverride = &enable
370	return c
371}
372
373// WithSleepDelay overrides the function used to sleep while waiting for the
374// next retry. Defaults to time.Sleep.
375func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
376	c.SleepDelay = fn
377	return c
378}
379
380// MergeIn merges the passed in configs into the existing config object.
381func (c *Config) MergeIn(cfgs ...*Config) {
382	for _, other := range cfgs {
383		mergeInConfig(c, other)
384	}
385}
386
387func mergeInConfig(dst *Config, other *Config) {
388	if other == nil {
389		return
390	}
391
392	if other.CredentialsChainVerboseErrors != nil {
393		dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
394	}
395
396	if other.Credentials != nil {
397		dst.Credentials = other.Credentials
398	}
399
400	if other.Endpoint != nil {
401		dst.Endpoint = other.Endpoint
402	}
403
404	if other.EndpointResolver != nil {
405		dst.EndpointResolver = other.EndpointResolver
406	}
407
408	if other.Region != nil {
409		dst.Region = other.Region
410	}
411
412	if other.DisableSSL != nil {
413		dst.DisableSSL = other.DisableSSL
414	}
415
416	if other.HTTPClient != nil {
417		dst.HTTPClient = other.HTTPClient
418	}
419
420	if other.LogLevel != nil {
421		dst.LogLevel = other.LogLevel
422	}
423
424	if other.Logger != nil {
425		dst.Logger = other.Logger
426	}
427
428	if other.MaxRetries != nil {
429		dst.MaxRetries = other.MaxRetries
430	}
431
432	if other.Retryer != nil {
433		dst.Retryer = other.Retryer
434	}
435
436	if other.DisableParamValidation != nil {
437		dst.DisableParamValidation = other.DisableParamValidation
438	}
439
440	if other.DisableComputeChecksums != nil {
441		dst.DisableComputeChecksums = other.DisableComputeChecksums
442	}
443
444	if other.S3ForcePathStyle != nil {
445		dst.S3ForcePathStyle = other.S3ForcePathStyle
446	}
447
448	if other.S3Disable100Continue != nil {
449		dst.S3Disable100Continue = other.S3Disable100Continue
450	}
451
452	if other.S3UseAccelerate != nil {
453		dst.S3UseAccelerate = other.S3UseAccelerate
454	}
455
456	if other.S3DisableContentMD5Validation != nil {
457		dst.S3DisableContentMD5Validation = other.S3DisableContentMD5Validation
458	}
459
460	if other.UseDualStack != nil {
461		dst.UseDualStack = other.UseDualStack
462	}
463
464	if other.EC2MetadataDisableTimeoutOverride != nil {
465		dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
466	}
467
468	if other.SleepDelay != nil {
469		dst.SleepDelay = other.SleepDelay
470	}
471
472	if other.DisableRestProtocolURICleaning != nil {
473		dst.DisableRestProtocolURICleaning = other.DisableRestProtocolURICleaning
474	}
475
476	if other.EnforceShouldRetryCheck != nil {
477		dst.EnforceShouldRetryCheck = other.EnforceShouldRetryCheck
478	}
479}
480
481// Copy will return a shallow copy of the Config object. If any additional
482// configurations are provided they will be merged into the new config returned.
483func (c *Config) Copy(cfgs ...*Config) *Config {
484	dst := &Config{}
485	dst.MergeIn(c)
486
487	for _, cfg := range cfgs {
488		dst.MergeIn(cfg)
489	}
490
491	return dst
492}
493