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