Search in sources :

Example 26 with StringGenerationParameters

use of org.cloudfoundry.credhub.request.StringGenerationParameters 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)

Example 27 with StringGenerationParameters

use of org.cloudfoundry.credhub.request.StringGenerationParameters in project credhub by cloudfoundry-incubator.

the class JsonObjectMapperTest method deserializeBackwardsCompatibleValue_supportsSnakeCaseDeserialization.

@Test
public void deserializeBackwardsCompatibleValue_supportsSnakeCaseDeserialization() throws IOException {
    final String testSnakeCaseString = "{\"exclude_lower\":true,\"include_special\":true}";
    StringGenerationParameters generationParameters = new StringGenerationParameters().setExcludeLower(true).setIncludeSpecial(true);
    final StringGenerationParameters actualGenerationParameters = new JsonObjectMapper().deserializeBackwardsCompatibleValue(testSnakeCaseString, StringGenerationParameters.class);
    assertThat(generationParameters, equalTo(actualGenerationParameters));
}
Also used : StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters) Test(org.junit.Test)

Example 28 with StringGenerationParameters

use of org.cloudfoundry.credhub.request.StringGenerationParameters in project credhub by cloudfoundry-incubator.

the class UserGenerator method generateCredential.

public UserCredentialValue generateCredential(GenerationParameters p) {
    StringGenerationParameters params = (StringGenerationParameters) p;
    String username = params.getUsername();
    if (username == null) {
        username = usernameGenerator.generateCredential().getStringCredential();
    }
    String password = passwordGenerator.generateCredential(params).getStringCredential();
    return new UserCredentialValue(username, password, cryptSaltFactory.generateSalt(password));
}
Also used : UserCredentialValue(org.cloudfoundry.credhub.credential.UserCredentialValue) StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters)

Example 29 with StringGenerationParameters

use of org.cloudfoundry.credhub.request.StringGenerationParameters in project credhub by cloudfoundry-incubator.

the class UsernameGenerator method generateCredential.

public StringCredentialValue generateCredential() {
    final StringGenerationParameters parameters = new StringGenerationParameters();
    parameters.setLength(20);
    parameters.setExcludeNumber(true);
    return passayStringCredentialGenerator.generateCredential(parameters);
}
Also used : StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters)

Example 30 with StringGenerationParameters

use of org.cloudfoundry.credhub.request.StringGenerationParameters in project credhub by cloudfoundry-incubator.

the class CredentialsControllerTypeSpecificGenerateTest method parameters.

@Parameterized.Parameters(name = "{0}")
public static Collection<Object> parameters() {
    credentialUuid = UUID.randomUUID();
    Collection<Object> params = new ArrayList<>();
    TestParameterizer passwordParameters = new TestParameterizer("password", "{\"exclude_number\": true}") {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value", FAKE_PASSWORD);
        }

        void credentialAssertions(CredentialVersion credential) {
            PasswordCredentialVersion passwordCredential = (PasswordCredentialVersion) credential;
            assertThat(passwordCredential.getGenerationParameters().isExcludeNumber(), equalTo(true));
            assertThat(passwordCredential.getPassword(), equalTo(FAKE_PASSWORD));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new PasswordCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPasswordAndGenerationParameters(FAKE_PASSWORD, new StringGenerationParameters().setExcludeNumber(true)).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParameterizer userParameterizer = new TestParameterizer("user", "null") {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.username", USERNAME, "$.value.password", FAKE_PASSWORD);
        }

        void credentialAssertions(CredentialVersion credential) {
            UserCredentialVersion userCredential = (UserCredentialVersion) credential;
            assertThat(userCredential.getUsername(), equalTo(USERNAME));
            assertThat(userCredential.getPassword(), equalTo(FAKE_PASSWORD));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new UserCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPassword(FAKE_PASSWORD).setUsername(USERNAME).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParameterizer certificateParameterizer = new TestParameterizer("certificate", "{\"common_name\":\"my-common-name\",\"self_sign\":true}") {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.certificate", "certificate", "$.value.private_key", "private_key", "$.value.ca", "ca");
        }

        void credentialAssertions(CredentialVersion credential) {
            CertificateCredentialVersion certificateCredential = (CertificateCredentialVersion) credential;
            assertThat(certificateCredential.getCa(), equalTo(CA));
            assertThat(certificateCredential.getCertificate(), equalTo(CERTIFICATE));
            assertThat(certificateCredential.getPrivateKey(), equalTo(PRIVATE_KEY));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new CertificateCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setCa(CA).setCertificate(CERTIFICATE).setPrivateKey(PRIVATE_KEY).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParameterizer sshParameterizer = new TestParameterizer("ssh", "null") {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.public_key", "public_key", "$.value.private_key", "private_key", "$.value.public_key_fingerprint", null);
        }

        void credentialAssertions(CredentialVersion credential) {
            SshCredentialVersion sshCredential = (SshCredentialVersion) credential;
            assertThat(sshCredential.getPublicKey(), equalTo(PUBLIC_KEY));
            assertThat(sshCredential.getPrivateKey(), equalTo(PRIVATE_KEY));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new SshCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPrivateKey(PRIVATE_KEY).setPublicKey(PUBLIC_KEY).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParameterizer rsaParameterizer = new TestParameterizer("rsa", "null") {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.public_key", "public_key", "$.value.private_key", "private_key");
        }

        void credentialAssertions(CredentialVersion credential) {
            RsaCredentialVersion rsaCredential = (RsaCredentialVersion) credential;
            assertThat(rsaCredential.getPublicKey(), equalTo(PUBLIC_KEY));
            assertThat(rsaCredential.getPrivateKey(), equalTo(PRIVATE_KEY));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new RsaCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPrivateKey(PRIVATE_KEY).setPublicKey(PUBLIC_KEY).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    params.add(passwordParameters);
    params.add(userParameterizer);
    params.add(certificateParameterizer);
    params.add(sshParameterizer);
    params.add(rsaParameterizer);
    return params;
}
Also used : RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Encryptor(org.cloudfoundry.credhub.domain.Encryptor) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion)

Aggregations

StringGenerationParameters (org.cloudfoundry.credhub.request.StringGenerationParameters)40 Test (org.junit.Test)24 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)8 CharacterRule (org.passay.CharacterRule)7 StringCredentialValue (org.cloudfoundry.credhub.credential.StringCredentialValue)6 EncryptedValue (org.cloudfoundry.credhub.entity.EncryptedValue)5 PasswordCredentialVersionData (org.cloudfoundry.credhub.entity.PasswordCredentialVersionData)5 Before (org.junit.Before)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 UserCredentialVersion (org.cloudfoundry.credhub.domain.UserCredentialVersion)3 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 UUID (java.util.UUID)2 EventAuditRecordParameters (org.cloudfoundry.credhub.audit.EventAuditRecordParameters)2 UserContext (org.cloudfoundry.credhub.auth.UserContext)2 UserCredentialVersionData (org.cloudfoundry.credhub.entity.UserCredentialVersionData)2 ParameterizedValidationException (org.cloudfoundry.credhub.exceptions.ParameterizedValidationException)2 PermissionService (org.cloudfoundry.credhub.service.PermissionService)2 PermissionedCredentialService (org.cloudfoundry.credhub.service.PermissionedCredentialService)2