Skip to content

Commit

Permalink
Use zone filtering in approval store
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Mar 22, 2017
1 parent 76fdfbb commit 50853fc
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 111 deletions.
@@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Cloud Foundry * Cloud Foundry
* Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved. * Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved.
* *
* This product is licensed to you under the Apache License, Version 2.0 (the "License"). * This product is licensed to you under the Apache License, Version 2.0 (the "License").
Expand All @@ -12,23 +12,14 @@
*******************************************************************************/ *******************************************************************************/
package org.cloudfoundry.identity.uaa.approval; package org.cloudfoundry.identity.uaa.approval;


import static org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus.APPROVED;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.cloudfoundry.identity.uaa.audit.event.ApprovalModifiedEvent;
import org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus; import org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus;
import org.cloudfoundry.identity.uaa.audit.event.ApprovalModifiedEvent;
import org.cloudfoundry.identity.uaa.resources.jdbc.JdbcPagingListFactory; import org.cloudfoundry.identity.uaa.resources.jdbc.JdbcPagingListFactory;
import org.cloudfoundry.identity.uaa.resources.jdbc.SearchQueryConverter; import org.cloudfoundry.identity.uaa.resources.jdbc.SearchQueryConverter;
import org.cloudfoundry.identity.uaa.resources.jdbc.SearchQueryConverter.ProcessedFilter; import org.cloudfoundry.identity.uaa.resources.jdbc.SearchQueryConverter.ProcessedFilter;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationEventPublisherAware;
Expand All @@ -43,6 +34,17 @@
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert; import org.springframework.util.Assert;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus.APPROVED;

