use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class CredentialsHandlerTest method getAllCredentialVersions_whenTheCredentialExists_returnsADataResponse.
@Test
public void getAllCredentialVersions_whenTheCredentialExists_returnsADataResponse() {
List<CredentialVersion> credentials = newArrayList(version1, version2);
when(permissionedCredentialService.findAllByName(eq(CREDENTIAL_NAME), eq(auditRecordParametersList))).thenReturn(credentials);
when(permissionCheckingService.hasPermission(USER, CREDENTIAL_NAME, PermissionOperation.READ)).thenReturn(true);
DataResponse credentialVersions = subject.getAllCredentialVersions(CREDENTIAL_NAME, auditRecordParametersList);
List<CredentialView> credentialViews = credentialVersions.getData();
assertThat(credentialViews, hasSize(2));
assertThat(credentialViews.get(0).getName(), equalTo(CREDENTIAL_NAME));
assertThat(credentialViews.get(0).getVersionCreatedAt(), equalTo(VERSION1_CREATED_AT));
assertThat(credentialViews.get(1).getName(), equalTo(CREDENTIAL_NAME));
assertThat(credentialViews.get(1).getVersionCreatedAt(), equalTo(VERSION2_CREATED_AT));
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class RegenerateHandlerTest method handleBulkRegenerate_regeneratesToNestedLevels.
@Test
public void handleBulkRegenerate_regeneratesToNestedLevels() throws Exception {
when(credentialService.findAllCertificateCredentialsByCaName(SIGNER_NAME)).thenReturn(newArrayList("/firstExpectedName", "/secondExpectedName"));
when(credentialService.findAllCertificateCredentialsByCaName("/firstExpectedName")).thenReturn(newArrayList("/thirdExpectedName", "/fourthExpectedName"));
when(credentialService.findMostRecent(anyString())).thenReturn(mock(CredentialVersion.class));
CredentialVersion credentialVersion = mock(CredentialVersion.class);
when(credentialService.save(anyObject(), anyObject(), anyObject(), anyList())).thenReturn(credentialVersion);
when(credentialVersion.getName()).thenReturn("placeholder");
CertificateGenerateRequest generateRequest1 = mock(CertificateGenerateRequest.class);
when(generateRequest1.getName()).thenReturn("/firstExpectedName");
CertificateGenerationParameters generationParams1 = mock(CertificateGenerationParameters.class);
when(generationParams1.isCa()).thenReturn(true);
when(generateRequest1.getGenerationParameters()).thenReturn(generationParams1);
CertificateGenerateRequest generateRequest2 = mock(CertificateGenerateRequest.class);
when(generateRequest2.getName()).thenReturn("/secondExpectedName");
CertificateGenerationParameters generationParams2 = mock(CertificateGenerationParameters.class);
when(generationParams2.isCa()).thenReturn(false);
when(generateRequest2.getGenerationParameters()).thenReturn(generationParams2);
CertificateGenerateRequest generateRequest3 = mock(CertificateGenerateRequest.class);
when(generateRequest3.getName()).thenReturn("/thirdExpectedName");
CertificateGenerationParameters generationParams3 = mock(CertificateGenerationParameters.class);
when(generationParams3.isCa()).thenReturn(false);
when(generateRequest3.getGenerationParameters()).thenReturn(generationParams3);
CertificateGenerateRequest generateRequest4 = mock(CertificateGenerateRequest.class);
when(generateRequest4.getName()).thenReturn("/fourthExpectedName");
CertificateGenerationParameters generationParams4 = mock(CertificateGenerationParameters.class);
when(generationParams4.isCa()).thenReturn(false);
when(generateRequest4.getGenerationParameters()).thenReturn(generationParams4);
when(generationRequestGenerator.createGenerateRequest(any(CredentialVersion.class), any(String.class), any(List.class))).thenReturn(generateRequest1).thenReturn(generateRequest3).thenReturn(generateRequest4).thenReturn(generateRequest2);
subject.handleBulkRegenerate(SIGNER_NAME, newArrayList());
verify(credentialService).save(any(), any(), eq(generateRequest1), any());
verify(credentialService).save(any(), any(), eq(generateRequest3), any());
verify(credentialService).save(any(), any(), eq(generateRequest4), any());
verify(credentialService).save(any(), any(), eq(generateRequest2), any());
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class RegenerateHandlerTest method handleBulkRegenerate_regeneratesEverythingInTheList.
@Test
public void handleBulkRegenerate_regeneratesEverythingInTheList() throws Exception {
when(credentialService.findAllCertificateCredentialsByCaName(SIGNER_NAME)).thenReturn(newArrayList("firstExpectedName", "secondExpectedName"));
when(credentialService.findMostRecent(anyString())).thenReturn(mock(CredentialVersion.class));
CredentialVersion credentialVersion = mock(CertificateCredentialVersion.class);
when(credentialService.save(anyObject(), anyObject(), anyObject(), anyList())).thenReturn(credentialVersion);
when(credentialVersion.getName()).thenReturn("someName");
CertificateGenerateRequest generateRequest1 = mock(CertificateGenerateRequest.class);
generateRequest1.setName("/firstExpectedName");
when(generateRequest1.getName()).thenReturn("/firstExpectedName");
CertificateGenerationParameters generationParams1 = mock(CertificateGenerationParameters.class);
when(generationParams1.isCa()).thenReturn(true);
when(generateRequest1.getGenerationParameters()).thenReturn(generationParams1);
CertificateGenerateRequest generateRequest2 = mock(CertificateGenerateRequest.class);
when(generateRequest2.getName()).thenReturn("/secondExpectedName");
CertificateGenerationParameters generationParams2 = mock(CertificateGenerationParameters.class);
when(generationParams2.isCa()).thenReturn(false);
when(generateRequest2.getGenerationParameters()).thenReturn(generationParams2);
when(generationRequestGenerator.createGenerateRequest(any(CredentialVersion.class), any(String.class), any(List.class))).thenReturn(generateRequest1).thenReturn(generateRequest2);
subject.handleBulkRegenerate(SIGNER_NAME, newArrayList());
verify(credentialService).save(any(), any(), eq(generateRequest1), any());
verify(credentialService).save(any(), any(), eq(generateRequest2), any());
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class CertificateServiceTest method findByUuid_ThrowsEntryNotFoundIfUuidMatchesNonCertificateCredential.
@Test(expected = EntryNotFoundException.class)
public void findByUuid_ThrowsEntryNotFoundIfUuidMatchesNonCertificateCredential() {
CredentialVersion credentialVersion = new RsaCredentialVersion();
when(certificateVersionDataService.findByCredentialUUID("rsaUuid")).thenReturn(null);
subject.findByCredentialUuid("rsaUuid", auditRecordParameters);
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class InterpolationHandler method interpolateCredHubReferences.
public Map<String, Object> interpolateCredHubReferences(Map<String, Object> servicesMap, List<EventAuditRecordParameters> auditRecordParameters) {
for (Object serviceProperties : servicesMap.values()) {
if (serviceProperties == null || !(serviceProperties instanceof ArrayList)) {
continue;
}
for (Object properties : (ArrayList) serviceProperties) {
if (!(properties instanceof Map)) {
continue;
}
Map<String, Object> propertiesMap = (Map) properties;
Object credentials = propertiesMap.get("credentials");
if (credentials == null || !(credentials instanceof Map)) {
continue;
}
// Allow either snake_case or kebab-case
Object credhubRef = ((Map) credentials).get("credhub_ref");
if (credhubRef == null) {
credhubRef = ((Map) credentials).get("credhub-ref");
}
if (credhubRef == null || !(credhubRef instanceof String)) {
continue;
}
String credentialName = getCredentialNameFromRef((String) credhubRef);
List<CredentialVersion> credentialVersions = credentialService.findNByName(credentialName, 1, auditRecordParameters);
if (credentialVersions.isEmpty()) {
throw new EntryNotFoundException("error.credential.invalid_access");
}
CredentialVersion credentialVersion = credentialVersions.get(0);
if (credentialVersion instanceof JsonCredentialVersion) {
propertiesMap.put("credentials", ((JsonCredentialVersion) credentialVersion).getValue());
} else {
throw new ParameterizedValidationException("error.interpolation.invalid_type", credentialName);
}
}
}
return servicesMap;
}
Aggregations