api_client.go 13 KB

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