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 MaxRetries 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	// STSRegionalEndpoint will enable regional or legacy endpoint resolving
251	STSRegionalEndpoint endpoints.STSRegionalEndpoint
252
253	// S3UsEast1RegionalEndpoint will enable regional or legacy endpoint resolving
254	S3UsEast1RegionalEndpoint endpoints.S3UsEast1RegionalEndpoint
255}
256
257// NewConfig returns a new Config pointer that can be chained with builder
258// methods to set multiple configuration values inline without using pointers.
259//
260//     // Create Session with MaxRetries configuration to be shared by multiple
261//     // service clients.
262//     sess := session.Must(session.NewSession(aws.NewConfig().
263//         WithMaxRetries(3),
264//     ))
265//
266//     // Create S3 service client with a specific Region.
267//     svc := s3.New(sess, aws.NewConfig().
268//         WithRegion("us-west-2"),
269//     )
270func NewConfig() *Config {
271	return &Config{}
272}
273
274// WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
275// a Config pointer.
276func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
277	c.CredentialsChainVerboseErrors = &verboseErrs
278	return c
279}
280
281// WithCredentials sets a config Credentials value returning a Config pointer
282// for chaining.
283func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
284	c.Credentials = creds
285	return c
286}
287
288// WithEndpoint sets a config Endpoint value returning a Config pointer for
289// chaining.
290func (c *Config) WithEndpoint(endpoint string) *Config {
291	c.Endpoint = &endpoint
292	return c
293}
294
295// WithEndpointResolver sets a config EndpointResolver value returning a
296// Config pointer for chaining.
297func (c *Config) WithEndpointResolver(resolver endpoints.Resolver) *Config {
298	c.EndpointResolver = resolver
299	return c
300}
301
302// WithRegion sets a config Region value returning a Config pointer for
303// chaining.
304func (c *Config) WithRegion(region string) *Config {
305	c.Region = &region
306	return c
307}
308
309// WithDisableSSL sets a config DisableSSL value returning a Config pointer
310// for chaining.
311func (c *Config) WithDisableSSL(disable bool) *Config {
312	c.DisableSSL = &disable
313	return c
314}
315
316// WithHTTPClient sets a config HTTPClient value returning a Config pointer
317// for chaining.
318func (c *Config) WithHTTPClient(client *http.Client) *Config {
319	c.HTTPClient = client
320	return c
321}
322
323// WithMaxRetries sets a config MaxRetries value returning a Config pointer
324// for chaining.
325func (c *Config) WithMaxRetries(max int) *Config {
326	c.MaxRetries = &max
327	return c
328}
329
330// WithDisableParamValidation sets a config DisableParamValidation value
331// returning a Config pointer for chaining.
332func (c *Config) WithDisableParamValidation(disable bool) *Config {
333	c.DisableParamValidation = &disable
334	return c
335}
336
337// WithDisableComputeChecksums sets a config DisableComputeChecksums value
338// returning a Config pointer for chaining.
339func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
340	c.DisableComputeChecksums = &disable
341	return c
342}
343
344// WithLogLevel sets a config LogLevel value returning a Config pointer for
345// chaining.
346func (c *Config) WithLogLevel(level LogLevelType) *Config {
347	c.LogLevel = &level
348	return c
349}
350
351// WithLogger sets a config Logger value returning a Config pointer for
352// chaining.
353func (c *Config) WithLogger(logger Logger) *Config {
354	c.Logger = logger
355	return c
356}
357
358// WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
359// pointer for chaining.
360func (c *Config) WithS3ForcePathStyle(force bool) *Config {
361	c.S3ForcePathStyle = &force
362	return c
363}
364
365// WithS3Disable100Continue sets a config S3Disable100Continue value returning
366// a Config pointer for chaining.
367func (c *Config) WithS3Disable100Continue(disable bool) *Config {
368	c.S3Disable100Continue = &disable
369	return c
370}
371
372// WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
373// pointer for chaining.
374func (c *Config) WithS3UseAccelerate(enable bool) *Config {
375	c.S3UseAccelerate = &enable
376	return c
377
378}
379
380// WithS3DisableContentMD5Validation sets a config
381// S3DisableContentMD5Validation value returning a Config pointer for chaining.
382func (c *Config) WithS3DisableContentMD5Validation(enable bool) *Config {
383	c.S3DisableContentMD5Validation = &enable
384	return c
385
386}
387
388// WithUseDualStack sets a config UseDualStack value returning a Config
389// pointer for chaining.
390func (c *Config) WithUseDualStack(enable bool) *Config {
391	c.UseDualStack = &enable
392	return c
393}
394
395// WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
396// returning a Config pointer for chaining.
397func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
398	c.EC2MetadataDisableTimeoutOverride = &enable
399	return c
400}
401
402// WithSleepDelay overrides the function used to sleep while waiting for the
403// next retry. Defaults to time.Sleep.
404func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
405	c.SleepDelay = fn
406	return c
407}
408
409// WithEndpointDiscovery will set whether or not to use endpoint discovery.
410func (c *Config) WithEndpointDiscovery(t bool) *Config {
411	c.EnableEndpointDiscovery = &t
412	return c
413}
414
415// WithDisableEndpointHostPrefix will set whether or not to use modeled host prefix
416// when making requests.
417func (c *Config) WithDisableEndpointHostPrefix(t bool) *Config {
418	c.DisableEndpointHostPrefix = &t
419	return c
420}
421
422// MergeIn merges the passed in configs into the existing config object.
423func (c *Config) MergeIn(cfgs ...*Config) {
424	for _, other := range cfgs {
425		mergeInConfig(c, other)
426	}
427}
428
429// WithSTSRegionalEndpoint will set whether or not to use regional endpoint flag
430// when resolving the endpoint for a service
431func (c *Config) WithSTSRegionalEndpoint(sre endpoints.STSRegionalEndpoint) *Config {
432	c.STSRegionalEndpoint = sre
433	return c
434}
435
436// WithS3UsEast1RegionalEndpoint will set whether or not to use regional endpoint flag
437// when resolving the endpoint for a service
438func (c *Config) WithS3UsEast1RegionalEndpoint(sre endpoints.S3UsEast1RegionalEndpoint) *Config {
439	c.S3UsEast1RegionalEndpoint = sre
440	return c
441}
442
443func mergeInConfig(dst *Config, other *Config) {
444	if other == nil {
445		return
446	}
447
448	if other.CredentialsChainVerboseErrors != nil {
449		dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
450	}
451
452	if other.Credentials != nil {
453		dst.Credentials = other.Credentials
454	}
455
456	if other.Endpoint != nil {
457		dst.Endpoint = other.Endpoint
458	}
459
460	if other.EndpointResolver != nil {
461		dst.EndpointResolver = other.EndpointResolver
462	}
463
464	if other.Region != nil {
465		dst.Region = other.Region
466	}
467
468	if other.DisableSSL != nil {
469		dst.DisableSSL = other.DisableSSL
470	}
471
472	if other.HTTPClient != nil {
473		dst.HTTPClient = other.HTTPClient
474	}
475
476	if other.LogLevel != nil {
477		dst.LogLevel = other.LogLevel
478	}
479
480	if other.Logger != nil {
481		dst.Logger = other.Logger
482	}
483
484	if other.MaxRetries != nil {
485		dst.MaxRetries = other.MaxRetries
486	}
487
488	if other.Retryer != nil {
489		dst.Retryer = other.Retryer
490	}
491
492	if other.DisableParamValidation != nil {
493		dst.DisableParamValidation = other.DisableParamValidation
494	}
495
496	if other.DisableComputeChecksums != nil {
497		dst.DisableComputeChecksums = other.DisableComputeChecksums
498	}
499
500	if other.S3ForcePathStyle != nil {
501		dst.S3ForcePathStyle = other.S3ForcePathStyle
502	}
503
504	if other.S3Disable100Continue != nil {
505		dst.S3Disable100Continue = other.S3Disable100Continue
506	}
507
508	if other.S3UseAccelerate != nil {
509		dst.S3UseAccelerate = other.S3UseAccelerate
510	}
511
512	if other.S3DisableContentMD5Validation != nil {
513		dst.S3DisableContentMD5Validation = other.S3DisableContentMD5Validation
514	}
515
516	if other.UseDualStack != nil {
517		dst.UseDualStack = other.UseDualStack
518	}
519
520	if other.EC2MetadataDisableTimeoutOverride != nil {
521		dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
522	}
523
524	if other.SleepDelay != nil {
525		dst.SleepDelay = other.SleepDelay
526	}
527
528	if other.DisableRestProtocolURICleaning != nil {
529		dst.DisableRestProtocolURICleaning = other.DisableRestProtocolURICleaning
530	}
531
532	if other.EnforceShouldRetryCheck != nil {
533		dst.EnforceShouldRetryCheck = other.EnforceShouldRetryCheck
534	}
535
536	if other.EnableEndpointDiscovery != nil {
537		dst.EnableEndpointDiscovery = other.EnableEndpointDiscovery
538	}
539
540	if other.DisableEndpointHostPrefix != nil {
541		dst.DisableEndpointHostPrefix = other.DisableEndpointHostPrefix
542	}
543
544	if other.STSRegionalEndpoint != endpoints.UnsetSTSEndpoint {
545		dst.STSRegionalEndpoint = other.STSRegionalEndpoint
546	}
547
548	if other.S3UsEast1RegionalEndpoint != endpoints.UnsetS3UsEast1Endpoint {
549		dst.S3UsEast1RegionalEndpoint = other.S3UsEast1RegionalEndpoint
550	}
551}
552
553// Copy will return a shallow copy of the Config object. If any additional
554// configurations are provided they will be merged into the new config returned.
555func (c *Config) Copy(cfgs ...*Config) *Config {
556	dst := &Config{}
557	dst.MergeIn(c)
558
559	for _, cfg := range cfgs {
560		dst.MergeIn(cfg)
561	}
562
563	return dst
564}
565