com.amazonaws.services.kms.model.NotFoundException Java Examples

The following examples show how to use com.amazonaws.services.kms.model.NotFoundException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KMSManagerTest.java    From strongbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreate() throws Exception {
    // Mocks the responses from AWS.
    CreateKeyRequest createKeyRequest = new CreateKeyRequest().withDescription(
            "This key is automatically managed by Strongbox");
    CreateKeyResult createKeyResult = new CreateKeyResult().withKeyMetadata(new KeyMetadata().withArn(KMS_ARN));
    CreateAliasRequest createAliasRequest = new CreateAliasRequest().withAliasName(ALIAS_KEY_NAME).withTargetKeyId(KMS_ARN);

    when(mockKMSClient.describeKey(describeKeyRequest))
            .thenThrow(NotFoundException.class)
            .thenThrow(NotFoundException.class)  // still waiting for creation
            .thenReturn(enabledKeyResult());
    when(mockKMSClient.createKey(createKeyRequest)).thenReturn(createKeyResult);

    // Check the result from create method.
    String arn = kmsManager.create();
    assertEquals(arn, KMS_ARN);

    // Verify correct number of calls was made to AWS.
    verify(mockKMSClient, times(3)).describeKey(describeKeyRequest);
    verify(mockKMSClient, times(1)).createAlias(createAliasRequest);
    verify(mockKMSClient, times(1)).createKey(createKeyRequest);
}
 
Example #2
Source File: KMSManagerTest.java    From strongbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteNonExisting() throws Exception {
    when(mockKMSClient.describeKey(describeKeyRequest)).thenThrow(NotFoundException.class);

    boolean exceptionThrown = false;
    try {
        kmsManager.delete();
    } catch (DoesNotExistException e) {
        assertEquals(e.getMessage(), String.format("Failed to find KMS key with alias '%s'", KMS_ALIAS_ARN));
        exceptionThrown = true;
    }
    verify(mockKMSClient, times(1)).describeKey(describeKeyRequest);
    verify(mockKMSClient, never()).scheduleKeyDeletion(any());
}
 
Example #3
Source File: AuthenticationService.java    From cerberus with Apache License 2.0 5 votes vote down vote up
/**
 * Encrypts the data provided using KMS based on the provided region and key id.
 *
 * @param regionName Region where key is located
 * @param keyId Key id
 * @param data Data to be encrypted
 * @return encrypted data
 */
private byte[] encrypt(final String regionName, final String keyId, final byte[] data) {
  Region region;
  try {
    region = Region.getRegion(Regions.fromName(regionName));
  } catch (IllegalArgumentException iae) {
    throw ApiException.newBuilder()
        .withApiErrors(DefaultApiError.AUTH_IAM_ROLE_AWS_REGION_INVALID)
        .withExceptionCause(iae)
        .build();
  }

  final AWSKMSClient kmsClient = kmsClientFactory.getClient(region);

  try {
    final EncryptResult encryptResult =
        kmsClient.encrypt(
            new EncryptRequest().withKeyId(keyId).withPlaintext(ByteBuffer.wrap(data)));

    return encryptResult.getCiphertextBlob().array();
  } catch (NotFoundException | KMSInvalidStateException keyNotUsableException) {
    throw new KeyInvalidForAuthException(
        String.format("Failed to encrypt token using KMS key with id: %s", keyId),
        keyNotUsableException);
  } catch (AmazonClientException ace) {
    throw ApiException.newBuilder()
        .withApiErrors(DefaultApiError.INTERNAL_SERVER_ERROR)
        .withExceptionCause(ace)
        .withExceptionMessage(
            String.format(
                "Unexpected error communicating with AWS KMS for region %s.", regionName))
        .build();
  }
}
 
Example #4
Source File: MockKMSClient.java    From aws-encryption-sdk-java with Apache License 2.0 5 votes vote down vote up
private void assertExists(String keyId) {
    if (keyAliases.containsKey(keyId)) {
        keyId = keyAliases.get(keyId);
    }
    if (keyId == null || !activeKeys.contains(keyId)) {
        throw new NotFoundException("Key doesn't exist: " + keyId);
    }
}
 
Example #5
Source File: KMSManagerTest.java    From strongbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testExistsKeyDoesNotExist() throws Exception {
    when(mockKMSClient.describeKey(describeKeyRequest)).thenThrow(NotFoundException.class);
    assertFalse(kmsManager.exists());
}