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