api_op_AssumeRole.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // Code generated by smithy-go-codegen DO NOT EDIT.
  2. package sts
  3. import (
  4. "context"
  5. awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
  6. "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
  7. "github.com/aws/aws-sdk-go-v2/service/sts/types"
  8. "github.com/aws/smithy-go/middleware"
  9. smithyhttp "github.com/aws/smithy-go/transport/http"
  10. )
  11. // Returns a set of temporary security credentials that you can use to access
  12. // Amazon Web Services resources. These temporary credentials consist of an access
  13. // key ID, a secret access key, and a security token. Typically, you use AssumeRole
  14. // within your account or for cross-account access. For a comparison of AssumeRole
  15. // with other API operations that produce temporary credentials, see Requesting
  16. // Temporary Security Credentials
  17. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html)
  18. // and Comparing the Amazon Web Services STS API operations
  19. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html#stsapi_comparison)
  20. // in the IAM User Guide. Permissions The temporary security credentials created by
  21. // AssumeRole can be used to make API calls to any Amazon Web Services service with
  22. // the following exception: You cannot call the Amazon Web Services STS
  23. // GetFederationToken or GetSessionToken API operations. (Optional) You can pass
  24. // inline or managed session policies
  25. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_session)
  26. // to this operation. You can pass a single JSON policy document to use as an
  27. // inline session policy. You can also specify up to 10 managed policy Amazon
  28. // Resource Names (ARNs) to use as managed session policies. The plaintext that you
  29. // use for both inline and managed session policies can't exceed 2,048 characters.
  30. // Passing policies to this operation returns new temporary credentials. The
  31. // resulting session's permissions are the intersection of the role's
  32. // identity-based policy and the session policies. You can use the role's temporary
  33. // credentials in subsequent Amazon Web Services API calls to access resources in
  34. // the account that owns the role. You cannot use session policies to grant more
  35. // permissions than those allowed by the identity-based policy of the role that is
  36. // being assumed. For more information, see Session Policies
  37. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_session)
  38. // in the IAM User Guide. When you create a role, you create two policies: A role
  39. // trust policy that specifies who can assume the role and a permissions policy
  40. // that specifies what can be done with the role. You specify the trusted principal
  41. // who is allowed to assume the role in the role trust policy. To assume a role
  42. // from a different account, your Amazon Web Services account must be trusted by
  43. // the role. The trust relationship is defined in the role's trust policy when the
  44. // role is created. That trust policy states which accounts are allowed to delegate
  45. // that access to users in the account. A user who wants to access a role in a
  46. // different account must also have permissions that are delegated from the user
  47. // account administrator. The administrator must attach a policy that allows the
  48. // user to call AssumeRole for the ARN of the role in the other account. To allow a
  49. // user to assume a role in the same account, you can do either of the
  50. // following:
  51. //
  52. // * Attach a policy to the user that allows the user to call
  53. // AssumeRole (as long as the role's trust policy trusts the account).
  54. //
  55. // * Add the
  56. // user as a principal directly in the role's trust policy.
  57. //
  58. // You can do either
  59. // because the role’s trust policy acts as an IAM resource-based policy. When a
  60. // resource-based policy grants access to a principal in the same account, no
  61. // additional identity-based policy is required. For more information about trust
  62. // policies and resource-based policies, see IAM Policies
  63. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html) in the
  64. // IAM User Guide. Tags (Optional) You can pass tag key-value pairs to your
  65. // session. These tags are called session tags. For more information about session
  66. // tags, see Passing Session Tags in STS
  67. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_session-tags.html) in the
  68. // IAM User Guide. An administrator must grant you the permissions necessary to
  69. // pass session tags. The administrator can also create granular permissions to
  70. // allow you to pass only specific session tags. For more information, see
  71. // Tutorial: Using Tags for Attribute-Based Access Control
  72. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_attribute-based-access-control.html)
  73. // in the IAM User Guide. You can set the session tags as transitive. Transitive
  74. // tags persist during role chaining. For more information, see Chaining Roles with
  75. // Session Tags
  76. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_session-tags.html#id_session-tags_role-chaining)
  77. // in the IAM User Guide. Using MFA with AssumeRole (Optional) You can include
  78. // multi-factor authentication (MFA) information when you call AssumeRole. This is
  79. // useful for cross-account scenarios to ensure that the user that assumes the role
  80. // has been authenticated with an Amazon Web Services MFA device. In that scenario,
  81. // the trust policy of the role being assumed includes a condition that tests for
  82. // MFA authentication. If the caller does not include valid MFA information, the
  83. // request to assume the role is denied. The condition in a trust policy that tests
  84. // for MFA authentication might look like the following example. "Condition":
  85. // {"Bool": {"aws:MultiFactorAuthPresent": true}} For more information, see
  86. // Configuring MFA-Protected API Access
  87. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/MFAProtectedAPI.html) in the
  88. // IAM User Guide guide. To use MFA with AssumeRole, you pass values for the
  89. // SerialNumber and TokenCode parameters. The SerialNumber value identifies the
  90. // user's hardware or virtual MFA device. The TokenCode is the time-based one-time
  91. // password (TOTP) that the MFA device produces.
  92. func (c *Client) AssumeRole(ctx context.Context, params *AssumeRoleInput, optFns ...func(*Options)) (*AssumeRoleOutput, error) {
  93. if params == nil {
  94. params = &AssumeRoleInput{}
  95. }
  96. result, metadata, err := c.invokeOperation(ctx, "AssumeRole", params, optFns, c.addOperationAssumeRoleMiddlewares)
  97. if err != nil {
  98. return nil, err
  99. }
  100. out := result.(*AssumeRoleOutput)
  101. out.ResultMetadata = metadata
  102. return out, nil
  103. }
  104. type AssumeRoleInput struct {
  105. // The Amazon Resource Name (ARN) of the role to assume.
  106. //
  107. // This member is required.
  108. RoleArn *string
  109. // An identifier for the assumed role session. Use the role session name to
  110. // uniquely identify a session when the same role is assumed by different
  111. // principals or for different reasons. In cross-account scenarios, the role
  112. // session name is visible to, and can be logged by the account that owns the role.
  113. // The role session name is also used in the ARN of the assumed role principal.
  114. // This means that subsequent cross-account API requests that use the temporary
  115. // security credentials will expose the role session name to the external account
  116. // in their CloudTrail logs. The regex used to validate this parameter is a string
  117. // of characters consisting of upper- and lower-case alphanumeric characters with
  118. // no spaces. You can also include underscores or any of the following characters:
  119. // =,.@-
  120. //
  121. // This member is required.
  122. RoleSessionName *string
  123. // The duration, in seconds, of the role session. The value specified can range
  124. // from 900 seconds (15 minutes) up to the maximum session duration set for the
  125. // role. The maximum session duration setting can have a value from 1 hour to 12
  126. // hours. If you specify a value higher than this setting or the administrator
  127. // setting (whichever is lower), the operation fails. For example, if you specify a
  128. // session duration of 12 hours, but your administrator set the maximum session
  129. // duration to 6 hours, your operation fails. Role chaining limits your Amazon Web
  130. // Services CLI or Amazon Web Services API role session to a maximum of one hour.
  131. // When you use the AssumeRole API operation to assume a role, you can specify the
  132. // duration of your role session with the DurationSeconds parameter. You can
  133. // specify a parameter value of up to 43200 seconds (12 hours), depending on the
  134. // maximum session duration setting for your role. However, if you assume a role
  135. // using role chaining and provide a DurationSeconds parameter value greater than
  136. // one hour, the operation fails. To learn how to view the maximum value for your
  137. // role, see View the Maximum Session Duration Setting for a Role
  138. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use.html#id_roles_use_view-role-max-session)
  139. // in the IAM User Guide. By default, the value is set to 3600 seconds. The
  140. // DurationSeconds parameter is separate from the duration of a console session
  141. // that you might request using the returned credentials. The request to the
  142. // federation endpoint for a console sign-in token takes a SessionDuration
  143. // parameter that specifies the maximum length of the console session. For more
  144. // information, see Creating a URL that Enables Federated Users to Access the
  145. // Amazon Web Services Management Console
  146. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-custom-url.html)
  147. // in the IAM User Guide.
  148. DurationSeconds *int32
  149. // A unique identifier that might be required when you assume a role in another
  150. // account. If the administrator of the account to which the role belongs provided
  151. // you with an external ID, then provide that value in the ExternalId parameter.
  152. // This value can be any string, such as a passphrase or account number. A
  153. // cross-account role is usually set up to trust everyone in an account. Therefore,
  154. // the administrator of the trusting account might send an external ID to the
  155. // administrator of the trusted account. That way, only someone with the ID can
  156. // assume the role, rather than everyone in the account. For more information about
  157. // the external ID, see How to Use an External ID When Granting Access to Your
  158. // Amazon Web Services Resources to a Third Party
  159. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user_externalid.html)
  160. // in the IAM User Guide. The regex used to validate this parameter is a string of
  161. // characters consisting of upper- and lower-case alphanumeric characters with no
  162. // spaces. You can also include underscores or any of the following characters:
  163. // =,.@:/-
  164. ExternalId *string
  165. // An IAM policy in JSON format that you want to use as an inline session policy.
  166. // This parameter is optional. Passing policies to this operation returns new
  167. // temporary credentials. The resulting session's permissions are the intersection
  168. // of the role's identity-based policy and the session policies. You can use the
  169. // role's temporary credentials in subsequent Amazon Web Services API calls to
  170. // access resources in the account that owns the role. You cannot use session
  171. // policies to grant more permissions than those allowed by the identity-based
  172. // policy of the role that is being assumed. For more information, see Session
  173. // Policies
  174. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_session)
  175. // in the IAM User Guide. The plaintext that you use for both inline and managed
  176. // session policies can't exceed 2,048 characters. The JSON policy characters can
  177. // be any ASCII character from the space character to the end of the valid
  178. // character list (\u0020 through \u00FF). It can also include the tab (\u0009),
  179. // linefeed (\u000A), and carriage return (\u000D) characters. An Amazon Web
  180. // Services conversion compresses the passed inline session policy, managed policy
  181. // ARNs, and session tags into a packed binary format that has a separate limit.
  182. // Your request can fail for this limit even if your plaintext meets the other
  183. // requirements. The PackedPolicySize response element indicates by percentage how
  184. // close the policies and tags for your request are to the upper size limit.
  185. Policy *string
  186. // The Amazon Resource Names (ARNs) of the IAM managed policies that you want to
  187. // use as managed session policies. The policies must exist in the same account as
  188. // the role. This parameter is optional. You can provide up to 10 managed policy
  189. // ARNs. However, the plaintext that you use for both inline and managed session
  190. // policies can't exceed 2,048 characters. For more information about ARNs, see
  191. // Amazon Resource Names (ARNs) and Amazon Web Services Service Namespaces
  192. // (https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html) in
  193. // the Amazon Web Services General Reference. An Amazon Web Services conversion
  194. // compresses the passed inline session policy, managed policy ARNs, and session
  195. // tags into a packed binary format that has a separate limit. Your request can
  196. // fail for this limit even if your plaintext meets the other requirements. The
  197. // PackedPolicySize response element indicates by percentage how close the policies
  198. // and tags for your request are to the upper size limit. Passing policies to this
  199. // operation returns new temporary credentials. The resulting session's permissions
  200. // are the intersection of the role's identity-based policy and the session
  201. // policies. You can use the role's temporary credentials in subsequent Amazon Web
  202. // Services API calls to access resources in the account that owns the role. You
  203. // cannot use session policies to grant more permissions than those allowed by the
  204. // identity-based policy of the role that is being assumed. For more information,
  205. // see Session Policies
  206. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_session)
  207. // in the IAM User Guide.
  208. PolicyArns []types.PolicyDescriptorType
  209. // The identification number of the MFA device that is associated with the user who
  210. // is making the AssumeRole call. Specify this value if the trust policy of the
  211. // role being assumed includes a condition that requires MFA authentication. The
  212. // value is either the serial number for a hardware device (such as GAHT12345678)
  213. // or an Amazon Resource Name (ARN) for a virtual device (such as
  214. // arn:aws:iam::123456789012:mfa/user). The regex used to validate this parameter
  215. // is a string of characters consisting of upper- and lower-case alphanumeric
  216. // characters with no spaces. You can also include underscores or any of the
  217. // following characters: =,.@-
  218. SerialNumber *string
  219. // The source identity specified by the principal that is calling the AssumeRole
  220. // operation. You can require users to specify a source identity when they assume a
  221. // role. You do this by using the sts:SourceIdentity condition key in a role trust
  222. // policy. You can use source identity information in CloudTrail logs to determine
  223. // who took actions with a role. You can use the aws:SourceIdentity condition key
  224. // to further control access to Amazon Web Services resources based on the value of
  225. // source identity. For more information about using source identity, see Monitor
  226. // and control actions taken with assumed roles
  227. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_control-access_monitor.html)
  228. // in the IAM User Guide. The regex used to validate this parameter is a string of
  229. // characters consisting of upper- and lower-case alphanumeric characters with no
  230. // spaces. You can also include underscores or any of the following characters:
  231. // =,.@-. You cannot use a value that begins with the text aws:. This prefix is
  232. // reserved for Amazon Web Services internal use.
  233. SourceIdentity *string
  234. // A list of session tags that you want to pass. Each session tag consists of a key
  235. // name and an associated value. For more information about session tags, see
  236. // Tagging Amazon Web Services STS Sessions
  237. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_session-tags.html) in the
  238. // IAM User Guide. This parameter is optional. You can pass up to 50 session tags.
  239. // The plaintext session tag keys can’t exceed 128 characters, and the values can’t
  240. // exceed 256 characters. For these and additional limits, see IAM and STS
  241. // Character Limits
  242. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-limits.html#reference_iam-limits-entity-length)
  243. // in the IAM User Guide. An Amazon Web Services conversion compresses the passed
  244. // inline session policy, managed policy ARNs, and session tags into a packed
  245. // binary format that has a separate limit. Your request can fail for this limit
  246. // even if your plaintext meets the other requirements. The PackedPolicySize
  247. // response element indicates by percentage how close the policies and tags for
  248. // your request are to the upper size limit. You can pass a session tag with the
  249. // same key as a tag that is already attached to the role. When you do, session
  250. // tags override a role tag with the same key. Tag key–value pairs are not case
  251. // sensitive, but case is preserved. This means that you cannot have separate
  252. // Department and department tag keys. Assume that the role has the
  253. // Department=Marketing tag and you pass the department=engineering session tag.
  254. // Department and department are not saved as separate tags, and the session tag
  255. // passed in the request takes precedence over the role tag. Additionally, if you
  256. // used temporary credentials to perform this operation, the new session inherits
  257. // any transitive session tags from the calling session. If you pass a session tag
  258. // with the same key as an inherited tag, the operation fails. To view the
  259. // inherited tags for a session, see the CloudTrail logs. For more information, see
  260. // Viewing Session Tags in CloudTrail
  261. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_session-tags.html#id_session-tags_ctlogs)
  262. // in the IAM User Guide.
  263. Tags []types.Tag
  264. // The value provided by the MFA device, if the trust policy of the role being
  265. // assumed requires MFA. (In other words, if the policy includes a condition that
  266. // tests for MFA). If the role being assumed requires MFA and if the TokenCode
  267. // value is missing or expired, the AssumeRole call returns an "access denied"
  268. // error. The format for this parameter, as described by its regex pattern, is a
  269. // sequence of six numeric digits.
  270. TokenCode *string
  271. // A list of keys for session tags that you want to set as transitive. If you set a
  272. // tag key as transitive, the corresponding key and value passes to subsequent
  273. // sessions in a role chain. For more information, see Chaining Roles with Session
  274. // Tags
  275. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_session-tags.html#id_session-tags_role-chaining)
  276. // in the IAM User Guide. This parameter is optional. When you set session tags as
  277. // transitive, the session policy and session tags packed binary limit is not
  278. // affected. If you choose not to specify a transitive tag key, then no tags are
  279. // passed from this session to any subsequent sessions.
  280. TransitiveTagKeys []string
  281. noSmithyDocumentSerde
  282. }
  283. // Contains the response to a successful AssumeRole request, including temporary
  284. // Amazon Web Services credentials that can be used to make Amazon Web Services
  285. // requests.
  286. type AssumeRoleOutput struct {
  287. // The Amazon Resource Name (ARN) and the assumed role ID, which are identifiers
  288. // that you can use to refer to the resulting temporary security credentials. For
  289. // example, you can reference these credentials as a principal in a resource-based
  290. // policy by using the ARN or assumed role ID. The ARN and ID include the
  291. // RoleSessionName that you specified when you called AssumeRole.
  292. AssumedRoleUser *types.AssumedRoleUser
  293. // The temporary security credentials, which include an access key ID, a secret
  294. // access key, and a security (or session) token. The size of the security token
  295. // that STS API operations return is not fixed. We strongly recommend that you make
  296. // no assumptions about the maximum size.
  297. Credentials *types.Credentials
  298. // A percentage value that indicates the packed size of the session policies and
  299. // session tags combined passed in the request. The request fails if the packed
  300. // size is greater than 100 percent, which means the policies and tags exceeded the
  301. // allowed space.
  302. PackedPolicySize *int32
  303. // The source identity specified by the principal that is calling the AssumeRole
  304. // operation. You can require users to specify a source identity when they assume a
  305. // role. You do this by using the sts:SourceIdentity condition key in a role trust
  306. // policy. You can use source identity information in CloudTrail logs to determine
  307. // who took actions with a role. You can use the aws:SourceIdentity condition key
  308. // to further control access to Amazon Web Services resources based on the value of
  309. // source identity. For more information about using source identity, see Monitor
  310. // and control actions taken with assumed roles
  311. // (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_control-access_monitor.html)
  312. // in the IAM User Guide. The regex used to validate this parameter is a string of
  313. // characters consisting of upper- and lower-case alphanumeric characters with no
  314. // spaces. You can also include underscores or any of the following characters:
  315. // =,.@-
  316. SourceIdentity *string
  317. // Metadata pertaining to the operation's result.
  318. ResultMetadata middleware.Metadata
  319. noSmithyDocumentSerde
  320. }
  321. func (c *Client) addOperationAssumeRoleMiddlewares(stack *middleware.Stack, options Options) (err error) {
  322. err = stack.Serialize.Add(&awsAwsquery_serializeOpAssumeRole{}, middleware.After)
  323. if err != nil {
  324. return err
  325. }
  326. err = stack.Deserialize.Add(&awsAwsquery_deserializeOpAssumeRole{}, middleware.After)
  327. if err != nil {
  328. return err
  329. }
  330. if err = addSetLoggerMiddleware(stack, options); err != nil {
  331. return err
  332. }
  333. if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
  334. return err
  335. }
  336. if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
  337. return err
  338. }
  339. if err = addResolveEndpointMiddleware(stack, options); err != nil {
  340. return err
  341. }
  342. if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
  343. return err
  344. }
  345. if err = addRetryMiddlewares(stack, options); err != nil {
  346. return err
  347. }
  348. if err = addHTTPSignerV4Middleware(stack, options); err != nil {
  349. return err
  350. }
  351. if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
  352. return err
  353. }
  354. if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
  355. return err
  356. }
  357. if err = addClientUserAgent(stack); err != nil {
  358. return err
  359. }
  360. if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
  361. return err
  362. }
  363. if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
  364. return err
  365. }
  366. if err = addOpAssumeRoleValidationMiddleware(stack); err != nil {
  367. return err
  368. }
  369. if err = stack.Initialize.Add(newServiceMetadataMiddleware_opAssumeRole(options.Region), middleware.Before); err != nil {
  370. return err
  371. }
  372. if err = addRequestIDRetrieverMiddleware(stack); err != nil {
  373. return err
  374. }
  375. if err = addResponseErrorMiddleware(stack); err != nil {
  376. return err
  377. }
  378. if err = addRequestResponseLogging(stack, options); err != nil {
  379. return err
  380. }
  381. return nil
  382. }
  383. func newServiceMetadataMiddleware_opAssumeRole(region string) *awsmiddleware.RegisterServiceMetadata {
  384. return &awsmiddleware.RegisterServiceMetadata{
  385. Region: region,
  386. ServiceID: ServiceID,
  387. SigningName: "sts",
  388. OperationName: "AssumeRole",
  389. }
  390. }
  391. // PresignAssumeRole is used to generate a presigned HTTP Request which contains
  392. // presigned URL, signed headers and HTTP method used.
  393. func (c *PresignClient) PresignAssumeRole(ctx context.Context, params *AssumeRoleInput, optFns ...func(*PresignOptions)) (*v4.PresignedHTTPRequest, error) {
  394. if params == nil {
  395. params = &AssumeRoleInput{}
  396. }
  397. options := c.options.copy()
  398. for _, fn := range optFns {
  399. fn(&options)
  400. }
  401. clientOptFns := append(options.ClientOptions, withNopHTTPClientAPIOption)
  402. result, _, err := c.client.invokeOperation(ctx, "AssumeRole", params, clientOptFns,
  403. c.client.addOperationAssumeRoleMiddlewares,
  404. presignConverter(options).convertToPresignMiddleware,
  405. )
  406. if err != nil {
  407. return nil, err
  408. }
  409. out := result.(*v4.PresignedHTTPRequest)
  410. return out, nil
  411. }