Search in sources :

Example 1 with CredentialVersion

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

the class CertificateAuthorityService method findActiveVersion.

public CertificateCredentialValue findActiveVersion(String caName) {
    if (!permissionCheckingService.hasPermission(userContextHolder.getUserContext().getActor(), caName, PermissionOperation.READ)) {
        throw new EntryNotFoundException("error.credential.invalid_access");
    }
    CredentialVersion mostRecent = certificateVersionDataService.findActive(caName);
    if (mostRecent == null) {
        throw new EntryNotFoundException("error.credential.invalid_access");
    }
    if (!(mostRecent instanceof CertificateCredentialVersion)) {
        throw new ParameterizedValidationException("error.not_a_ca_name");
    }
    CertificateCredentialVersion certificateCredential = (CertificateCredentialVersion) mostRecent;
    if (!certificateCredential.getParsedCertificate().isCa()) {
        throw new ParameterizedValidationException("error.cert_not_ca");
    }
    return new CertificateCredentialValue(null, certificateCredential.getCertificate(), certificateCredential.getPrivateKey(), null, certificateCredential.isVersionTransitional());
}
Also used : CertificateCredentialValue(org.cloudfoundry.credhub.credential.CertificateCredentialValue) EntryNotFoundException(org.cloudfoundry.credhub.exceptions.EntryNotFoundException) ParameterizedValidationException(org.cloudfoundry.credhub.exceptions.ParameterizedValidationException) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion)

Example 2 with CredentialVersion

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

the class CertificateVersionDataService method findActiveWithTransitional.

public List<CredentialVersion> findActiveWithTransitional(String certificateName) {
    ArrayList<CredentialVersion> result = new ArrayList<>();
    Credential credential = credentialDataService.find(certificateName);
    UUID uuid = credential.getUuid();
    if (credential == null) {
        return null;
    } else {
        CredentialVersionData active = credentialVersionRepository.findLatestNonTransitionalCertificateVersion(uuid);
        if (active != null) {
            result.add(credentialFactory.makeCredentialFromEntity(active));
        }
        CredentialVersionData transitional = credentialVersionRepository.findTransitionalCertificateVersion(uuid);
        if (transitional != null) {
            result.add(credentialFactory.makeCredentialFromEntity(transitional));
        }
        return result;
    }
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) ArrayList(java.util.ArrayList) CertificateCredentialVersionData(org.cloudfoundry.credhub.entity.CertificateCredentialVersionData) CredentialVersionData(org.cloudfoundry.credhub.entity.CredentialVersionData) UUID(java.util.UUID) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion)

Example 3 with CredentialVersion

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

the class PermissionedCertificateServiceTest method getVersions_returnsListWithVersions.

@Test
public void getVersions_returnsListWithVersions() throws Exception {
    CredentialVersion myCredential = mock(CredentialVersion.class);
    when(myCredential.getName()).thenReturn("my-credential");
    CredentialVersion secondVersion = mock(CredentialVersion.class);
    when(secondVersion.getName()).thenReturn("my-credential");
    List<CredentialVersion> versions = newArrayList(myCredential, secondVersion);
    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);
    uuid = UUID.randomUUID();
    when(certificateVersionDataService.findAllVersions(uuid)).thenReturn(versions);
    final List<CredentialVersion> certificates = subject.getVersions(uuid, false, newArrayList());
    assertThat(certificates, equalTo(versions));
}
Also used : UserContext(org.cloudfoundry.credhub.auth.UserContext) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

Example 4 with CredentialVersion

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

the class PermissionedCertificateServiceTest method getVersions_returnsAnError_whenUserDoesntHavePermission.

@Test(expected = EntryNotFoundException.class)
public void getVersions_returnsAnError_whenUserDoesntHavePermission() throws Exception {
    CredentialVersion myCredential = mock(CredentialVersion.class);
    when(myCredential.getName()).thenReturn("my-credential");
    CredentialVersion secondVersion = mock(CredentialVersion.class);
    when(secondVersion.getName()).thenReturn("my-credential");
    List<CredentialVersion> versions = newArrayList(myCredential, secondVersion);
    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(false);
    when(certificateVersionDataService.findAllVersions(uuid)).thenReturn(versions);
    subject.getVersions(uuid, false, newArrayList());
}
Also used : UserContext(org.cloudfoundry.credhub.auth.UserContext) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

Example 5 with CredentialVersion

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

the class PermissionedCredentialServiceTest method getCredentialVersion_whenTheVersionExists_setsCorrectAuditingParametersAndReturnsTheCredential.

@Test
public void getCredentialVersion_whenTheVersionExists_setsCorrectAuditingParametersAndReturnsTheCredential() {
    final CredentialVersion credentialVersionFound = subject.findVersionByUuid(VERSION_UUID_STRING, auditRecordParameters);
    assertThat(credentialVersionFound, equalTo(existingCredentialVersion));
    assertThat(auditRecordParameters, hasSize(1));
    assertThat(auditRecordParameters.get(0).getCredentialName(), equalTo(CREDENTIAL_NAME));
    assertThat(auditRecordParameters.get(0).getAuditingOperationCode(), equalTo(CREDENTIAL_ACCESS));
}
Also used : PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

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