Search in sources :

Example 1 with JsonCredentialVersion

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

the class InterpolationHandlerTest method setupValidRequest.

private void setupValidRequest() {
    String inputJsonString = "{" + "  \"pp-config-server\": [" + "    {" + "      \"credentials\": {" + "        \"credhub-ref\": \"((/cred1))\"" + "      }," + "      \"label\": \"pp-config-server\"" + "    }," + "    {" + "      \"credentials\": {" + "        \"credhub-ref\": \"((/cred2))\"" + "      }" + "    }" + "  ]," + "  \"pp-something-else\": [" + "    {" + "      \"credentials\": {" + "        \"credhub-ref\": \"((/cred3))\"" + "      }," + "      \"something\": [\"pp-config-server\"]" + "    }" + "  ]" + "}";
    Map<String, Object> inputJson = deserialize(inputJsonString, Map.class);
    JsonCredentialVersion jsonCredential = mock(JsonCredentialVersion.class);
    when(jsonCredential.getName()).thenReturn("/cred1");
    doReturn(Maps.newHashMap("secret1", "secret1-value")).when(jsonCredential).getValue();
    JsonCredentialVersion jsonCredential1 = mock(JsonCredentialVersion.class);
    when(jsonCredential1.getName()).thenReturn("/cred2");
    doReturn(Maps.newHashMap("secret2", "secret2-value")).when(jsonCredential1).getValue();
    JsonCredentialVersion jsonCredential2 = mock(JsonCredentialVersion.class);
    when(jsonCredential2.getName()).thenReturn("/cred3");
    Map<String, String> jsonCredetials = Maps.newHashMap("secret3-1", "secret3-1-value");
    jsonCredetials.put("secret3-2", "secret3-2-value");
    doReturn(jsonCredetials).when(jsonCredential2).getValue();
    doReturn(singletonList(jsonCredential)).when(credentialService).findNByName("/cred1", 1, newArrayList());
    doReturn(singletonList(jsonCredential1)).when(credentialService).findNByName("/cred2", 1, newArrayList());
    doReturn(singletonList(jsonCredential2)).when(credentialService).findNByName("/cred3", 1, newArrayList());
    response = subject.interpolateCredHubReferences(inputJson, eventAuditRecordParameters);
}
Also used : JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion)

Example 2 with JsonCredentialVersion

use of org.cloudfoundry.credhub.domain.JsonCredentialVersion 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)

Example 3 with JsonCredentialVersion

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

the class InterpolationControllerTest method POST_replacesTheCredHubRefWithTheCredentialValue.

@Test
public void POST_replacesTheCredHubRefWithTheCredentialValue() throws Exception {
    JsonCredentialVersion jsonCredential = mock(JsonCredentialVersion.class);
    doReturn(Maps.newHashMap("secret1", "secret1-value")).when(jsonCredential).getValue();
    when(jsonCredential.getName()).thenReturn("/cred1");
    JsonCredentialVersion jsonCredential1 = mock(JsonCredentialVersion.class);
    doReturn(Maps.newHashMap("secret2", "secret2-value")).when(jsonCredential1).getValue();
    when(jsonCredential1.getName()).thenReturn("/cred2");
    doReturn(Arrays.asList(jsonCredential)).when(mockCredentialVersionDataService).findNByName("/cred1", 1);
    doReturn(Arrays.asList(jsonCredential1)).when(mockCredentialVersionDataService).findNByName("/cred2", 1);
    mockMvc.perform(makeValidPostRequest()).andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.pp-config-server[0].credentials.secret1").value(equalTo("secret1-value"))).andExpect(jsonPath("$.pp-something-else[0].credentials.secret2").value(equalTo("secret2-value")));
}
Also used : JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with JsonCredentialVersion

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

the class JsonViewTest method beforeEach.

