examples_test.go 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. // Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT.
  2. package kms_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/kms"
  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 cancel deletion of a customer master key (CMK)
  23. //
  24. // The following example cancels deletion of the specified CMK.
  25. func ExampleKMS_CancelKeyDeletion_shared00() {
  26. svc := kms.New(session.New())
  27. input := &kms.CancelKeyDeletionInput{
  28. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  29. }
  30. result, err := svc.CancelKeyDeletion(input)
  31. if err != nil {
  32. if aerr, ok := err.(awserr.Error); ok {
  33. switch aerr.Code() {
  34. case kms.ErrCodeNotFoundException:
  35. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  36. case kms.ErrCodeInvalidArnException:
  37. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  38. case kms.ErrCodeDependencyTimeoutException:
  39. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  40. case kms.ErrCodeInternalException:
  41. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  42. case kms.ErrCodeInvalidStateException:
  43. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  44. default:
  45. fmt.Println(aerr.Error())
  46. }
  47. } else {
  48. // Print the error, cast err to awserr.Error to get the Code and
  49. // Message from an error.
  50. fmt.Println(err.Error())
  51. }
  52. return
  53. }
  54. fmt.Println(result)
  55. }
  56. // To create an alias
  57. //
  58. // The following example creates an alias for the specified customer master key (CMK).
  59. func ExampleKMS_CreateAlias_shared00() {
  60. svc := kms.New(session.New())
  61. input := &kms.CreateAliasInput{
  62. AliasName: aws.String("alias/ExampleAlias"),
  63. TargetKeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  64. }
  65. result, err := svc.CreateAlias(input)
  66. if err != nil {
  67. if aerr, ok := err.(awserr.Error); ok {
  68. switch aerr.Code() {
  69. case kms.ErrCodeDependencyTimeoutException:
  70. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  71. case kms.ErrCodeAlreadyExistsException:
  72. fmt.Println(kms.ErrCodeAlreadyExistsException, aerr.Error())
  73. case kms.ErrCodeNotFoundException:
  74. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  75. case kms.ErrCodeInvalidAliasNameException:
  76. fmt.Println(kms.ErrCodeInvalidAliasNameException, aerr.Error())
  77. case kms.ErrCodeInternalException:
  78. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  79. case kms.ErrCodeLimitExceededException:
  80. fmt.Println(kms.ErrCodeLimitExceededException, aerr.Error())
  81. case kms.ErrCodeInvalidStateException:
  82. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  83. default:
  84. fmt.Println(aerr.Error())
  85. }
  86. } else {
  87. // Print the error, cast err to awserr.Error to get the Code and
  88. // Message from an error.
  89. fmt.Println(err.Error())
  90. }
  91. return
  92. }
  93. fmt.Println(result)
  94. }
  95. // To create a grant
  96. //
  97. // The following example creates a grant that allows the specified IAM role to encrypt
  98. // data with the specified customer master key (CMK).
  99. func ExampleKMS_CreateGrant_shared00() {
  100. svc := kms.New(session.New())
  101. input := &kms.CreateGrantInput{
  102. GranteePrincipal: aws.String("arn:aws:iam::111122223333:role/ExampleRole"),
  103. KeyId: aws.String("arn:aws:kms:us-east-2:444455556666:key/1234abcd-12ab-34cd-56ef-1234567890ab"),
  104. Operations: []*string{
  105. aws.String("Encrypt"),
  106. aws.String("Decrypt"),
  107. },
  108. }
  109. result, err := svc.CreateGrant(input)
  110. if err != nil {
  111. if aerr, ok := err.(awserr.Error); ok {
  112. switch aerr.Code() {
  113. case kms.ErrCodeNotFoundException:
  114. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  115. case kms.ErrCodeDisabledException:
  116. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  117. case kms.ErrCodeDependencyTimeoutException:
  118. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  119. case kms.ErrCodeInvalidArnException:
  120. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  121. case kms.ErrCodeInternalException:
  122. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  123. case kms.ErrCodeInvalidGrantTokenException:
  124. fmt.Println(kms.ErrCodeInvalidGrantTokenException, aerr.Error())
  125. case kms.ErrCodeLimitExceededException:
  126. fmt.Println(kms.ErrCodeLimitExceededException, aerr.Error())
  127. case kms.ErrCodeInvalidStateException:
  128. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  129. default:
  130. fmt.Println(aerr.Error())
  131. }
  132. } else {
  133. // Print the error, cast err to awserr.Error to get the Code and
  134. // Message from an error.
  135. fmt.Println(err.Error())
  136. }
  137. return
  138. }
  139. fmt.Println(result)
  140. }
  141. // To create a customer master key (CMK)
  142. //
  143. // The following example creates a CMK.
  144. func ExampleKMS_CreateKey_shared00() {
  145. svc := kms.New(session.New())
  146. input := &kms.CreateKeyInput{
  147. Tags: []*kms.Tag{
  148. {
  149. TagKey: aws.String("CreatedBy"),
  150. TagValue: aws.String("ExampleUser"),
  151. },
  152. },
  153. }
  154. result, err := svc.CreateKey(input)
  155. if err != nil {
  156. if aerr, ok := err.(awserr.Error); ok {
  157. switch aerr.Code() {
  158. case kms.ErrCodeMalformedPolicyDocumentException:
  159. fmt.Println(kms.ErrCodeMalformedPolicyDocumentException, aerr.Error())
  160. case kms.ErrCodeDependencyTimeoutException:
  161. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  162. case kms.ErrCodeInvalidArnException:
  163. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  164. case kms.ErrCodeUnsupportedOperationException:
  165. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  166. case kms.ErrCodeInternalException:
  167. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  168. case kms.ErrCodeLimitExceededException:
  169. fmt.Println(kms.ErrCodeLimitExceededException, aerr.Error())
  170. case kms.ErrCodeTagException:
  171. fmt.Println(kms.ErrCodeTagException, aerr.Error())
  172. case kms.ErrCodeCustomKeyStoreNotFoundException:
  173. fmt.Println(kms.ErrCodeCustomKeyStoreNotFoundException, aerr.Error())
  174. case kms.ErrCodeCustomKeyStoreInvalidStateException:
  175. fmt.Println(kms.ErrCodeCustomKeyStoreInvalidStateException, aerr.Error())
  176. case kms.ErrCodeCloudHsmClusterInvalidConfigurationException:
  177. fmt.Println(kms.ErrCodeCloudHsmClusterInvalidConfigurationException, aerr.Error())
  178. default:
  179. fmt.Println(aerr.Error())
  180. }
  181. } else {
  182. // Print the error, cast err to awserr.Error to get the Code and
  183. // Message from an error.
  184. fmt.Println(err.Error())
  185. }
  186. return
  187. }
  188. fmt.Println(result)
  189. }
  190. // To decrypt data
  191. //
  192. // The following example decrypts data that was encrypted with a customer master key
  193. // (CMK) in AWS KMS.
  194. func ExampleKMS_Decrypt_shared00() {
  195. svc := kms.New(session.New())
  196. input := &kms.DecryptInput{
  197. CiphertextBlob: []byte("<binary data>"),
  198. }
  199. result, err := svc.Decrypt(input)
  200. if err != nil {
  201. if aerr, ok := err.(awserr.Error); ok {
  202. switch aerr.Code() {
  203. case kms.ErrCodeNotFoundException:
  204. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  205. case kms.ErrCodeDisabledException:
  206. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  207. case kms.ErrCodeInvalidCiphertextException:
  208. fmt.Println(kms.ErrCodeInvalidCiphertextException, aerr.Error())
  209. case kms.ErrCodeKeyUnavailableException:
  210. fmt.Println(kms.ErrCodeKeyUnavailableException, aerr.Error())
  211. case kms.ErrCodeDependencyTimeoutException:
  212. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  213. case kms.ErrCodeInvalidGrantTokenException:
  214. fmt.Println(kms.ErrCodeInvalidGrantTokenException, aerr.Error())
  215. case kms.ErrCodeInternalException:
  216. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  217. case kms.ErrCodeInvalidStateException:
  218. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  219. default:
  220. fmt.Println(aerr.Error())
  221. }
  222. } else {
  223. // Print the error, cast err to awserr.Error to get the Code and
  224. // Message from an error.
  225. fmt.Println(err.Error())
  226. }
  227. return
  228. }
  229. fmt.Println(result)
  230. }
  231. // To delete an alias
  232. //
  233. // The following example deletes the specified alias.
  234. func ExampleKMS_DeleteAlias_shared00() {
  235. svc := kms.New(session.New())
  236. input := &kms.DeleteAliasInput{
  237. AliasName: aws.String("alias/ExampleAlias"),
  238. }
  239. result, err := svc.DeleteAlias(input)
  240. if err != nil {
  241. if aerr, ok := err.(awserr.Error); ok {
  242. switch aerr.Code() {
  243. case kms.ErrCodeDependencyTimeoutException:
  244. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  245. case kms.ErrCodeNotFoundException:
  246. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  247. case kms.ErrCodeInternalException:
  248. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  249. case kms.ErrCodeInvalidStateException:
  250. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  251. default:
  252. fmt.Println(aerr.Error())
  253. }
  254. } else {
  255. // Print the error, cast err to awserr.Error to get the Code and
  256. // Message from an error.
  257. fmt.Println(err.Error())
  258. }
  259. return
  260. }
  261. fmt.Println(result)
  262. }
  263. // To delete imported key material
  264. //
  265. // The following example deletes the imported key material from the specified customer
  266. // master key (CMK).
  267. func ExampleKMS_DeleteImportedKeyMaterial_shared00() {
  268. svc := kms.New(session.New())
  269. input := &kms.DeleteImportedKeyMaterialInput{
  270. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  271. }
  272. result, err := svc.DeleteImportedKeyMaterial(input)
  273. if err != nil {
  274. if aerr, ok := err.(awserr.Error); ok {
  275. switch aerr.Code() {
  276. case kms.ErrCodeInvalidArnException:
  277. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  278. case kms.ErrCodeUnsupportedOperationException:
  279. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  280. case kms.ErrCodeDependencyTimeoutException:
  281. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  282. case kms.ErrCodeNotFoundException:
  283. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  284. case kms.ErrCodeInternalException:
  285. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  286. case kms.ErrCodeInvalidStateException:
  287. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  288. default:
  289. fmt.Println(aerr.Error())
  290. }
  291. } else {
  292. // Print the error, cast err to awserr.Error to get the Code and
  293. // Message from an error.
  294. fmt.Println(err.Error())
  295. }
  296. return
  297. }
  298. fmt.Println(result)
  299. }
  300. // To obtain information about a customer master key (CMK)
  301. //
  302. // The following example returns information (metadata) about the specified CMK.
  303. func ExampleKMS_DescribeKey_shared00() {
  304. svc := kms.New(session.New())
  305. input := &kms.DescribeKeyInput{
  306. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  307. }
  308. result, err := svc.DescribeKey(input)
  309. if err != nil {
  310. if aerr, ok := err.(awserr.Error); ok {
  311. switch aerr.Code() {
  312. case kms.ErrCodeNotFoundException:
  313. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  314. case kms.ErrCodeInvalidArnException:
  315. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  316. case kms.ErrCodeDependencyTimeoutException:
  317. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  318. case kms.ErrCodeInternalException:
  319. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  320. default:
  321. fmt.Println(aerr.Error())
  322. }
  323. } else {
  324. // Print the error, cast err to awserr.Error to get the Code and
  325. // Message from an error.
  326. fmt.Println(err.Error())
  327. }
  328. return
  329. }
  330. fmt.Println(result)
  331. }
  332. // To disable a customer master key (CMK)
  333. //
  334. // The following example disables the specified CMK.
  335. func ExampleKMS_DisableKey_shared00() {
  336. svc := kms.New(session.New())
  337. input := &kms.DisableKeyInput{
  338. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  339. }
  340. result, err := svc.DisableKey(input)
  341. if err != nil {
  342. if aerr, ok := err.(awserr.Error); ok {
  343. switch aerr.Code() {
  344. case kms.ErrCodeNotFoundException:
  345. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  346. case kms.ErrCodeInvalidArnException:
  347. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  348. case kms.ErrCodeDependencyTimeoutException:
  349. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  350. case kms.ErrCodeInternalException:
  351. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  352. case kms.ErrCodeInvalidStateException:
  353. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  354. default:
  355. fmt.Println(aerr.Error())
  356. }
  357. } else {
  358. // Print the error, cast err to awserr.Error to get the Code and
  359. // Message from an error.
  360. fmt.Println(err.Error())
  361. }
  362. return
  363. }
  364. fmt.Println(result)
  365. }
  366. // To disable automatic rotation of key material
  367. //
  368. // The following example disables automatic annual rotation of the key material for
  369. // the specified CMK.
  370. func ExampleKMS_DisableKeyRotation_shared00() {
  371. svc := kms.New(session.New())
  372. input := &kms.DisableKeyRotationInput{
  373. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  374. }
  375. result, err := svc.DisableKeyRotation(input)
  376. if err != nil {
  377. if aerr, ok := err.(awserr.Error); ok {
  378. switch aerr.Code() {
  379. case kms.ErrCodeNotFoundException:
  380. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  381. case kms.ErrCodeDisabledException:
  382. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  383. case kms.ErrCodeInvalidArnException:
  384. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  385. case kms.ErrCodeDependencyTimeoutException:
  386. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  387. case kms.ErrCodeInternalException:
  388. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  389. case kms.ErrCodeInvalidStateException:
  390. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  391. case kms.ErrCodeUnsupportedOperationException:
  392. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  393. default:
  394. fmt.Println(aerr.Error())
  395. }
  396. } else {
  397. // Print the error, cast err to awserr.Error to get the Code and
  398. // Message from an error.
  399. fmt.Println(err.Error())
  400. }
  401. return
  402. }
  403. fmt.Println(result)
  404. }
  405. // To enable a customer master key (CMK)
  406. //
  407. // The following example enables the specified CMK.
  408. func ExampleKMS_EnableKey_shared00() {
  409. svc := kms.New(session.New())
  410. input := &kms.EnableKeyInput{
  411. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  412. }
  413. result, err := svc.EnableKey(input)
  414. if err != nil {
  415. if aerr, ok := err.(awserr.Error); ok {
  416. switch aerr.Code() {
  417. case kms.ErrCodeNotFoundException:
  418. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  419. case kms.ErrCodeInvalidArnException:
  420. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  421. case kms.ErrCodeDependencyTimeoutException:
  422. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  423. case kms.ErrCodeInternalException:
  424. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  425. case kms.ErrCodeLimitExceededException:
  426. fmt.Println(kms.ErrCodeLimitExceededException, aerr.Error())
  427. case kms.ErrCodeInvalidStateException:
  428. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  429. default:
  430. fmt.Println(aerr.Error())
  431. }
  432. } else {
  433. // Print the error, cast err to awserr.Error to get the Code and
  434. // Message from an error.
  435. fmt.Println(err.Error())
  436. }
  437. return
  438. }
  439. fmt.Println(result)
  440. }
  441. // To enable automatic rotation of key material
  442. //
  443. // The following example enables automatic annual rotation of the key material for the
  444. // specified CMK.
  445. func ExampleKMS_EnableKeyRotation_shared00() {
  446. svc := kms.New(session.New())
  447. input := &kms.EnableKeyRotationInput{
  448. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  449. }
  450. result, err := svc.EnableKeyRotation(input)
  451. if err != nil {
  452. if aerr, ok := err.(awserr.Error); ok {
  453. switch aerr.Code() {
  454. case kms.ErrCodeNotFoundException:
  455. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  456. case kms.ErrCodeDisabledException:
  457. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  458. case kms.ErrCodeInvalidArnException:
  459. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  460. case kms.ErrCodeDependencyTimeoutException:
  461. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  462. case kms.ErrCodeInternalException:
  463. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  464. case kms.ErrCodeInvalidStateException:
  465. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  466. case kms.ErrCodeUnsupportedOperationException:
  467. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  468. default:
  469. fmt.Println(aerr.Error())
  470. }
  471. } else {
  472. // Print the error, cast err to awserr.Error to get the Code and
  473. // Message from an error.
  474. fmt.Println(err.Error())
  475. }
  476. return
  477. }
  478. fmt.Println(result)
  479. }
  480. // To encrypt data
  481. //
  482. // The following example encrypts data with the specified customer master key (CMK).
  483. func ExampleKMS_Encrypt_shared00() {
  484. svc := kms.New(session.New())
  485. input := &kms.EncryptInput{
  486. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  487. Plaintext: []byte("<binary data>"),
  488. }
  489. result, err := svc.Encrypt(input)
  490. if err != nil {
  491. if aerr, ok := err.(awserr.Error); ok {
  492. switch aerr.Code() {
  493. case kms.ErrCodeNotFoundException:
  494. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  495. case kms.ErrCodeDisabledException:
  496. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  497. case kms.ErrCodeKeyUnavailableException:
  498. fmt.Println(kms.ErrCodeKeyUnavailableException, aerr.Error())
  499. case kms.ErrCodeDependencyTimeoutException:
  500. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  501. case kms.ErrCodeInvalidKeyUsageException:
  502. fmt.Println(kms.ErrCodeInvalidKeyUsageException, aerr.Error())
  503. case kms.ErrCodeInvalidGrantTokenException:
  504. fmt.Println(kms.ErrCodeInvalidGrantTokenException, aerr.Error())
  505. case kms.ErrCodeInternalException:
  506. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  507. case kms.ErrCodeInvalidStateException:
  508. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  509. default:
  510. fmt.Println(aerr.Error())
  511. }
  512. } else {
  513. // Print the error, cast err to awserr.Error to get the Code and
  514. // Message from an error.
  515. fmt.Println(err.Error())
  516. }
  517. return
  518. }
  519. fmt.Println(result)
  520. }
  521. // To generate a data key
  522. //
  523. // The following example generates a 256-bit symmetric data encryption key (data key)
  524. // in two formats. One is the unencrypted (plainext) data key, and the other is the
  525. // data key encrypted with the specified customer master key (CMK).
  526. func ExampleKMS_GenerateDataKey_shared00() {
  527. svc := kms.New(session.New())
  528. input := &kms.GenerateDataKeyInput{
  529. KeyId: aws.String("alias/ExampleAlias"),
  530. KeySpec: aws.String("AES_256"),
  531. }
  532. result, err := svc.GenerateDataKey(input)
  533. if err != nil {
  534. if aerr, ok := err.(awserr.Error); ok {
  535. switch aerr.Code() {
  536. case kms.ErrCodeNotFoundException:
  537. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  538. case kms.ErrCodeDisabledException:
  539. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  540. case kms.ErrCodeKeyUnavailableException:
  541. fmt.Println(kms.ErrCodeKeyUnavailableException, aerr.Error())
  542. case kms.ErrCodeDependencyTimeoutException:
  543. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  544. case kms.ErrCodeInvalidKeyUsageException:
  545. fmt.Println(kms.ErrCodeInvalidKeyUsageException, aerr.Error())
  546. case kms.ErrCodeInvalidGrantTokenException:
  547. fmt.Println(kms.ErrCodeInvalidGrantTokenException, aerr.Error())
  548. case kms.ErrCodeInternalException:
  549. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  550. case kms.ErrCodeInvalidStateException:
  551. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  552. default:
  553. fmt.Println(aerr.Error())
  554. }
  555. } else {
  556. // Print the error, cast err to awserr.Error to get the Code and
  557. // Message from an error.
  558. fmt.Println(err.Error())
  559. }
  560. return
  561. }
  562. fmt.Println(result)
  563. }
  564. // To generate an encrypted data key
  565. //
  566. // The following example generates an encrypted copy of a 256-bit symmetric data encryption
  567. // key (data key). The data key is encrypted with the specified customer master key
  568. // (CMK).
  569. func ExampleKMS_GenerateDataKeyWithoutPlaintext_shared00() {
  570. svc := kms.New(session.New())
  571. input := &kms.GenerateDataKeyWithoutPlaintextInput{
  572. KeyId: aws.String("alias/ExampleAlias"),
  573. KeySpec: aws.String("AES_256"),
  574. }
  575. result, err := svc.GenerateDataKeyWithoutPlaintext(input)
  576. if err != nil {
  577. if aerr, ok := err.(awserr.Error); ok {
  578. switch aerr.Code() {
  579. case kms.ErrCodeNotFoundException:
  580. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  581. case kms.ErrCodeDisabledException:
  582. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  583. case kms.ErrCodeKeyUnavailableException:
  584. fmt.Println(kms.ErrCodeKeyUnavailableException, aerr.Error())
  585. case kms.ErrCodeDependencyTimeoutException:
  586. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  587. case kms.ErrCodeInvalidKeyUsageException:
  588. fmt.Println(kms.ErrCodeInvalidKeyUsageException, aerr.Error())
  589. case kms.ErrCodeInvalidGrantTokenException:
  590. fmt.Println(kms.ErrCodeInvalidGrantTokenException, aerr.Error())
  591. case kms.ErrCodeInternalException:
  592. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  593. case kms.ErrCodeInvalidStateException:
  594. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  595. default:
  596. fmt.Println(aerr.Error())
  597. }
  598. } else {
  599. // Print the error, cast err to awserr.Error to get the Code and
  600. // Message from an error.
  601. fmt.Println(err.Error())
  602. }
  603. return
  604. }
  605. fmt.Println(result)
  606. }
  607. // To generate random data
  608. //
  609. // The following example uses AWS KMS to generate 32 bytes of random data.
  610. func ExampleKMS_GenerateRandom_shared00() {
  611. svc := kms.New(session.New())
  612. input := &kms.GenerateRandomInput{
  613. NumberOfBytes: aws.Int64(32),
  614. }
  615. result, err := svc.GenerateRandom(input)
  616. if err != nil {
  617. if aerr, ok := err.(awserr.Error); ok {
  618. switch aerr.Code() {
  619. case kms.ErrCodeDependencyTimeoutException:
  620. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  621. case kms.ErrCodeInternalException:
  622. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  623. case kms.ErrCodeCustomKeyStoreNotFoundException:
  624. fmt.Println(kms.ErrCodeCustomKeyStoreNotFoundException, aerr.Error())
  625. case kms.ErrCodeCustomKeyStoreInvalidStateException:
  626. fmt.Println(kms.ErrCodeCustomKeyStoreInvalidStateException, aerr.Error())
  627. default:
  628. fmt.Println(aerr.Error())
  629. }
  630. } else {
  631. // Print the error, cast err to awserr.Error to get the Code and
  632. // Message from an error.
  633. fmt.Println(err.Error())
  634. }
  635. return
  636. }
  637. fmt.Println(result)
  638. }
  639. // To retrieve a key policy
  640. //
  641. // The following example retrieves the key policy for the specified customer master
  642. // key (CMK).
  643. func ExampleKMS_GetKeyPolicy_shared00() {
  644. svc := kms.New(session.New())
  645. input := &kms.GetKeyPolicyInput{
  646. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  647. PolicyName: aws.String("default"),
  648. }
  649. result, err := svc.GetKeyPolicy(input)
  650. if err != nil {
  651. if aerr, ok := err.(awserr.Error); ok {
  652. switch aerr.Code() {
  653. case kms.ErrCodeNotFoundException:
  654. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  655. case kms.ErrCodeInvalidArnException:
  656. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  657. case kms.ErrCodeDependencyTimeoutException:
  658. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  659. case kms.ErrCodeInternalException:
  660. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  661. case kms.ErrCodeInvalidStateException:
  662. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  663. default:
  664. fmt.Println(aerr.Error())
  665. }
  666. } else {
  667. // Print the error, cast err to awserr.Error to get the Code and
  668. // Message from an error.
  669. fmt.Println(err.Error())
  670. }
  671. return
  672. }
  673. fmt.Println(result)
  674. }
  675. // To retrieve the rotation status for a customer master key (CMK)
  676. //
  677. // The following example retrieves the status of automatic annual rotation of the key
  678. // material for the specified CMK.
  679. func ExampleKMS_GetKeyRotationStatus_shared00() {
  680. svc := kms.New(session.New())
  681. input := &kms.GetKeyRotationStatusInput{
  682. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  683. }
  684. result, err := svc.GetKeyRotationStatus(input)
  685. if err != nil {
  686. if aerr, ok := err.(awserr.Error); ok {
  687. switch aerr.Code() {
  688. case kms.ErrCodeNotFoundException:
  689. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  690. case kms.ErrCodeInvalidArnException:
  691. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  692. case kms.ErrCodeDependencyTimeoutException:
  693. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  694. case kms.ErrCodeInternalException:
  695. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  696. case kms.ErrCodeInvalidStateException:
  697. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  698. case kms.ErrCodeUnsupportedOperationException:
  699. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  700. default:
  701. fmt.Println(aerr.Error())
  702. }
  703. } else {
  704. // Print the error, cast err to awserr.Error to get the Code and
  705. // Message from an error.
  706. fmt.Println(err.Error())
  707. }
  708. return
  709. }
  710. fmt.Println(result)
  711. }
  712. // To retrieve the public key and import token for a customer master key (CMK)
  713. //
  714. // The following example retrieves the public key and import token for the specified
  715. // CMK.
  716. func ExampleKMS_GetParametersForImport_shared00() {
  717. svc := kms.New(session.New())
  718. input := &kms.GetParametersForImportInput{
  719. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  720. WrappingAlgorithm: aws.String("RSAES_OAEP_SHA_1"),
  721. WrappingKeySpec: aws.String("RSA_2048"),
  722. }
  723. result, err := svc.GetParametersForImport(input)
  724. if err != nil {
  725. if aerr, ok := err.(awserr.Error); ok {
  726. switch aerr.Code() {
  727. case kms.ErrCodeInvalidArnException:
  728. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  729. case kms.ErrCodeUnsupportedOperationException:
  730. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  731. case kms.ErrCodeDependencyTimeoutException:
  732. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  733. case kms.ErrCodeNotFoundException:
  734. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  735. case kms.ErrCodeInternalException:
  736. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  737. case kms.ErrCodeInvalidStateException:
  738. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  739. default:
  740. fmt.Println(aerr.Error())
  741. }
  742. } else {
  743. // Print the error, cast err to awserr.Error to get the Code and
  744. // Message from an error.
  745. fmt.Println(err.Error())
  746. }
  747. return
  748. }
  749. fmt.Println(result)
  750. }
  751. // To import key material into a customer master key (CMK)
  752. //
  753. // The following example imports key material into the specified CMK.
  754. func ExampleKMS_ImportKeyMaterial_shared00() {
  755. svc := kms.New(session.New())
  756. input := &kms.ImportKeyMaterialInput{
  757. EncryptedKeyMaterial: []byte("<binary data>"),
  758. ExpirationModel: aws.String("KEY_MATERIAL_DOES_NOT_EXPIRE"),
  759. ImportToken: []byte("<binary data>"),
  760. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  761. }
  762. result, err := svc.ImportKeyMaterial(input)
  763. if err != nil {
  764. if aerr, ok := err.(awserr.Error); ok {
  765. switch aerr.Code() {
  766. case kms.ErrCodeInvalidArnException:
  767. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  768. case kms.ErrCodeUnsupportedOperationException:
  769. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  770. case kms.ErrCodeDependencyTimeoutException:
  771. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  772. case kms.ErrCodeNotFoundException:
  773. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  774. case kms.ErrCodeInternalException:
  775. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  776. case kms.ErrCodeInvalidStateException:
  777. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  778. case kms.ErrCodeInvalidCiphertextException:
  779. fmt.Println(kms.ErrCodeInvalidCiphertextException, aerr.Error())
  780. case kms.ErrCodeIncorrectKeyMaterialException:
  781. fmt.Println(kms.ErrCodeIncorrectKeyMaterialException, aerr.Error())
  782. case kms.ErrCodeExpiredImportTokenException:
  783. fmt.Println(kms.ErrCodeExpiredImportTokenException, aerr.Error())
  784. case kms.ErrCodeInvalidImportTokenException:
  785. fmt.Println(kms.ErrCodeInvalidImportTokenException, aerr.Error())
  786. default:
  787. fmt.Println(aerr.Error())
  788. }
  789. } else {
  790. // Print the error, cast err to awserr.Error to get the Code and
  791. // Message from an error.
  792. fmt.Println(err.Error())
  793. }
  794. return
  795. }
  796. fmt.Println(result)
  797. }
  798. // To list aliases
  799. //
  800. // The following example lists aliases.
  801. func ExampleKMS_ListAliases_shared00() {
  802. svc := kms.New(session.New())
  803. input := &kms.ListAliasesInput{}
  804. result, err := svc.ListAliases(input)
  805. if err != nil {
  806. if aerr, ok := err.(awserr.Error); ok {
  807. switch aerr.Code() {
  808. case kms.ErrCodeDependencyTimeoutException:
  809. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  810. case kms.ErrCodeInvalidMarkerException:
  811. fmt.Println(kms.ErrCodeInvalidMarkerException, aerr.Error())
  812. case kms.ErrCodeInternalException:
  813. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  814. case kms.ErrCodeInvalidArnException:
  815. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  816. case kms.ErrCodeNotFoundException:
  817. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  818. default:
  819. fmt.Println(aerr.Error())
  820. }
  821. } else {
  822. // Print the error, cast err to awserr.Error to get the Code and
  823. // Message from an error.
  824. fmt.Println(err.Error())
  825. }
  826. return
  827. }
  828. fmt.Println(result)
  829. }
  830. // To list grants for a customer master key (CMK)
  831. //
  832. // The following example lists grants for the specified CMK.
  833. func ExampleKMS_ListGrants_shared00() {
  834. svc := kms.New(session.New())
  835. input := &kms.ListGrantsInput{
  836. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  837. }
  838. result, err := svc.ListGrants(input)
  839. if err != nil {
  840. if aerr, ok := err.(awserr.Error); ok {
  841. switch aerr.Code() {
  842. case kms.ErrCodeNotFoundException:
  843. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  844. case kms.ErrCodeDependencyTimeoutException:
  845. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  846. case kms.ErrCodeInvalidMarkerException:
  847. fmt.Println(kms.ErrCodeInvalidMarkerException, aerr.Error())
  848. case kms.ErrCodeInvalidArnException:
  849. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  850. case kms.ErrCodeInternalException:
  851. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  852. case kms.ErrCodeInvalidStateException:
  853. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  854. default:
  855. fmt.Println(aerr.Error())
  856. }
  857. } else {
  858. // Print the error, cast err to awserr.Error to get the Code and
  859. // Message from an error.
  860. fmt.Println(err.Error())
  861. }
  862. return
  863. }
  864. fmt.Println(result)
  865. }
  866. // To list key policies for a customer master key (CMK)
  867. //
  868. // The following example lists key policies for the specified CMK.
  869. func ExampleKMS_ListKeyPolicies_shared00() {
  870. svc := kms.New(session.New())
  871. input := &kms.ListKeyPoliciesInput{
  872. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  873. }
  874. result, err := svc.ListKeyPolicies(input)
  875. if err != nil {
  876. if aerr, ok := err.(awserr.Error); ok {
  877. switch aerr.Code() {
  878. case kms.ErrCodeNotFoundException:
  879. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  880. case kms.ErrCodeInvalidArnException:
  881. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  882. case kms.ErrCodeDependencyTimeoutException:
  883. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  884. case kms.ErrCodeInternalException:
  885. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  886. case kms.ErrCodeInvalidStateException:
  887. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  888. default:
  889. fmt.Println(aerr.Error())
  890. }
  891. } else {
  892. // Print the error, cast err to awserr.Error to get the Code and
  893. // Message from an error.
  894. fmt.Println(err.Error())
  895. }
  896. return
  897. }
  898. fmt.Println(result)
  899. }
  900. // To list customer master keys (CMKs)
  901. //
  902. // The following example lists CMKs.
  903. func ExampleKMS_ListKeys_shared00() {
  904. svc := kms.New(session.New())
  905. input := &kms.ListKeysInput{}
  906. result, err := svc.ListKeys(input)
  907. if err != nil {
  908. if aerr, ok := err.(awserr.Error); ok {
  909. switch aerr.Code() {
  910. case kms.ErrCodeDependencyTimeoutException:
  911. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  912. case kms.ErrCodeInternalException:
  913. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  914. case kms.ErrCodeInvalidMarkerException:
  915. fmt.Println(kms.ErrCodeInvalidMarkerException, aerr.Error())
  916. default:
  917. fmt.Println(aerr.Error())
  918. }
  919. } else {
  920. // Print the error, cast err to awserr.Error to get the Code and
  921. // Message from an error.
  922. fmt.Println(err.Error())
  923. }
  924. return
  925. }
  926. fmt.Println(result)
  927. }
  928. // To list tags for a customer master key (CMK)
  929. //
  930. // The following example lists tags for a CMK.
  931. func ExampleKMS_ListResourceTags_shared00() {
  932. svc := kms.New(session.New())
  933. input := &kms.ListResourceTagsInput{
  934. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  935. }
  936. result, err := svc.ListResourceTags(input)
  937. if err != nil {
  938. if aerr, ok := err.(awserr.Error); ok {
  939. switch aerr.Code() {
  940. case kms.ErrCodeInternalException:
  941. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  942. case kms.ErrCodeNotFoundException:
  943. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  944. case kms.ErrCodeInvalidArnException:
  945. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  946. case kms.ErrCodeInvalidMarkerException:
  947. fmt.Println(kms.ErrCodeInvalidMarkerException, aerr.Error())
  948. default:
  949. fmt.Println(aerr.Error())
  950. }
  951. } else {
  952. // Print the error, cast err to awserr.Error to get the Code and
  953. // Message from an error.
  954. fmt.Println(err.Error())
  955. }
  956. return
  957. }
  958. fmt.Println(result)
  959. }
  960. // To list grants that the specified principal can retire
  961. //
  962. // The following example lists the grants that the specified principal (identity) can
  963. // retire.
  964. func ExampleKMS_ListRetirableGrants_shared00() {
  965. svc := kms.New(session.New())
  966. input := &kms.ListRetirableGrantsInput{
  967. RetiringPrincipal: aws.String("arn:aws:iam::111122223333:role/ExampleRole"),
  968. }
  969. result, err := svc.ListRetirableGrants(input)
  970. if err != nil {
  971. if aerr, ok := err.(awserr.Error); ok {
  972. switch aerr.Code() {
  973. case kms.ErrCodeDependencyTimeoutException:
  974. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  975. case kms.ErrCodeInvalidMarkerException:
  976. fmt.Println(kms.ErrCodeInvalidMarkerException, aerr.Error())
  977. case kms.ErrCodeInvalidArnException:
  978. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  979. case kms.ErrCodeNotFoundException:
  980. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  981. case kms.ErrCodeInternalException:
  982. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  983. default:
  984. fmt.Println(aerr.Error())
  985. }
  986. } else {
  987. // Print the error, cast err to awserr.Error to get the Code and
  988. // Message from an error.
  989. fmt.Println(err.Error())
  990. }
  991. return
  992. }
  993. fmt.Println(result)
  994. }
  995. // To attach a key policy to a customer master key (CMK)
  996. //
  997. // The following example attaches a key policy to the specified CMK.
  998. func ExampleKMS_PutKeyPolicy_shared00() {
  999. svc := kms.New(session.New())
  1000. input := &kms.PutKeyPolicyInput{
  1001. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  1002. Policy: aws.String("{\n \"Version\": \"2012-10-17\",\n \"Id\": \"custom-policy-2016-12-07\",\n \"Statement\": [\n {\n \"Sid\": \"Enable IAM User Permissions\",\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"AWS\": \"arn:aws:iam::111122223333:root\"\n },\n \"Action\": \"kms:*\",\n \"Resource\": \"*\"\n },\n {\n \"Sid\": \"Allow access for Key Administrators\",\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"AWS\": [\n \"arn:aws:iam::111122223333:user/ExampleAdminUser\",\n \"arn:aws:iam::111122223333:role/ExampleAdminRole\"\n ]\n },\n \"Action\": [\n \"kms:Create*\",\n \"kms:Describe*\",\n \"kms:Enable*\",\n \"kms:List*\",\n \"kms:Put*\",\n \"kms:Update*\",\n \"kms:Revoke*\",\n \"kms:Disable*\",\n \"kms:Get*\",\n \"kms:Delete*\",\n \"kms:ScheduleKeyDeletion\",\n \"kms:CancelKeyDeletion\"\n ],\n \"Resource\": \"*\"\n },\n {\n \"Sid\": \"Allow use of the key\",\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"AWS\": \"arn:aws:iam::111122223333:role/ExamplePowerUserRole\"\n },\n \"Action\": [\n \"kms:Encrypt\",\n \"kms:Decrypt\",\n \"kms:ReEncrypt*\",\n \"kms:GenerateDataKey*\",\n \"kms:DescribeKey\"\n ],\n \"Resource\": \"*\"\n },\n {\n \"Sid\": \"Allow attachment of persistent resources\",\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"AWS\": \"arn:aws:iam::111122223333:role/ExamplePowerUserRole\"\n },\n \"Action\": [\n \"kms:CreateGrant\",\n \"kms:ListGrants\",\n \"kms:RevokeGrant\"\n ],\n \"Resource\": \"*\",\n \"Condition\": {\n \"Bool\": {\n \"kms:GrantIsForAWSResource\": \"true\"\n }\n }\n }\n ]\n}\n"),
  1003. PolicyName: aws.String("default"),
  1004. }
  1005. result, err := svc.PutKeyPolicy(input)
  1006. if err != nil {
  1007. if aerr, ok := err.(awserr.Error); ok {
  1008. switch aerr.Code() {
  1009. case kms.ErrCodeNotFoundException:
  1010. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1011. case kms.ErrCodeInvalidArnException:
  1012. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  1013. case kms.ErrCodeMalformedPolicyDocumentException:
  1014. fmt.Println(kms.ErrCodeMalformedPolicyDocumentException, aerr.Error())
  1015. case kms.ErrCodeDependencyTimeoutException:
  1016. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  1017. case kms.ErrCodeUnsupportedOperationException:
  1018. fmt.Println(kms.ErrCodeUnsupportedOperationException, aerr.Error())
  1019. case kms.ErrCodeInternalException:
  1020. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1021. case kms.ErrCodeLimitExceededException:
  1022. fmt.Println(kms.ErrCodeLimitExceededException, aerr.Error())
  1023. case kms.ErrCodeInvalidStateException:
  1024. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1025. default:
  1026. fmt.Println(aerr.Error())
  1027. }
  1028. } else {
  1029. // Print the error, cast err to awserr.Error to get the Code and
  1030. // Message from an error.
  1031. fmt.Println(err.Error())
  1032. }
  1033. return
  1034. }
  1035. fmt.Println(result)
  1036. }
  1037. // To reencrypt data
  1038. //
  1039. // The following example reencrypts data with the specified CMK.
  1040. func ExampleKMS_ReEncrypt_shared00() {
  1041. svc := kms.New(session.New())
  1042. input := &kms.ReEncryptInput{
  1043. CiphertextBlob: []byte("<binary data>"),
  1044. DestinationKeyId: aws.String("0987dcba-09fe-87dc-65ba-ab0987654321"),
  1045. }
  1046. result, err := svc.ReEncrypt(input)
  1047. if err != nil {
  1048. if aerr, ok := err.(awserr.Error); ok {
  1049. switch aerr.Code() {
  1050. case kms.ErrCodeNotFoundException:
  1051. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1052. case kms.ErrCodeDisabledException:
  1053. fmt.Println(kms.ErrCodeDisabledException, aerr.Error())
  1054. case kms.ErrCodeInvalidCiphertextException:
  1055. fmt.Println(kms.ErrCodeInvalidCiphertextException, aerr.Error())
  1056. case kms.ErrCodeKeyUnavailableException:
  1057. fmt.Println(kms.ErrCodeKeyUnavailableException, aerr.Error())
  1058. case kms.ErrCodeDependencyTimeoutException:
  1059. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  1060. case kms.ErrCodeInvalidKeyUsageException:
  1061. fmt.Println(kms.ErrCodeInvalidKeyUsageException, aerr.Error())
  1062. case kms.ErrCodeInvalidGrantTokenException:
  1063. fmt.Println(kms.ErrCodeInvalidGrantTokenException, aerr.Error())
  1064. case kms.ErrCodeInternalException:
  1065. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1066. case kms.ErrCodeInvalidStateException:
  1067. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1068. default:
  1069. fmt.Println(aerr.Error())
  1070. }
  1071. } else {
  1072. // Print the error, cast err to awserr.Error to get the Code and
  1073. // Message from an error.
  1074. fmt.Println(err.Error())
  1075. }
  1076. return
  1077. }
  1078. fmt.Println(result)
  1079. }
  1080. // To retire a grant
  1081. //
  1082. // The following example retires a grant.
  1083. func ExampleKMS_RetireGrant_shared00() {
  1084. svc := kms.New(session.New())
  1085. input := &kms.RetireGrantInput{
  1086. GrantId: aws.String("0c237476b39f8bc44e45212e08498fbe3151305030726c0590dd8d3e9f3d6a60"),
  1087. KeyId: aws.String("arn:aws:kms:us-east-2:444455556666:key/1234abcd-12ab-34cd-56ef-1234567890ab"),
  1088. }
  1089. result, err := svc.RetireGrant(input)
  1090. if err != nil {
  1091. if aerr, ok := err.(awserr.Error); ok {
  1092. switch aerr.Code() {
  1093. case kms.ErrCodeInvalidArnException:
  1094. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  1095. case kms.ErrCodeInvalidGrantTokenException:
  1096. fmt.Println(kms.ErrCodeInvalidGrantTokenException, aerr.Error())
  1097. case kms.ErrCodeInvalidGrantIdException:
  1098. fmt.Println(kms.ErrCodeInvalidGrantIdException, aerr.Error())
  1099. case kms.ErrCodeNotFoundException:
  1100. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1101. case kms.ErrCodeDependencyTimeoutException:
  1102. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  1103. case kms.ErrCodeInternalException:
  1104. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1105. case kms.ErrCodeInvalidStateException:
  1106. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1107. default:
  1108. fmt.Println(aerr.Error())
  1109. }
  1110. } else {
  1111. // Print the error, cast err to awserr.Error to get the Code and
  1112. // Message from an error.
  1113. fmt.Println(err.Error())
  1114. }
  1115. return
  1116. }
  1117. fmt.Println(result)
  1118. }
  1119. // To revoke a grant
  1120. //
  1121. // The following example revokes a grant.
  1122. func ExampleKMS_RevokeGrant_shared00() {
  1123. svc := kms.New(session.New())
  1124. input := &kms.RevokeGrantInput{
  1125. GrantId: aws.String("0c237476b39f8bc44e45212e08498fbe3151305030726c0590dd8d3e9f3d6a60"),
  1126. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  1127. }
  1128. result, err := svc.RevokeGrant(input)
  1129. if err != nil {
  1130. if aerr, ok := err.(awserr.Error); ok {
  1131. switch aerr.Code() {
  1132. case kms.ErrCodeNotFoundException:
  1133. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1134. case kms.ErrCodeDependencyTimeoutException:
  1135. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  1136. case kms.ErrCodeInvalidArnException:
  1137. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  1138. case kms.ErrCodeInvalidGrantIdException:
  1139. fmt.Println(kms.ErrCodeInvalidGrantIdException, aerr.Error())
  1140. case kms.ErrCodeInternalException:
  1141. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1142. case kms.ErrCodeInvalidStateException:
  1143. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1144. default:
  1145. fmt.Println(aerr.Error())
  1146. }
  1147. } else {
  1148. // Print the error, cast err to awserr.Error to get the Code and
  1149. // Message from an error.
  1150. fmt.Println(err.Error())
  1151. }
  1152. return
  1153. }
  1154. fmt.Println(result)
  1155. }
  1156. // To schedule a customer master key (CMK) for deletion
  1157. //
  1158. // The following example schedules the specified CMK for deletion.
  1159. func ExampleKMS_ScheduleKeyDeletion_shared00() {
  1160. svc := kms.New(session.New())
  1161. input := &kms.ScheduleKeyDeletionInput{
  1162. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  1163. PendingWindowInDays: aws.Int64(7),
  1164. }
  1165. result, err := svc.ScheduleKeyDeletion(input)
  1166. if err != nil {
  1167. if aerr, ok := err.(awserr.Error); ok {
  1168. switch aerr.Code() {
  1169. case kms.ErrCodeNotFoundException:
  1170. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1171. case kms.ErrCodeInvalidArnException:
  1172. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  1173. case kms.ErrCodeDependencyTimeoutException:
  1174. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  1175. case kms.ErrCodeInternalException:
  1176. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1177. case kms.ErrCodeInvalidStateException:
  1178. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1179. default:
  1180. fmt.Println(aerr.Error())
  1181. }
  1182. } else {
  1183. // Print the error, cast err to awserr.Error to get the Code and
  1184. // Message from an error.
  1185. fmt.Println(err.Error())
  1186. }
  1187. return
  1188. }
  1189. fmt.Println(result)
  1190. }
  1191. // To tag a customer master key (CMK)
  1192. //
  1193. // The following example tags a CMK.
  1194. func ExampleKMS_TagResource_shared00() {
  1195. svc := kms.New(session.New())
  1196. input := &kms.TagResourceInput{
  1197. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  1198. Tags: []*kms.Tag{
  1199. {
  1200. TagKey: aws.String("Purpose"),
  1201. TagValue: aws.String("Test"),
  1202. },
  1203. },
  1204. }
  1205. result, err := svc.TagResource(input)
  1206. if err != nil {
  1207. if aerr, ok := err.(awserr.Error); ok {
  1208. switch aerr.Code() {
  1209. case kms.ErrCodeInternalException:
  1210. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1211. case kms.ErrCodeNotFoundException:
  1212. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1213. case kms.ErrCodeInvalidArnException:
  1214. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  1215. case kms.ErrCodeInvalidStateException:
  1216. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1217. case kms.ErrCodeLimitExceededException:
  1218. fmt.Println(kms.ErrCodeLimitExceededException, aerr.Error())
  1219. case kms.ErrCodeTagException:
  1220. fmt.Println(kms.ErrCodeTagException, aerr.Error())
  1221. default:
  1222. fmt.Println(aerr.Error())
  1223. }
  1224. } else {
  1225. // Print the error, cast err to awserr.Error to get the Code and
  1226. // Message from an error.
  1227. fmt.Println(err.Error())
  1228. }
  1229. return
  1230. }
  1231. fmt.Println(result)
  1232. }
  1233. // To remove tags from a customer master key (CMK)
  1234. //
  1235. // The following example removes tags from a CMK.
  1236. func ExampleKMS_UntagResource_shared00() {
  1237. svc := kms.New(session.New())
  1238. input := &kms.UntagResourceInput{
  1239. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  1240. TagKeys: []*string{
  1241. aws.String("Purpose"),
  1242. aws.String("CostCenter"),
  1243. },
  1244. }
  1245. result, err := svc.UntagResource(input)
  1246. if err != nil {
  1247. if aerr, ok := err.(awserr.Error); ok {
  1248. switch aerr.Code() {
  1249. case kms.ErrCodeInternalException:
  1250. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1251. case kms.ErrCodeNotFoundException:
  1252. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1253. case kms.ErrCodeInvalidArnException:
  1254. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  1255. case kms.ErrCodeInvalidStateException:
  1256. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1257. case kms.ErrCodeTagException:
  1258. fmt.Println(kms.ErrCodeTagException, aerr.Error())
  1259. default:
  1260. fmt.Println(aerr.Error())
  1261. }
  1262. } else {
  1263. // Print the error, cast err to awserr.Error to get the Code and
  1264. // Message from an error.
  1265. fmt.Println(err.Error())
  1266. }
  1267. return
  1268. }
  1269. fmt.Println(result)
  1270. }
  1271. // To update an alias
  1272. //
  1273. // The following example updates the specified alias to refer to the specified customer
  1274. // master key (CMK).
  1275. func ExampleKMS_UpdateAlias_shared00() {
  1276. svc := kms.New(session.New())
  1277. input := &kms.UpdateAliasInput{
  1278. AliasName: aws.String("alias/ExampleAlias"),
  1279. TargetKeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  1280. }
  1281. result, err := svc.UpdateAlias(input)
  1282. if err != nil {
  1283. if aerr, ok := err.(awserr.Error); ok {
  1284. switch aerr.Code() {
  1285. case kms.ErrCodeDependencyTimeoutException:
  1286. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  1287. case kms.ErrCodeNotFoundException:
  1288. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1289. case kms.ErrCodeInternalException:
  1290. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1291. case kms.ErrCodeInvalidStateException:
  1292. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1293. default:
  1294. fmt.Println(aerr.Error())
  1295. }
  1296. } else {
  1297. // Print the error, cast err to awserr.Error to get the Code and
  1298. // Message from an error.
  1299. fmt.Println(err.Error())
  1300. }
  1301. return
  1302. }
  1303. fmt.Println(result)
  1304. }
  1305. // To update the description of a customer master key (CMK)
  1306. //
  1307. // The following example updates the description of the specified CMK.
  1308. func ExampleKMS_UpdateKeyDescription_shared00() {
  1309. svc := kms.New(session.New())
  1310. input := &kms.UpdateKeyDescriptionInput{
  1311. Description: aws.String("Example description that indicates the intended use of this CMK."),
  1312. KeyId: aws.String("1234abcd-12ab-34cd-56ef-1234567890ab"),
  1313. }
  1314. result, err := svc.UpdateKeyDescription(input)
  1315. if err != nil {
  1316. if aerr, ok := err.(awserr.Error); ok {
  1317. switch aerr.Code() {
  1318. case kms.ErrCodeNotFoundException:
  1319. fmt.Println(kms.ErrCodeNotFoundException, aerr.Error())
  1320. case kms.ErrCodeInvalidArnException:
  1321. fmt.Println(kms.ErrCodeInvalidArnException, aerr.Error())
  1322. case kms.ErrCodeDependencyTimeoutException:
  1323. fmt.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
  1324. case kms.ErrCodeInternalException:
  1325. fmt.Println(kms.ErrCodeInternalException, aerr.Error())
  1326. case kms.ErrCodeInvalidStateException:
  1327. fmt.Println(kms.ErrCodeInvalidStateException, aerr.Error())
  1328. default:
  1329. fmt.Println(aerr.Error())
  1330. }
  1331. } else {
  1332. // Print the error, cast err to awserr.Error to get the Code and
  1333. // Message from an error.
  1334. fmt.Println(err.Error())
  1335. }
  1336. return
  1337. }
  1338. fmt.Println(result)
  1339. }