Search in sources :

Example 1 with Credential

use of org.cloudfoundry.credhub.entity.Credential 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 2 with Credential

use of org.cloudfoundry.credhub.entity.Credential in project credhub by cloudfoundry-incubator.

the class PermissionDataService method getAllowedOperations.

public List<PermissionOperation> getAllowedOperations(String name, String actor) {
    List<PermissionOperation> operations = newArrayList();
    Credential credential = credentialDataService.find(name);
    PermissionData permissionData = permissionRepository.findByCredentialAndActor(credential, actor);
    if (permissionData != null) {
        if (permissionData.hasReadPermission()) {
            operations.add(PermissionOperation.READ);
        }
        if (permissionData.hasWritePermission()) {
            operations.add(PermissionOperation.WRITE);
        }
        if (permissionData.hasDeletePermission()) {
            operations.add(PermissionOperation.DELETE);
        }
        if (permissionData.hasReadAclPermission()) {
            operations.add(PermissionOperation.READ_ACL);
        }
        if (permissionData.hasWriteAclPermission()) {
            operations.add(PermissionOperation.WRITE_ACL);
        }
    }
    return operations;
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) PermissionOperation(org.cloudfoundry.credhub.request.PermissionOperation) PermissionData(org.cloudfoundry.credhub.entity.PermissionData)

Example 3 with Credential

use of org.cloudfoundry.credhub.entity.Credential in project credhub by cloudfoundry-incubator.

the class PermissionDataService method hasPermission.

public boolean hasPermission(String user, String name, PermissionOperation requiredPermission) {
    Credential credential = credentialDataService.find(name);
    final PermissionData permissionData = permissionRepository.findByCredentialAndActor(credential, user);
    return permissionData != null && permissionData.hasPermission(requiredPermission);
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) PermissionData(org.cloudfoundry.credhub.entity.PermissionData)

Example 4 with Credential

use of org.cloudfoundry.credhub.entity.Credential in project credhub by cloudfoundry-incubator.

the class PermissionedCertificateServiceTest method getAllByName_returnsCertificateWithMatchingNameIfCurrentUserHasAccess.

@Test
public void getAllByName_returnsCertificateWithMatchingNameIfCurrentUserHasAccess() throws Exception {
    Credential myCredential = mock(Credential.class);
    when(myCredential.getName()).thenReturn("my-credential");
    Credential otherCredential = mock(Credential.class);
    when(otherCredential.getName()).thenReturn("other-credential");
    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(permissionCheckingService.hasPermission(user, "other-credential", PermissionOperation.READ)).thenReturn(true);
    when(certificateDataService.findByName("my-credential")).thenReturn(myCredential);
    final List<Credential> certificates = subject.getByName("my-credential", newArrayList());
    assertThat(certificates, equalTo(newArrayList(myCredential)));
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) UserContext(org.cloudfoundry.credhub.auth.UserContext) Test(org.junit.Test)

Example 5 with Credential

use of org.cloudfoundry.credhub.entity.Credential in project credhub by cloudfoundry-incubator.

the class PermissionedCertificateServiceTest method getAll_returnsAllCertificatesTheCurrentUserCanAccess.

@Test
public void getAll_returnsAllCertificatesTheCurrentUserCanAccess() throws Exception {
    Credential myCredential = mock(Credential.class);
    when(myCredential.getName()).thenReturn("my-credential");
    Credential yourCredential = mock(Credential.class);
    when(yourCredential.getName()).thenReturn("your-credential");
    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(permissionCheckingService.hasPermission(user, "your-credential", PermissionOperation.READ)).thenReturn(false);
    when(certificateDataService.findAll()).thenReturn(newArrayList(myCredential, yourCredential));
    final List<Credential> certificates = subject.getAll(newArrayList());
    assertThat(certificates, equalTo(newArrayList(myCredential)));
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) UserContext(org.cloudfoundry.credhub.auth.UserContext) Test(org.junit.Test)

Aggregations

Credential (org.cloudfoundry.credhub.entity.Credential)49 Test (org.junit.Test)30 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)15 UUID (java.util.UUID)13 UserContext (org.cloudfoundry.credhub.auth.UserContext)13 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)13 EventAuditRecordParameters (org.cloudfoundry.credhub.audit.EventAuditRecordParameters)9 CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)9 EntryNotFoundException (org.cloudfoundry.credhub.exceptions.EntryNotFoundException)9 EncryptedValue (org.cloudfoundry.credhub.entity.EncryptedValue)7 CertificateCredentialVersionData (org.cloudfoundry.credhub.entity.CertificateCredentialVersionData)5 PasswordCredentialVersionData (org.cloudfoundry.credhub.entity.PasswordCredentialVersionData)5 CertificateCredentialValue (org.cloudfoundry.credhub.credential.CertificateCredentialValue)4 CredentialVersionData (org.cloudfoundry.credhub.entity.CredentialVersionData)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 AuditingOperationCode (org.cloudfoundry.credhub.audit.AuditingOperationCode)3 UserContextHolder (org.cloudfoundry.credhub.auth.UserContextHolder)3 ValueCredentialVersionData (org.cloudfoundry.credhub.entity.ValueCredentialVersionData)3 ParameterizedValidationException (org.cloudfoundry.credhub.exceptions.ParameterizedValidationException)3