@Before
public void beforeEach() {
    value = new HashMap<>();
    value.put("string", "something");
    value.put("num", 10);
    value.put("camelCase", "blabla");
    serializedValue = serializeToString(value);
    encryptor = mock(Encryptor.class);
    uuid = UUID.randomUUID();
    entity = new JsonCredentialVersion("/foo").setEncryptor(encryptor).setUuid(uuid);
    when(encryptor.decrypt(any())).thenReturn(serializedValue);
}
Also used : JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) Encryptor(org.cloudfoundry.credhub.domain.Encryptor) Before(org.junit.Before)

Example 5 with JsonCredentialVersion

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

the class CredentialsControllerTypeSpecificSetTest method parameters.

@Parameterized.Parameters(name = "{0}")
public static Collection<Object> parameters() {
    UUID credentialUuid = UUID.randomUUID();
    Collection<Object> params = new ArrayList<>();
    TestParametizer valueParameters = new TestParametizer("value", "\"" + VALUE_VALUE + "\"") {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value", VALUE_VALUE);
        }

        void credentialAssertions(CredentialVersion credential) {
            assertThat(((ValueCredentialVersion) credential).getValue(), equalTo(VALUE_VALUE));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new ValueCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setValue(VALUE_VALUE).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer passwordParameters = new TestParametizer("password", "\"" + PASSWORD_VALUE + "\"") {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value", PASSWORD_VALUE);
        }

        void credentialAssertions(CredentialVersion credential) {
            assertThat(((PasswordCredentialVersion) credential).getPassword(), equalTo(PASSWORD_VALUE));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new PasswordCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPasswordAndGenerationParameters(PASSWORD_VALUE, null).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer certificateParameters = new TestParametizer("certificate", CERTIFICATE_VALUE_JSON_STRING) {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.certificate", TestConstants.TEST_CERTIFICATE, "$.value.private_key", TestConstants.TEST_PRIVATE_KEY, "$.value.ca", TestConstants.TEST_CA);
        }

        void credentialAssertions(CredentialVersion credential) {
            CertificateCredentialVersion certificateCredential = (CertificateCredentialVersion) credential;
            assertThat(certificateCredential.getCa(), equalTo(TestConstants.TEST_CA));
            assertThat(certificateCredential.getCertificate(), equalTo(TestConstants.TEST_CERTIFICATE));
            assertThat(certificateCredential.getPrivateKey(), equalTo(TestConstants.TEST_PRIVATE_KEY));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new CertificateCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setCa(TestConstants.TEST_CA).setCertificate(TestConstants.TEST_CERTIFICATE).setPrivateKey(TestConstants.TEST_PRIVATE_KEY).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer sshParameters = new TestParametizer("ssh", SSH_VALUE_JSON_STRING) {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.public_key", TestConstants.SSH_PUBLIC_KEY_4096_WITH_COMMENT, "$.value.private_key", TestConstants.PRIVATE_KEY_4096, "$.value.public_key_fingerprint", "UmqxK9UJJR4Jrcw0DcwqJlCgkeQoKp8a+HY+0p0nOgc");
        }

        void credentialAssertions(CredentialVersion credential) {
            SshCredentialVersion sshCredential = (SshCredentialVersion) credential;
            assertThat(sshCredential.getPublicKey(), equalTo(TestConstants.SSH_PUBLIC_KEY_4096_WITH_COMMENT));
            assertThat(sshCredential.getPrivateKey(), equalTo(TestConstants.PRIVATE_KEY_4096));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new SshCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPrivateKey(TestConstants.PRIVATE_KEY_4096).setPublicKey(TestConstants.SSH_PUBLIC_KEY_4096_WITH_COMMENT).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer rsaParameters = new TestParametizer("rsa", RSA_VALUE_JSON_STRING) {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.public_key", TestConstants.RSA_PUBLIC_KEY_4096, "$.value.private_key", TestConstants.PRIVATE_KEY_4096);
        }

        void credentialAssertions(CredentialVersion credential) {
            RsaCredentialVersion rsaCredential = (RsaCredentialVersion) credential;
            assertThat(rsaCredential.getPublicKey(), equalTo(TestConstants.RSA_PUBLIC_KEY_4096));
            assertThat(rsaCredential.getPrivateKey(), equalTo(TestConstants.PRIVATE_KEY_4096));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new RsaCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setPrivateKey(TestConstants.PRIVATE_KEY_4096).setPublicKey(TestConstants.RSA_PUBLIC_KEY_4096).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer jsonParameters = new TestParametizer("json", JSON_VALUE_JSON_STRING) {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value", jsonValueMap);
        }

        void credentialAssertions(CredentialVersion credential) {
            JsonCredentialVersion jsonCredential = (JsonCredentialVersion) credential;
            assertThat(jsonCredential.getValue(), equalTo(jsonValueMap));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new JsonCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setValue(jsonValueMap).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    TestParametizer userParameters = new TestParametizer("user", USER_VALUE_JSON_STRING) {

        ResultMatcher jsonAssertions() {
            return multiJsonPath("$.value.username", USERNAME_VALUE, "$.value.password", PASSWORD_VALUE);
        }

        void credentialAssertions(CredentialVersion credential) {
            UserCredentialVersion userCredential = (UserCredentialVersion) credential;
            assertThat(userCredential.getUsername(), equalTo(USERNAME_VALUE));
            assertThat(userCredential.getPassword(), equalTo(PASSWORD_VALUE));
        }

        CredentialVersion createCredential(Encryptor encryptor) {
            return new UserCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setUsername(USERNAME_VALUE).setPassword(PASSWORD_VALUE).setUuid(credentialUuid).setVersionCreatedAt(FROZEN_TIME.minusSeconds(1));
        }
    };
    params.add(valueParameters);
    params.add(passwordParameters);
    params.add(certificateParameters);
    params.add(sshParameters);
    params.add(rsaParameters);
    params.add(jsonParameters);
    params.add(userParameters);
    return params;
}
Also used : JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Encryptor(org.cloudfoundry.credhub.domain.Encryptor) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) JSONObject(net.minidev.json.JSONObject) UUID(java.util.UUID) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) PasswordCredentialVersion(org.cloudfoundry.credhub.domain.PasswordCredentialVersion) RsaCredentialVersion(org.cloudfoundry.credhub.domain.RsaCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) JsonCredentialVersion(org.cloudfoundry.credhub.domain.JsonCredentialVersion) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) SshCredentialVersion(org.cloudfoundry.credhub.domain.SshCredentialVersion) UserCredentialVersion(org.cloudfoundry.credhub.domain.UserCredentialVersion) CertificateCredentialVersion(org.cloudfoundry.credhub.domain.CertificateCredentialVersion)

Aggregations

JsonCredentialVersion (org.cloudfoundry.credhub.domain.JsonCredentialVersion)6 ArrayList (java.util.ArrayList)2 CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)2 Encryptor (org.cloudfoundry.credhub.domain.Encryptor)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Map (java.util.Map)1 UUID (java.util.UUID)1 JSONObject (net.minidev.json.JSONObject)1 EventAuditRecordParameters (org.cloudfoundry.credhub.audit.EventAuditRecordParameters)1 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)1 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)1 RsaCredentialVersion (org.cloudfoundry.credhub.domain.RsaCredentialVersion)1 SshCredentialVersion (org.cloudfoundry.credhub.domain.SshCredentialVersion)1 UserCredentialVersion (org.cloudfoundry.credhub.domain.UserCredentialVersion)1 ValueCredentialVersion (org.cloudfoundry.credhub.domain.ValueCredentialVersion)1 EntryNotFoundException (org.cloudfoundry.credhub.exceptions.EntryNotFoundException)1 ParameterizedValidationException (org.cloudfoundry.credhub.exceptions.ParameterizedValidationException)1 Before (org.junit.Before)1