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());
}
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());
}
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));
}
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));
}
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());
}
Aggregations