Search in sources :

Example 11 with ValueCredentialVersion

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

the class ValueViewTest method beforeEach.

@Before
public void beforeEach() {
    uuid = UUID.randomUUID();
    encryptor = mock(Encryptor.class);
    when(encryptor.decrypt(any())).thenReturn("fake-plaintext-value");
    entity = new ValueCredentialVersion("/foo").setEncryptor(encryptor).setUuid(uuid);
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) Encryptor(org.cloudfoundry.credhub.domain.Encryptor) Before(org.junit.Before)

Example 12 with ValueCredentialVersion

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

the class CredentialsControllerConcurrencySetTest method whenAnotherThreadsWinsARaceToUpdateACredential_retriesAndReturnsTheValueWrittenByTheOtherThread.

@Test
public void whenAnotherThreadsWinsARaceToUpdateACredential_retriesAndReturnsTheValueWrittenByTheOtherThread() throws Exception {
    UUID uuid = UUID.randomUUID();
    ValueCredentialVersion valueCredential = new ValueCredentialVersion(credentialName);
    valueCredential.setEncryptor(encryptor);
    valueCredential.setValue(credentialValue);
    valueCredential.setUuid(uuid);
    doReturn(null).doReturn(valueCredential).when(credentialVersionDataService).findMostRecent(anyString());
    doThrow(new DataIntegrityViolationException("we already have one of those")).when(credentialVersionDataService).save(any(CredentialVersion.class));
    final MockHttpServletRequestBuilder put = put("/api/v1/data").header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "\"type\":\"value\"," + "\"name\":\"" + credentialName + "\"," + "\"value\":\"" + credentialValue + "\"}");
    mockMvc.perform(put).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)).andExpect(jsonPath("$.type").value("value")).andExpect(jsonPath("$.value").value(credentialValue)).andExpect(jsonPath("$.id").value(uuid.toString()));
    verify(credentialVersionDataService).save(any(CredentialVersion.class));
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UUID(java.util.UUID) ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 13 with ValueCredentialVersion

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

the class CredentialsControllerGetTest method gettingACredential_byId_returnsTheCredentialAndPersistAnAuditEntry.

@Test
public void gettingACredential_byId_returnsTheCredentialAndPersistAnAuditEntry() throws Exception {
    UUID uuid = UUID.randomUUID();
    ValueCredentialVersion credential = new ValueCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setUuid(uuid).setVersionCreatedAt(FROZEN_TIME);
    doReturn(CREDENTIAL_VALUE).when(encryptor).decrypt(any());
    doReturn(credential).when(credentialVersionDataService).findByUuid(uuid.toString());
    final MockHttpServletRequestBuilder request = get("/api/v1/data/" + uuid).header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON);
    mockMvc.perform(request).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)).andExpect(jsonPath("$.type").value("value")).andExpect(jsonPath("$.value").value(CREDENTIAL_VALUE)).andExpect(jsonPath("$.id").value(uuid.toString())).andExpect(jsonPath("$.version_created_at").value(FROZEN_TIME.toString()));
    auditingHelper.verifyAuditing(AuditingOperationCode.CREDENTIAL_ACCESS, CREDENTIAL_NAME, AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_ACTOR_ID, "/api/v1/data/" + uuid.toString(), 200);
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UUID(java.util.UUID) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 14 with ValueCredentialVersion

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

the class CredentialsControllerGetTest method gettingACredential_byName_thatExists_returnsTheCredential.

@Test
public void gettingACredential_byName_thatExists_returnsTheCredential() throws Exception {
    doReturn(true).when(permissionCheckingService).hasPermission(any(String.class), any(String.class), eq(PermissionOperation.READ));
    UUID uuid = UUID.randomUUID();
    ValueCredentialVersion credential = new ValueCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setUuid(uuid).setVersionCreatedAt(FROZEN_TIME);
    doReturn(CREDENTIAL_VALUE).when(encryptor).decrypt(any());
    doReturn(newArrayList(credential)).when(credentialVersionDataService).findAllByName(CREDENTIAL_NAME);
    final MockHttpServletRequestBuilder request = get("/api/v1/data?name=" + CREDENTIAL_NAME).header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON);
    mockMvc.perform(request).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)).andExpect(jsonPath("$.data[0]" + ".type").value("value")).andExpect(jsonPath("$.data[0]" + ".value").value(CREDENTIAL_VALUE)).andExpect(jsonPath("$.data[0]" + ".id").value(uuid.toString())).andExpect(jsonPath("$.data[0]" + ".version_created_at").value(FROZEN_TIME.toString()));
    auditingHelper.verifyAuditing(AuditingOperationCode.CREDENTIAL_ACCESS, CREDENTIAL_NAME, AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_ACTOR_ID, "/api/v1/data", 200);
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UUID(java.util.UUID) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 15 with ValueCredentialVersion

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

the class CredentialsControllerGetTest method gettingACredential_byName_withCurrentSetToFalse_andNumberOfVersions_returnsTheSpecifiedNumberOfVersions.

@Test
public void gettingACredential_byName_withCurrentSetToFalse_andNumberOfVersions_returnsTheSpecifiedNumberOfVersions() throws Exception {
    UUID uuid = UUID.randomUUID();
    Instant credential1Instant = Instant.ofEpochSecond(1400000001L);
    Instant credential2Instant = Instant.ofEpochSecond(1400000002L);
    Instant credential3Instant = Instant.ofEpochSecond(1400000003L);
    ValueCredentialVersion valueCredential1 = new ValueCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setUuid(uuid).setVersionCreatedAt(credential1Instant);
    ValueCredentialVersion valueCredential2 = new ValueCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setUuid(uuid).setVersionCreatedAt(credential2Instant);
    ValueCredentialVersion valueCredential3 = new ValueCredentialVersion(CREDENTIAL_NAME).setEncryptor(encryptor).setUuid(uuid).setVersionCreatedAt(credential3Instant);
    doReturn(CREDENTIAL_VALUE).when(encryptor).decrypt(any());
    doReturn(newArrayList(valueCredential1, valueCredential2)).when(credentialVersionDataService).findNByName(CREDENTIAL_NAME, 2);
    mockMvc.perform(get("/api/v1/data?current=false&name=" + CREDENTIAL_NAME + "&versions=2").header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)).andExpect(jsonPath("$.data", hasSize(equalTo(2)))).andExpect(jsonPath("$.data[0].version_created_at", equalTo(credential1Instant.toString()))).andExpect(jsonPath("$.data[1].version_created_at", equalTo(credential2Instant.toString())));
}
Also used : ValueCredentialVersion(org.cloudfoundry.credhub.domain.ValueCredentialVersion) Instant(java.time.Instant) UUID(java.util.UUID) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

ValueCredentialVersion (org.cloudfoundry.credhub.domain.ValueCredentialVersion)18 Test (org.junit.Test)14 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)14 UUID (java.util.UUID)9 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)7 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)6 CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)4 ValueCredentialVersionData (org.cloudfoundry.credhub.entity.ValueCredentialVersionData)4 CertificateCredentialVersion (org.cloudfoundry.credhub.domain.CertificateCredentialVersion)3 EncryptedValue (org.cloudfoundry.credhub.entity.EncryptedValue)3 PasswordCredentialVersionData (org.cloudfoundry.credhub.entity.PasswordCredentialVersionData)3 Encryptor (org.cloudfoundry.credhub.domain.Encryptor)2 CertificateCredentialVersionData (org.cloudfoundry.credhub.entity.CertificateCredentialVersionData)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Before (org.junit.Before)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONObject (net.minidev.json.JSONObject)1