Search in sources :

Example 41 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class CredentialsControllerTypeSpecificSetTest method settingACredential_returnsTheExpectedResponse.

@Test
public void settingACredential_returnsTheExpectedResponse() throws Exception {
    MockHttpServletRequestBuilder request = put("/api/v1/data").header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "\"name\":\"" + CREDENTIAL_NAME + "\"," + "\"type\":\"" + parametizer.credentialType + "\"," + "\"value\":" + parametizer.credentialValue + "," + "\"overwrite\":" + false + "," + "\"additional_permissions\": [" + "{\"actor\": \"app1-guid\"," + "\"operations\": [\"read\"]}]" + "}");
    ResultActions response = mockMvc.perform(request);
    ArgumentCaptor<CredentialVersion> argumentCaptor = ArgumentCaptor.forClass(CredentialVersion.class);
    verify(credentialVersionDataService, times(1)).save(argumentCaptor.capture());
    response.andExpect(parametizer.jsonAssertions()).andExpect(multiJsonPath("$.type", parametizer.credentialType, "$.id", argumentCaptor.getValue().getUuid().toString(), "$.version_created_at", FROZEN_TIME.toString())).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 42 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class CredentialsControllerTypeSpecificSetTest method settingACredential_shouldAcceptAnyCasingForType.

@Test
public void settingACredential_shouldAcceptAnyCasingForType() throws Exception {
    MockHttpServletRequestBuilder request = put("/api/v1/data").header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "\"name\":\"" + CREDENTIAL_NAME + "\"," + "\"type\":\"" + parametizer.credentialType.toUpperCase() + "\"," + "\"value\":" + parametizer.credentialValue + "," + "\"overwrite\":" + false + "," + "\"additional_permissions\": [" + "{\"actor\": \"app1-guid\"," + "\"operations\": [\"read\"]}]" + "}");
    ResultActions response = mockMvc.perform(request);
    ArgumentCaptor<CredentialVersion> argumentCaptor = ArgumentCaptor.forClass(CredentialVersion.class);
    verify(credentialVersionDataService, times(1)).save(argumentCaptor.capture());
    response.andExpect(status().isOk()).andExpect(parametizer.jsonAssertions()).andExpect(multiJsonPath("$.type", parametizer.credentialType, "$.id", argumentCaptor.getValue().getUuid().toString(), "$.version_created_at", FROZEN_TIME.toString())).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 43 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class CredentialsControllerTypeSpecificSetTest method settingACredential_expectsDataServiceToPersistTheCredential.

@Test
public void settingACredential_expectsDataServiceToPersistTheCredential() throws Exception {
    MockHttpServletRequestBuilder request = put("/api/v1/data").header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "\"name\":\"" + CREDENTIAL_NAME + "\"," + "\"type\":\"" + parametizer.credentialType + "\"," + "\"value\":" + parametizer.credentialValue + "," + "\"overwrite\":" + false + "," + "\"additional_permissions\": [" + "{\"actor\": \"app1-guid\"," + "\"operations\": [\"read\"]}]" + "}");
    ArgumentCaptor<CredentialVersion> argumentCaptor = ArgumentCaptor.forClass(CredentialVersion.class);
    mockMvc.perform(request);
    verify(setHandler, times(1)).handle(isA(BaseCredentialSetRequest.class), any());
    verify(credentialVersionDataService, times(1)).save(argumentCaptor.capture());
    CredentialVersion newCredentialVersion = argumentCaptor.getValue();
    parametizer.credentialAssertions(newCredentialVersion);
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) BaseCredentialSetRequest(org.cloudfoundry.credhub.request.BaseCredentialSetRequest) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 44 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class CredentialsControllerTypeSpecificSetTest method parameters.

