Search in sources :

Example 16 with EncryptedValue

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

the class PasswordCredentialVersionTest method getPassword_shouldCallDecryptOnce.

@Test
public void getPassword_shouldCallDecryptOnce() {
    subject = new PasswordCredentialVersion("/Foo");
    subject.setEncryptor(encryptor);
    when(encryptor.encrypt(null)).thenReturn(new EncryptedValue(canaryUuid, "", ""));
    subject.setPasswordAndGenerationParameters(PASSWORD, null);
    subject.getPassword();
    verify(encryptor, times(1)).decrypt(any());
}
Also used : EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) Test(org.junit.Test)

Example 17 with EncryptedValue

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

the class PasswordCredentialVersionTest method beforeEach.

@Before
public void beforeEach() throws Exception {
    canaryUuid = UUID.randomUUID();
    encryptor = mock(Encryptor.class);
    encryptedValue = "fake-encrypted-value".getBytes();
    nonce = "fake-nonce".getBytes();
    encryptedParametersValue = "fake-encrypted-parameters".getBytes();
    parametersNonce = "fake-parameters-nonce".getBytes();
    generationParameters = new StringGenerationParameters().setExcludeLower(true).setLength(10);
    String generationParametersJson = new JsonObjectMapper().writeValueAsString(generationParameters);
    when(encryptor.encrypt(null)).thenReturn(new EncryptedValue(canaryUuid, "", ""));
    final EncryptedValue encryption = new EncryptedValue(canaryUuid, encryptedValue, nonce);
    when(encryptor.encrypt(PASSWORD)).thenReturn(encryption);
    final EncryptedValue parametersEncryption = new EncryptedValue(canaryUuid, encryptedParametersValue, parametersNonce);
    when(encryptor.encrypt(eq(generationParametersJson))).thenReturn(parametersEncryption);
    when(encryptor.decrypt(encryption)).thenReturn(PASSWORD);
    when(encryptor.decrypt(parametersEncryption)).thenReturn(generationParametersJson);
    passwordCredentialData = new PasswordCredentialVersionData("/Foo");
    subject = new PasswordCredentialVersion(passwordCredentialData);
    subject.setEncryptor(encryptor);
}
Also used : JsonObjectMapper(org.cloudfoundry.credhub.util.JsonObjectMapper) PasswordCredentialVersionData(org.cloudfoundry.credhub.entity.PasswordCredentialVersionData) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters) Before(org.junit.Before)

Example 18 with EncryptedValue

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

the class UserCredentialVersionTest method getPassword_returnsDecryptedPassword_andOnlyDecryptsOnce.

@Test
public void getPassword_returnsDecryptedPassword_andOnlyDecryptsOnce() {
    final EncryptedValue encryption = new EncryptedValue(ENCRYPTION_KEY_UUID, ENCRYPTED_PASSWORD, NONCE);
    when(encryptor.decrypt(encryption)).thenReturn(USER_PASSWORD);
    userCredentialData = new UserCredentialVersionData().setEncryptedValueData(new EncryptedValue().setEncryptedValue(ENCRYPTED_PASSWORD).setNonce(NONCE).setEncryptionKeyUuid(ENCRYPTION_KEY_UUID));
    subject = new UserCredentialVersion(userCredentialData).setEncryptor(encryptor);
    String password = subject.getPassword();
    assertThat(password, equalTo(USER_PASSWORD));
    verify(encryptor, times(1)).decrypt(any());
}
Also used : UserCredentialVersionData(org.cloudfoundry.credhub.entity.UserCredentialVersionData) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) Test(org.junit.Test)

Example 19 with EncryptedValue

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

the class UserCredentialVersionTest method getGenerationParameters_decryptsGenerationParameters.

