Skip to content

Commit 60dcd3b

Browse files
committed
Updated Spec for KMS Basics
1 parent 8bd79d0 commit 60dcd3b

File tree

2 files changed

+102
-88
lines changed

2 files changed

+102
-88
lines changed

basics_scenarios/kms_scenario/SPECIFICATION.md

Lines changed: 95 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# AWS Key Management Service Scenario Specification
1+
# AWS Key Management Service Basics Scenario Specification
22

33
## Overview
4-
This SDK getting started scenario demonstrates how to interact with AWS Key Management Service (AWS KMS) using the AWS SDK. It demonstrates various tasks such as creating AWS KMS Keys, encrypting data, digitally signing data, creating aliases, tagging keys, and so on. Finally this scenario demonstrates how to clean up resources. Its purpose is to demonstrate how to get up and running with AWS KMS and the AWS SDK.
4+
This SDK Basics scenario demonstrates how to interact with AWS Key Management Service (AWS KMS) using the AWS SDK. It demonstrates various tasks such as creating AWS KMS Keys, encrypting data, digitally signing data, creating aliases, tagging keys, and so on. Finally this scenario demonstrates how to clean up resources. Its purpose is to demonstrate how to get up and running with AWS KMS and the AWS SDK.
55

66
## Resources and User Input
7-
The only required resource for this SDK getting started scenario is the grantee principal. The `GranteePrincipal` is the AWS Identity and Access Management (IAM) entity (user, role, or service) that is granted permission to perform specific actions.
7+
The only required resource for this SDK Basics scenario is the grantee principal. The `GranteePrincipal` is the AWS Identity and Access Management (IAM) entity (user, role, or service) that is granted permission to perform specific actions.
88

