Search in sources :

Example 6 with PermissionsView

use of org.cloudfoundry.credhub.view.PermissionsView in project credhub by cloudfoundry-incubator.

the class PermissionsHandler method getPermissions.

public PermissionsView getPermissions(String name, List<EventAuditRecordParameters> auditRecordParameters) {
    CredentialVersion credentialVersion = permissionedCredentialService.findMostRecent(name);
    final List<PermissionEntry> permissions = permissionService.getPermissions(credentialVersion, auditRecordParameters, name);
    return new PermissionsView(credentialVersion.getName(), permissions);
}
Also used : PermissionsView(org.cloudfoundry.credhub.view.PermissionsView) PermissionEntry(org.cloudfoundry.credhub.request.PermissionEntry) CredentialVersion(org.cloudfoundry.credhub.domain.CredentialVersion)

Example 7 with PermissionsView

use of org.cloudfoundry.credhub.view.PermissionsView in project credhub by cloudfoundry-incubator.

the class CredentialsControllerTypeSpecificGenerateTest method generatingANewCredential_addsFullPermissionsForCurrentUser.

@Test
public void generatingANewCredential_addsFullPermissionsForCurrentUser() throws Exception {
    MockHttpServletRequestBuilder request = createGenerateNewCredentialRequest();
    mockMvc.perform(request);
    MockHttpServletRequestBuilder getRequest = get("/api/v1/permissions?credential_name=" + CREDENTIAL_NAME).header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN);
    MvcResult result = mockMvc.perform(getRequest).andExpect(status().isOk()).andReturn();
    String content = result.getResponse().getContentAsString();
    PermissionsView acl = JsonTestHelper.deserialize(content, PermissionsView.class);
    assertThat(acl.getCredentialName(), equalTo(CREDENTIAL_NAME));
    assertThat(acl.getPermissions(), Matchers.contains(samePropertyValuesAs(new PermissionEntry(AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_ACTOR_ID, asList(READ, WRITE, DELETE, READ_ACL, WRITE_ACL)))));
}
Also used : PermissionsView(org.cloudfoundry.credhub.view.PermissionsView) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) PermissionEntry(org.cloudfoundry.credhub.request.PermissionEntry) Matchers.anyString(org.mockito.Matchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 8 with PermissionsView

use of org.cloudfoundry.credhub.view.PermissionsView in project credhub by cloudfoundry-incubator.

the class CredentialsControllerTypeSpecificSetTest method creatingACredential_createsRequestedPermissions_andFullPermissionsForCurrentUser.

@Test
public void creatingACredential_createsRequestedPermissions_andFullPermissionsForCurrentUser() throws Exception {
    MockHttpServletRequestBuilder putRequest = put("/api/v1/data").header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "\"name\":\"" + CREDENTIAL_NAME + "\"," + "\"type\":\"" + parametizer.credentialType + "\"," + "\"value\":" + parametizer.credentialValue + "," + "\"overwrite\":" + false + "," + "\"additional_permissions\": [" + "{\"actor\": \"app1-guid\"," + "\"operations\": [\"read\"]}]" + "}");
    MockHttpServletRequestBuilder getRequest = get("/api/v1/permissions?credential_name=" + CREDENTIAL_NAME).header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN);
    mockMvc.perform(putRequest).andExpect(status().isOk());
    String responseContent = mockMvc.perform(getRequest).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
    PermissionsView acl = JsonTestHelper.deserialize(responseContent, PermissionsView.class);
    assertThat(acl.getCredentialName(), equalTo(CREDENTIAL_NAME));
    assertThat(acl.getPermissions(), containsInAnyOrder(samePropertyValuesAs(new PermissionEntry(UAA_OAUTH2_PASSWORD_GRANT_ACTOR_ID, asList(READ, WRITE, DELETE, READ_ACL, WRITE_ACL))), samePropertyValuesAs(new PermissionEntry("app1-guid", asList(READ)))));
}
Also used : PermissionsView(org.cloudfoundry.credhub.view.PermissionsView) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) PermissionEntry(org.cloudfoundry.credhub.request.PermissionEntry) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 9 with PermissionsView

use of org.cloudfoundry.credhub.view.PermissionsView in project credhub by cloudfoundry-incubator.

the class PermissionsControllerTest method GET_whenTheCredentialNameDoesNotHaveALeadingSlash_returnsThePermissionsForTheCredential.

