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) });
}
}
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");
}
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 + "\"" + "}}"));
}
Aggregations