Search in sources :

Example 6 with ValueCredentialVersion

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

the class InterpolationControllerTest method POST_whenAReferencedCredentialIsNotJsonType_throwsAnError.

@Test
public void POST_whenAReferencedCredentialIsNotJsonType_throwsAnError() throws Exception {
    ValueCredentialVersion valueCredential = mock(ValueCredentialVersion.class);
    doReturn("something").when(valueCredential).getValue();
    doReturn(Arrays.asList(valueCredential)).when(mockCredentialVersionDataService).findNByName("/cred1", 1);
    String expectedMessage = "The credential '/cred1' is not the expected type. A credhub-ref credential must be of type 'JSON'.";
    mockMvc.perform(post("/api/v1/interpolate").header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN).contentType(MediaType.APPLICATION_JSON).content("{" + "    \"pp-config-server\": [" + "      {" + "        \"credentials\": {" + "          \"credhub-ref\": \"/cred1\"" + "        }," + "        \"label\": \"pp-config-server\"" + "      }" + "    ]" + "}")).andExpect(status().is4xxClientError()).andExpect(jsonPath("$.error", equalTo(expectedMessage)));
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with ValueCredentialVersion

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

the class CredentialVersionDataServiceTest method save_givenAnExistingCredential_throwsExceptionIfTypeMismatch.

@Test(expected = ParameterizedValidationException.class)
public void save_givenAnExistingCredential_throwsExceptionIfTypeMismatch() {
    PasswordCredentialVersionData passwordCredentialData = new PasswordCredentialVersionData("/my-credential-3");
    passwordCredentialData.setEncryptedValueData(new EncryptedValue().setEncryptionKeyUuid(activeCanaryUuid).setEncryptedValue(new byte[] {}).setNonce(new byte[] {}));
    PasswordCredentialVersion credential = new PasswordCredentialVersion(passwordCredentialData);
    subject.save(credential);
    ValueCredentialVersionData newCredentialData = new ValueCredentialVersionData();
    newCredentialData.setEncryptedValueData(new EncryptedValue().setEncryptionKeyUuid(activeCanaryUuid).setEncryptedValue("some value".getBytes()));
    newCredentialData.setCredential(passwordCredentialData.getCredential());
    ValueCredentialVersion newCredential = new ValueCredentialVersion(newCredentialData);
    subject.save(newCredential);
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) PasswordCredentialVersionData(org.cloudfoundry.credhub.entity.PasswordCredentialVersionData) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) ValueCredentialVersionData(org.cloudfoundry.credhub.entity.ValueCredentialVersionData) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 8 with ValueCredentialVersion

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

the class CredentialVersionDataServiceTest method findAllPaths_returnsCompleteDirectoryStructure.

@Test
public void findAllPaths_returnsCompleteDirectoryStructure() {
    String valueOther = "/fubario";
    String valueName = "/value/Credential";
    String passwordName = "/password/Credential";
    String certificateName = "/certif/ic/ateCredential";
    ValueCredentialVersionData valueCredentialData = new ValueCredentialVersionData(valueOther);
    ValueCredentialVersion valueCredential = new ValueCredentialVersion(valueCredentialData);
    subject.save(valueCredential);
    valueCredentialData = new ValueCredentialVersionData(valueName);
    valueCredential = new ValueCredentialVersion(valueCredentialData);
    subject.save(valueCredential);
    PasswordCredentialVersionData passwordCredentialData = new PasswordCredentialVersionData(passwordName);
    PasswordCredentialVersion passwordCredential = new PasswordCredentialVersion(passwordCredentialData);
    subject.save(passwordCredential);
    CertificateCredentialVersionData certificateCredentialData = new CertificateCredentialVersionData(certificateName);
    CertificateCredentialVersion certificateCredential = new CertificateCredentialVersion(certificateCredentialData);
    subject.save(certificateCredential);
    assertThat(subject.findAllPaths(), equalTo(newArrayList("/", "/certif/", "/certif/ic/", "/password/", "/value/")));
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) CertificateCredentialVersionData(org.cloudfoundry.credhub.entity.CertificateCredentialVersionData) PasswordCredentialVersionData(org.cloudfoundry.credhub.entity.PasswordCredentialVersionData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) ValueCredentialVersionData(org.cloudfoundry.credhub.entity.ValueCredentialVersionData) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 9 with ValueCredentialVersion

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

