use of org.cloudfoundry.credhub.entity.Credential 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));
}
use of org.cloudfoundry.credhub.entity.Credential 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));
}
use of org.cloudfoundry.credhub.entity.Credential 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);
}
use of org.cloudfoundry.credhub.entity.Credential in project credhub by cloudfoundry-incubator.
the class CredentialVersionDataRepositoryTest method canSaveStringsOfLength7000WhichMeans7016ForGCM.
@Test
public void canSaveStringsOfLength7000WhichMeans7016ForGCM() {
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);
ValueCredentialVersionData entity = new ValueCredentialVersionData();
Credential credential = credentialRepository.save(new Credential(name));
entity.setCredential(credential);
entity.setEncryptedValueData(new EncryptedValue().setEncryptedValue(encryptedValue).setEncryptionKeyUuid(canaryUuid).setNonce("nonce".getBytes()));
subject.save(entity);
assertThat(subject.findFirstByCredentialUuidOrderByVersionCreatedAtDesc(credential.getUuid()).getEncryptedValueData().getEncryptedValue().length, equalTo(7016));
}
use of org.cloudfoundry.credhub.entity.Credential in project credhub by cloudfoundry-incubator.
the class CertificatesHandler method handleGetAllVersionsRequest.
public List<CertificateView> handleGetAllVersionsRequest(String uuidString, List<EventAuditRecordParameters> auditRecordParameters, boolean current) {
UUID uuid;
try {
uuid = UUID.fromString(uuidString);
} catch (IllegalArgumentException e) {
auditRecordParameters.add(new EventAuditRecordParameters(AuditingOperationCode.CREDENTIAL_ACCESS, null));
throw new EntryNotFoundException("error.credential.invalid_access");
}
final List<CredentialVersion> credentialList = permissionedCertificateService.getVersions(uuid, current, auditRecordParameters);
List<CertificateView> list = credentialList.stream().map(credential -> new CertificateView((CertificateCredentialVersion) credential)).collect(Collectors.toList());
return list;
}
Aggregations