api_client.go (16226B)
1 // Code generated by smithy-go-codegen DO NOT EDIT. 2 3 package sts 4 5 import ( 6 "context" 7 "github.com/aws/aws-sdk-go-v2/aws" 8 "github.com/aws/aws-sdk-go-v2/aws/defaults" 9 awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware" 10 "github.com/aws/aws-sdk-go-v2/aws/protocol/query" 11 "github.com/aws/aws-sdk-go-v2/aws/retry" 12 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" 13 awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" 14 internalConfig "github.com/aws/aws-sdk-go-v2/internal/configsources" 15 presignedurlcust "github.com/aws/aws-sdk-go-v2/service/internal/presigned-url" 16 smithy "github.com/aws/smithy-go" 17 smithydocument "github.com/aws/smithy-go/document" 18 "github.com/aws/smithy-go/logging" 19 "github.com/aws/smithy-go/middleware" 20 smithyhttp "github.com/aws/smithy-go/transport/http" 21 "net" 22 "net/http" 23 "time" 24 ) 25 26 const ServiceID = "STS" 27 const ServiceAPIVersion = "2011-06-15" 28 29 // Client provides the API client to make operations call for AWS Security Token 30 // Service. 31 type Client struct { 32 options Options 33 } 34 35 // New returns an initialized Client based on the functional options. Provide 36 // additional functional options to further configure the behavior of the client, 37 // such as changing the client's endpoint or adding custom middleware behavior. 38 func New(options Options, optFns ...func(*Options)) *Client { 39 options = options.Copy() 40 41 resolveDefaultLogger(&options) 42 43 setResolvedDefaultsMode(&options) 44 45 resolveRetryer(&options) 46 47 resolveHTTPClient(&options) 48 49 resolveHTTPSignerV4(&options) 50 51 resolveDefaultEndpointConfiguration(&options) 52 53 for _, fn := range optFns { 54 fn(&options) 55 } 56 57 client := &Client{ 58 options: options, 59 } 60 61 return client 62 } 63 64 type Options struct { 65 // Set of options to modify how an operation is invoked. These apply to all 66 // operations invoked for this client. Use functional options on operation call to 67 // modify this list for per operation behavior. 68 APIOptions []func(*middleware.Stack) error 69 70 // Configures the events that will be sent to the configured logger. 71 ClientLogMode aws.ClientLogMode 72 73 // The credentials object to use when signing requests. 74 Credentials aws.CredentialsProvider 75 76 // The configuration DefaultsMode that the SDK should use when constructing the 77 // clients initial default settings. 78 DefaultsMode aws.DefaultsMode 79 80 // The endpoint options to be used when attempting to resolve an endpoint. 81 EndpointOptions EndpointResolverOptions 82 83 // The service endpoint resolver. 84 EndpointResolver EndpointResolver 85 86 // Signature Version 4 (SigV4) Signer 87 HTTPSignerV4 HTTPSignerV4 88 89 // The logger writer interface to write logging messages to. 90 Logger logging.Logger 91 92 // The region to send requests to. (Required) 93 Region string 94 95 // RetryMaxAttempts specifies the maximum number attempts an API client will call 96 // an operation that fails with a retryable error. A value of 0 is ignored, and 97 // will not be used to configure the API client created default retryer, or modify 98 // per operation call's retry max attempts. When creating a new API Clients this 99 // member will only be used if the Retryer Options member is nil. This value will 100 // be ignored if Retryer is not nil. If specified in an operation call's functional 101 // options with a value that is different than the constructed client's Options, 102 // the Client's Retryer will be wrapped to use the operation's specific 103 // RetryMaxAttempts value. 104 RetryMaxAttempts int 105 106 // RetryMode specifies the retry mode the API client will be created with, if 107 // Retryer option is not also specified. When creating a new API Clients this 108 // member will only be used if the Retryer Options member is nil. This value will 109 // be ignored if Retryer is not nil. Currently does not support per operation call 110 // overrides, may in the future. 111 RetryMode aws.RetryMode 112 113 // Retryer guides how HTTP requests should be retried in case of recoverable 114 // failures. When nil the API client will use a default retryer. The kind of 115 // default retry created by the API client can be changed with the RetryMode 116 // option. 117 Retryer aws.Retryer 118 119 // The RuntimeEnvironment configuration, only populated if the DefaultsMode is set 120 // to DefaultsModeAuto and is initialized using config.LoadDefaultConfig. You 121 // should not populate this structure programmatically, or rely on the values here 122 // within your applications. 123 RuntimeEnvironment aws.RuntimeEnvironment 124 125 // The initial DefaultsMode used when the client options were constructed. If the 126 // DefaultsMode was set to aws.DefaultsModeAuto this will store what the resolved 127 // value was at that point in time. Currently does not support per operation call 128 // overrides, may in the future. 129 resolvedDefaultsMode aws.DefaultsMode 130 131 // The HTTP client to invoke API calls with. Defaults to client's default HTTP 132 // implementation if nil. 133 HTTPClient HTTPClient 134 } 135 136 // WithAPIOptions returns a functional option for setting the Client's APIOptions 137 // option. 138 func WithAPIOptions(optFns ...func(*middleware.Stack) error) func(*Options) { 139 return func(o *Options) { 140 o.APIOptions = append(o.APIOptions, optFns...) 141 } 142 } 143 144 // WithEndpointResolver returns a functional option for setting the Client's 145 // EndpointResolver option. 146 func WithEndpointResolver(v EndpointResolver) func(*Options) { 147 return func(o *Options) { 148 o.EndpointResolver = v 149 } 150 } 151 152 type HTTPClient interface { 153 Do(*http.Request) (*http.Response, error) 154 } 155 156 // Copy creates a clone where the APIOptions list is deep copied. 157 func (o Options) Copy() Options { 158 to := o 159 to.APIOptions = make([]func(*middleware.Stack) error, len(o.APIOptions)) 160 copy(to.APIOptions, o.APIOptions) 161 162 return to 163 } 164 func (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) { 165 ctx = middleware.ClearStackValues(ctx) 166 stack := middleware.NewStack(opID, smithyhttp.NewStackRequest) 167 options := c.options.Copy() 168 for _, fn := range optFns { 169 fn(&options) 170 } 171 172 finalizeRetryMaxAttemptOptions(&options, *c) 173 174 finalizeClientEndpointResolverOptions(&options) 175 176 for _, fn := range stackFns { 177 if err := fn(stack, options); err != nil { 178 return nil, metadata, err 179 } 180 } 181 182 for _, fn := range options.APIOptions { 183 if err := fn(stack); err != nil { 184 return nil, metadata, err 185 } 186 } 187 188 handler := middleware.DecorateHandler(smithyhttp.NewClientHandler(options.HTTPClient), stack) 189 result, metadata, err = handler.Handle(ctx, params) 190 if err != nil { 191 err = &smithy.OperationError{ 192 ServiceID: ServiceID, 193 OperationName: opID, 194 Err: err, 195 } 196 } 197 return result, metadata, err 198 } 199 200 type noSmithyDocumentSerde = smithydocument.NoSerde 201 202 func resolveDefaultLogger(o *Options) { 203 if o.Logger != nil { 204 return 205 } 206 o.Logger = logging.Nop{} 207 } 208 209 func addSetLoggerMiddleware(stack *middleware.Stack, o Options) error { 210 return middleware.AddSetLoggerMiddleware(stack, o.Logger) 211 } 212 213 func setResolvedDefaultsMode(o *Options) { 214 if len(o.resolvedDefaultsMode) > 0 { 215 return 216 } 217 218 var mode aws.DefaultsMode 219 mode.SetFromString(string(o.DefaultsMode)) 220 221 if mode == aws.DefaultsModeAuto { 222 mode = defaults.ResolveDefaultsModeAuto(o.Region, o.RuntimeEnvironment) 223 } 224 225 o.resolvedDefaultsMode = mode 226 } 227 228 // NewFromConfig returns a new client from the provided config. 229 func NewFromConfig(cfg aws.Config, optFns ...func(*Options)) *Client { 230 opts := Options{ 231 Region: cfg.Region, 232 DefaultsMode: cfg.DefaultsMode, 233 RuntimeEnvironment: cfg.RuntimeEnvironment, 234 HTTPClient: cfg.HTTPClient, 235 Credentials: cfg.Credentials, 236 APIOptions: cfg.APIOptions, 237 Logger: cfg.Logger, 238 ClientLogMode: cfg.ClientLogMode, 239 } 240 resolveAWSRetryerProvider(cfg, &opts) 241 resolveAWSRetryMaxAttempts(cfg, &opts) 242 resolveAWSRetryMode(cfg, &opts) 243 resolveAWSEndpointResolver(cfg, &opts) 244 resolveUseDualStackEndpoint(cfg, &opts) 245 resolveUseFIPSEndpoint(cfg, &opts) 246 return New(opts, optFns...) 247 } 248 249 func resolveHTTPClient(o *Options) { 250 var buildable *awshttp.BuildableClient 251 252 if o.HTTPClient != nil { 253 var ok bool 254 buildable, ok = o.HTTPClient.(*awshttp.BuildableClient) 255 if !ok { 256 return 257 } 258 } else { 259 buildable = awshttp.NewBuildableClient() 260 } 261 262 modeConfig, err := defaults.GetModeConfiguration(o.resolvedDefaultsMode) 263 if err == nil { 264 buildable = buildable.WithDialerOptions(func(dialer *net.Dialer) { 265 if dialerTimeout, ok := modeConfig.GetConnectTimeout(); ok { 266 dialer.Timeout = dialerTimeout 267 } 268 }) 269 270 buildable = buildable.WithTransportOptions(func(transport *http.Transport) { 271 if tlsHandshakeTimeout, ok := modeConfig.GetTLSNegotiationTimeout(); ok { 272 transport.TLSHandshakeTimeout = tlsHandshakeTimeout 273 } 274 }) 275 } 276 277 o.HTTPClient = buildable 278 } 279 280 func resolveRetryer(o *Options) { 281 if o.Retryer != nil { 282 return 283 } 284 285 if len(o.RetryMode) == 0 { 286 modeConfig, err := defaults.GetModeConfiguration(o.resolvedDefaultsMode) 287 if err == nil { 288 o.RetryMode = modeConfig.RetryMode 289 } 290 } 291 if len(o.RetryMode) == 0 { 292 o.RetryMode = aws.RetryModeStandard 293 } 294 295 var standardOptions []func(*retry.StandardOptions) 296 if v := o.RetryMaxAttempts; v != 0 { 297 standardOptions = append(standardOptions, func(so *retry.StandardOptions) { 298 so.MaxAttempts = v 299 }) 300 } 301 302 switch o.RetryMode { 303 case aws.RetryModeAdaptive: 304 var adaptiveOptions []func(*retry.AdaptiveModeOptions) 305 if len(standardOptions) != 0 { 306 adaptiveOptions = append(adaptiveOptions, func(ao *retry.AdaptiveModeOptions) { 307 ao.StandardOptions = append(ao.StandardOptions, standardOptions...) 308 }) 309 } 310 o.Retryer = retry.NewAdaptiveMode(adaptiveOptions...) 311 312 default: 313 o.Retryer = retry.NewStandard(standardOptions...) 314 } 315 } 316 317 func resolveAWSRetryerProvider(cfg aws.Config, o *Options) { 318 if cfg.Retryer == nil { 319 return 320 } 321 o.Retryer = cfg.Retryer() 322 } 323 324 func resolveAWSRetryMode(cfg aws.Config, o *Options) { 325 if len(cfg.RetryMode) == 0 { 326 return 327 } 328 o.RetryMode = cfg.RetryMode 329 } 330 func resolveAWSRetryMaxAttempts(cfg aws.Config, o *Options) { 331 if cfg.RetryMaxAttempts == 0 { 332 return 333 } 334 o.RetryMaxAttempts = cfg.RetryMaxAttempts 335 } 336 337 func finalizeRetryMaxAttemptOptions(o *Options, client Client) { 338 if v := o.RetryMaxAttempts; v == 0 || v == client.options.RetryMaxAttempts { 339 return 340 } 341 342 o.Retryer = retry.AddWithMaxAttempts(o.Retryer, o.RetryMaxAttempts) 343 } 344 345 func resolveAWSEndpointResolver(cfg aws.Config, o *Options) { 346 if cfg.EndpointResolver == nil && cfg.EndpointResolverWithOptions == nil { 347 return 348 } 349 o.EndpointResolver = withEndpointResolver(cfg.EndpointResolver, cfg.EndpointResolverWithOptions, NewDefaultEndpointResolver()) 350 } 351 352 func addClientUserAgent(stack *middleware.Stack) error { 353 return awsmiddleware.AddSDKAgentKeyValue(awsmiddleware.APIMetadata, "sts", goModuleVersion)(stack) 354 } 355 356 func addHTTPSignerV4Middleware(stack *middleware.Stack, o Options) error { 357 mw := v4.NewSignHTTPRequestMiddleware(v4.SignHTTPRequestMiddlewareOptions{ 358 CredentialsProvider: o.Credentials, 359 Signer: o.HTTPSignerV4, 360 LogSigning: o.ClientLogMode.IsSigning(), 361 }) 362 return stack.Finalize.Add(mw, middleware.After) 363 } 364 365 type HTTPSignerV4 interface { 366 SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time, optFns ...func(*v4.SignerOptions)) error 367 } 368 369 func resolveHTTPSignerV4(o *Options) { 370 if o.HTTPSignerV4 != nil { 371 return 372 } 373 o.HTTPSignerV4 = newDefaultV4Signer(*o) 374 } 375 376 func newDefaultV4Signer(o Options) *v4.Signer { 377 return v4.NewSigner(func(so *v4.SignerOptions) { 378 so.Logger = o.Logger 379 so.LogSigning = o.ClientLogMode.IsSigning() 380 }) 381 } 382 383 func addRetryMiddlewares(stack *middleware.Stack, o Options) error { 384 mo := retry.AddRetryMiddlewaresOptions{ 385 Retryer: o.Retryer, 386 LogRetryAttempts: o.ClientLogMode.IsRetries(), 387 } 388 return retry.AddRetryMiddlewares(stack, mo) 389 } 390 391 // resolves dual-stack endpoint configuration 392 func resolveUseDualStackEndpoint(cfg aws.Config, o *Options) error { 393 if len(cfg.ConfigSources) == 0 { 394 return nil 395 } 396 value, found, err := internalConfig.ResolveUseDualStackEndpoint(context.Background(), cfg.ConfigSources) 397 if err != nil { 398 return err 399 } 400 if found { 401 o.EndpointOptions.UseDualStackEndpoint = value 402 } 403 return nil 404 } 405 406 // resolves FIPS endpoint configuration 407 func resolveUseFIPSEndpoint(cfg aws.Config, o *Options) error { 408 if len(cfg.ConfigSources) == 0 { 409 return nil 410 } 411 value, found, err := internalConfig.ResolveUseFIPSEndpoint(context.Background(), cfg.ConfigSources) 412 if err != nil { 413 return err 414 } 415 if found { 416 o.EndpointOptions.UseFIPSEndpoint = value 417 } 418 return nil 419 } 420 421 func addRequestIDRetrieverMiddleware(stack *middleware.Stack) error { 422 return awsmiddleware.AddRequestIDRetrieverMiddleware(stack) 423 } 424 425 func addResponseErrorMiddleware(stack *middleware.Stack) error { 426 return awshttp.AddResponseErrorMiddleware(stack) 427 } 428 429 // HTTPPresignerV4 represents presigner interface used by presign url client 430 type HTTPPresignerV4 interface { 431 PresignHTTP( 432 ctx context.Context, credentials aws.Credentials, r *http.Request, 433 payloadHash string, service string, region string, signingTime time.Time, 434 optFns ...func(*v4.SignerOptions), 435 ) (url string, signedHeader http.Header, err error) 436 } 437 438 // PresignOptions represents the presign client options 439 type PresignOptions struct { 440 441 // ClientOptions are list of functional options to mutate client options used by 442 // the presign client. 443 ClientOptions []func(*Options) 444 445 // Presigner is the presigner used by the presign url client 446 Presigner HTTPPresignerV4 447 } 448 449 func (o PresignOptions) copy() PresignOptions { 450 clientOptions := make([]func(*Options), len(o.ClientOptions)) 451 copy(clientOptions, o.ClientOptions) 452 o.ClientOptions = clientOptions 453 return o 454 } 455 456 // WithPresignClientFromClientOptions is a helper utility to retrieve a function 457 // that takes PresignOption as input 458 func WithPresignClientFromClientOptions(optFns ...func(*Options)) func(*PresignOptions) { 459 return withPresignClientFromClientOptions(optFns).options 460 } 461 462 type withPresignClientFromClientOptions []func(*Options) 463 464 func (w withPresignClientFromClientOptions) options(o *PresignOptions) { 465 o.ClientOptions = append(o.ClientOptions, w...) 466 } 467 468 // PresignClient represents the presign url client 469 type PresignClient struct { 470 client *Client 471 options PresignOptions 472 } 473 474 // NewPresignClient generates a presign client using provided API Client and 475 // presign options 476 func NewPresignClient(c *Client, optFns ...func(*PresignOptions)) *PresignClient { 477 var options PresignOptions 478 for _, fn := range optFns { 479 fn(&options) 480 } 481 if len(options.ClientOptions) != 0 { 482 c = New(c.options, options.ClientOptions...) 483 } 484 485 if options.Presigner == nil { 486 options.Presigner = newDefaultV4Signer(c.options) 487 } 488 489 return &PresignClient{ 490 client: c, 491 options: options, 492 } 493 } 494 495 func withNopHTTPClientAPIOption(o *Options) { 496 o.HTTPClient = smithyhttp.NopClient{} 497 } 498 499 type presignConverter PresignOptions 500 501 func (c presignConverter) convertToPresignMiddleware(stack *middleware.Stack, options Options) (err error) { 502 stack.Finalize.Clear() 503 stack.Deserialize.Clear() 504 stack.Build.Remove((*awsmiddleware.ClientRequestID)(nil).ID()) 505 stack.Build.Remove("UserAgent") 506 pmw := v4.NewPresignHTTPRequestMiddleware(v4.PresignHTTPRequestMiddlewareOptions{ 507 CredentialsProvider: options.Credentials, 508 Presigner: c.Presigner, 509 LogSigning: options.ClientLogMode.IsSigning(), 510 }) 511 err = stack.Finalize.Add(pmw, middleware.After) 512 if err != nil { 513 return err 514 } 515 if err = smithyhttp.AddNoPayloadDefaultContentTypeRemover(stack); err != nil { 516 return err 517 } 518 // convert request to a GET request 519 err = query.AddAsGetRequestMiddleware(stack) 520 if err != nil { 521 return err 522 } 523 err = presignedurlcust.AddAsIsPresigingMiddleware(stack) 524 if err != nil { 525 return err 526 } 527 return nil 528 } 529 530 func addRequestResponseLogging(stack *middleware.Stack, o Options) error { 531 return stack.Deserialize.Add(&smithyhttp.RequestResponseLogger{ 532 LogRequest: o.ClientLogMode.IsRequest(), 533 LogRequestWithBody: o.ClientLogMode.IsRequestWithBody(), 534 LogResponse: o.ClientLogMode.IsResponse(), 535 LogResponseWithBody: o.ClientLogMode.IsResponseWithBody(), 536 }, middleware.After) 537 }