use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method getVersions_withCurrentTrue_returnsCurrentVersions.
@Test
public void getVersions_withCurrentTrue_returnsCurrentVersions() throws Exception {
Credential aCredential = new Credential("my-credential");
CredentialVersion credentialVersion1 = mock(CredentialVersion.class);
when(credentialVersion1.getName()).thenReturn("my-credential");
CredentialVersion credentialVersion2 = mock(CredentialVersion.class);
when(credentialVersion2.getName()).thenReturn("my-credential");
List<CredentialVersion> versions = newArrayList(credentialVersion1, credentialVersion2);
UserContext userContext = mock(UserContext.class);
when(userContextHolder.getUserContext()).thenReturn(userContext);
String user = "my-user";
when(userContext.getActor()).thenReturn(user);
when(permissionCheckingService.hasPermission(user, "my-credential", PermissionOperation.READ)).thenReturn(true);
when(certificateDataService.findByUuid(uuid)).thenReturn(aCredential);
when(certificateVersionDataService.findActiveWithTransitional("my-credential")).thenReturn(versions);
final List<CredentialVersion> certificates = subject.getVersions(uuid, true, newArrayList());
assertThat(certificates, equalTo(versions));
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class PermissionedCredentialServiceTest method save_whenThereIsAnExistingCredentialAndOverwriteModeIsConvergeAndParametersAreSame_DoesNotOverwriteCredential.
@Test
public void save_whenThereIsAnExistingCredentialAndOverwriteModeIsConvergeAndParametersAreSame_DoesNotOverwriteCredential() {
when(request.getType()).thenReturn("password");
when(request.getOverwriteMode()).thenReturn(CredentialWriteMode.CONVERGE.mode);
when(credentialVersionDataService.save(any(CredentialVersion.class))).thenReturn(new PasswordCredentialVersion().setEncryptor(encryptor));
final PasswordCredentialVersion newVersion = new PasswordCredentialVersion();
CredentialVersion originalCredentialVersion = mock(CredentialVersion.class);
when(originalCredentialVersion.matchesGenerationParameters(generationParameters)).thenReturn(true);
when(credentialVersionDataService.findMostRecent(CREDENTIAL_NAME)).thenReturn(originalCredentialVersion);
when(originalCredentialVersion.getCredentialType()).thenReturn("password");
when(credentialFactory.makeNewCredentialVersion(CredentialType.valueOf("password"), CREDENTIAL_NAME, credentialValue, originalCredentialVersion, generationParameters)).thenReturn(newVersion);
subject.save(originalCredentialVersion, credentialValue, request, auditRecordParameters);
verify(credentialVersionDataService, never()).save(newVersion);
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class PermissionedCredentialServiceTest method save_whenThereIsAnExistingCredentialAndOverwriteModeIsConvergeAndParametersAreDifferent_OverwritesCredential.
@Test
public void save_whenThereIsAnExistingCredentialAndOverwriteModeIsConvergeAndParametersAreDifferent_OverwritesCredential() {
when(request.getType()).thenReturn("password");
when(request.getOverwriteMode()).thenReturn(CredentialWriteMode.CONVERGE.mode);
when(credentialVersionDataService.save(any(CredentialVersion.class))).thenReturn(new PasswordCredentialVersion().setEncryptor(encryptor));
final PasswordCredentialVersion newVersion = new PasswordCredentialVersion();
CredentialVersion originalCredentialVersion = mock(CredentialVersion.class);
when(originalCredentialVersion.matchesGenerationParameters(generationParameters)).thenReturn(false);
when(credentialVersionDataService.findMostRecent(CREDENTIAL_NAME)).thenReturn(originalCredentialVersion);
when(originalCredentialVersion.getCredentialType()).thenReturn("password");
when(credentialFactory.makeNewCredentialVersion(CredentialType.valueOf("password"), CREDENTIAL_NAME, credentialValue, originalCredentialVersion, generationParameters)).thenReturn(newVersion);
subject.save(originalCredentialVersion, credentialValue, request, auditRecordParameters);
verify(credentialVersionDataService).save(newVersion);
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class CertificatesHandler method handleGetAllVersionsRequest.
public List<CertificateView> handleGetAllVersionsRequest(String uuidString, List<EventAuditRecordParameters> auditRecordParameters, boolean current) {
UUID uuid;
try {
uuid = UUID.fromString(uuidString);
} catch (IllegalArgumentException e) {
auditRecordParameters.add(new EventAuditRecordParameters(AuditingOperationCode.CREDENTIAL_ACCESS, null));
throw new EntryNotFoundException("error.credential.invalid_access");
}
final List<CredentialVersion> credentialList = permissionedCertificateService.getVersions(uuid, current, auditRecordParameters);
List<CertificateView> list = credentialList.stream().map(credential -> new CertificateView((CertificateCredentialVersion) credential)).collect(Collectors.toList());
return list;
}
use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.
the class GenerateHandler method handle.
public CredentialView handle(BaseCredentialGenerateRequest generateRequest, List<EventAuditRecordParameters> auditRecordParameters) {
CredentialVersion existingCredentialVersion = credentialService.findMostRecent(generateRequest.getName());
CredentialValue value = credentialGenerator.generate(generateRequest);
final CredentialVersion credentialVersion = credentialService.save(existingCredentialVersion, value, generateRequest, auditRecordParameters);
final boolean isNewCredential = existingCredentialVersion == null;
if (isNewCredential || generateRequest.isOverwrite()) {
permissionService.savePermissions(credentialVersion, generateRequest.getAdditionalPermissions(), auditRecordParameters, isNewCredential, generateRequest.getName());
}
return CredentialView.fromEntity(credentialVersion);
}
Aggregations