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 have the S3 service client to use the region specified
165	// in the ARN, when an ARN is provided as an argument to a bucket parameter.
166	S3UseARNRegion *bool
167
168	// Set this to `true` to enable the SDK to unmarshal API response header maps to
169	// normalized lower case map keys.
170	//
171	// For example S3's X-Amz-Meta prefixed header will be unmarshaled to lower case
172	// Metadata member's map keys. The value of the header in the map is unaffected.
173	LowerCaseHeaderMaps *bool
174
175	// Set this to `true` to disable the EC2Metadata client from overriding the
176	// default http.Client's Timeout. This is helpful if you do not want the
177	// EC2Metadata client to create a new http.Client. This options is only
178	// meaningful if you're not already using a custom HTTP client with the
179	// SDK. Enabled by default.
180	//
181	// Must be set and provided to the session.NewSession() in order to disable
182	// the EC2Metadata overriding the timeout for default credentials chain.
183	//
184	// Example:
185	//    sess := session.Must(session.NewSession(aws.NewConfig()
186	//       .WithEC2MetadataDiableTimeoutOverride(true)))
187	//
188	//    svc := s3.New(sess)
189	//
190	EC2MetadataDisableTimeoutOverride *bool
191
192	// Instructs the endpoint to be generated for a service client to
193	// be the dual stack endpoint. The dual stack endpoint will support
194	// both IPv4 and IPv6 addressing.
195	//
196	// Setting this for a service which does not support dual stack will fail
197	// to make requets. It is not recommended to set this value on the session
198	// as it will apply to all service clients created with the session. Even
199	// services which don't support dual stack endpoints.
200	//
201	// If the Endpoint config value is also provided the UseDualStack flag
202	// will be ignored.
203	//
204	// Only supported with.
205	//
206	//     sess := session.Must(session.NewSession())
207	//
208	//     svc := s3.New(sess, &aws.Config{
209	//         UseDualStack: aws.Bool(true),
210	//     })
211	UseDualStack *bool
212
213	// SleepDelay is an override for the func the SDK will call when sleeping
214	// during the lifecycle of a request. Specifically this will be used for
215	// request delays. This value should only be used for testing. To adjust
216	// the delay of a request see the aws/client.DefaultRetryer and
217	// aws/request.Retryer.
218	//
219	// SleepDelay will prevent any Context from being used for canceling retry
220	// delay of an API operation. It is recommended to not use SleepDelay at all
221	// and specify a Retryer instead.
222	SleepDelay func(time.Duration)
223
224	// DisableRestProtocolURICleaning will not clean the URL path when making rest protocol requests.
225	// Will default to false. This would only be used for empty directory names in s3 requests.
226	//
227	// Example:
228	//    sess := session.Must(session.NewSession(&aws.Config{
229	//         DisableRestProtocolURICleaning: aws.Bool(true),
230	//    }))
231	//
232	//    svc := s3.New(sess)
233	//    out, err := svc.GetObject(&s3.GetObjectInput {
234	//    	Bucket: aws.String("bucketname"),
235	//    	Key: aws.String("//foo//bar//moo"),
236	//    })
237	DisableRestProtocolURICleaning *bool
238
239	// EnableEndpointDiscovery will allow for endpoint discovery on operations that
240	// have the definition in its model. By default, endpoint discovery is off.
241	//
242	// Example:
243	//    sess := session.Must(session.NewSession(&aws.Config{
244	//         EnableEndpointDiscovery: aws.Bool(true),
245	//    }))
246	//
247	//    svc := s3.New(sess)
248	//    out, err := svc.GetObject(&s3.GetObjectInput {
249	//    	Bucket: aws.String("bucketname"),
250	//    	Key: aws.String("/foo/bar/moo"),
251	//    })
252	EnableEndpointDiscovery *bool
253
254	// DisableEndpointHostPrefix will disable the SDK's behavior of prefixing
255	// request endpoint hosts with modeled information.
256	//
257	// Disabling this feature is useful when you want to use local endpoints
258	// for testing that do not support the modeled host prefix pattern.
259	DisableEndpointHostPrefix *bool
260
261	// STSRegionalEndpoint will enable regional or legacy endpoint resolving
262	STSRegionalEndpoint endpoints.STSRegionalEndpoint
263
264	// S3UsEast1RegionalEndpoint will enable regional or legacy endpoint resolving
265	S3UsEast1RegionalEndpoint endpoints.S3UsEast1RegionalEndpoint
266}
267
268// NewConfig returns a new Config pointer that can be chained with builder
269// methods to set multiple configuration values inline without using pointers.
270//
271//     // Create Session with MaxRetries configuration to be shared by multiple
272//     // service clients.
273//     sess := session.Must(session.NewSession(aws.NewConfig().
274//         WithMaxRetries(3),
275//     ))
276//
277//     // Create S3 service client with a specific Region.
278//     svc := s3.New(sess, aws.NewConfig().
279//         WithRegion("us-west-2"),
280//     )
281func NewConfig() *Config {
282	return &Config{}
283}
284
285// WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
286// a Config pointer.
287func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
288	c.CredentialsChainVerboseErrors = &verboseErrs
289	return c
290}
291
292// WithCredentials sets a config Credentials value returning a Config pointer
293// for chaining.
294func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
295	c.Credentials = creds
296	return c
297}
298
299// WithEndpoint sets a config Endpoint value returning a Config pointer for
300// chaining.
301func (c *Config) WithEndpoint(endpoint string) *Config {
302	c.Endpoint = &endpoint
303	return c
304}
305
306// WithEndpointResolver sets a config EndpointResolver value returning a
307// Config pointer for chaining.
308func (c *Config) WithEndpointResolver(resolver endpoints.Resolver) *Config {
309	c.EndpointResolver = resolver
310	return c
311}
312
313// WithRegion sets a config Region value returning a Config pointer for
314// chaining.
315func (c *Config) WithRegion(region string) *Config {
316	c.Region = &region
317	return c
318}
319
320// WithDisableSSL sets a config DisableSSL value returning a Config pointer
321// for chaining.
322func (c *Config) WithDisableSSL(disable bool) *Config {
323	c.DisableSSL = &disable
324	return c
325}
326
327// WithHTTPClient sets a config HTTPClient value returning a Config pointer
328// for chaining.
329func (c *Config) WithHTTPClient(client *http.Client) *Config {
330	c.HTTPClient = client
331	return c
332}
333
334// WithMaxRetries sets a config MaxRetries value returning a Config pointer
335// for chaining.
336func (c *Config) WithMaxRetries(max int) *Config {
337	c.MaxRetries = &max
338	return c
339}
340
341// WithDisableParamValidation sets a config DisableParamValidation value
342// returning a Config pointer for chaining.
343func (c *Config) WithDisableParamValidation(disable bool) *Config {
344	c.DisableParamValidation = &disable
345	return c
346}
347
348// WithDisableComputeChecksums sets a config DisableComputeChecksums value
349// returning a Config pointer for chaining.
350func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
351	c.DisableComputeChecksums = &disable
352	return c
353}
354
355// WithLogLevel sets a config LogLevel value returning a Config pointer for
356// chaining.
357func (c *Config) WithLogLevel(level LogLevelType) *Config {
358	c.LogLevel = &level
359	return c
360}
361
362// WithLogger sets a config Logger value returning a Config pointer for
363// chaining.
364func (c *Config) WithLogger(logger Logger) *Config {
365	c.Logger = logger
366	return c
367}
368
369// WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
370// pointer for chaining.
371func (c *Config) WithS3ForcePathStyle(force bool) *Config {
372	c.S3ForcePathStyle = &force
373	return c
374}
375
376// WithS3Disable100Continue sets a config S3Disable100Continue value returning
377// a Config pointer for chaining.
378func (c *Config) WithS3Disable100Continue(disable bool) *Config {
379	c.S3Disable100Continue = &disable
380	return c
381}
382
383// WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
384// pointer for chaining.
385func (c *Config) WithS3UseAccelerate(enable bool) *Config {
386	c.S3UseAccelerate = &enable
387	return c
388
389}
390
391// WithS3DisableContentMD5Validation sets a config
392// S3DisableContentMD5Validation value returning a Config pointer for chaining.
393func (c *Config) WithS3DisableContentMD5Validation(enable bool) *Config {
394	c.S3DisableContentMD5Validation = &enable
395	return c
396
397}
398
399// WithS3UseARNRegion sets a config S3UseARNRegion value and
400// returning a Config pointer for chaining
401func (c *Config) WithS3UseARNRegion(enable bool) *Config {
402	c.S3UseARNRegion = &enable
403	return c
404}
405
406// WithUseDualStack sets a config UseDualStack value returning a Config
407// pointer for chaining.
408func (c *Config) WithUseDualStack(enable bool) *Config {
409	c.UseDualStack = &enable
410	return c
411}
412
413// WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
414// returning a Config pointer for chaining.
415func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
416	c.EC2MetadataDisableTimeoutOverride = &enable
417	return c
418}
419
420// WithSleepDelay overrides the function used to sleep while waiting for the
421// next retry. Defaults to time.Sleep.
422func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
423	c.SleepDelay = fn
424	return c
425}
426
427// WithEndpointDiscovery will set whether or not to use endpoint discovery.
428func (c *Config) WithEndpointDiscovery(t bool) *Config {
429	c.EnableEndpointDiscovery = &t
430	return c
431}
432
433// WithDisableEndpointHostPrefix will set whether or not to use modeled host prefix
434// when making requests.
435func (c *Config) WithDisableEndpointHostPrefix(t bool) *Config {
436	c.DisableEndpointHostPrefix = &t
437	return c
438}
439
440// MergeIn merges the passed in configs into the existing config object.
441func (c *Config) MergeIn(cfgs ...*Config) {
442	for _, other := range cfgs {
443		mergeInConfig(c, other)
444	}
445}
446
447// WithSTSRegionalEndpoint will set whether or not to use regional endpoint flag
448// when resolving the endpoint for a service
449func (c *Config) WithSTSRegionalEndpoint(sre endpoints.STSRegionalEndpoint) *Config {
450	c.STSRegionalEndpoint = sre
451	return c
452}
453
454// WithS3UsEast1RegionalEndpoint will set whether or not to use regional endpoint flag
455// when resolving the endpoint for a service
456func (c *Config) WithS3UsEast1RegionalEndpoint(sre endpoints.S3UsEast1RegionalEndpoint) *Config {
457	c.S3UsEast1RegionalEndpoint = sre
458	return c
459}
460
461func mergeInConfig(dst *Config, other *Config) {
462	if other == nil {
463		return
464	}
465
466	if other.CredentialsChainVerboseErrors != nil {
467		dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
468	}
469
470	if other.Credentials != nil {
471		dst.Credentials = other.Credentials
472	}
473
474	if other.Endpoint != nil {
475		dst.Endpoint = other.Endpoint
476	}
477
478	if other.EndpointResolver != nil {
479		dst.EndpointResolver = other.EndpointResolver
480	}
481
482	if other.Region != nil {
483		dst.Region = other.Region
484	}
485
486	if other.DisableSSL != nil {
487		dst.DisableSSL = other.DisableSSL
488	}
489
490	if other.HTTPClient != nil {
491		dst.HTTPClient = other.HTTPClient
492	}
493
494	if other.LogLevel != nil {
495		dst.LogLevel = other.LogLevel
496	}
497
498	if other.Logger != nil {
499		dst.Logger = other.Logger
500	}
501
502	if other.MaxRetries != nil {
503		dst.MaxRetries = other.MaxRetries
504	}
505
506	if other.Retryer != nil {
507		dst.Retryer = other.Retryer
508	}
509
510	if other.DisableParamValidation != nil {
511		dst.DisableParamValidation = other.DisableParamValidation
512	}
513
514	if other.DisableComputeChecksums != nil {
515		dst.DisableComputeChecksums = other.DisableComputeChecksums
516	}
517
518	if other.S3ForcePathStyle != nil {
519		dst.S3ForcePathStyle = other.S3ForcePathStyle
520	}
521
522	if other.S3Disable100Continue != nil {
523		dst.S3Disable100Continue = other.S3Disable100Continue
524	}
525
526	if other.S3UseAccelerate != nil {
527		dst.S3UseAccelerate = other.S3UseAccelerate
528	}
529
530	if other.S3DisableContentMD5Validation != nil {
531		dst.S3DisableContentMD5Validation = other.S3DisableContentMD5Validation
532	}
533
534	if other.S3UseARNRegion != nil {
535		dst.S3UseARNRegion = other.S3UseARNRegion
536	}
537
538	if other.UseDualStack != nil {
539		dst.UseDualStack = other.UseDualStack
540	}
541
542	if other.EC2MetadataDisableTimeoutOverride != nil {
543		dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
544	}
545
546	if other.SleepDelay != nil {
547		dst.SleepDelay = other.SleepDelay
548	}
549
550	if other.DisableRestProtocolURICleaning != nil {
551		dst.DisableRestProtocolURICleaning = other.DisableRestProtocolURICleaning
552	}
553
554	if other.EnforceShouldRetryCheck != nil {
555		dst.EnforceShouldRetryCheck = other.EnforceShouldRetryCheck
556	}
557
558	if other.EnableEndpointDiscovery != nil {
559		dst.EnableEndpointDiscovery = other.EnableEndpointDiscovery
560	}
561
562	if other.DisableEndpointHostPrefix != nil {
563		dst.DisableEndpointHostPrefix = other.DisableEndpointHostPrefix
564	}
565
566	if other.STSRegionalEndpoint != endpoints.UnsetSTSEndpoint {
567		dst.STSRegionalEndpoint = other.STSRegionalEndpoint
568	}
569
570	if other.S3UsEast1RegionalEndpoint != endpoints.UnsetS3UsEast1Endpoint {
571		dst.S3UsEast1RegionalEndpoint = other.S3UsEast1RegionalEndpoint
572	}
573}
574
575// Copy will return a shallow copy of the Config object. If any additional
576// configurations are provided they will be merged into the new config returned.
577func (c *Config) Copy(cfgs ...*Config) *Config {
578	dst := &Config{}
579	dst.MergeIn(c)
580
581	for _, cfg := range cfgs {
582		dst.MergeIn(cfg)
583	}
584
585	return dst
586}
587