@Parameterized.Parameters(name = "{0}")
public static Collection<Object> parameters() {
    UUID credentialUuid = UUID.randomUUID();
    Collection<Object> params = new ArrayList<>();
    TestParametizer valueParameters = new TestParametizer("value", "\"" + VALUE_VALUE + "\"") {

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

        void credentialAssertions(CredentialVersion credential) {
            assertThat(((ValueCredentialVersion) credential).getValue(), equalTo(VALUE_VALUE));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new ValueCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setValue(VALUE_VALUE).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer passwordParameters = new TestParametizer("password", "\"" + PASSWORD_VALUE + "\"") {

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

        void credentialAssertions(CredentialVersion credential) {
            assertThat(((PasswordCredentialVersion) credential).getPassword(), equalTo(PASSWORD_VALUE));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new PasswordCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPasswordAndGenerationParameters(PASSWORD_VALUE, null).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer certificateParameters = new TestParametizer("certificate", CERTIFICATE_VALUE_JSON_STRING) {

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

        void credentialAssertions(CredentialVersion credential) {
            CertificateCredentialVersion certificateCredential = (CertificateCredentialVersion) credential;
            assertThat(certificateCredential.getCa(), equalTo(TestConstants.TEST_CA));
            assertThat(certificateCredential.getCertificate(), equalTo(TestConstants.TEST_CERTIFICATE));
            assertThat(certificateCredential.getPrivateKey(), equalTo(TestConstants.TEST_PRIVATE_KEY));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new CertificateCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setCa(TestConstants.TEST_CA).setCertificate(TestConstants.TEST_CERTIFICATE).setPrivateKey(TestConstants.TEST_PRIVATE_KEY).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer sshParameters = new TestParametizer("ssh", SSH_VALUE_JSON_STRING) {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.public_key", TestConstants.SSH_PUBLIC_KEY_4096_WITH_COMMENT, "$.value.private_key", TestConstants.PRIVATE_KEY_4096, "$.value.public_key_fingerprint", "UmqxK9UJJR4Jrcw0DcwqJlCgkeQoKp8a+HY+0p0nOgc");
        }

        void credentialAssertions(CredentialVersion credential) {
            SshCredentialVersion sshCredential = (SshCredentialVersion) credential;
            assertThat(sshCredential.getPublicKey(), equalTo(TestConstants.SSH_PUBLIC_KEY_4096_WITH_COMMENT));
            assertThat(sshCredential.getPrivateKey(), equalTo(TestConstants.PRIVATE_KEY_4096));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new SshCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPrivateKey(TestConstants.PRIVATE_KEY_4096).setPublicKey(TestConstants.SSH_PUBLIC_KEY_4096_WITH_COMMENT).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer rsaParameters = new TestParametizer("rsa", RSA_VALUE_JSON_STRING) {

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

        void credentialAssertions(CredentialVersion credential) {
            RsaCredentialVersion rsaCredential = (RsaCredentialVersion) credential;
            assertThat(rsaCredential.getPublicKey(), equalTo(TestConstants.RSA_PUBLIC_KEY_4096));
            assertThat(rsaCredential.getPrivateKey(), equalTo(TestConstants.PRIVATE_KEY_4096));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new RsaCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPrivateKey(TestConstants.PRIVATE_KEY_4096).setPublicKey(TestConstants.RSA_PUBLIC_KEY_4096).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer jsonParameters = new TestParametizer("json", JSON_VALUE_JSON_STRING) {

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

        void credentialAssertions(CredentialVersion credential) {
            JsonCredentialVersion jsonCredential = (JsonCredentialVersion) credential;
            assertThat(jsonCredential.getValue(), equalTo(jsonValueMap));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new JsonCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setValue(jsonValueMap).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer userParameters = new TestParametizer("user", USER_VALUE_JSON_STRING) {

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

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

        CredentialVersion createCredential(Encryptor encryptor) {
            return new UserCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setUsername(USERNAME_VALUE).setPassword(PASSWORD_VALUE).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    params.add(valueParameters);
    params.add(passwordParameters);
    params.add(certificateParameters);
    params.add(sshParameters);
    params.add(rsaParameters);
    params.add(jsonParameters);
    params.add(userParameters);
    return params;
}
Also used : JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Encryptor(org.cloudfoundry.credhub.domain.Encryptor) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) JSONObject(net.minidev.json.JSONObject) UUID(java.util.UUID) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) 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) JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion)

Example 45 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class CredentialVersionDataServiceTest method findActiveByName_whenAskedForCertificate_returnsTransitionalValueInAddition.

@Test
public void findActiveByName_whenAskedForCertificate_returnsTransitionalValueInAddition() throws Exception {
    CertificateCredentialVersion certificate = saveCertificate(2000000000123L, "/some-certificate");
    CertificateCredentialVersion version2 = saveTransitionalCertificate(2000000000123L, "/some-certificate");
    CertificateCredentialVersion version3 = saveCertificate(2000000000229L, "/some-certificate");
    List<CredentialVersion> credentialVersions = subject.findActiveByName("/some-certificate");
    assertThat(credentialVersions.size(), equalTo(2));
    assertThat(credentialVersions, containsInAnyOrder(hasProperty("uuid", equalTo(version2.getUuid())), hasProperty("uuid", equalTo(version3.getUuid()))));
}
Also used : PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)50 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)35 Test (org.junit.Test)33 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)27 SshCredentialVersion (org.cloudfoundry.credhub.domain.SshCredentialVersion)22 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)19 ValueCredentialVersion (org.cloudfoundry.credhub.domain.ValueCredentialVersion)17 RsaCredentialVersion (org.cloudfoundry.credhub.domain.RsaCredentialVersion)14 UserCredentialVersion (org.cloudfoundry.credhub.domain.UserCredentialVersion)13 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)11 JsonCredentialVersion (org.cloudfoundry.credhub.domain.JsonCredentialVersion)10 Credential (org.cloudfoundry.credhub.entity.Credential)8 EntryNotFoundException (org.cloudfoundry.credhub.exceptions.EntryNotFoundException)7 EventAuditRecordParameters (org.cloudfoundry.credhub.audit.EventAuditRecordParameters)6 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 CredentialVersionData (org.cloudfoundry.credhub.entity.CredentialVersionData)5 ResultActions (org.springframework.test.web.servlet.ResultActions)5 CertificateCredentialValue (org.cloudfoundry.credhub.credential.CertificateCredentialValue)4 UserContext (org.cloudfoundry.credhub.auth.UserContext)3