the class CredentialsControllerAuditLogTest method whenARequestHasMultipleXForwardedForHeaders_logsAllXForwardedForValues.

@Test
public void whenARequestHasMultipleXForwardedForHeaders_logsAllXForwardedForValues() throws Exception {
    when(credentialVersionDataService.save(any(CredentialVersion.class))).thenAnswer(invocation -> {
        ValueCredentialVersion valueCredential = invocation.getArgumentAt(0, ValueCredentialVersion.class);
        valueCredential.setUuid(UUID.randomUUID());
        return valueCredential;
    });
    MockHttpServletRequestBuilder putRequest = MockMvcRequestBuilders.put(CredentialsController.API_V1_DATA).accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).header("X-Forwarded-For", "1.1.1.1,2.2.2.2").header("X-Forwarded-For", "3.3.3.3").content("{\"type\":\"value\",\"name\":\"foo\",\"value\":\"password\"}").with(request -> {
        request.setRemoteAddr("12346");
        return request;
    });
    mockMvc.perform(putRequest).andExpect(status().isOk());
    ArgumentCaptor<RequestAuditRecord> recordCaptor = ArgumentCaptor.forClass(RequestAuditRecord.class);
    verify(requestAuditRecordDataService, times(1)).save(recordCaptor.capture());
    RequestAuditRecord auditRecord = recordCaptor.getValue();
    assertThat(auditRecord.getXForwardedFor(), equalTo("1.1.1.1,2.2.2.2,3.3.3.3"));
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) RequestAuditRecord(org.cloudfoundry.credhub.entity.RequestAuditRecord) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 10 with ValueCredentialVersion

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

the class CredentialsControllerAuditLogTest method settingACredential_makesACredentialUpdateLogEntry.

@Test
public void settingACredential_makesACredentialUpdateLogEntry() throws Exception {
    when(credentialVersionDataService.save(any(CredentialVersion.class))).thenAnswer(invocation -> {
        ValueCredentialVersion valueCredential = invocation.getArgumentAt(0, ValueCredentialVersion.class);
        valueCredential.setEncryptor(encryptor);
        valueCredential.setUuid(UUID.randomUUID());
        return valueCredential;
    });
    MockHttpServletRequestBuilder set = MockMvcRequestBuilders.put(CredentialsController.API_V1_DATA).accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).header("X-Forwarded-For", "1.1.1.1,2.2.2.2").content("{\"type\":\"value\",\"name\":\"foo\",\"value\":\"credential\"}").with(request -> {
        request.setRemoteAddr("12346");
        return request;
    });
    mockMvc.perform(set).andExpect(status().isOk());
    ArgumentCaptor<List> recordCaptor = ArgumentCaptor.forClass(List.class);
    verify(eventAuditRecordDataService, times(1)).save(recordCaptor.capture());
    EventAuditRecord auditRecord = (EventAuditRecord) recordCaptor.getValue().get(0);
    assertThat(auditRecord.getCredentialName(), equalTo("/foo"));
    assertThat(auditRecord.getOperation(), equalTo(CREDENTIAL_UPDATE.toString()));
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) EventAuditRecord(org.cloudfoundry.credhub.entity.EventAuditRecord) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) List(java.util.List) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

ValueCredentialVersion (org.cloudfoundry.credhub.domain.ValueCredentialVersion)18 Test (org.junit.Test)14 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)14 UUID (java.util.UUID)9 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)7 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)6 CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)4 ValueCredentialVersionData (org.cloudfoundry.credhub.entity.ValueCredentialVersionData)4 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)3 EncryptedValue (org.cloudfoundry.credhub.entity.EncryptedValue)3 PasswordCredentialVersionData (org.cloudfoundry.credhub.entity.PasswordCredentialVersionData)3 Encryptor (org.cloudfoundry.credhub.domain.Encryptor)2 CertificateCredentialVersionData (org.cloudfoundry.credhub.entity.CertificateCredentialVersionData)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Before (org.junit.Before)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONObject (net.minidev.json.JSONObject)1