Search in sources :

Example 1 with AwsIamRoleKmsKeyRecord

use of com.nike.cerberus.record.AwsIamRoleKmsKeyRecord in project cerberus by Nike-Inc.

the class AuthenticationService method authenticate.

private EncryptedAuthDataWrapper authenticate(AwsIamKmsAuthRequest credentials, Map<String, String> authPrincipalMetadata) {
    final AwsIamRoleKmsKeyRecord kmsKeyRecord;
    final AwsIamRoleRecord iamRoleRecord;
    try {
        iamRoleRecord = getIamPrincipalRecord(credentials.getIamPrincipalArn());
        kmsKeyRecord = getKmsKeyRecordForIamPrincipal(iamRoleRecord, credentials.getRegion());
    } catch (AmazonServiceException e) {
        if ("InvalidArnException".equals(e.getErrorCode())) {
            String msg = String.format("Failed to lazily provision KMS key for %s in region: %s", credentials.getIamPrincipalArn(), credentials.getRegion());
            throw ApiException.newBuilder().withApiErrors(CustomApiError.createCustomApiError(DefaultApiError.AUTH_IAM_ROLE_REJECTED, msg)).withExceptionCause(e).withExceptionMessage(msg).build();
        }
        throw e;
    }
    AuthTokenResponse authResponse = createToken(iamRoleRecord.getAwsIamRoleArn(), PrincipalType.IAM, authPrincipalMetadata, iamTokenTTL);
    byte[] authResponseJson;
    try {
        authResponseJson = objectMapper.writeValueAsBytes(authResponse);
    } catch (JsonProcessingException e) {
        String msg = "Failed to write IAM role authentication response as JSON for encrypting.";
        throw ApiException.newBuilder().withApiErrors(DefaultApiError.INTERNAL_SERVER_ERROR).withExceptionCause(e).withExceptionMessage(msg).build();
    }
    authResponseJson = validateAuthPayloadSizeAndTruncateIfLargerThanMaxKmsSupportedSize(authResponseJson, authResponse, credentials.getIamPrincipalArn());
    final byte[] encryptedAuthResponse = safeEncryptWithRetry(kmsKeyRecord.getAwsIamRoleId(), credentials.getIamPrincipalArn(), kmsKeyRecord.getId(), kmsKeyRecord.getAwsKmsKeyId(), credentials.getRegion(), authResponseJson);
    EncryptedAuthDataWrapper encryptedAuthDataWrapper = new EncryptedAuthDataWrapper();
    encryptedAuthDataWrapper.setAuthData(Base64.encodeBase64String(encryptedAuthResponse));
    return encryptedAuthDataWrapper;
}
Also used : AuthTokenResponse(com.nike.cerberus.domain.AuthTokenResponse) EncryptedAuthDataWrapper(com.nike.cerberus.domain.EncryptedAuthDataWrapper) AwsIamRoleKmsKeyRecord(com.nike.cerberus.record.AwsIamRoleKmsKeyRecord) AwsIamRoleRecord(com.nike.cerberus.record.AwsIamRoleRecord) AmazonServiceException(com.amazonaws.AmazonServiceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with AwsIamRoleKmsKeyRecord

use of com.nike.cerberus.record.AwsIamRoleKmsKeyRecord in project cerberus by Nike-Inc.

the class KmsService method updateKmsKey.

/**
 * Updates the KMS CMK record for the specified IAM role and region
 *
 * @param awsIamRoleId The IAM role that this CMK will be associated with
 * @param awsRegion The region to provision the key in
 * @param user The user requesting it
 * @param lastedUpdatedTs The date when the record was last updated
 * @param lastValidatedTs The date when the record was last validated
 */
@Transactional
public void updateKmsKey(final String awsIamRoleId, final String awsRegion, final String user, final OffsetDateTime lastedUpdatedTs, final OffsetDateTime lastValidatedTs) {
    final Optional<AwsIamRoleKmsKeyRecord> kmsKey = awsIamRoleDao.getKmsKey(awsIamRoleId, awsRegion);
    if (kmsKey.isEmpty()) {
        String msg = "Unable to update a KMS key that does not exist.";
        throw ApiException.newBuilder().withApiErrors(CustomApiError.createCustomApiError(DefaultApiError.ENTITY_NOT_FOUND, msg)).withExceptionMessage(msg).build();
    }
    AwsIamRoleKmsKeyRecord kmsKeyRecord = kmsKey.get();
    AwsIamRoleKmsKeyRecord updatedKmsKeyRecord = new AwsIamRoleKmsKeyRecord();
    updatedKmsKeyRecord.setAwsIamRoleId(kmsKeyRecord.getAwsIamRoleId());
    updatedKmsKeyRecord.setLastUpdatedBy(user);
    updatedKmsKeyRecord.setLastUpdatedTs(lastedUpdatedTs);
    updatedKmsKeyRecord.setLastValidatedTs(lastValidatedTs);
    updatedKmsKeyRecord.setAwsRegion(kmsKeyRecord.getAwsRegion());
    awsIamRoleDao.updateIamRoleKmsKey(updatedKmsKeyRecord);
}
Also used : AwsIamRoleKmsKeyRecord(com.nike.cerberus.record.AwsIamRoleKmsKeyRecord) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AwsIamRoleKmsKeyRecord

use of com.nike.cerberus.record.AwsIamRoleKmsKeyRecord in project cerberus by Nike-Inc.

the class KmsServiceTest method test_validatePolicy_validates_policy_when_validate_interval_has_passed.

@Test
public void test_validatePolicy_validates_policy_when_validate_interval_has_passed() {
    String kmsKeyArn = "kms key arn";
    String awsIamRoleRecordId = "aws iam role record id";
    String kmsCMKRegion = "kmsCMKRegion";
    String policy = "policy";
    OffsetDateTime lastValidated = OffsetDateTime.of(2016, 1, 1, 1, 1, 1, 1, ZoneOffset.UTC);
    OffsetDateTime now = OffsetDateTime.now();
    AWSKMSClient client = mock(AWSKMSClient.class);
    when(client.describeKey(anyObject())).thenReturn(new DescribeKeyResult().withKeyMetadata(new KeyMetadata().withKeyState(KeyState.Enabled)));
    when(kmsClientFactory.getClient(kmsCMKRegion)).thenReturn(client);
    GetKeyPolicyResult result = mock(GetKeyPolicyResult.class);
    when(result.getPolicy()).thenReturn(policy);
    when(client.getKeyPolicy(new GetKeyPolicyRequest().withKeyId(kmsKeyArn).withPolicyName("default"))).thenReturn(result);
    when(kmsPolicyService.isPolicyValid(policy)).thenReturn(true);
    AwsIamRoleKmsKeyRecord kmsKey = mock(AwsIamRoleKmsKeyRecord.class);
    when(kmsKey.getAwsIamRoleId()).thenReturn(awsIamRoleRecordId);
    when(kmsKey.getAwsKmsKeyId()).thenReturn(kmsKeyArn);
    when(kmsKey.getAwsRegion()).thenReturn(kmsCMKRegion);
    when(kmsKey.getLastValidatedTs()).thenReturn(lastValidated);
    when(awsIamRoleDao.getKmsKey(awsIamRoleRecordId, kmsCMKRegion)).thenReturn(Optional.of(kmsKey));
    when(dateTimeSupplier.get()).thenReturn(now);
    kmsService.validateKeyAndPolicy(kmsKey, kmsKeyArn);
    verify(client, times(1)).getKeyPolicy(new GetKeyPolicyRequest().withKeyId(kmsKeyArn).withPolicyName("default"));
    verify(kmsPolicyService, times(1)).isPolicyValid(policy);
}
Also used : AuthKmsKeyMetadata(com.nike.cerberus.domain.AuthKmsKeyMetadata) KeyMetadata(com.amazonaws.services.kms.model.KeyMetadata) OffsetDateTime(java.time.OffsetDateTime) DescribeKeyResult(com.amazonaws.services.kms.model.DescribeKeyResult) GetKeyPolicyResult(com.amazonaws.services.kms.model.GetKeyPolicyResult) AwsIamRoleKmsKeyRecord(com.nike.cerberus.record.AwsIamRoleKmsKeyRecord) AWSKMSClient(com.amazonaws.services.kms.AWSKMSClient) GetKeyPolicyRequest(com.amazonaws.services.kms.model.GetKeyPolicyRequest) Test(org.junit.Test)

Example 4 with AwsIamRoleKmsKeyRecord

use of com.nike.cerberus.record.AwsIamRoleKmsKeyRecord in project cerberus by Nike-Inc.

the class CleanUpServiceTest method test_that_cleanUpInactiveAndOrphanedKmsKeys_succeeds.

@Test
public void test_that_cleanUpInactiveAndOrphanedKmsKeys_succeeds() {
    int inactivePeriod = 30;
    String keyRecordId = "key record id";
    String awsKeyId = "aws key id";
    String keyRegion = "key region";
    AwsIamRoleKmsKeyRecord keyRecord = mock(AwsIamRoleKmsKeyRecord.class);
    when(keyRecord.getId()).thenReturn(keyRecordId);
    when(keyRecord.getAwsKmsKeyId()).thenReturn(awsKeyId);
    when(keyRecord.getAwsRegion()).thenReturn(keyRegion);
    when(dateTimeSupplier.get()).thenReturn(now);
    OffsetDateTime inactiveCutoffDate = now.minusDays(inactivePeriod);
    when(awsIamRoleDao.getInactiveOrOrphanedKmsKeys(inactiveCutoffDate)).thenReturn(Lists.newArrayList(keyRecord));
    // perform the call
    cleanUpService.cleanUpInactiveAndOrphanedKmsKeys(inactivePeriod, 0);
    verify(awsIamRoleDao).getInactiveOrOrphanedKmsKeys(inactiveCutoffDate);
    verify(kmsService).deleteKmsKeyById(keyRecordId);
    verify(kmsService).scheduleKmsKeyDeletion(awsKeyId, keyRegion, SOONEST_A_KMS_KEY_CAN_BE_DELETED);
}
Also used : OffsetDateTime(java.time.OffsetDateTime) AwsIamRoleKmsKeyRecord(com.nike.cerberus.record.AwsIamRoleKmsKeyRecord) Test(org.junit.Test)

Example 5 with AwsIamRoleKmsKeyRecord

use of com.nike.cerberus.record.AwsIamRoleKmsKeyRecord in project cerberus by Nike-Inc.

the class AuthenticationServiceTest method test_that_getKeyId_only_validates_kms_policy_one_time_within_interval.

@Test
public void test_that_getKeyId_only_validates_kms_policy_one_time_within_interval() {
    String principalArn = "principal arn";
    String region = "region";
    String iamRoleId = "iam role id";
    String kmsKeyId = "kms id";
    String cmkId = "key id";
    // ensure that validate interval is passed
    OffsetDateTime dateTime = OffsetDateTime.of(2016, 1, 1, 1, 1, 1, 1, ZoneOffset.UTC);
    OffsetDateTime now = OffsetDateTime.now();
    AwsIamRoleRecord awsIamRoleRecord = new AwsIamRoleRecord().setAwsIamRoleArn(principalArn);
    awsIamRoleRecord.setAwsIamRoleArn(principalArn);
    awsIamRoleRecord.setId(iamRoleId);
    when(awsIamRoleDao.getIamRole(principalArn)).thenReturn(Optional.of(awsIamRoleRecord));
    AwsIamRoleKmsKeyRecord awsIamRoleKmsKeyRecord = new AwsIamRoleKmsKeyRecord();
    awsIamRoleKmsKeyRecord.setId(kmsKeyId);
    awsIamRoleKmsKeyRecord.setAwsKmsKeyId(cmkId);
    awsIamRoleKmsKeyRecord.setLastValidatedTs(dateTime);
    when(awsIamRoleDao.getKmsKey(iamRoleId, region)).thenReturn(Optional.of(awsIamRoleKmsKeyRecord));
    when(dateTimeSupplier.get()).thenReturn(now);
    String result = authenticationService.getKmsKeyRecordForIamPrincipal(awsIamRoleRecord, region).getAwsKmsKeyId();
    // verify validate is called once interval has passed
    assertEquals(cmkId, result);
    verify(kmsService, times(1)).validateKeyAndPolicy(awsIamRoleKmsKeyRecord, principalArn);
}
Also used : OffsetDateTime(java.time.OffsetDateTime) AwsIamRoleKmsKeyRecord(com.nike.cerberus.record.AwsIamRoleKmsKeyRecord) AwsIamRoleRecord(com.nike.cerberus.record.AwsIamRoleRecord) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

AwsIamRoleKmsKeyRecord (com.nike.cerberus.record.AwsIamRoleKmsKeyRecord)15 OffsetDateTime (java.time.OffsetDateTime)11 Test (org.junit.Test)9 AuthKmsKeyMetadata (com.nike.cerberus.domain.AuthKmsKeyMetadata)4 AWSKMSClient (com.amazonaws.services.kms.AWSKMSClient)3 AwsIamRoleRecord (com.nike.cerberus.record.AwsIamRoleRecord)3 GetKeyPolicyRequest (com.amazonaws.services.kms.model.GetKeyPolicyRequest)2 GetKeyPolicyResult (com.amazonaws.services.kms.model.GetKeyPolicyResult)2 KeyMetadata (com.amazonaws.services.kms.model.KeyMetadata)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 CreateAliasRequest (com.amazonaws.services.kms.model.CreateAliasRequest)1 CreateKeyRequest (com.amazonaws.services.kms.model.CreateKeyRequest)1 CreateKeyResult (com.amazonaws.services.kms.model.CreateKeyResult)1 DescribeKeyResult (com.amazonaws.services.kms.model.DescribeKeyResult)1 Tag (com.amazonaws.services.kms.model.Tag)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 AuthTokenResponse (com.nike.cerberus.domain.AuthTokenResponse)1 EncryptedAuthDataWrapper (com.nike.cerberus.domain.EncryptedAuthDataWrapper)1 Matchers.anyString (org.mockito.Matchers.anyString)1