@@ -88,13 +88,20 @@ public class MockKMSClient extends AWSKMSClient {
8888 private static final SecureRandom rnd = new SecureRandom ();
8989 private static final String ACCOUNT_ID = "01234567890" ;
9090 private final Map <DecryptMapKey , DecryptResult > results_ = new HashMap <>();
91- private final Map <String , String > idToArnMap = new HashMap <>();
9291 private final Set <String > activeKeys = new HashSet <>();
92+ private final Map <String , String > keyAliases = new HashMap <>();
9393 private Region region_ = Region .getRegion (Regions .DEFAULT_REGION );
9494
9595 @ Override
9696 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 ();
98105 }
99106
100107 @ Override
@@ -111,8 +118,9 @@ public CreateKeyResult createKey() throws AmazonServiceException, AmazonClientEx
111118 public CreateKeyResult createKey (CreateKeyRequest req ) throws AmazonServiceException , AmazonClientException {
112119 String keyId = UUID .randomUUID ().toString ();
113120 String arn = "arn:aws:kms:" + region_ .getName () + ":" + ACCOUNT_ID + ":key/" + keyId ;
114- idToArnMap .put (keyId , arn );
115121 activeKeys .add (arn );
122+ keyAliases .put (keyId , arn );
123+ keyAliases .put (arn , arn );
116124 CreateKeyResult result = new CreateKeyResult ();
117125 result .setKeyMetadata (new KeyMetadata ().withAWSAccountId (ACCOUNT_ID ).withCreationDate (new Date ())
118126 .withDescription (req .getDescription ()).withEnabled (true ).withKeyId (keyId )
@@ -183,7 +191,7 @@ private EncryptResult encrypt0(EncryptRequest req) throws AmazonServiceException
183191 final byte [] cipherText = new byte [512 ];
184192 rnd .nextBytes (cipherText );
185193 DecryptResult dec = new DecryptResult ();
186- dec .withKeyId (req .getKeyId ()).withPlaintext (req .getPlaintext ().asReadOnlyBuffer ());
194+ dec .withKeyId (retrieveArn ( req .getKeyId () )).withPlaintext (req .getPlaintext ().asReadOnlyBuffer ());
187195 ByteBuffer ctBuff = ByteBuffer .wrap (cipherText );
188196
189197 results_ .put (new DecryptMapKey (ctBuff , req .getEncryptionContext ()), dec );
@@ -336,20 +344,17 @@ public void deleteKey(final String keyId) {
336344 }
337345
338346 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 );
343348 assertExists (arn );
344349 return arn ;
345350 }
346351
347352 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 );
350355 }
351356 if (keyId == null || !activeKeys .contains (keyId )) {
352- throw new NotFoundException ("Key doesn't exist" );
357+ throw new NotFoundException ("Key doesn't exist: " + keyId );
353358 }
354359 }
355360
0 commit comments