examples_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT.
  2. package sts_test
  3. import (
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/awserr"
  9. "github.com/aws/aws-sdk-go/aws/session"
  10. "github.com/aws/aws-sdk-go/service/sts"
  11. )
  12. var _ time.Duration
  13. var _ strings.Reader
  14. var _ aws.Config
  15. func parseTime(layout, value string) *time.Time {
  16. t, err := time.Parse(layout, value)
  17. if err != nil {
  18. panic(err)
  19. }
  20. return &t
  21. }
  22. // To assume a role
  23. //
  24. func ExampleSTS_AssumeRole_shared00() {
  25. svc := sts.New(session.New())
  26. input := &sts.AssumeRoleInput{
  27. DurationSeconds: aws.Int64(3600),
  28. ExternalId: aws.String("123ABC"),
  29. Policy: aws.String("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Stmt1\",\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}"),
  30. RoleArn: aws.String("arn:aws:iam::123456789012:role/demo"),
  31. RoleSessionName: aws.String("Bob"),
  32. }
  33. result, err := svc.AssumeRole(input)
  34. if err != nil {
  35. if aerr, ok := err.(awserr.Error); ok {
  36. switch aerr.Code() {
  37. case sts.ErrCodeMalformedPolicyDocumentException:
  38. fmt.Println(sts.ErrCodeMalformedPolicyDocumentException, aerr.Error())
  39. case sts.ErrCodePackedPolicyTooLargeException:
  40. fmt.Println(sts.ErrCodePackedPolicyTooLargeException, aerr.Error())
  41. case sts.ErrCodeRegionDisabledException:
  42. fmt.Println(sts.ErrCodeRegionDisabledException, aerr.Error())
  43. default:
  44. fmt.Println(aerr.Error())
  45. }
  46. } else {
  47. // Print the error, cast err to awserr.Error to get the Code and
  48. // Message from an error.
  49. fmt.Println(err.Error())
  50. }
  51. return
  52. }
  53. fmt.Println(result)
  54. }
  55. // To assume a role as an OpenID Connect-federated user
  56. //
  57. func ExampleSTS_AssumeRoleWithWebIdentity_shared00() {
  58. svc := sts.New(session.New())
  59. input := &sts.AssumeRoleWithWebIdentityInput{
  60. DurationSeconds: aws.Int64(3600),
  61. ProviderId: aws.String("www.amazon.com"),
  62. RoleArn: aws.String("arn:aws:iam::123456789012:role/FederatedWebIdentityRole"),
  63. RoleSessionName: aws.String("app1"),
  64. WebIdentityToken: aws.String("Atza%7CIQEBLjAsAhRFiXuWpUXuRvQ9PZL3GMFcYevydwIUFAHZwXZXXXXXXXXJnrulxKDHwy87oGKPznh0D6bEQZTSCzyoCtL_8S07pLpr0zMbn6w1lfVZKNTBdDansFBmtGnIsIapjI6xKR02Yc_2bQ8LZbUXSGm6Ry6_BG7PrtLZtj_dfCTj92xNGed-CrKqjG7nPBjNIL016GGvuS5gSvPRUxWES3VYfm1wl7WTI7jn-Pcb6M-buCgHhFOzTQxod27L9CqnOLio7N3gZAGpsp6n1-AJBOCJckcyXe2c6uD0srOJeZlKUm2eTDVMf8IehDVI0r1QOnTV6KzzAI3OY87Vd_cVMQ"),
  65. }
  66. result, err := svc.AssumeRoleWithWebIdentity(input)
  67. if err != nil {
  68. if aerr, ok := err.(awserr.Error); ok {
  69. switch aerr.Code() {
  70. case sts.ErrCodeMalformedPolicyDocumentException:
  71. fmt.Println(sts.ErrCodeMalformedPolicyDocumentException, aerr.Error())
  72. case sts.ErrCodePackedPolicyTooLargeException:
  73. fmt.Println(sts.ErrCodePackedPolicyTooLargeException, aerr.Error())
  74. case sts.ErrCodeIDPRejectedClaimException:
  75. fmt.Println(sts.ErrCodeIDPRejectedClaimException, aerr.Error())
  76. case sts.ErrCodeIDPCommunicationErrorException:
  77. fmt.Println(sts.ErrCodeIDPCommunicationErrorException, aerr.Error())
  78. case sts.ErrCodeInvalidIdentityTokenException:
  79. fmt.Println(sts.ErrCodeInvalidIdentityTokenException, aerr.Error())
  80. case sts.ErrCodeExpiredTokenException:
  81. fmt.Println(sts.ErrCodeExpiredTokenException, aerr.Error())
  82. case sts.ErrCodeRegionDisabledException:
  83. fmt.Println(sts.ErrCodeRegionDisabledException, aerr.Error())
  84. default:
  85. fmt.Println(aerr.Error())
  86. }
  87. } else {
  88. // Print the error, cast err to awserr.Error to get the Code and
  89. // Message from an error.
  90. fmt.Println(err.Error())
  91. }
  92. return
  93. }
  94. fmt.Println(result)
  95. }
  96. // To decode information about an authorization status of a request
  97. //
  98. func ExampleSTS_DecodeAuthorizationMessage_shared00() {
  99. svc := sts.New(session.New())
  100. input := &sts.DecodeAuthorizationMessageInput{
  101. EncodedMessage: aws.String("<encoded-message>"),
  102. }
  103. result, err := svc.DecodeAuthorizationMessage(input)
  104. if err != nil {
  105. if aerr, ok := err.(awserr.Error); ok {
  106. switch aerr.Code() {
  107. case sts.ErrCodeInvalidAuthorizationMessageException:
  108. fmt.Println(sts.ErrCodeInvalidAuthorizationMessageException, aerr.Error())
  109. default:
  110. fmt.Println(aerr.Error())
  111. }
  112. } else {
  113. // Print the error, cast err to awserr.Error to get the Code and
  114. // Message from an error.
  115. fmt.Println(err.Error())
  116. }
  117. return
  118. }
  119. fmt.Println(result)
  120. }
  121. // To get details about a calling IAM user
  122. //
  123. // This example shows a request and response made with the credentials for a user named
  124. // Alice in the AWS account 123456789012.
  125. func ExampleSTS_GetCallerIdentity_shared00() {
  126. svc := sts.New(session.New())
  127. input := &sts.GetCallerIdentityInput{}
  128. result, err := svc.GetCallerIdentity(input)
  129. if err != nil {
  130. if aerr, ok := err.(awserr.Error); ok {
  131. switch aerr.Code() {
  132. default:
  133. fmt.Println(aerr.Error())
  134. }
  135. } else {
  136. // Print the error, cast err to awserr.Error to get the Code and
  137. // Message from an error.
  138. fmt.Println(err.Error())
  139. }
  140. return
  141. }
  142. fmt.Println(result)
  143. }
  144. // To get details about a calling user federated with AssumeRole
  145. //
  146. // This example shows a request and response made with temporary credentials created
  147. // by AssumeRole. The name of the assumed role is my-role-name, and the RoleSessionName
  148. // is set to my-role-session-name.
  149. func ExampleSTS_GetCallerIdentity_shared01() {
  150. svc := sts.New(session.New())
  151. input := &sts.GetCallerIdentityInput{}
  152. result, err := svc.GetCallerIdentity(input)
  153. if err != nil {
  154. if aerr, ok := err.(awserr.Error); ok {
  155. switch aerr.Code() {
  156. default:
  157. fmt.Println(aerr.Error())
  158. }
  159. } else {
  160. // Print the error, cast err to awserr.Error to get the Code and
  161. // Message from an error.
  162. fmt.Println(err.Error())
  163. }
  164. return
  165. }
  166. fmt.Println(result)
  167. }
  168. // To get details about a calling user federated with GetFederationToken
  169. //
  170. // This example shows a request and response made with temporary credentials created
  171. // by using GetFederationToken. The Name parameter is set to my-federated-user-name.
  172. func ExampleSTS_GetCallerIdentity_shared02() {
  173. svc := sts.New(session.New())
  174. input := &sts.GetCallerIdentityInput{}
  175. result, err := svc.GetCallerIdentity(input)
  176. if err != nil {
  177. if aerr, ok := err.(awserr.Error); ok {
  178. switch aerr.Code() {
  179. default:
  180. fmt.Println(aerr.Error())
  181. }
  182. } else {
  183. // Print the error, cast err to awserr.Error to get the Code and
  184. // Message from an error.
  185. fmt.Println(err.Error())
  186. }
  187. return
  188. }
  189. fmt.Println(result)
  190. }
  191. // To get temporary credentials for a role by using GetFederationToken
  192. //
  193. func ExampleSTS_GetFederationToken_shared00() {
  194. svc := sts.New(session.New())
  195. input := &sts.GetFederationTokenInput{
  196. DurationSeconds: aws.Int64(3600),
  197. Name: aws.String("Bob"),
  198. Policy: aws.String("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Stmt1\",\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}"),
  199. }
  200. result, err := svc.GetFederationToken(input)
  201. if err != nil {
  202. if aerr, ok := err.(awserr.Error); ok {
  203. switch aerr.Code() {
  204. case sts.ErrCodeMalformedPolicyDocumentException:
  205. fmt.Println(sts.ErrCodeMalformedPolicyDocumentException, aerr.Error())
  206. case sts.ErrCodePackedPolicyTooLargeException:
  207. fmt.Println(sts.ErrCodePackedPolicyTooLargeException, aerr.Error())
  208. case sts.ErrCodeRegionDisabledException:
  209. fmt.Println(sts.ErrCodeRegionDisabledException, aerr.Error())
  210. default:
  211. fmt.Println(aerr.Error())
  212. }
  213. } else {
  214. // Print the error, cast err to awserr.Error to get the Code and
  215. // Message from an error.
  216. fmt.Println(err.Error())
  217. }
  218. return
  219. }
  220. fmt.Println(result)
  221. }
  222. // To get temporary credentials for an IAM user or an AWS account
  223. //
  224. func ExampleSTS_GetSessionToken_shared00() {
  225. svc := sts.New(session.New())
  226. input := &sts.GetSessionTokenInput{
  227. DurationSeconds: aws.Int64(3600),
  228. SerialNumber: aws.String("YourMFASerialNumber"),
  229. TokenCode: aws.String("123456"),
  230. }
  231. result, err := svc.GetSessionToken(input)
  232. if err != nil {
  233. if aerr, ok := err.(awserr.Error); ok {
  234. switch aerr.Code() {
  235. case sts.ErrCodeRegionDisabledException:
  236. fmt.Println(sts.ErrCodeRegionDisabledException, aerr.Error())
  237. default:
  238. fmt.Println(aerr.Error())
  239. }
  240. } else {
  241. // Print the error, cast err to awserr.Error to get the Code and
  242. // Message from an error.
  243. fmt.Println(err.Error())
  244. }
  245. return
  246. }
  247. fmt.Println(result)
  248. }