@@ -88,13 +88,20 @@ public class MockKMSClient extends AWSKMSClient {
88
88
private static final SecureRandom rnd = new SecureRandom ();
89
89
private static final String ACCOUNT_ID = "01234567890" ;
90
90
private final Map <DecryptMapKey , DecryptResult > results_ = new HashMap <>();
91
- private final Map <String , String > idToArnMap = new HashMap <>();
92
91
private final Set <String > activeKeys = new HashSet <>();
92
+ private final Map <String , String > keyAliases = new HashMap <>();
93
93
private Region region_ = Region .getRegion (Regions .DEFAULT_REGION );
94
94
95
95
@ Override
96
96
public CreateAliasResult createAlias (CreateAliasRequest arg0 ) throws AmazonServiceException , AmazonClientException {
97
- throw new java .lang .UnsupportedOperationException ();
97
+ assertExists (arg0 .getTargetKeyId ());
98
+
99
+ keyAliases .put (
100
+ "alias/" + arg0 .getAliasName (),
101
+ keyAliases .get (arg0 .getTargetKeyId ())
102
+ );
103
+
104
+ return new CreateAliasResult ();
98
105
}
99
106
100
107
@ Override
@@ -111,8 +118,9 @@ public CreateKeyResult createKey() throws AmazonServiceException, AmazonClientEx
111
118
public CreateKeyResult createKey (CreateKeyRequest req ) throws AmazonServiceException , AmazonClientException {
112
119
String keyId = UUID .randomUUID ().toString ();
113
120
String arn = "arn:aws:kms:" + region_ .getName () + ":" + ACCOUNT_ID + ":key/" + keyId ;
114
- idToArnMap .put (keyId , arn );
115
121
activeKeys .add (arn );
122
+ keyAliases .put (keyId , arn );
123
+ keyAliases .put (arn , arn );
116
124
CreateKeyResult result = new CreateKeyResult ();
117
125
result .setKeyMetadata (new KeyMetadata ().withAWSAccountId (ACCOUNT_ID ).withCreationDate (new Date ())
118
126
.withDescription (req .getDescription ()).withEnabled (true ).withKeyId (keyId )
@@ -183,7 +191,7 @@ private EncryptResult encrypt0(EncryptRequest req) throws AmazonServiceException
183
191
final byte [] cipherText = new byte [512 ];
184
192
rnd .nextBytes (cipherText );
185
193
DecryptResult dec = new DecryptResult ();
186
- dec .withKeyId (req .getKeyId ()).withPlaintext (req .getPlaintext ().asReadOnlyBuffer ());
194
+ dec .withKeyId (retrieveArn ( req .getKeyId () )).withPlaintext (req .getPlaintext ().asReadOnlyBuffer ());
187
195
ByteBuffer ctBuff = ByteBuffer .wrap (cipherText );
188
196
189
197
results_ .put (new DecryptMapKey (ctBuff , req .getEncryptionContext ()), dec );
@@ -336,20 +344,17 @@ public void deleteKey(final String keyId) {
336
344
}
337
345
338
346
private String retrieveArn (final String keyId ) {
339
- String arn = keyId ;
340
- if (keyId .contains ("arn:" ) == false ) {
341
- arn = idToArnMap .get (keyId );
342
- }
347
+ String arn = keyAliases .get (keyId );
343
348
assertExists (arn );
344
349
return arn ;
345
350
}
346
351
347
352
private void assertExists (String keyId ) {
348
- if (idToArnMap .containsKey (keyId )) {
349
- keyId = idToArnMap .get (keyId );
353
+ if (keyAliases .containsKey (keyId )) {
354
+ keyId = keyAliases .get (keyId );
350
355
}
351
356
if (keyId == null || !activeKeys .contains (keyId )) {
352
- throw new NotFoundException ("Key doesn't exist" );
357
+ throw new NotFoundException ("Key doesn't exist: " + keyId );
353
358
}
354
359
}
355
360
0 commit comments