Search in sources :

Example 31 with UserContext

use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.

the class PermissionedCertificateServiceTest method deleteVersion_deletesTheProvidedVersion.

@Test
public void deleteVersion_deletesTheProvidedVersion() {
    UUID versionUuid = UUID.randomUUID();
    UUID certificateUuid = UUID.randomUUID();
    CertificateCredentialVersion versionToDelete = mock(CertificateCredentialVersion.class);
    when(certificateVersionDataService.findVersion(versionUuid)).thenReturn(versionToDelete);
    UserContext userContext = mock(UserContext.class);
    when(userContextHolder.getUserContext()).thenReturn(userContext);
    String user = "my-user";
    String credentialName = "my-credential";
    when(userContext.getActor()).thenReturn(user);
    when(permissionCheckingService.hasPermission(user, credentialName, PermissionOperation.DELETE)).thenReturn(true);
    Credential certificate = mock(Credential.class);
    when(certificate.getName()).thenReturn(credentialName);
    when(certificateDataService.findByUuid(certificateUuid)).thenReturn(certificate);
    when(certificate.getUuid()).thenReturn(UUID.randomUUID());
    when(certificateVersionDataService.findVersion(versionUuid)).thenReturn(versionToDelete);
    when(versionToDelete.getCredential()).thenReturn(certificate);
    CertificateCredentialVersion certificateCredentialVersion = subject.deleteVersion(certificateUuid, versionUuid, newArrayList());
    assertThat(certificateCredentialVersion, equalTo(versionToDelete));
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) UserContext(org.cloudfoundry.credhub.auth.UserContext) UUID(java.util.UUID) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) Test(org.junit.Test)

Example 32 with UserContext

use of org.cloudfoundry.credhub.auth.UserContext 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 33 with UserContext

use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.

the class PermissionedCredentialServiceTest method setUp.

@Before
public void setUp() throws Exception {
    initMocks(this);
    userContext = mock(UserContext.class);
    UserContextHolder userContextHolder = new UserContextHolder();
    userContextHolder.setUserContext(userContext);
    subject = new PermissionedCredentialService(credentialVersionDataService, credentialFactory, permissionCheckingService, certificateAuthorityService, userContextHolder, credentialDataService);
    auditRecordParameters = new ArrayList<>();
    generationParameters = mock(StringGenerationParameters.class);
    credentialValue = mock(CredentialValue.class);
    credential = new Credential(CREDENTIAL_NAME);
    accessControlEntries = new ArrayList<>();
    when(userContext.getActor()).thenReturn(USER);
    existingCredentialVersion = new PasswordCredentialVersion(CREDENTIAL_NAME);
    existingCredentialVersion.setEncryptor(encryptor);
    when(permissionCheckingService.hasPermission(USER, CREDENTIAL_NAME, READ)).thenReturn(true);
    when(permissionCheckingService.hasPermission(USER, CREDENTIAL_NAME, WRITE)).thenReturn(true);
    when(credentialDataService.findByUUID(CREDENTIAL_UUID)).thenReturn(credential);
    when(credentialVersionDataService.findByUuid(VERSION_UUID_STRING)).thenReturn(existingCredentialVersion);
    when(request.getName()).thenReturn(CREDENTIAL_NAME);
    when(request.getGenerationParameters()).thenReturn(generationParameters);
    when(request.getAdditionalPermissions()).thenReturn(accessControlEntries);
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) CredentialValue(org.cloudfoundry.credhub.credential.CredentialValue) UserContext(org.cloudfoundry.credhub.auth.UserContext) UserContextHolder(org.cloudfoundry.credhub.auth.UserContextHolder) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) StringGenerationParameters(org.cloudfoundry.credhub.request.StringGenerationParameters) Before(org.junit.Before)

Example 34 with UserContext

use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.

the class AuditInterceptorTest method setup.

