AssumeRole.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package com.fdkankan.cloud.acl.controller;
  4. // snippet-start:[sts.java2.assume_role.main]
  5. // snippet-start:[sts.java2.assume_role.import]
  6. import software.amazon.awssdk.regions.Region;
  7. import software.amazon.awssdk.services.sts.StsClient;
  8. import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
  9. import software.amazon.awssdk.services.sts.model.StsException;
  10. import software.amazon.awssdk.services.sts.model.AssumeRoleResponse;
  11. import software.amazon.awssdk.services.sts.model.Credentials;
  12. import java.time.Instant;
  13. import java.time.ZoneId;
  14. import java.time.format.DateTimeFormatter;
  15. import java.time.format.FormatStyle;
  16. import java.util.Locale;
  17. // snippet-end:[sts.java2.assume_role.import]
  18. /**
  19. * To make this code example work, create a Role that you want to assume.
  20. * Then define a Trust Relationship in the AWS Console. You can use this as an
  21. * example:
  22. *
  23. * {
  24. * "Version": "2012-10-17",
  25. * "Statement": [
  26. * {
  27. * "Effect": "Allow",
  28. * "Principal": {
  29. * "AWS": "<Specify the ARN of your IAM user you are using in this code
  30. * example>"
  31. * },
  32. * "Action": "sts:AssumeRole"
  33. * }
  34. * ]
  35. * }
  36. *
  37. * For more information, see "Editing the Trust Relationship for an Existing
  38. * Role" in the AWS Directory Service guide.
  39. *
  40. * Also, set up your development environment, including your credentials.
  41. *
  42. * For information, see this documentation topic:
  43. *
  44. * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
  45. */
  46. public class AssumeRole {
  47. public static void main(String[] args) {
  48. final String usage = "";
  49. if (args.length != 2) {
  50. System.out.println(usage);
  51. System.exit(1);
  52. }
  53. String roleArn = args[0];
  54. String roleSessionName = args[1];
  55. Region region = Region.EU_WEST_2;
  56. StsClient stsClient = StsClient.builder()
  57. .region(region)
  58. .build();
  59. assumeGivenRole(stsClient, roleArn, roleSessionName);
  60. stsClient.close();
  61. }
  62. public static void assumeGivenRole(StsClient stsClient, String roleArn, String roleSessionName) {
  63. try {
  64. AssumeRoleRequest roleRequest = AssumeRoleRequest.builder()
  65. .roleArn(roleArn)
  66. .roleSessionName(roleSessionName)
  67. .build();
  68. AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
  69. Credentials myCreds = roleResponse.credentials();
  70. // Display the time when the temp creds expire.
  71. Instant exTime = myCreds.expiration();
  72. String tokenInfo = myCreds.sessionToken();
  73. // Convert the Instant to readable date.
  74. DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
  75. .withLocale(Locale.US)
  76. .withZone(ZoneId.systemDefault());
  77. formatter.format(exTime);
  78. System.out.println("The token " + tokenInfo + " expires on " + exTime);
  79. } catch (StsException e) {
  80. System.err.println(e.getMessage());
  81. System.exit(1);
  82. }
  83. }
  84. }
  85. // snippet-end:[sts.java2.assume_role.main]