Search in sources :

Example 31 with EncryptedValue

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

the class RetryingEncryptionServiceTest method usingTwoThread_wontRetryTwice.

@Test
public void usingTwoThread_wontRetryTwice() throws Exception {
    final Object lock = new Object();
    final Thread firstThread = new Thread("first") {

        @Override
        public void run() {
            try {
                subject.decrypt(new EncryptedValue(activeKeyUuid, "a value 1".getBytes(), "nonce".getBytes()));
            } catch (Exception e) {
            // do nothing
            }
        }
    };
    final Thread secondThread = new Thread("second") {

        @Override
        public void run() {
            try {
                subject.decrypt(new EncryptedValue(activeKeyUuid, "a value 2".getBytes(), "nonce".getBytes()));
            } catch (Exception e) {
            // do nothing
            }
        }
    };
    subject = new RacingRetryingEncryptionServiceForTest(firstThread, secondThread, lock);
    when(keySet.get(activeKeyUuid)).thenReturn(firstActiveKey);
    when(keySet.getActive()).thenReturn(firstActiveKey);
    when(firstActiveKey.decrypt(any(byte[].class), any(byte[].class))).thenThrow(new ProviderException("function 'C_GenerateRandom' returns 0x30"));
    firstThread.start();
    firstThread.join();
    secondThread.join();
    verify(keySet, times(1)).reload();
}
Also used : ProviderException(java.security.ProviderException) 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 32 with EncryptedValue

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

the class RetryingEncryptionServiceTest method decrypt_whenThrowsAnError_retriesDecryptionFailure.

@Test
public void decrypt_whenThrowsAnError_retriesDecryptionFailure() throws Exception {
    when(keySet.get(activeKeyUuid)).thenReturn(firstActiveKey);
    when(keySet.getActive()).thenReturn(firstActiveKey);
    when(firstActiveKey.decrypt(any(byte[].class), any(byte[].class))).thenThrow(new ProviderException("function 'C_GenerateRandom' returns 0x30"));
    try {
        subject.decrypt(new EncryptedValue(activeKeyUuid, "an encrypted value".getBytes(), "a nonce".getBytes()));
        fail("Expected exception");
    } catch (ProviderException e) {
    // expected
    }
    final InOrder inOrder = inOrder(firstActiveKey);
    inOrder.verify(firstActiveKey).decrypt(any(byte[].class), any(byte[].class));
    inOrder.verify(firstActiveKey).reconnect(any(ProviderException.class));
    inOrder.verify(firstActiveKey).decrypt(any(byte[].class), any(byte[].class));
}
Also used : InOrder(org.mockito.InOrder) ProviderException(java.security.ProviderException) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) Test(org.junit.Test)

Example 33 with EncryptedValue

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

the class RetryingEncryptionServiceTest method decrypt_whenTheOperationSucceedsOnlyAfterReconnection.

@Test
public void decrypt_whenTheOperationSucceedsOnlyAfterReconnection() throws Exception {
    when(keySet.get(activeKeyUuid)).thenReturn(firstActiveKey).thenReturn(secondActiveKey);
    when(keySet.getActive()).thenReturn(firstActiveKey);
    when(firstActiveKey.decrypt("fake-encrypted-value".getBytes(), "fake-nonce".getBytes())).thenThrow(new IllegalBlockSizeException("test exception"));
    when(secondActiveKey.decrypt("fake-encrypted-value".getBytes(), "fake-nonce".getBytes())).thenReturn("fake-plaintext");
    assertThat(subject.decrypt(new EncryptedValue(activeKeyUuid, "fake-encrypted-value".getBytes(), "fake-nonce".getBytes())), equalTo("fake-plaintext"));
    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 34 with EncryptedValue

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

the class CredentialVersionDataRepositoryTest method canSaveStringsOfLength7000WhichMeans7016ForGCM.

@Test
public void canSaveStringsOfLength7000WhichMeans7016ForGCM() {
    byte[] encryptedValue = new byte[7016];
    Arrays.fill(encryptedValue, (byte) 'A');
    final StringBuilder stringBuilder = new StringBuilder(7000);
    Stream.generate(() -> "a").limit(stringBuilder.capacity()).forEach(stringBuilder::append);
    ValueCredentialVersionData entity = new ValueCredentialVersionData();
    Credential credential = credentialRepository.save(new Credential(name));
    entity.setCredential(credential);
    entity.setEncryptedValueData(new EncryptedValue().setEncryptedValue(encryptedValue).setEncryptionKeyUuid(canaryUuid).setNonce("nonce".getBytes()));
    subject.save(entity);
    assertThat(subject.findFirstByCredentialUuidOrderByVersionCreatedAtDesc(credential.getUuid()).getEncryptedValueData().getEncryptedValue().length, equalTo(7016));
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) ValueCredentialVersionData(org.cloudfoundry.credhub.entity.ValueCredentialVersionData) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Example 35 with EncryptedValue

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

the class SshViewTest method beforeEach.

@Before
public void beforeEach() {
    Encryptor encryptor = mock(Encryptor.class);
    final EncryptedValue encryption = new EncryptedValue(UUID.randomUUID(), "encrypted".getBytes(), "nonce".getBytes());
    when(encryptor.encrypt(TestConstants.PRIVATE_KEY_4096)).thenReturn(encryption);
    when(encryptor.decrypt(encryption)).thenReturn(TestConstants.PRIVATE_KEY_4096);
    entity = new SshCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPublicKey(TestConstants.SSH_PUBLIC_KEY_4096_WITH_COMMENT).setPrivateKey(TestConstants.PRIVATE_KEY_4096);
    entity.setUuid(CREDENTIAL_UUID);
}
Also used : Encryptor(org.cloudfoundry.credhub.domain.Encryptor) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) Before(org.junit.Before)

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