Search in sources :

Example 6 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class CredentialsHandlerTest method getAllCredentialVersions_whenTheCredentialExists_returnsADataResponse.

@Test
public void getAllCredentialVersions_whenTheCredentialExists_returnsADataResponse() {
    List<CredentialVersion> credentials = newArrayList(version1, version2);
    when(permissionedCredentialService.findAllByName(eq(CREDENTIAL_NAME), eq(auditRecordParametersList))).thenReturn(credentials);
    when(permissionCheckingService.hasPermission(USER, CREDENTIAL_NAME, PermissionOperation.READ)).thenReturn(true);
    DataResponse credentialVersions = subject.getAllCredentialVersions(CREDENTIAL_NAME, auditRecordParametersList);
    List<CredentialView> credentialViews = credentialVersions.getData();
    assertThat(credentialViews, hasSize(2));
    assertThat(credentialViews.get(0).getName(), equalTo(CREDENTIAL_NAME));
    assertThat(credentialViews.get(0).getVersionCreatedAt(), equalTo(VERSION1_CREATED_AT));
    assertThat(credentialViews.get(1).getName(), equalTo(CREDENTIAL_NAME));
    assertThat(credentialViews.get(1).getVersionCreatedAt(), equalTo(VERSION2_CREATED_AT));
}
Also used : DataResponse(org.cloudfoundry.credhub.view.DataResponse) CredentialView(org.cloudfoundry.credhub.view.CredentialView) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

Example 7 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class RegenerateHandlerTest method handleBulkRegenerate_regeneratesToNestedLevels.

@Test
public void handleBulkRegenerate_regeneratesToNestedLevels() throws Exception {
    when(credentialService.findAllCertificateCredentialsByCaName(SIGNER_NAME)).thenReturn(newArrayList("/firstExpectedName", "/secondExpectedName"));
    when(credentialService.findAllCertificateCredentialsByCaName("/firstExpectedName")).thenReturn(newArrayList("/thirdExpectedName", "/fourthExpectedName"));
    when(credentialService.findMostRecent(anyString())).thenReturn(mock(CredentialVersion.class));
    CredentialVersion credentialVersion = mock(CredentialVersion.class);
    when(credentialService.save(anyObject(), anyObject(), anyObject(), anyList())).thenReturn(credentialVersion);
    when(credentialVersion.getName()).thenReturn("placeholder");
    CertificateGenerateRequest generateRequest1 = mock(CertificateGenerateRequest.class);
    when(generateRequest1.getName()).thenReturn("/firstExpectedName");
    CertificateGenerationParameters generationParams1 = mock(CertificateGenerationParameters.class);
    when(generationParams1.isCa()).thenReturn(true);
    when(generateRequest1.getGenerationParameters()).thenReturn(generationParams1);
    CertificateGenerateRequest generateRequest2 = mock(CertificateGenerateRequest.class);
    when(generateRequest2.getName()).thenReturn("/secondExpectedName");
    CertificateGenerationParameters generationParams2 = mock(CertificateGenerationParameters.class);
    when(generationParams2.isCa()).thenReturn(false);
    when(generateRequest2.getGenerationParameters()).thenReturn(generationParams2);
    CertificateGenerateRequest generateRequest3 = mock(CertificateGenerateRequest.class);
    when(generateRequest3.getName()).thenReturn("/thirdExpectedName");
    CertificateGenerationParameters generationParams3 = mock(CertificateGenerationParameters.class);
    when(generationParams3.isCa()).thenReturn(false);
    when(generateRequest3.getGenerationParameters()).thenReturn(generationParams3);
    CertificateGenerateRequest generateRequest4 = mock(CertificateGenerateRequest.class);
    when(generateRequest4.getName()).thenReturn("/fourthExpectedName");
    CertificateGenerationParameters generationParams4 = mock(CertificateGenerationParameters.class);
    when(generationParams4.isCa()).thenReturn(false);
    when(generateRequest4.getGenerationParameters()).thenReturn(generationParams4);
    when(generationRequestGenerator.createGenerateRequest(any(CredentialVersion.class), any(String.class), any(List.class))).thenReturn(generateRequest1).thenReturn(generateRequest3).thenReturn(generateRequest4).thenReturn(generateRequest2);
    subject.handleBulkRegenerate(SIGNER_NAME, newArrayList());
    verify(credentialService).save(any(), any(), eq(generateRequest1), any());
    verify(credentialService).save(any(), any(), eq(generateRequest3), any());
    verify(credentialService).save(any(), any(), eq(generateRequest4), any());
    verify(credentialService).save(any(), any(), eq(generateRequest2), any());
}
Also used : CertificateGenerateRequest(org.cloudfoundry.credhub.request.CertificateGenerateRequest) CertificateGenerationParameters(org.cloudfoundry.credhub.domain.CertificateGenerationParameters) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) Test(org.junit.Test)

Example 8 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class RegenerateHandlerTest method handleBulkRegenerate_regeneratesEverythingInTheList.