@Before
public void setup() {
    requestAuditRecordDataService = mock(RequestAuditRecordDataService.class);
    securityEventsLogService = mock(SecurityEventsLogService.class);
    auditLogFactory = mock(AuditLogFactory.class);
    userContextFactory = mock(UserContextFactory.class);
    userContext = mock(UserContext.class);
    requestUuid = new RequestUuid();
    subject = new AuditInterceptor(requestAuditRecordDataService, securityEventsLogService, auditLogFactory, userContextFactory);
    request = mock(HttpServletRequest.class);
    response = mock(HttpServletResponse.class);
    final Authentication authentication = mock(Authentication.class);
    when(request.getUserPrincipal()).thenReturn(authentication);
    userContext = mock(UserContext.class);
    when(userContextFactory.createUserContext(any())).thenReturn(userContext);
    when(userContext.getActor()).thenReturn("user");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserContext(org.cloudfoundry.credhub.auth.UserContext) Authentication(org.springframework.security.core.Authentication) AuditLogFactory(org.cloudfoundry.credhub.audit.AuditLogFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) UserContextFactory(org.cloudfoundry.credhub.auth.UserContextFactory) RequestAuditRecordDataService(org.cloudfoundry.credhub.data.RequestAuditRecordDataService) RequestUuid(org.cloudfoundry.credhub.audit.RequestUuid) SecurityEventsLogService(org.cloudfoundry.credhub.service.SecurityEventsLogService) Before(org.junit.Before)

Example 35 with UserContext

use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.

the class CertificateAuthorityServiceTest method beforeEach.

@Before
public void beforeEach() {
    certificate = new CertificateCredentialValue(null, CertificateStringConstants.SELF_SIGNED_CA_CERT, "my-key", null);
    certificateCredential = mock(CertificateCredentialVersion.class);
    permissionCheckingService = mock(PermissionCheckingService.class);
    userContext = mock(UserContext.class);
    when(userContext.getActor()).thenReturn(USER_NAME);
    when(certificateCredential.getName()).thenReturn(CREDENTIAL_NAME);
    when(permissionCheckingService.hasPermission(USER_NAME, CREDENTIAL_NAME, PermissionOperation.READ)).thenReturn(true);
    certificateVersionDataService = mock(CertificateVersionDataService.class);
    UserContextHolder userContextHolder = new UserContextHolder();
    userContextHolder.setUserContext(userContext);
    certificateAuthorityService = new CertificateAuthorityService(certificateVersionDataService, permissionCheckingService, userContextHolder);
}
Also used : PermissionCheckingService(org.cloudfoundry.credhub.service.PermissionCheckingService) CertificateCredentialValue(org.cloudfoundry.credhub.credential.CertificateCredentialValue) UserContext(org.cloudfoundry.credhub.auth.UserContext) UserContextHolder(org.cloudfoundry.credhub.auth.UserContextHolder) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) Before(org.junit.Before)

Aggregations

UserContext (org.cloudfoundry.credhub.auth.UserContext)37 Test (org.junit.Test)21 UUID (java.util.UUID)16 Before (org.junit.Before)14 Credential (org.cloudfoundry.credhub.entity.Credential)13 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)9 UserContextHolder (org.cloudfoundry.credhub.auth.UserContextHolder)7 EventAuditRecord (org.cloudfoundry.credhub.entity.EventAuditRecord)6 CertificateCredentialValue (org.cloudfoundry.credhub.credential.CertificateCredentialValue)4 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)4 StringGenerationParameters (org.cloudfoundry.credhub.request.StringGenerationParameters)4 PermissionCheckingService (org.cloudfoundry.credhub.service.PermissionCheckingService)4 CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)3 PermissionedCredentialService (org.cloudfoundry.credhub.service.PermissionedCredentialService)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 UserContextFactory (org.cloudfoundry.credhub.auth.UserContextFactory)2 CertificateAuthorityService (org.cloudfoundry.credhub.data.CertificateAuthorityService)2 PermissionDataService (org.cloudfoundry.credhub.data.PermissionDataService)2 CertificateGenerationParameters (org.cloudfoundry.credhub.domain.CertificateGenerationParameters)2 Encryptor (org.cloudfoundry.credhub.domain.Encryptor)2