Search in sources :

Example 21 with EncryptedValue

use of org.cloudfoundry.credhub.entity.EncryptedValue in project credhub by cloudfoundry-incubator.

the class RetryingEncryptionServiceTest method encryption_whenTheOperationSucceedsOnlyAfterReconnection_shouldReturnTheEncryptedString.

@Test
public void encryption_whenTheOperationSucceedsOnlyAfterReconnection_shouldReturnTheEncryptedString() throws Exception {
    EncryptedValue expectedEncryption = mock(EncryptedValue.class);
    when(keySet.getActive()).thenReturn(firstActiveKey).thenReturn(secondActiveKey);
    when(firstActiveKey.encrypt("fake-plaintext")).thenThrow(new IllegalBlockSizeException("test exception"));
    when(secondActiveKey.encrypt("fake-plaintext")).thenReturn(expectedEncryption);
    assertThat(subject.encrypt("fake-plaintext"), equalTo(expectedEncryption));
    verify(keySet.getActive(), times(1)).reconnect(any(IllegalBlockSizeException.class));
    verify(keySet, times(1)).reload();
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) Test(org.junit.Test)

Example 22 with EncryptedValue

use of org.cloudfoundry.credhub.entity.EncryptedValue in project credhub by cloudfoundry-incubator.

the class RetryingEncryptionServiceTest method decrypt_locksAndUnlocksTheReconnectLockWhenLoginError.

@Test
public void decrypt_locksAndUnlocksTheReconnectLockWhenLoginError() throws Exception {
    when(keySet.get(activeKeyUuid)).thenReturn(firstActiveKey);
    when(firstActiveKey.decrypt(any(byte[].class), any(byte[].class))).thenThrow(new ProviderException("function 'C_GenerateRandom' returns 0x30"));
    reset(writeLock);
    doThrow(new RuntimeException()).when(encryptionService).reconnect(any(Exception.class));
    try {
        subject.decrypt(new EncryptedValue(activeKeyUuid, "an encrypted value".getBytes(), "a nonce".getBytes()));
    } catch (IllegalBlockSizeException | RuntimeException e) {
    // expected
    }
    verify(readLock, times(2)).lock();
    verify(readLock, times(2)).unlock();
    verify(writeLock, times(1)).lock();
    verify(writeLock, times(1)).unlock();
}
Also used : ProviderException(java.security.ProviderException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ProviderException(java.security.ProviderException) KeyNotFoundException(org.cloudfoundry.credhub.exceptions.KeyNotFoundException) Test(org.junit.Test)

Example 23 with EncryptedValue

use of org.cloudfoundry.credhub.entity.EncryptedValue in project credhub by cloudfoundry-incubator.

the class EventAuditLogServiceTest method beforeEach.

@Before
public void beforeEach() {
    canaries = encryptionKeyCanaryRepository.findAll();
    mockOutCurrentTimeProvider(currentTimeProvider).accept(now.toEpochMilli());
    userContext = mockUserContext(true);
    userContextHolder.setUserContext(userContext);
    entity = new ValueCredentialVersionData("keyName");
    entity.setEncryptedValueData(new EncryptedValue(canaries.get(0).getUuid(), "value", "nonce"));
}
Also used : EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) ValueCredentialVersionData(org.cloudfoundry.credhub.entity.ValueCredentialVersionData) Before(org.junit.Before)

Example 24 with EncryptedValue

use of org.cloudfoundry.credhub.entity.EncryptedValue in project credhub by cloudfoundry-incubator.

the class EncryptionKeyRotatorTest method createCertificateWithOldKey.

private void createCertificateWithOldKey(Key oldKey) throws Exception {
    final EncryptedValue encryption = encryptionService.encrypt(oldCanary.getUuid(), oldKey, "old-certificate-private-key");
    CertificateCredentialVersionData certificateCredentialData1 = new CertificateCredentialVersionData("/old-key");
    certificateCredentialData1.setEncryptedValueData(encryption);
    credentialVersionWithOldKey = new CertificateCredentialVersion(certificateCredentialData1);
    credentialVersionDataService.save(credentialVersionWithOldKey);
}
Also used : CertificateCredentialVersionData(org.cloudfoundry.credhub.entity.CertificateCredentialVersionData) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion)

Example 25 with EncryptedValue

use of org.cloudfoundry.credhub.entity.EncryptedValue in project credhub by cloudfoundry-incubator.

the class EncryptionKeyRotatorTest method createPasswordWithOldKey.

private void createPasswordWithOldKey(Key oldKey) throws Exception {
    final EncryptedValue credentialEncryption = encryptionService.encrypt(oldCanary.getUuid(), oldKey, "test-password-plaintext");
    PasswordCredentialVersionData passwordCredentialData = new PasswordCredentialVersionData(passwordName);
    passwordCredentialData.setEncryptedValueData(credentialEncryption);
    StringGenerationParameters parameters = new StringGenerationParameters();
    parameters.setExcludeNumber(true);
    final EncryptedValue parameterEncryption = encryptionService.encrypt(oldCanary.getUuid(), oldKey, new ObjectMapper().writeValueAsString(parameters));
    passwordCredentialData.setEncryptedGenerationParameters(parameterEncryption);
    password = new PasswordCredentialVersion(passwordCredentialData);
    credentialVersionDataService.save(password);
}
Also used : PasswordCredentialVersionData(org.cloudfoundry.credhub.entity.PasswordCredentialVersionData) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters)

Aggregations

EncryptedValue (org.cloudfoundry.credhub.entity.EncryptedValue)56 Test (org.junit.Test)31 PasswordCredentialVersionData (org.cloudfoundry.credhub.entity.PasswordCredentialVersionData)12 Before (org.junit.Before)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 CertificateCredentialVersionData (org.cloudfoundry.credhub.entity.CertificateCredentialVersionData)7 Credential (org.cloudfoundry.credhub.entity.Credential)7 UUID (java.util.UUID)6 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)6 ValueCredentialVersionData (org.cloudfoundry.credhub.entity.ValueCredentialVersionData)6 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)5 UserCredentialVersionData (org.cloudfoundry.credhub.entity.UserCredentialVersionData)5 StringGenerationParameters (org.cloudfoundry.credhub.request.StringGenerationParameters)5 ProviderException (java.security.ProviderException)4 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)4 ValueCredentialVersion (org.cloudfoundry.credhub.domain.ValueCredentialVersion)4 EncryptionKeyCanary (org.cloudfoundry.credhub.entity.EncryptionKeyCanary)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Key (java.security.Key)3 KeyNotFoundException (org.cloudfoundry.credhub.exceptions.KeyNotFoundException)3