Search in sources :

Example 6 with Credential

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

the class PermissionedCertificateServiceTest method deleteVersion_whenTheUserDoesNotHavePermission_returnsAnError.

@Test(expected = EntryNotFoundException.class)
public void deleteVersion_whenTheUserDoesNotHavePermission_returnsAnError() throws Exception {
    UUID versionUuid = UUID.randomUUID();
    UUID certificateUuid = UUID.randomUUID();
    UserContext userContext = mock(UserContext.class);
    when(userContextHolder.getUserContext()).thenReturn(userContext);
    String user = "my-user";
    when(userContext.getActor()).thenReturn(user);
    String credentialName = "my-credential";
    when(permissionCheckingService.hasPermission(user, credentialName, PermissionOperation.DELETE)).thenReturn(false);
    Credential certificate = mock(Credential.class);
    when(certificate.getName()).thenReturn(credentialName);
    when(certificateDataService.findByUuid(certificateUuid)).thenReturn(certificate);
    CertificateCredentialVersion versionToDelete = mock(CertificateCredentialVersion.class);
    when(certificate.getUuid()).thenReturn(UUID.randomUUID());
    when(certificateVersionDataService.findVersion(versionUuid)).thenReturn(versionToDelete);
    when(versionToDelete.getCredential()).thenReturn(certificate);
    subject.deleteVersion(certificateUuid, versionUuid, newArrayList());
}
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 7 with Credential

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

the class PermissionedCertificateServiceTest method set_whenTheUserDoesNotHavePermission_throwsAnException.

@Test(expected = EntryNotFoundException.class)
public void set_whenTheUserDoesNotHavePermission_throwsAnException() {
    UUID certificateUuid = UUID.randomUUID();
    String credentialName = "my-credential";
    Credential certificate = mock(Credential.class);
    when(certificate.getName()).thenReturn(credentialName);
    String user = "my-user";
    UserContext userContext = mock(UserContext.class);
    when(userContextHolder.getUserContext()).thenReturn(userContext);
    when(userContext.getActor()).thenReturn(user);
    when(certificateDataService.findByUuid(certificateUuid)).thenReturn(certificate);
    when(permissionCheckingService.hasPermission(user, credentialName, PermissionOperation.WRITE)).thenReturn(false);
    subject.set(certificateUuid, mock(CertificateCredentialValue.class), newArrayList());
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) UserContext(org.cloudfoundry.credhub.auth.UserContext) CertificateCredentialValue(org.cloudfoundry.credhub.credential.CertificateCredentialValue) UUID(java.util.UUID) Test(org.junit.Test)

Example 8 with Credential

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

the class EncryptionKeyRotatorTest method rotation_canRotateCertificateCredentials.

@Test
public void rotation_canRotateCertificateCredentials() throws Exception {
    String certificateName = name + "-certificate";
    MockHttpServletRequestBuilder post = post("/api/v1/data").header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "  \"name\": \"" + certificateName + "\"," + "  \"type\": \"certificate\"," + "  \"parameters\": { " + "    \"is_ca\": true,\n" + "    \"common_name\": \"Pivotal CA\"" + "  }" + "}");
    String content = this.mockMvc.perform(post).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
    String originalCert = parse(content).get("value").get("private_key").textValue();
    Credential credential = credentialDataService.find(certificateName);
    final byte[] firstEncryption = credentialVersionRepository.findAllByCredentialUuidOrderByVersionCreatedAtDesc(credential.getUuid()).get(0).getEncryptedValueData().getEncryptedValue().clone();
    setActiveKey(1);
    encryptionKeyRotator.rotate();
    final CertificateCredentialVersionData secondEncryption = (CertificateCredentialVersionData) credentialVersionRepository.findAllByCredentialUuidOrderByVersionCreatedAtDesc(credential.getUuid()).get(0);
    assertThat(firstEncryption, not(equalTo(secondEncryption.getEncryptedValueData().getEncryptedValue())));
    final MockHttpServletRequestBuilder get = get("/api/v1/data?name=" + certificateName).header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN);
    this.mockMvc.perform(get).andExpect(status().isOk()).andExpect(jsonPath("$.data[0].value.private_key").value(originalCert));
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) CertificateCredentialVersionData(org.cloudfoundry.credhub.entity.CertificateCredentialVersionData) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 9 with Credential

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