99
## Hello AWS KSM
1010
This program is intended for users not familiar with the AWS KSM SDK to easily get up and running. The logic is to show use of `KmsClient.listKeysPaginator()`.
@@ -18,26 +18,69 @@ The key ARN is: arn:aws:kms:us-west-2:123456789012:key/12345678-abcd-abcd-abcd-1
1818
```
1919

2020
## Scenario Program Flow
21-
The AWS Key Management SDK getting started scenario executes the following steps:
22-
1. Creates an AWS KMS client.
23-
2. Prompts the user to create an AWS KMS Symmetric key for encrypting and decrypting data.
24-
3. Describes the newly created key.
25-
4. Enables the AWS KMS key.
26-
5. Encrypts the plaintext data using the KMS key.
27-
6. Allows the user to create a custom alias for the KMS key.
28-
7. Enables automatic rotation of the KMS key.
29-
8. Grants permissions to an IAM principal to use the KMS key.
30-
9. Lists the grants for the KMS key.
31-
10. Revokes the previously created grant.
32-
11. Decrypts the encrypted data using the KMS key.
33-
12. Creates a key policy for the KMS key.
34-
13. Retrieves the key policy.
35-
14. Creates an AWS KMS Asymmetric key for signing and verifying data.
36-
15. Signs and verifies data using the newly created key.
37-
16. Tags the KMS key.
38-
17. Prompts the user to delete the KMS resources.
39-
18. If confirmed, deletes the custom alias, disables, and deletes the KMS key.
40-
19. Concludes the AWS Key Management SDK getting started scenario.
21+
22+
The AWS Key Management SDK Basics scenario follows these steps:
23+
24+
1. **Create a Symmetric KMS Key**:
25+
- **Description**: Creates a symmetric KMS key for encrypting and decrypting data. Use the `createKey` method.
26+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
27+
28+
2. **Enable a KMS Key**:
29+
- **Description**: Checks if the KMS key is enabled. Use the `describeKey` method.
30+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
31+
32+
3. **Encrypt Data with the Symmetric KMS Key**:
33+
- **Description**: Encrypts data using the symmetric KMS key. Use the `encrypt` method.
34+
- **Exception Handling**: If a `DisabledException` is thrown (indicating the key is disabled), display the error message and terminate the program.
35+
36+
4. **Create an Alias**:
37+
- **Description**: Creates an alias for the KMS key, prefixed with 'alias/'. Use the `createAlias` method.
38+
- **Exception Handling**: If an `AlreadyExistsException` is thrown (indicating the alias already exists), display the message and continue execution.
39+
40+
5. **List All Aliases**:
41+
- **Description**: Lists all KMS key aliases. Use the `listAliasesPaginator` method (or a standard list method if pagination is unavailable).
42+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
43+
44+
6. **Enable Automatic Rotation for the KMS Key**:
45+
- **Description**: Enables automatic key rotation. Use the `enableKeyRotation` method.
46+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
47+
48+
7. **Create a Grant**:
49+
- **Description**: Creates a grant for the KMS key. Use the `createGrant` method.
50+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
51+
52+
8. **List Grants for the KMS Key**:
53+
- **Description**: Lists all grants associated with the KMS key. Use the `listGrantsPaginator` method (or a standard list method if pagination is unavailable).
54+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
55+
56+
9. **Revoke a Grant**:
57+
- **Description**: Revokes a specific grant for the KMS key. Use the `revokeGrant` method.
58+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
59+
60+
10. **Decrypt the Data**:
61+
- **Description**: Decrypts previously encrypted data using the KMS key. Use the `decrypt` method.
62+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
63+
64+
11. **Replace a Key Policy**:
65+
- **Description**: Replaces the key policy for the KMS key. Use the `putKeyPolicy` method.
66+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
67+
68+
12. **Retrieve the Key Policy**:
69+
- **Description**: Retrieves the key policy for the KMS key. Use the `getKeyPolicy` method.
70+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
71+
72+
13. **Create an Asymmetric KMS Key and Sign Data**:
73+
- **Description**: Creates an asymmetric KMS key and uses it to sign data. Use the `createKey` method and the `sign` method.
74+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
75+
76+
14. **Tag the Symmetric KMS Key**:
77+
- **Description**: Tags the symmetric KMS key. Use the `tagResource` method.
78+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
79+
80+
15. **Schedule the Deletion of the KMS Key**:
81+
- **Description**: Schedules the deletion of the KMS key. Ensure users are aware of the warning associated with key deletion (refer to the Java implementation). Invoke the following methods in order: `deleteAlias`, `disableKey`, and `scheduleKeyDeletion`.
82+
- **Exception Handling**: If a `KmsException` is thrown, display the error message and terminate the program.
83+
4184

4285
### Program execution
4386
The following shows the output of the program in the console.
@@ -56,22 +99,19 @@ and an asymmetric key used to digitally sign data.
5699
Let's get started...
57100
58101
59-
Enter 'c' followed by <ENTER> to continue:
60-
c
102+
Press <ENTER> to continue:
61103
Continuing with the program...
62104
63105
--------------------------------------------------------------------------------
64106
1. Create a symmetric KMS key
65107
First, we will create a symmetric KMS key that is used to encrypt and decrypt data by invoking createKey().
66108
67-
Enter 'c' followed by <ENTER> to continue:
68-
c
109+
Press <ENTER> to continue:
69110
Continuing with the program...
70111
71112
Created a customer key with ARN arn:aws:kms:us-west-2:123456789012:key/11223344-aaaa-bbbb-cccc-111222233344
72113
73-
Enter 'c' followed by <ENTER> to continue:
74-
c
114+
Press <ENTER> to continue:
75115
Continuing with the program...
76116
77117
--------------------------------------------------------------------------------
@@ -80,15 +120,12 @@ Continuing with the program...
80120
By default when you create an AWS key, it is enabled. The code checks to
81121
determine if the key is enabled. If it is not enabled, the code enables it.
82122
83-
84-
Enter 'c' followed by <ENTER> to continue:
85-
c
123+
Press <ENTER> to continue:
86124
Continuing with the program...
87125
88126
The key is enabled.
89127
90-
Enter 'c' followed by <ENTER> to continue:
91-
c
128+
Press <ENTER> to continue:
92129
Continuing with the program...
93130
94131
--------------------------------------------------------------------------------
@@ -97,14 +134,12 @@ One of the main uses of symmetric keys is to encrypt and decrypt data.
97134
Next, you encrypt the string 'Hello, AWS KMS!' with the SYMMETRIC_DEFAULT encryption algorithm.
98135
99136
100-
Enter 'c' followed by <ENTER> to continue:
101-
c
137+
Press <ENTER> to continue:
102138
Continuing with the program...
103139
104140
The encryption algorithm is SYMMETRIC_DEFAULT
105141
106-
Enter 'c' followed by <ENTER> to continue:
107-
c
142+
Press <ENTER> to continue:
108143
Continuing with the program...
109144
110145
--------------------------------------------------------------------------------
@@ -115,15 +150,13 @@ For example, 'alias/myFirstKey'.
115150
116151
alias/dev-encryption-key was successfully created.
117152
118-
Enter 'c' followed by <ENTER> to continue:
119-
c
153+
Press <ENTER> to continue:
120154
Continuing with the program...
121155
122156
--------------------------------------------------------------------------------
123157
5. List all of your aliases.
124158
125-
Enter 'c' followed by <ENTER> to continue:
126-
c
159+
Press <ENTER> to continue:
127160
Continuing with the program...
128161
129162
The alias name is: alias/Scott
@@ -142,8 +175,7 @@ The alias name is: alias/aws/lex
142175
The alias name is: alias/aws/rds
143176
The alias name is: alias/aws/redshift
144177
145-
Enter 'c' followed by <ENTER> to continue:
146-
c
178+
Press <ENTER> to continue:
147179
Continuing with the program...
148180
149181
--------------------------------------------------------------------------------
@@ -152,15 +184,12 @@ By default, when you enable automatic rotation of a KMS key,
152184
KMS rotates the key material of the KMS key one year (approximately 365 days) from the enable date and every year
153185
thereafter.
154186
155-
156-
Enter 'c' followed by <ENTER> to continue:
157-
c
187+
Press <ENTER> to continue:
158188
Continuing with the program...
159189
160190
You have enabled key rotation for key 11223344-aaaa-bbbb-cccc-111222233344
161191
162-
Enter 'c' followed by <ENTER> to continue:
163-
c
192+
Press <ENTER> to continue:
164193
Continuing with the program...
165194
166195
--------------------------------------------------------------------------------
@@ -170,41 +199,34 @@ A grant is a policy instrument that allows Amazon Web Services principals to use
170199
It also can allow them to view a KMS key (DescribeKey) and create and manage grants.
171200
When authorizing access to a KMS key, grants are considered along with key policies and IAM policies.
172201
173-
174-
Enter 'c' followed by <ENTER> to continue:
175-
c
202+
Press <ENTER> to continue:
176203
Continuing with the program...
177204
178205
The grant id is 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
179206
180-
Enter 'c' followed by <ENTER> to continue:
181-
c
207+
Press <ENTER> to continue:
182208
Continuing with the program...
183209
184210
--------------------------------------------------------------------------------
185211
8. List grants for the KMS key.
186212
187-
Enter 'c' followed by <ENTER> to continue:
188-
c
213+
Press <ENTER> to continue:
189214
Continuing with the program...
190215
191216
The grant Id is: 8f1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
192217
193-
Enter 'c' followed by <ENTER> to continue:
194-
c
218+
Press <ENTER> to continue:
195219
Continuing with the program...
196220
197221
--------------------------------------------------------------------------------
198222
9. Revoke the grant.
199223
200-
Enter 'c' followed by <ENTER> to continue:
201-
c
224+
Press <ENTER> to continue:
202225
Continuing with the program...
203226
204227
1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef was successfully revoked!
205228
206-
Enter 'c' followed by <ENTER> to continue:
207-
c
229+
Press <ENTER> to continue:
208230
Continuing with the program...
209231
210232
--------------------------------------------------------------------------------
@@ -213,14 +235,12 @@ Lets decrypt the data that was encrypted in an early step.
213235
We'll use the same key to decrypt the string that we encrypted earlier in the program.
214236

215237

216-
Enter 'c' followed by <ENTER> to continue:
217-
c
238+
Press <ENTER> to continue:
218239
Continuing with the program...
219240

220241
Decrypted text is: Hello, AWS KMS!
221242

222-
Enter 'c' followed by <ENTER> to continue:
223-
c
243+
Press <ENTER> to continue:
224244
Continuing with the program...
225245

226246
--------------------------------------------------------------------------------
@@ -241,23 +261,20 @@ We will set a key policy.
241261
}]
242262

243263

244-
Enter 'c' followed by <ENTER> to continue:
245-
c
264+
Press <ENTER> to continue:
246265
Continuing with the program...
247266

248267
Policy Name: default
249268
The Key already has a policy.
250269

251-
Enter 'c' followed by <ENTER> to continue:
252-
c
270+
Press <ENTER> to continue:
253271
Continuing with the program...
254272

255273
--------------------------------------------------------------------------------
256274
11. Get the key policy.
257275
Lets get the key policy to make sure it exists.
258276

259-
Enter 'c' followed by <ENTER> to continue:
260-
c
277+
Press <ENTER> to continue:
261278
Continuing with the program...
262279

263280
The response is {
@@ -274,8 +291,7 @@ The response is {
274291
} ]
275292
}
276293

277-
Enter 'c' followed by <ENTER> to continue:
278-
c
294+
Press <ENTER> to continue:
279295
Continuing with the program...
280296

281297
--------------------------------------------------------------------------------
@@ -287,15 +303,13 @@ Continuing with the program...
287303
of your organization.
288304

289305

290-
Enter 'c' followed by <ENTER> to continue:
291-
c
306+
Press <ENTER> to continue:
292307
Continuing with the program...
293308

294309
Created KMS key with ID: 12345678-abcd-abcd-abcd-123456789012
295310
Signature verification result: true
296311

297-
Enter 'c' followed by <ENTER> to continue:
298-
c
312+
Press <ENTER> to continue:
299313
Continuing with the program...
300314

301315
--------------------------------------------------------------------------------
@@ -305,14 +319,12 @@ KMS keys, making it easier to organize, track, and control access to your encryp
305319
your AWS environment
306320

307321

308-
Enter 'c' followed by <ENTER> to continue:
309-
c
322+
Press <ENTER> to continue:
310323
Continuing with the program...
311324

312325
Tagged KMS key with key-value pair
313326

314-
Enter 'c' followed by <ENTER> to continue:
315-
c
327+
Press <ENTER> to continue:
316328
Continuing with the program...
317329

318330
--------------------------------------------------------------------------------
@@ -329,8 +341,7 @@ Would you like to delete the Key Management resources? (y/n)
329341
y
330342
You selected to delete the AWS KMS resources.
331343
332-
Enter 'c' followed by <ENTER> to continue:
333-
c
344+
Press <ENTER> to continue:
334345
Continuing with the program...
335346
336347
The key will be deleted in 7 days.

javav2/example_code/kms/src/main/java/com/example/kms/HelloKMS.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
// snippet-start:[kms.java2_list_keys.main]
77
// snippet-start:[kms.java2_list_keys.import]
8-
import software.amazon.awssdk.regions.Region;
98
import software.amazon.awssdk.services.kms.KmsAsyncClient;
109
import software.amazon.awssdk.services.kms.model.ListKeysRequest;
1110
import software.amazon.awssdk.services.kms.paginators.ListKeysPublisher;
@@ -26,14 +25,19 @@ public static void main(String[] args) {
2625
}
2726

2827
public static void listAllKeys() {
29-
Region region = Region.US_WEST_2;
3028
KmsAsyncClient kmsAsyncClient = KmsAsyncClient.builder()
31-
.region(region)
3229
.build();
3330
ListKeysRequest listKeysRequest = ListKeysRequest.builder()
3431
.limit(15)
3532
.build();
3633

34+
/*
35+
* The `subscribe` method is required when using paginator methods in the AWS SDK
36+
* because paginator methods return an instance of a `ListKeysPublisher`, which is
37+
* based on a reactive stream. This allows asynchronous retrieval of paginated
38+
* results as they become available. By subscribing to the stream, we can process
39+
* each page of results as they are emitted.
40+
*/
3741
ListKeysPublisher keysPublisher = kmsAsyncClient.listKeysPaginator(listKeysRequest);
3842
CompletableFuture<Void> future = keysPublisher
3943
.subscribe(r -> r.keys().forEach(key ->
@@ -46,7 +50,6 @@ public static void listAllKeys() {
4650
}
4751
});
4852

49-
// Wait for the asynchronous operation to complete
5053
try {
5154
future.join();
5255
} catch (Exception e) {

0 commit comments

Comments
 (0)