Search in sources :

Example 31 with CredentialVersion

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));
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) UserContext(org.cloudfoundry.credhub.auth.UserContext) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

Example 32 with CredentialVersion

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);
}
Also used : PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

Example 33 with CredentialVersion

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);
}
Also used : PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

Example 34 with CredentialVersion

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;
}
Also used : CredentialView(org.cloudfoundry.credhub.view.CredentialView) PermissionedCertificateService(org.cloudfoundry.credhub.service.PermissionedCertificateService) BaseCredentialGenerateRequest(org.cloudfoundry.credhub.request.BaseCredentialGenerateRequest) CertificateCredentialsView(org.cloudfoundry.credhub.view.CertificateCredentialsView) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CreateVersionRequest(org.cloudfoundry.credhub.request.CreateVersionRequest) UUID(java.util.UUID) CertificateView(org.cloudfoundry.credhub.view.CertificateView) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Collectors(java.util.stream.Collectors) CertificateCredentialView(org.cloudfoundry.credhub.view.CertificateCredentialView) CertificateRegenerateRequest(org.cloudfoundry.credhub.request.CertificateRegenerateRequest) UpdateTransitionalVersionRequest(org.cloudfoundry.credhub.request.UpdateTransitionalVersionRequest) List(java.util.List) Credential(org.cloudfoundry.credhub.entity.Credential) EventAuditRecordParameters(org.cloudfoundry.credhub.audit.EventAuditRecordParameters) AuditingOperationCode(org.cloudfoundry.credhub.audit.AuditingOperationCode) Service(org.springframework.stereotype.Service) CertificateCredentialValue(org.cloudfoundry.credhub.credential.CertificateCredentialValue) EntryNotFoundException(org.cloudfoundry.credhub.exceptions.EntryNotFoundException) CertificateService(org.cloudfoundry.credhub.service.CertificateService) EntryNotFoundException(org.cloudfoundry.credhub.exceptions.EntryNotFoundException) UUID(java.util.UUID) EventAuditRecordParameters(org.cloudfoundry.credhub.audit.EventAuditRecordParameters) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) CertificateView(org.cloudfoundry.credhub.view.CertificateView)

Example 35 with CredentialVersion

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);
}
Also used : CredentialValue(org.cloudfoundry.credhub.credential.CredentialValue) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion)

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