Added code fix for 500 errors when not using a token. Now accepts client credentials as well.
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/3a442b0b Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/3a442b0b Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/3a442b0b Branch: refs/heads/master Commit: 3a442b0bb580e6917ece785ffae5e9b47484f092 Parents: a462244 Author: George Reyes <g...@apache.org> Authored: Wed Apr 13 12:59:45 2016 -0700 Committer: George Reyes <g...@apache.org> Committed: Wed Apr 13 12:59:45 2016 -0700 ---------------------------------------------------------------------- .../collection/CollectionsResourceIT.java | 40 ++++++++++++++++++++ .../rest/test/resource/ClientSetup.java | 10 +++++ .../services/AbstractCollectionService.java | 23 +++++++++-- 3 files changed, 70 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/3a442b0b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java index e0b75ba..d052564 100644 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java @@ -25,6 +25,7 @@ import org.apache.usergrid.persistence.entities.Application; import org.apache.usergrid.rest.test.resource.AbstractRestIT; import org.apache.usergrid.rest.test.resource.model.ApiResponse; import org.apache.usergrid.rest.test.resource.model.Collection; +import org.apache.usergrid.rest.test.resource.model.Credentials; import org.apache.usergrid.rest.test.resource.model.Entity; import org.apache.usergrid.rest.test.resource.model.QueryParameters; import org.apache.usergrid.rest.test.resource.model.Token; @@ -37,6 +38,9 @@ import org.slf4j.LoggerFactory; import javax.ws.rs.BadRequestException; import javax.ws.rs.ClientErrorException; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.MediaType; + import java.io.IOException; import java.util.HashMap; import java.util.LinkedHashMap; @@ -119,6 +123,42 @@ public class CollectionsResourceIT extends AbstractRestIT { } @Test + public void postToCollectionSchemaUsingOrgAppCreds(){ + //Creating schema. + //this could be changed to a hashmap. + ArrayList<String> indexingArray = new ArrayList<>( ); + + + //field "fields" is required. + Entity payload = new Entity(); + payload.put( "fields", indexingArray); + + + Credentials appCredentials = clientSetup.getAppCredentials(); + + + try { + + this.pathResource( getOrgAppPath( "testcollections/_indexes" ) ).post( false, payload, + new QueryParameters().addParam( "grant_type", "client_credentials" ).addParam( "client_id", + String.valueOf( ( ( Map ) appCredentials.get( "credentials" ) ).get( "client_id" ) ) ) + .addParam( "client_secret", String.valueOf( + ( ( Map ) appCredentials.get( "credentials" ) ).get( "client_secret" ) ) ) ); + }catch(Exception e){ + fail("This should return a success."); + } + + refreshIndex(); + + + Collection collection = this.app().collection( "testCollections" ).collection( "_index" ).get(); + + LinkedHashMap testCollectionSchema = (LinkedHashMap)collection.getResponse().getData(); + assertEquals( "app credentials",testCollectionSchema.get( "lastUpdateBy" ) ); + assertEquals( 0,testCollectionSchema.get( "lastReindexed" ) ); + } + + @Test public void deleteCollectionSchema() throws Exception { //Creating schema. //this could be changed to a hashmap. http://git-wip-us.apache.org/repos/asf/usergrid/blob/3a442b0b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ClientSetup.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ClientSetup.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ClientSetup.java index 2a13880..38a7e12 100644 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ClientSetup.java +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ClientSetup.java @@ -49,6 +49,7 @@ public class ClientSetup implements TestRule { protected String superuserName = "superuser"; protected String superuserPassword = "superpassword"; protected Credentials clientCredentials; + protected Credentials appCredentials; protected Organization organization; protected Entity application; @@ -108,11 +109,16 @@ public class ClientSetup implements TestRule { restClient.management().token().get(username, password); clientCredentials = restClient.management().orgs().org(orgName).credentials().get(Credentials.class); + //appCredentials = restClient.management().orgs().org(orgName).app().path//.credentials().get(Credentials.class); + ApiResponse appResponse = restClient.management().orgs() .org(organization.getName()).app().post(new Application(appName)); appUuid = (String) appResponse.getEntities().get(0).get("uuid"); application = new Application(appResponse); + + appCredentials = restClient.pathResource( "management/orgs/"+orgName+"/apps/"+appName+"/credentials" ).get( Credentials.class,true ); + refreshIndex(); ApiResponse response = restClient.management().token().post(new Token(username, password)); @@ -163,6 +169,10 @@ public class ClientSetup implements TestRule { return clientCredentials; } + public Credentials getAppCredentials(){ + return appCredentials; + } + public void refreshIndex() { this.restClient.refreshIndex(getOrganizationName(), getAppName(), CpNamingUtils.getManagementApplicationId().getUuid().toString()); http://git-wip-us.apache.org/repos/asf/usergrid/blob/3a442b0b/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java ---------------------------------------------------------------------- diff --git a/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java b/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java index 8b23e7a..c5473cf 100644 --- a/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java +++ b/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java @@ -38,6 +38,8 @@ import org.apache.usergrid.persistence.exceptions.EntityNotFoundException; import org.apache.usergrid.persistence.exceptions.UnexpectedEntityTypeException; import org.apache.usergrid.persistence.Query.Level; import org.apache.usergrid.security.shiro.principals.AdminUserPrincipal; +import org.apache.usergrid.security.shiro.principals.ApplicationPrincipal; +import org.apache.usergrid.security.shiro.principals.PrincipalIdentifier; import org.apache.usergrid.security.shiro.utils.SubjectUtils; import org.apache.usergrid.services.ServiceResults.Type; import org.apache.usergrid.services.exceptions.ForbiddenServiceOperationException; @@ -334,11 +336,26 @@ public class AbstractCollectionService extends AbstractService { ServiceContext context = serviceRequest.getAppContext(); checkPermissionsForCollection( context ); + //TODO: write rest test for these line of codes Subject currentUser = SubjectUtils.getSubject(); - Object currentUser2 =currentUser.getPrincipal(); + Object currentUserPrincipal =currentUser.getPrincipal(); - Map collectionSchema = em.createCollectionSchema(context.getCollectionName(), - ( ( AdminUserPrincipal ) currentUser2 ).getUser().getEmail(),context.getProperties()); + Map collectionSchema = null; + + if(currentUserPrincipal instanceof AdminUserPrincipal) { + AdminUserPrincipal adminUserPrincipal = ( AdminUserPrincipal ) currentUserPrincipal; + + collectionSchema = em.createCollectionSchema( context.getCollectionName(), + adminUserPrincipal.getUser().getEmail(), context.getProperties() ); + } + else if(currentUserPrincipal instanceof ApplicationPrincipal){ + collectionSchema = em.createCollectionSchema( context.getCollectionName(), + "app credentials", context.getProperties() ); + } + else if ( currentUserPrincipal instanceof PrincipalIdentifier ) { + collectionSchema = em.createCollectionSchema( context.getCollectionName(), + "generic credentials", context.getProperties() ); + } return new ServiceResults( this, context, Type.COLLECTION, Results.fromData( collectionSchema ), null, null );