Search in sources :

Example 1 with CryptSaltFactory

use of org.cloudfoundry.credhub.credential.CryptSaltFactory in project credhub by cloudfoundry-incubator.

the class V41_1__set_salt_in_existing_user_credentials method migrate.

@Override
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    CryptSaltFactory saltFactory = new CryptSaltFactory();
    String databaseName = jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName().toLowerCase();
    List<UUID> uuids = jdbcTemplate.query("select uuid from user_credential", (rowSet, rowNum) -> {
        byte[] uuidBytes = rowSet.getBytes("uuid");
        if (databaseName.equals("postgresql")) {
            return UUID.fromString(new String(uuidBytes));
        } else {
            ByteBuffer byteBuffer = ByteBuffer.wrap(uuidBytes);
            return new UUID(byteBuffer.getLong(), byteBuffer.getLong());
        }
    });
    for (UUID uuid : uuids) {
        String salt = saltFactory.generateSalt();
        jdbcTemplate.update("update user_credential set salt = ? where uuid = ?", new Object[] { salt, getUuidParam(databaseName, uuid) });
    }
}
Also used : CryptSaltFactory(org.cloudfoundry.credhub.credential.CryptSaltFactory) UUID(java.util.UUID) ByteBuffer(java.nio.ByteBuffer)

Example 2 with CryptSaltFactory

use of org.cloudfoundry.credhub.credential.CryptSaltFactory in project credhub by cloudfoundry-incubator.

the class UserGeneratorTest method beforeEach.

@Before
public void beforeEach() {
    UsernameGenerator usernameGenerator = mock(UsernameGenerator.class);
    PasswordCredentialGenerator passwordGenerator = mock(PasswordCredentialGenerator.class);
    CryptSaltFactory cryptSaltFactory = mock(CryptSaltFactory.class);
    passwordParameters = new StringGenerationParameters();
    userContext = null;
    subject = new UserGenerator(usernameGenerator, passwordGenerator, cryptSaltFactory);
    StringCredentialValue generatedUsername = new StringCredentialValue("fake-generated-username");
    StringCredentialValue generatedPassword = new StringCredentialValue("fake-generated-password");
    when(usernameGenerator.generateCredential()).thenReturn(generatedUsername);
    when(passwordGenerator.generateCredential(eq(passwordParameters))).thenReturn(generatedPassword);
    when(cryptSaltFactory.generateSalt(generatedPassword.getStringCredential())).thenReturn("fake-generated-salt");
}
Also used : StringCredentialValue(org.cloudfoundry.credhub.credential.StringCredentialValue) CryptSaltFactory(org.cloudfoundry.credhub.credential.CryptSaltFactory) StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters) Before(org.junit.Before)

Example 3 with CryptSaltFactory

use of org.cloudfoundry.credhub.credential.CryptSaltFactory in project credhub by cloudfoundry-incubator.

the class UserViewTest method canCreateViewFromEntity.

@Test
public void canCreateViewFromEntity() throws IOException {
    final UUID uuid = UUID.randomUUID();
    final String salt = new CryptSaltFactory().generateSalt("test-password");
    final String passwordHash = Crypt.crypt("test-password", salt);
    final UserCredentialVersion userCredential = mock(UserCredentialVersion.class);
    when(userCredential.getName()).thenReturn("/foo");
    when(userCredential.getUuid()).thenReturn(uuid);
    when(userCredential.getCredentialType()).thenReturn("user");
    when(userCredential.getPassword()).thenReturn("test-password");
    when(userCredential.getUsername()).thenReturn("test-username");
    when(userCredential.getSalt()).thenReturn(salt);
    UserView actual = (UserView) UserView.fromEntity(userCredential);
    assertThat(JsonTestHelper.serializeToString(actual), equalTo("{" + "\"type\":\"user\"," + "\"version_created_at\":null," + "\"id\":\"" + uuid.toString() + "\"," + "\"name\":\"/foo\"," + "\"value\":{" + "\"username\":\"test-username\"," + "\"password\":\"test-password\"," + "\"password_hash\":\"" + passwordHash + "\"" + "}}"));
}
Also used : CryptSaltFactory(org.cloudfoundry.credhub.credential.CryptSaltFactory) UUID(java.util.UUID) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) Test(org.junit.Test)

Aggregations

CryptSaltFactory (org.cloudfoundry.credhub.credential.CryptSaltFactory)3 UUID (java.util.UUID)2 ByteBuffer (java.nio.ByteBuffer)1 StringCredentialValue (org.cloudfoundry.credhub.credential.StringCredentialValue)1 UserCredentialVersion (org.cloudfoundry.credhub.domain.UserCredentialVersion)1 StringGenerationParameters (org.cloudfoundry.credhub.request.StringGenerationParameters)1 Before (org.junit.Before)1 Test (org.junit.Test)1