@Test
public void handleBulkRegenerate_regeneratesEverythingInTheList() throws Exception {
    when(credentialService.findAllCertificateCredentialsByCaName(SIGNER_NAME)).thenReturn(newArrayList("firstExpectedName", "secondExpectedName"));
    when(credentialService.findMostRecent(anyString())).thenReturn(mock(CredentialVersion.class));
    CredentialVersion credentialVersion = mock(CertificateCredentialVersion.class);
    when(credentialService.save(anyObject(), anyObject(), anyObject(), anyList())).thenReturn(credentialVersion);
    when(credentialVersion.getName()).thenReturn("someName");
    CertificateGenerateRequest generateRequest1 = mock(CertificateGenerateRequest.class);
    generateRequest1.setName("/firstExpectedName");
    when(generateRequest1.getName()).thenReturn("/firstExpectedName");
    CertificateGenerationParameters generationParams1 = mock(CertificateGenerationParameters.class);
    when(generationParams1.isCa()).thenReturn(true);
    when(generateRequest1.getGenerationParameters()).thenReturn(generationParams1);
    CertificateGenerateRequest generateRequest2 = mock(CertificateGenerateRequest.class);
    when(generateRequest2.getName()).thenReturn("/secondExpectedName");
    CertificateGenerationParameters generationParams2 = mock(CertificateGenerationParameters.class);
    when(generationParams2.isCa()).thenReturn(false);
    when(generateRequest2.getGenerationParameters()).thenReturn(generationParams2);
    when(generationRequestGenerator.createGenerateRequest(any(CredentialVersion.class), any(String.class), any(List.class))).thenReturn(generateRequest1).thenReturn(generateRequest2);
    subject.handleBulkRegenerate(SIGNER_NAME, newArrayList());
    verify(credentialService).save(any(), any(), eq(generateRequest1), any());
    verify(credentialService).save(any(), any(), eq(generateRequest2), any());
}
Also used : CertificateGenerateRequest(org.cloudfoundry.credhub.request.CertificateGenerateRequest) CertificateGenerationParameters(org.cloudfoundry.credhub.domain.CertificateGenerationParameters) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) Test(org.junit.Test)

Example 9 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class CertificateServiceTest method findByUuid_ThrowsEntryNotFoundIfUuidMatchesNonCertificateCredential.

@Test(expected = EntryNotFoundException.class)
public void findByUuid_ThrowsEntryNotFoundIfUuidMatchesNonCertificateCredential() {
    CredentialVersion credentialVersion = new RsaCredentialVersion();
    when(certificateVersionDataService.findByCredentialUUID("rsaUuid")).thenReturn(null);
    subject.findByCredentialUuid("rsaUuid", auditRecordParameters);
}
Also used : RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) Test(org.junit.Test)

Example 10 with CredentialVersion

use of org.cloudfoundry.credhub.domain.CredentialVersion in project credhub by cloudfoundry-incubator.

the class InterpolationHandler method interpolateCredHubReferences.

public Map<String, Object> interpolateCredHubReferences(Map<String, Object> servicesMap, List<EventAuditRecordParameters> auditRecordParameters) {
    for (Object serviceProperties : servicesMap.values()) {
        if (serviceProperties == null || !(serviceProperties instanceof ArrayList)) {
            continue;
        }
        for (Object properties : (ArrayList) serviceProperties) {
            if (!(properties instanceof Map)) {
                continue;
            }
            Map<String, Object> propertiesMap = (Map) properties;
            Object credentials = propertiesMap.get("credentials");
            if (credentials == null || !(credentials instanceof Map)) {
                continue;
            }
            // Allow either snake_case or kebab-case
            Object credhubRef = ((Map) credentials).get("credhub_ref");
            if (credhubRef == null) {
                credhubRef = ((Map) credentials).get("credhub-ref");
            }
            if (credhubRef == null || !(credhubRef instanceof String)) {
                continue;
            }
            String credentialName = getCredentialNameFromRef((String) credhubRef);
            List<CredentialVersion> credentialVersions = credentialService.findNByName(credentialName, 1, auditRecordParameters);
            if (credentialVersions.isEmpty()) {
                throw new EntryNotFoundException("error.credential.invalid_access");
            }
            CredentialVersion credentialVersion = credentialVersions.get(0);
            if (credentialVersion instanceof JsonCredentialVersion) {
                propertiesMap.put("credentials", ((JsonCredentialVersion) credentialVersion).getValue());
            } else {
                throw new ParameterizedValidationException("error.interpolation.invalid_type", credentialName);
            }
        }
    }
    return servicesMap;
}
Also used : JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ArrayList(java.util.ArrayList) EntryNotFoundException(org.cloudfoundry.credhub.exceptions.EntryNotFoundException) Map(java.util.Map) ParameterizedValidationException(org.cloudfoundry.credhub.exceptions.ParameterizedValidationException) JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion)

Aggregations

CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)50 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)35 Test (org.junit.Test)33 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)27 SshCredentialVersion (org.cloudfoundry.credhub.domain.SshCredentialVersion)22 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)19 ValueCredentialVersion (org.cloudfoundry.credhub.domain.ValueCredentialVersion)17 RsaCredentialVersion (org.cloudfoundry.credhub.domain.RsaCredentialVersion)14 UserCredentialVersion (org.cloudfoundry.credhub.domain.UserCredentialVersion)13 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)11 JsonCredentialVersion (org.cloudfoundry.credhub.domain.JsonCredentialVersion)10 Credential (org.cloudfoundry.credhub.entity.Credential)8 EntryNotFoundException (org.cloudfoundry.credhub.exceptions.EntryNotFoundException)7 EventAuditRecordParameters (org.cloudfoundry.credhub.audit.EventAuditRecordParameters)6 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 CredentialVersionData (org.cloudfoundry.credhub.entity.CredentialVersionData)5 ResultActions (org.springframework.test.web.servlet.ResultActions)5 CertificateCredentialValue (org.cloudfoundry.credhub.credential.CertificateCredentialValue)4 UserContext (org.cloudfoundry.credhub.auth.UserContext)3