@Test
public void GET_whenTheCredentialNameDoesNotHaveALeadingSlash_returnsThePermissionsForTheCredential() throws Exception {
    PermissionsView permissionsView = new PermissionsView("/test_credential_name", newArrayList());
    when(permissionsHandler.getPermissions(eq("/test_credential_name"), any(List.class))).thenReturn(permissionsView);
    PermissionsView permissions = getPermissions(mockMvc, "test_credential_name", UAA_OAUTH2_PASSWORD_GRANT_TOKEN);
    assertThat(permissions.getCredentialName(), equalTo("/test_credential_name"));
    assertThat(permissions.getPermissions(), hasSize(0));
}
Also used : PermissionsView(org.cloudfoundry.credhub.view.PermissionsView) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 10 with PermissionsView

use of org.cloudfoundry.credhub.view.PermissionsView in project credhub by cloudfoundry-incubator.

the class NoOverwriteTest method whenMultipleThreadsGenerateCredentialWithSameNameAndNoOverwrite_itShouldNotOverwrite.

@Test
public void whenMultipleThreadsGenerateCredentialWithSameNameAndNoOverwrite_itShouldNotOverwrite() throws Exception {
    // We need to set the parameters so that we can determine which actor's request won,
    // even with authorization enforcement disabled.
    runRequestsConcurrently(CREDENTIAL_NAME, ",\"parameters\":{\"exclude_lower\":true,\"exclude_upper\":true}", ",\"parameters\":{\"exclude_number\":true}", () -> post("/api/v1/data"));
    MvcResult result1 = responses[0].andDo(print()).andReturn();
    final DocumentContext context1 = JsonPath.parse(result1.getResponse().getContentAsString());
    MvcResult result2 = responses[1].andDo(print()).andReturn();
    final DocumentContext context2 = JsonPath.parse(result2.getResponse().getContentAsString());
    assertThat(context1.read("$.value"), equalTo(context2.read("$.value")));
    MockHttpServletResponse response1 = mockMvc.perform(get("/api/v1/permissions?credential_name=" + CREDENTIAL_NAME).header("Authorization", "Bearer " + UAA_OAUTH2_PASSWORD_GRANT_TOKEN)).andDo(print()).andReturn().getResponse();
    MockHttpServletResponse response2 = mockMvc.perform(get("/api/v1/permissions?credential_name=" + CREDENTIAL_NAME).header("Authorization", "Bearer " + UAA_OAUTH2_CLIENT_CREDENTIALS_TOKEN)).andDo(print()).andReturn().getResponse();
    String winningPassword = context1.read("$.value");
    String winningActor;
    String winningResponse;
    if (winningPassword.matches("\\d+")) {
        winningActor = UAA_OAUTH2_PASSWORD_GRANT_ACTOR_ID;
        winningResponse = response1.getContentAsString();
    } else {
        winningActor = UAA_OAUTH2_CLIENT_CREDENTIALS_ACTOR_ID;
        winningResponse = response2.getContentAsString();
    }
    PermissionsView acl = JsonTestHelper.deserialize(winningResponse, PermissionsView.class);
    assertThat(acl.getPermissions(), containsInAnyOrder(samePropertyValuesAs(new PermissionEntry(winningActor, asList(READ, WRITE, DELETE, READ_ACL, WRITE_ACL))), samePropertyValuesAs(new PermissionEntry("uaa-client:a-different-actor", singletonList(READ)))));
}
Also used : PermissionsView(org.cloudfoundry.credhub.view.PermissionsView) PermissionEntry(org.cloudfoundry.credhub.request.PermissionEntry) MvcResult(org.springframework.test.web.servlet.MvcResult) DocumentContext(com.jayway.jsonpath.DocumentContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

PermissionsView (org.cloudfoundry.credhub.view.PermissionsView)24 Test (org.junit.Test)20 PermissionEntry (org.cloudfoundry.credhub.request.PermissionEntry)19 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 EventAuditRecordParameters (org.cloudfoundry.credhub.audit.EventAuditRecordParameters)7 MvcResult (org.springframework.test.web.servlet.MvcResult)6 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 DocumentContext (com.jayway.jsonpath.DocumentContext)2 List (java.util.List)2 CredentialVersion (org.cloudfoundry.credhub.domain.CredentialVersion)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 PasswordCredentialVersion (org.cloudfoundry.credhub.domain.PasswordCredentialVersion)1 PermissionOperation (org.cloudfoundry.credhub.request.PermissionOperation)1 Matchers.anyString (org.mockito.Matchers.anyString)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1