1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package sts
4
5import (
6	"context"
7	"github.com/aws/aws-sdk-go-v2/aws"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/protocol/query"
10	"github.com/aws/aws-sdk-go-v2/aws/retry"
11	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
12	awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
13	presignedurlcust "github.com/aws/aws-sdk-go-v2/service/internal/presigned-url"
14	smithy "github.com/aws/smithy-go"
15	"github.com/aws/smithy-go/logging"
16	"github.com/aws/smithy-go/middleware"
17	smithyhttp "github.com/aws/smithy-go/transport/http"
18	"net/http"
19	"time"
20)
21
22const ServiceID = "STS"
23const ServiceAPIVersion = "2011-06-15"
24
25// Client provides the API client to make operations call for AWS Security Token
26// Service.
27type Client struct {
28	options Options
29}
30
31// New returns an initialized Client based on the functional options. Provide
32// additional functional options to further configure the behavior of the client,
33// such as changing the client's endpoint or adding custom middleware behavior.
34func New(options Options, optFns ...func(*Options)) *Client {
35	options = options.Copy()
36
37	resolveDefaultLogger(&options)
38
39	resolveRetryer(&options)
40
41	resolveHTTPClient(&options)
42
43	resolveHTTPSignerV4(&options)
44
45	resolveDefaultEndpointConfiguration(&options)
46
47	for _, fn := range optFns {
48		fn(&options)
49	}
50
51	client := &Client{
52		options: options,
53	}
54
55	return client
56}
57
58type Options struct {
59	// Set of options to modify how an operation is invoked. These apply to all
60	// operations invoked for this client. Use functional options on operation call to
61	// modify this list for per operation behavior.
62	APIOptions []func(*middleware.Stack) error
63
64	// Configures the events that will be sent to the configured logger.
65	ClientLogMode aws.ClientLogMode
66
67	// The credentials object to use when signing requests.
68	Credentials aws.CredentialsProvider
69
70	// The endpoint options to be used when attempting to resolve an endpoint.
71	EndpointOptions EndpointResolverOptions
72
73	// The service endpoint resolver.
74	EndpointResolver EndpointResolver
75
76	// Signature Version 4 (SigV4) Signer
77	HTTPSignerV4 HTTPSignerV4
78
79	// The logger writer interface to write logging messages to.
80	Logger logging.Logger
81
82	// The region to send requests to. (Required)
83	Region string
84
85	// Retryer guides how HTTP requests should be retried in case of recoverable
86	// failures. When nil the API client will use a default retryer.
87	Retryer aws.Retryer
88
89	// The HTTP client to invoke API calls with. Defaults to client's default HTTP
90	// implementation if nil.
91	HTTPClient HTTPClient
92}
93
94// WithAPIOptions returns a functional option for setting the Client's APIOptions
95// option.
96func WithAPIOptions(optFns ...func(*middleware.Stack) error) func(*Options) {
97	return func(o *Options) {
98		o.APIOptions = append(o.APIOptions, optFns...)
99	}
100}
101
102// WithEndpointResolver returns a functional option for setting the Client's
103// EndpointResolver option.
104func WithEndpointResolver(v EndpointResolver) func(*Options) {
105	return func(o *Options) {
106		o.EndpointResolver = v
107	}
108}
109
110type HTTPClient interface {
111	Do(*http.Request) (*http.Response, error)
112}
113
114// Copy creates a clone where the APIOptions list is deep copied.
115func (o Options) Copy() Options {
116	to := o
117	to.APIOptions = make([]func(*middleware.Stack) error, len(o.APIOptions))
118	copy(to.APIOptions, o.APIOptions)
119	return to
120}
121func (c *Client) invokeOperation(ctx context.Context, opID string, params interface{}, optFns []func(*Options), stackFns ...func(*middleware.Stack, Options) error) (result interface{}, metadata middleware.Metadata, err error) {
122	ctx = middleware.ClearStackValues(ctx)
123	stack := middleware.NewStack(opID, smithyhttp.NewStackRequest)
124	options := c.options.Copy()
125	for _, fn := range optFns {
126		fn(&options)
127	}
128
129	for _, fn := range stackFns {
130		if err := fn(stack, options); err != nil {
131			return nil, metadata, err
132		}
133	}
134
135	for _, fn := range options.APIOptions {
136		if err := fn(stack); err != nil {
137			return nil, metadata, err
138		}
139	}
140
141	handler := middleware.DecorateHandler(smithyhttp.NewClientHandler(options.HTTPClient), stack)
142	result, metadata, err = handler.Handle(ctx, params)
143	if err != nil {
144		err = &smithy.OperationError{
145			ServiceID:     ServiceID,
146			OperationName: opID,
147			Err:           err,
148		}
149	}
150	return result, metadata, err
151}
152
153func resolveDefaultLogger(o *Options) {
154	if o.Logger != nil {
155		return
156	}
157	o.Logger = logging.Nop{}
158}
159
160func addSetLoggerMiddleware(stack *middleware.Stack, o Options) error {
161	return middleware.AddSetLoggerMiddleware(stack, o.Logger)
162}
163
164// NewFromConfig returns a new client from the provided config.
165func NewFromConfig(cfg aws.Config, optFns ...func(*Options)) *Client {
166	opts := Options{
167		Region:        cfg.Region,
168		HTTPClient:    cfg.HTTPClient,
169		Credentials:   cfg.Credentials,
170		APIOptions:    cfg.APIOptions,
171		Logger:        cfg.Logger,
172		ClientLogMode: cfg.ClientLogMode,
173	}
174	resolveAWSRetryerProvider(cfg, &opts)
175	resolveAWSEndpointResolver(cfg, &opts)
176	return New(opts, optFns...)
177}
178
179func resolveHTTPClient(o *Options) {
180	if o.HTTPClient != nil {
181		return
182	}
183	o.HTTPClient = awshttp.NewBuildableClient()
184}
185
186func resolveRetryer(o *Options) {
187	if o.Retryer != nil {
188		return
189	}
190	o.Retryer = retry.NewStandard()
191}
192
193func resolveAWSRetryerProvider(cfg aws.Config, o *Options) {
194	if cfg.Retryer == nil {
195		return
196	}
197	o.Retryer = cfg.Retryer()
198}
199
200func resolveAWSEndpointResolver(cfg aws.Config, o *Options) {
201	if cfg.EndpointResolver == nil {
202		return
203	}
204	o.EndpointResolver = withEndpointResolver(cfg.EndpointResolver, NewDefaultEndpointResolver())
205}
206
207func addClientUserAgent(stack *middleware.Stack) error {
208	return awsmiddleware.AddRequestUserAgentMiddleware(stack)
209}
210
211func addHTTPSignerV4Middleware(stack *middleware.Stack, o Options) error {
212	mw := v4.NewSignHTTPRequestMiddleware(v4.SignHTTPRequestMiddlewareOptions{
213		CredentialsProvider: o.Credentials,
214		Signer:              o.HTTPSignerV4,
215		LogSigning:          o.ClientLogMode.IsSigning(),
216	})
217	return stack.Finalize.Add(mw, middleware.After)
218}
219
220type HTTPSignerV4 interface {
221	SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time, optFns ...func(*v4.SignerOptions)) error
222}
223
224func resolveHTTPSignerV4(o *Options) {
225	if o.HTTPSignerV4 != nil {
226		return
227	}
228	o.HTTPSignerV4 = newDefaultV4Signer(*o)
229}
230
231func newDefaultV4Signer(o Options) *v4.Signer {
232	return v4.NewSigner(func(so *v4.SignerOptions) {
233		so.Logger = o.Logger
234		so.LogSigning = o.ClientLogMode.IsSigning()
235	})
236}
237
238func addRetryMiddlewares(stack *middleware.Stack, o Options) error {
239	mo := retry.AddRetryMiddlewaresOptions{
240		Retryer:          o.Retryer,
241		LogRetryAttempts: o.ClientLogMode.IsRetries(),
242	}
243	return retry.AddRetryMiddlewares(stack, mo)
244}
245
246func addRequestIDRetrieverMiddleware(stack *middleware.Stack) error {
247	return awsmiddleware.AddRequestIDRetrieverMiddleware(stack)
248}
249
250func addResponseErrorMiddleware(stack *middleware.Stack) error {
251	return awshttp.AddResponseErrorMiddleware(stack)
252}
253
254// HTTPPresignerV4 represents presigner interface used by presign url client
255type HTTPPresignerV4 interface {
256	PresignHTTP(
257		ctx context.Context, credentials aws.Credentials, r *http.Request,
258		payloadHash string, service string, region string, signingTime time.Time,
259		optFns ...func(*v4.SignerOptions),
260	) (url string, signedHeader http.Header, err error)
261}
262
263// PresignOptions represents the presign client options
264type PresignOptions struct {
265
266	// ClientOptions are list of functional options to mutate client options used by
267	// the presign client.
268	ClientOptions []func(*Options)
269
270	// Presigner is the presigner used by the presign url client
271	Presigner HTTPPresignerV4
272}
273
274func (o PresignOptions) copy() PresignOptions {
275	clientOptions := make([]func(*Options), len(o.ClientOptions))
276	copy(clientOptions, o.ClientOptions)
277	o.ClientOptions = clientOptions
278	return o
279}
280
281// WithPresignClientFromClientOptions is a helper utility to retrieve a function
282// that takes PresignOption as input
283func WithPresignClientFromClientOptions(optFns ...func(*Options)) func(*PresignOptions) {
284	return withPresignClientFromClientOptions(optFns).options
285}
286
287type withPresignClientFromClientOptions []func(*Options)
288
289func (w withPresignClientFromClientOptions) options(o *PresignOptions) {
290	o.ClientOptions = append(o.ClientOptions, w...)
291}
292
293// PresignClient represents the presign url client
294type PresignClient struct {
295	client  *Client
296	options PresignOptions
297}
298
299// NewPresignClient generates a presign client using provided API Client and
300// presign options
301func NewPresignClient(c *Client, optFns ...func(*PresignOptions)) *PresignClient {
302	var options PresignOptions
303	for _, fn := range optFns {
304		fn(&options)
305	}
306	if len(options.ClientOptions) != 0 {
307		c = New(c.options, options.ClientOptions...)
308	}
309
310	if options.Presigner == nil {
311		options.Presigner = newDefaultV4Signer(c.options)
312	}
313
314	return &PresignClient{
315		client:  c,
316		options: options,
317	}
318}
319
320func withNopHTTPClientAPIOption(o *Options) {
321	o.HTTPClient = smithyhttp.NopClient{}
322}
323
324type presignConverter PresignOptions
325
326func (c presignConverter) convertToPresignMiddleware(stack *middleware.Stack, options Options) (err error) {
327	stack.Finalize.Clear()
328	stack.Deserialize.Clear()
329	stack.Build.Remove((*awsmiddleware.ClientRequestID)(nil).ID())
330	pmw := v4.NewPresignHTTPRequestMiddleware(v4.PresignHTTPRequestMiddlewareOptions{
331		CredentialsProvider: options.Credentials,
332		Presigner:           c.Presigner,
333		LogSigning:          options.ClientLogMode.IsSigning(),
334	})
335	err = stack.Finalize.Add(pmw, middleware.After)
336	if err != nil {
337		return err
338	}
339	// convert request to a GET request
340	err = query.AddAsGetRequestMiddleware(stack)
341	if err != nil {
342		return err
343	}
344	err = presignedurlcust.AddAsIsPresigingMiddleware(stack)
345	if err != nil {
346		return err
347	}
348	return nil
349}
350
351func addRequestResponseLogging(stack *middleware.Stack, o Options) error {
352	return stack.Deserialize.Add(&smithyhttp.RequestResponseLogger{
353		LogRequest:          o.ClientLogMode.IsRequest(),
354		LogRequestWithBody:  o.ClientLogMode.IsRequestWithBody(),
355		LogResponse:         o.ClientLogMode.IsResponse(),
356		LogResponseWithBody: o.ClientLogMode.IsResponseWithBody(),
357	}, middleware.After)
358}
359