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