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)));
}
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);
}
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/")));
}
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"));
}
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()));
}
Aggregations