@Test
public void getGenerationParameters_decryptsGenerationParameters() {
    final EncryptedValue parameterEncryption = new EncryptedValue(ENCRYPTION_KEY_UUID, ENCRYPTED_GENERATION_PARAMS, PARAMETERS_NONCE);
    final EncryptedValue passwordEncryption = new EncryptedValue(ENCRYPTION_KEY_UUID, ENCRYPTED_PASSWORD, NONCE);
    when(encryptor.decrypt(parameterEncryption)).thenReturn(USER_GENERATION_PARAMS_STRING);
    when(encryptor.decrypt(passwordEncryption)).thenReturn(USER_PASSWORD);
    userCredentialData = new UserCredentialVersionData().setEncryptedValueData(passwordEncryption).setEncryptedGenerationParameters(parameterEncryption);
    subject = new UserCredentialVersion(userCredentialData).setEncryptor(encryptor);
    StringGenerationParameters generationParameters = subject.getGenerationParameters();
    assertThat(generationParameters, samePropertyValuesAs(STRING_GENERATION_PARAMS));
    verify(encryptor, times(2)).decrypt(any());
}
Also used : UserCredentialVersionData(org.cloudfoundry.credhub.entity.UserCredentialVersionData) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters) Test(org.junit.Test)

Example 20 with EncryptedValue

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

the class UserCredentialVersionTest method rotate_reEncryptsPasswordWithNewEncryptionKey.

@Test
public void rotate_reEncryptsPasswordWithNewEncryptionKey() {
    UUID oldEncryptionKeyUuid = UUID.randomUUID();
    byte[] oldEncryptedPassword = "old-encrypted-password".getBytes();
    byte[] oldEncryptedGenerationParams = "old-encrypted-generation-params".getBytes();
    byte[] oldNonce = "old-nonce".getBytes();
    byte[] oldParametersNonce = "old-parameters-nonce".getBytes();
    EncryptedValue parametersEncryption = new EncryptedValue(oldEncryptionKeyUuid, oldEncryptedGenerationParams, oldParametersNonce);
    EncryptedValue encryptedUserValue = new EncryptedValue().setEncryptionKeyUuid(oldEncryptionKeyUuid).setEncryptedValue(oldEncryptedPassword).setNonce(oldNonce);
    userCredentialData = new UserCredentialVersionData(CREDENTIAL_NAME).setEncryptedValueData(encryptedUserValue).setEncryptedGenerationParameters(parametersEncryption);
    subject = new UserCredentialVersion(userCredentialData).setEncryptor(encryptor);
    when(encryptor.decrypt(new EncryptedValue(oldEncryptionKeyUuid, oldEncryptedPassword, oldNonce))).thenReturn(USER_PASSWORD);
    when(encryptor.decrypt(new EncryptedValue(oldEncryptionKeyUuid, oldEncryptedGenerationParams, oldParametersNonce))).thenReturn(USER_GENERATION_PARAMS_STRING);
    when(encryptor.encrypt(eq(USER_PASSWORD))).thenReturn(new EncryptedValue(ENCRYPTION_KEY_UUID, ENCRYPTED_PASSWORD, NONCE));
    when(encryptor.encrypt(eq(USER_GENERATION_PARAMS_STRING))).thenReturn(new EncryptedValue(ENCRYPTION_KEY_UUID, ENCRYPTED_GENERATION_PARAMS, PARAMETERS_NONCE));
    subject.rotate();
    verify(encryptor, times(2)).decrypt(any());
    verify(encryptor).encrypt(USER_PASSWORD);
    verify(encryptor).encrypt(USER_GENERATION_PARAMS_STRING);
    assertThat(userCredentialData.getEncryptionKeyUuid(), equalTo(ENCRYPTION_KEY_UUID));
    assertThat(userCredentialData.getEncryptedValueData().getEncryptedValue(), equalTo(ENCRYPTED_PASSWORD));
    assertThat(userCredentialData.getEncryptedGenerationParameters().getEncryptedValue(), equalTo(ENCRYPTED_GENERATION_PARAMS));
    assertThat(userCredentialData.getNonce(), equalTo(NONCE));
    assertThat(userCredentialData.getEncryptedGenerationParameters().getNonce(), equalTo(PARAMETERS_NONCE));
}
Also used : UserCredentialVersionData(org.cloudfoundry.credhub.entity.UserCredentialVersionData) UUID(java.util.UUID) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) Test(org.junit.Test)

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