public class JdbcApprovalStore implements ApprovalStore, ApplicationEventPublisherAware { public class JdbcApprovalStore implements ApprovalStore, ApplicationEventPublisherAware {


private final JdbcTemplate jdbcTemplate; private final JdbcTemplate jdbcTemplate;
Expand Down Expand Up @@ -143,17 +145,17 @@ public boolean revokeApprovals(String filter) {
logger.debug(String.format("Filtering approvals with filter: [%s]", where)); logger.debug(String.format("Filtering approvals with filter: [%s]", where));


String sql; String sql;
Map<String, Object> sqlParams; Map<String, Object> sqlParams = new HashMap<>(where.getParams());
if (handleRevocationsAsExpiry) { if (handleRevocationsAsExpiry) {
// just expire all approvals matching the filter // just expire all approvals matching the filter
sql = EXPIRE_AUTHZ_SQL + " where " + where.getSql(); sql = EXPIRE_AUTHZ_SQL + " where " + where.getSql();
sqlParams = where.getParams();
sqlParams.put("expiry", new Timestamp(new Date().getTime() - 1)); sqlParams.put("expiry", new Timestamp(new Date().getTime() - 1));
} else { } else {
// delete the records // delete the records
sql = DELETE_AUTHZ_SQL + " where " + where.getSql(); sql = DELETE_AUTHZ_SQL + " where " + where.getSql();
sqlParams = where.getParams();
} }
sqlParams.put("__identity_zone_id", IdentityZoneHolder.get().getId());
sql = sql + " and user_id in (select id from users where identity_zone_id = :__identity_zone_id)";


try { try {
int revoked = new NamedParameterJdbcTemplate(jdbcTemplate).update(sql, sqlParams); int revoked = new NamedParameterJdbcTemplate(jdbcTemplate).update(sql, sqlParams);
Expand Down Expand Up @@ -185,8 +187,14 @@ public List<Approval> getApprovals(String filter) {
ProcessedFilter where = queryConverter.convert(filter, null, true); ProcessedFilter where = queryConverter.convert(filter, null, true);
logger.debug(String.format("Filtering approvals with filter: [%s]", where)); logger.debug(String.format("Filtering approvals with filter: [%s]", where));
try { try {
return pagingListFactory.createJdbcPagingList(GET_AUTHZ_SQL + " where " + Map<String, Object> params = new HashMap(where.getParams());
where.getSql(), where.getParams(), rowMapper, 200); params.put("__identity_zone_id", IdentityZoneHolder.get().getId());
return pagingListFactory.createJdbcPagingList(
GET_AUTHZ_SQL + " where " + where.getSql() + " and user_id in (select id from users where identity_zone_id = :__identity_zone_id)",
params,
rowMapper,
200
);
} catch (DataAccessException e) { } catch (DataAccessException e) {
logger.error("Error filtering approvals with filter: " + where, e); logger.error("Error filtering approvals with filter: " + where, e);
throw new IllegalArgumentException("Invalid filter: " + filter); throw new IllegalArgumentException("Invalid filter: " + filter);
Expand Down
@@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Cloud Foundry * Cloud Foundry
* Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved. * Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved.
* *
* This product is licensed to you under the Apache License, Version 2.0 (the "License"). * This product is licensed to you under the Apache License, Version 2.0 (the "License").
Expand All @@ -12,22 +12,6 @@
*******************************************************************************/ *******************************************************************************/
package org.cloudfoundry.identity.uaa.oauth; package org.cloudfoundry.identity.uaa.oauth;


import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import static java.util.Collections.singleton;
import static org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus.APPROVED;
import static org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus.DENIED;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import org.cloudfoundry.identity.uaa.approval.Approval; import org.cloudfoundry.identity.uaa.approval.Approval;
import org.cloudfoundry.identity.uaa.approval.ApprovalStore; import org.cloudfoundry.identity.uaa.approval.ApprovalStore;
import org.cloudfoundry.identity.uaa.approval.JdbcApprovalStore; import org.cloudfoundry.identity.uaa.approval.JdbcApprovalStore;
Expand All @@ -49,6 +33,22 @@
import org.springframework.security.oauth2.provider.ClientDetails; import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails; import org.springframework.security.oauth2.provider.client.BaseClientDetails;


import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import static java.util.Collections.singleton;
import static org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus.APPROVED;
import static org.cloudfoundry.identity.uaa.approval.Approval.ApprovalStatus.DENIED;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class UserManagedAuthzApprovalHandlerTests extends JdbcTestBase { public class UserManagedAuthzApprovalHandlerTests extends JdbcTestBase {


private final UserManagedAuthzApprovalHandler handler = new UserManagedAuthzApprovalHandler(); private final UserManagedAuthzApprovalHandler handler = new UserManagedAuthzApprovalHandler();
Expand All @@ -70,17 +70,18 @@ public void initUserManagedAuthzApprovalHandlerTests() {
handler.setApprovalStore(approvalStore); handler.setApprovalStore(approvalStore);
handler.setClientDetailsService( handler.setClientDetailsService(
mockClientDetailsService( mockClientDetailsService(
"foo", "foo",
new String[]{ new String[]{
"cloud_controller.read", "cloud_controller.read",
"cloud_controller.write", "cloud_controller.write",
"openid", "openid",
"space.*.developer" "space.*.developer"
}, },
Collections.emptySet() Collections.emptySet()
) )
); );
userId = new RandomValueStringGenerator().generate(); userId = new RandomValueStringGenerator().generate();
testAccounts.addRandomUser(jdbcTemplate, userId);
userAuthentication = new TestAuthentication(userId, testAccounts.getUserName(), true); userAuthentication = new TestAuthentication(userId, testAccounts.getUserName(), true);
} }


Expand All @@ -106,7 +107,7 @@ public void testNoScopeApproval() {
@Test @Test
public void testNoPreviouslyApprovedScopes() { public void testNoPreviouslyApprovedScopes() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList("cloud_controller.read", "cloud_controller.write") Arrays.asList("cloud_controller.read", "cloud_controller.write")
) )
Expand All @@ -121,7 +122,7 @@ public void testNoPreviouslyApprovedScopes() {
@Test @Test
public void testAuthzApprovedButNoPreviouslyApprovedScopes() { public void testAuthzApprovedButNoPreviouslyApprovedScopes() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList("cloud_controller.read", "cloud_controller.write") Arrays.asList("cloud_controller.read", "cloud_controller.write")
) )
Expand Down Expand Up @@ -162,7 +163,7 @@ public void testNoRequestedScopesButSomeApprovedScopes() {
@Test @Test
public void testRequestedScopesDontMatchApprovalsAtAll() { public void testRequestedScopesDontMatchApprovalsAtAll() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList("openid") Arrays.asList("openid")
) )
Expand Down Expand Up @@ -193,7 +194,7 @@ public void testRequestedScopesDontMatchApprovalsAtAll() {
@Test @Test
public void testOnlySomeRequestedScopeMatchesApproval() { public void testOnlySomeRequestedScopeMatchesApproval() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList("openid", "cloud_controller.read") Arrays.asList("openid", "cloud_controller.read")
) )
Expand Down Expand Up @@ -224,7 +225,7 @@ public void testOnlySomeRequestedScopeMatchesApproval() {
@Test @Test
public void testOnlySomeRequestedScopeMatchesDeniedApprovalButScopeAutoApproved() { public void testOnlySomeRequestedScopeMatchesDeniedApprovalButScopeAutoApproved() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList("openid", "cloud_controller.read") Arrays.asList("openid", "cloud_controller.read")
) )
Expand All @@ -236,10 +237,10 @@ public void testOnlySomeRequestedScopeMatchesDeniedApprovalButScopeAutoApproved(


handler.setClientDetailsService( handler.setClientDetailsService(
mockClientDetailsService( mockClientDetailsService(
"foo", "foo",
new String[]{ new String[]{
"cloud_controller.read", "cloud_controller.read",
"cloud_controller.write", "cloud_controller.write",
"openid" "openid"
}, },
singleton("true") singleton("true")
Expand All @@ -266,11 +267,11 @@ public void testOnlySomeRequestedScopeMatchesDeniedApprovalButScopeAutoApproved(
@Test @Test
public void testRequestedScopesMatchApprovalButAdditionalScopesRequested() { public void testRequestedScopesMatchApprovalButAdditionalScopesRequested() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList( Arrays.asList(
"openid", "openid",
"cloud_controller.read", "cloud_controller.read",
"cloud_controller.write" "cloud_controller.write"
) )
) )
Expand Down Expand Up @@ -301,11 +302,11 @@ public void testRequestedScopesMatchApprovalButAdditionalScopesRequested() {
@Test @Test
public void testAllRequestedScopesMatchApproval() { public void testAllRequestedScopesMatchApproval() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList( Arrays.asList(
"openid", "openid",
"cloud_controller.read", "cloud_controller.read",
"cloud_controller.write" "cloud_controller.write"
) )
) )
Expand Down Expand Up @@ -342,11 +343,11 @@ public void testAllRequestedScopesMatchApproval() {
@Test @Test
public void testRequestedScopesMatchApprovalButSomeDenied() { public void testRequestedScopesMatchApprovalButSomeDenied() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList( Arrays.asList(
"openid", "openid",
"cloud_controller.read", "cloud_controller.read",
"cloud_controller.write" "cloud_controller.write"
) )
) )
Expand Down Expand Up @@ -383,11 +384,11 @@ public void testRequestedScopesMatchApprovalButSomeDenied() {
@Test @Test
public void testRequestedScopesMatchApprovalSomeDeniedButDeniedScopesAutoApproved() { public void testRequestedScopesMatchApprovalSomeDeniedButDeniedScopesAutoApproved() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>( new HashSet<>(
Arrays.asList( Arrays.asList(
"openid", "openid",
"cloud_controller.read", "cloud_controller.read",
"cloud_controller.write" "cloud_controller.write"
) )
) )
Expand All @@ -400,7 +401,7 @@ public void testRequestedScopesMatchApprovalSomeDeniedButDeniedScopesAutoApprove
"foo", "foo",
new String[]{ new String[]{
"cloud_controller.read", "cloud_controller.read",
"cloud_controller.write", "cloud_controller.write",
"openid" "openid"
}, },
singleton("cloud_controller.write"))); singleton("cloud_controller.write")));
Expand Down Expand Up @@ -561,7 +562,7 @@ public void testRequestedScopesMatchByWildcard() {
@Test @Test
public void testSomeRequestedScopesMatchApproval() { public void testSomeRequestedScopesMatchApproval() {
AuthorizationRequest request = new AuthorizationRequest( AuthorizationRequest request = new AuthorizationRequest(
"foo", "foo",
new HashSet<>(Arrays.asList("openid")) new HashSet<>(Arrays.asList("openid"))
); );
request.setApproved(false); request.setApproved(false);
Expand Down

0 comments on commit 50853fc

Please sign in to comment.