the class CredentialVersionDataRepositoryTest method canSaveCertificatesOfLength7000WhichMeans7016ForGCM.

@Test
public void canSaveCertificatesOfLength7000WhichMeans7016ForGCM() {
    byte[] encryptedValue = new byte[7016];
    Arrays.fill(encryptedValue, (byte) 'A');
    final StringBuilder stringBuilder = new StringBuilder(7000);
    Stream.generate(() -> "a").limit(stringBuilder.capacity()).forEach(stringBuilder::append);
    Credential credential = credentialRepository.save(new Credential(name));
    final String longString = stringBuilder.toString();
    CertificateCredentialVersionData entity = new CertificateCredentialVersionData();
    entity.setCredential(credential);
    entity.setCa(longString);
    entity.setCertificate(longString);
    entity.setEncryptedValueData(new EncryptedValue().setEncryptionKeyUuid(canaryUuid).setEncryptedValue(encryptedValue).setNonce("nonce".getBytes()));
    subject.save(entity);
    CertificateCredentialVersionData credentialData = (CertificateCredentialVersionData) subject.findFirstByCredentialUuidOrderByVersionCreatedAtDesc(credential.getUuid());
    assertThat(credentialData.getCa().length(), equalTo(7000));
    assertThat(credentialData.getCertificate().length(), equalTo(7000));
    assertThat(credentialData.getEncryptedValueData().getEncryptedValue(), equalTo(encryptedValue));
    assertThat(credentialData.getEncryptedValueData().getEncryptedValue().length, equalTo(7016));
}
Also used : Credential(org.cloudfoundry.credhub.entity.Credential) CertificateCredentialVersionData(org.cloudfoundry.credhub.entity.CertificateCredentialVersionData) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Example 10 with Credential

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

the class PermissionedCertificateService method getAll.

public List<Credential> getAll(List<EventAuditRecordParameters> auditRecordParameters) {
    auditRecordParameters.add(new EventAuditRecordParameters(AuditingOperationCode.CREDENTIAL_FIND, null));
    final List<Credential> allCertificates = certificateDataService.findAll();
    return allCertificates.stream().filter(credential -> permissionCheckingService.hasPermission(userContextHolder.getUserContext().getActor(), credential.getName(), PermissionOperation.READ)).collect(Collectors.toList());
}
Also used : ParameterizedValidationException(org.cloudfoundry.credhub.exceptions.ParameterizedValidationException) BaseCredentialGenerateRequest(org.cloudfoundry.credhub.request.BaseCredentialGenerateRequest) PermissionOperation(org.cloudfoundry.credhub.request.PermissionOperation) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) Autowired(org.springframework.beans.factory.annotation.Autowired) UUID(java.util.UUID) CertificateDataService(org.cloudfoundry.credhub.data.CertificateDataService) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Collectors(java.util.stream.Collectors) UserContextHolder(org.cloudfoundry.credhub.auth.UserContextHolder) List(java.util.List) CertificateVersionDataService(org.cloudfoundry.credhub.data.CertificateVersionDataService) Credential(org.cloudfoundry.credhub.entity.Credential) EventAuditRecordParameters(org.cloudfoundry.credhub.audit.EventAuditRecordParameters) InvalidQueryParameterException(org.cloudfoundry.credhub.exceptions.InvalidQueryParameterException) AuditingOperationCode(org.cloudfoundry.credhub.audit.AuditingOperationCode) CredentialVersionDataService(org.cloudfoundry.credhub.data.CredentialVersionDataService) Service(org.springframework.stereotype.Service) CertificateCredentialValue(org.cloudfoundry.credhub.credential.CertificateCredentialValue) Collections(java.util.Collections) EntryNotFoundException(org.cloudfoundry.credhub.exceptions.EntryNotFoundException) CertificateCredentialFactory(org.cloudfoundry.credhub.domain.CertificateCredentialFactory) Credential(org.cloudfoundry.credhub.entity.Credential) EventAuditRecordParameters(org.cloudfoundry.credhub.audit.EventAuditRecordParameters)

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