api_client.go 16 KB

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