Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B7A94200CAC for ; Mon, 19 Jun 2017 21:58:45 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B67C7160BCD; Mon, 19 Jun 2017 19:58:45 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B8257160BE4 for ; Mon, 19 Jun 2017 21:58:43 +0200 (CEST) Received: (qmail 19930 invoked by uid 500); 19 Jun 2017 19:58:43 -0000 Mailing-List: contact commits-help@usergrid.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@usergrid.apache.org Delivered-To: mailing list commits@usergrid.apache.org Received: (qmail 19837 invoked by uid 99); 19 Jun 2017 19:58:42 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 19 Jun 2017 19:58:42 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 5082BDF97F; Mon, 19 Jun 2017 19:58:39 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mrusso@apache.org To: commits@usergrid.apache.org Date: Mon, 19 Jun 2017 19:58:41 -0000 Message-Id: In-Reply-To: <426e35935ee4401f8451edea724e9bf4@git.apache.org> References: <426e35935ee4401f8451edea724e9bf4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [3/7] usergrid git commit: Finish convert token service persistence to Datastax. Add tests, and rename the package for TokenServiceImpl to impl from cassandra. archived-at: Mon, 19 Jun 2017 19:58:45 -0000 Finish convert token service persistence to Datastax. Add tests, and rename the package for TokenServiceImpl to impl from cassandra. Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/146e47d6 Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/146e47d6 Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/146e47d6 Branch: refs/heads/master Commit: 146e47d668788d72c4fc54a61b7155eeeef508b6 Parents: 179f12d Author: Michael Russo Authored: Sat Jun 17 23:47:47 2017 -0700 Committer: Michael Russo Committed: Sat Jun 17 23:47:47 2017 -0700 ---------------------------------------------------------------------- .../src/test/resources/usergrid-test.properties | 2 - stack/core/pom.xml | 6 + .../usergrid/corepersistence/CoreModule.java | 2 + .../usergrid/corepersistence/CpSetup.java | 11 - .../persistence/cassandra/CassandraService.java | 3 - .../map/impl/MapSerializationImpl.java | 35 +- .../persistence/token/TokenSerialization.java | 50 ++ .../persistence/token/guice/TokenModule.java | 10 +- .../token/impl/TokenSerialization.java | 50 -- .../token/impl/TokenSerializationImpl.java | 482 +++++------ .../persistence/token/TestTokenModule.java | 38 + .../token/TokenSerializationTest.java | 202 +++++ .../persistence/token/TokenTestModule.java | 40 - .../resources/usergrid-standalone.properties | 2 - .../rest/management/ManagementResource.java | 4 +- .../rest/management/users/UserResource.java | 2 +- .../rest/management/users/UsersResource.java | 3 +- .../rest/management/ManagementResourceIT.java | 8 +- .../security/sso/SSOProviderFactory.java | 2 +- .../tokens/cassandra/TokenServiceImpl.java | 848 ------------------- .../security/tokens/impl/TokenServiceImpl.java | 731 ++++++++++++++++ .../resources/usergrid-services-context.xml | 3 +- .../security/tokens/TokenServiceIT.java | 2 +- .../test/resources/testApplicationContext.xml | 2 +- 24 files changed, 1253 insertions(+), 1285 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/config/src/test/resources/usergrid-test.properties ---------------------------------------------------------------------- diff --git a/stack/config/src/test/resources/usergrid-test.properties b/stack/config/src/test/resources/usergrid-test.properties index 143d60a..5def24e 100644 --- a/stack/config/src/test/resources/usergrid-test.properties +++ b/stack/config/src/test/resources/usergrid-test.properties @@ -104,8 +104,6 @@ collection.uniquevalues.authoritative.region=us-east usergrid.counter.batch.interval=10 #usergrid.auth.token_secret_salt=super secret token value -#usergrid.auth.token_expires_from_last_use=false -#usergrid.auth.token_refresh_reuses_id=false # max time to persist tokens for (milliseconds) #usergrid.auth.token.persist.expires=0 http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/core/pom.xml ---------------------------------------------------------------------- diff --git a/stack/core/pom.xml b/stack/core/pom.xml index 09047bb..658320e 100644 --- a/stack/core/pom.xml +++ b/stack/core/pom.xml @@ -428,6 +428,12 @@ org.apache.usergrid + token + ${project.version} + + + + org.apache.usergrid actorsystem ${project.version} http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java index af297f2..ec6b775 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java @@ -44,6 +44,7 @@ import org.apache.usergrid.persistence.core.scope.ApplicationScope; import org.apache.usergrid.persistence.graph.guice.GraphModule; import org.apache.usergrid.persistence.graph.serialization.impl.migration.GraphNode; import org.apache.usergrid.persistence.index.guice.IndexModule; +import org.apache.usergrid.persistence.token.guice.TokenModule; import org.safehaus.guicyfig.GuicyFigModule; import java.util.Properties; @@ -67,6 +68,7 @@ public class CoreModule extends AbstractModule { install( new CommonModule()); install( new LockModule()); install( new CacheModule()); + install( new TokenModule()); install( new CollectionModule() { /** * configure our migration data provider for all entities in the system http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpSetup.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpSetup.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpSetup.java index 5d50ef8..5aa556a 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpSetup.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpSetup.java @@ -33,14 +33,9 @@ import org.apache.usergrid.persistence.exceptions.OrganizationAlreadyExistsExcep import com.google.inject.Injector; -import me.prettyprint.hector.api.ddl.ComparatorType; - -import static me.prettyprint.hector.api.factory.HFactory.createColumnFamilyDefinition; import static org.apache.usergrid.persistence.cassandra.CassandraPersistenceUtils.getCfDefs; import static org.apache.usergrid.persistence.cassandra.CassandraService.DEFAULT_ORGANIZATION; import static org.apache.usergrid.persistence.cassandra.CassandraService.MANAGEMENT_APPLICATION; -import static org.apache.usergrid.persistence.cassandra.CassandraService.PRINCIPAL_TOKEN_CF; -import static org.apache.usergrid.persistence.cassandra.CassandraService.TOKENS_CF; import static org.apache.usergrid.persistence.cassandra.CassandraService.getApplicationKeyspace; @@ -116,12 +111,6 @@ public class CpSetup implements Setup { logger.info( "Initialize keyspace and legacy column families" ); - cass.createColumnFamily( getApplicationKeyspace(), - createColumnFamilyDefinition( getApplicationKeyspace(), TOKENS_CF, ComparatorType.BYTESTYPE ) ); - - cass.createColumnFamily( getApplicationKeyspace(), - createColumnFamilyDefinition( getApplicationKeyspace(), PRINCIPAL_TOKEN_CF, ComparatorType.UUIDTYPE ) ); - cass.createColumnFamilies( getApplicationKeyspace(), getCfDefs( ApplicationCF.class, getApplicationKeyspace() ) ); http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java index f8b1f6c..3e08cf7 100644 --- a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java +++ b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java @@ -64,9 +64,6 @@ public class CassandraService { public static final boolean USE_VIRTUAL_KEYSPACES = true; - public static final String TOKENS_CF = "Tokens"; - public static final String PRINCIPAL_TOKEN_CF = "PrincipalTokens"; - public static final int DEFAULT_COUNT = 1000; public static final int ALL_COUNT = 100000; public static final int INDEX_ENTRY_LIST_COUNT = 1000; http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/map/src/main/java/org/apache/usergrid/persistence/map/impl/MapSerializationImpl.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/map/src/main/java/org/apache/usergrid/persistence/map/impl/MapSerializationImpl.java b/stack/corepersistence/map/src/main/java/org/apache/usergrid/persistence/map/impl/MapSerializationImpl.java index 26a1890..dda50c6 100644 --- a/stack/corepersistence/map/src/main/java/org/apache/usergrid/persistence/map/impl/MapSerializationImpl.java +++ b/stack/corepersistence/map/src/main/java/org/apache/usergrid/persistence/map/impl/MapSerializationImpl.java @@ -154,43 +154,44 @@ public class MapSerializationImpl implements MapSerialization { Preconditions.checkNotNull( key, "key is required" ); Preconditions.checkNotNull( value, "value is required" ); + final BatchStatement batchStatement = new BatchStatement(); + Statement mapEntry; Statement mapKey; if (ttl > 0){ Using timeToLive = QueryBuilder.ttl(ttl); - mapEntry = QueryBuilder.insertInto(MAP_ENTRIES_TABLE) + batchStatement.add(QueryBuilder.insertInto(MAP_ENTRIES_TABLE) .using(timeToLive) .value("key", getMapEntryPartitionKey(scope, key)) .value("column1", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)) - .value("value", DataType.text().serialize(value, ProtocolVersion.NEWEST_SUPPORTED)); + .value("value", DataType.text().serialize(value, ProtocolVersion.NEWEST_SUPPORTED))); final int bucket = BUCKET_LOCATOR.getCurrentBucket( scope.getName() ); - mapKey = QueryBuilder.insertInto(MAP_KEYS_TABLE) + batchStatement.add(QueryBuilder.insertInto(MAP_KEYS_TABLE) .using(timeToLive) .value("key", getMapKeyPartitionKey(scope, bucket)) .value("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED)) - .value("value", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)); + .value("value", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED))); }else{ - mapEntry = QueryBuilder.insertInto(MAP_ENTRIES_TABLE) + batchStatement.add(QueryBuilder.insertInto(MAP_ENTRIES_TABLE) .value("key", getMapEntryPartitionKey(scope, key)) .value("column1", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)) - .value("value", DataType.text().serialize(value, ProtocolVersion.NEWEST_SUPPORTED)); + .value("value", DataType.text().serialize(value, ProtocolVersion.NEWEST_SUPPORTED))); // get a bucket number for the map keys table final int bucket = BUCKET_LOCATOR.getCurrentBucket( scope.getName() ); - mapKey = QueryBuilder.insertInto(MAP_KEYS_TABLE) + batchStatement.add(QueryBuilder.insertInto(MAP_KEYS_TABLE) .value("key", getMapKeyPartitionKey(scope, bucket)) .value("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED)) - .value("value", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)); + .value("value", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED))); } - session.execute(mapEntry); - session.execute(mapKey); + session.execute(batchStatement); } @@ -211,23 +212,23 @@ public class MapSerializationImpl implements MapSerialization { Preconditions.checkNotNull( key, "key is required" ); Preconditions.checkNotNull( putUuid, "value is required" ); + final BatchStatement batchStatement = new BatchStatement(); - Statement mapEntry = QueryBuilder.insertInto(MAP_ENTRIES_TABLE) + batchStatement.add(QueryBuilder.insertInto(MAP_ENTRIES_TABLE) .value("key", getMapEntryPartitionKey(scope, key)) .value("column1", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)) - .value("value", DataType.uuid().serialize(putUuid, ProtocolVersion.NEWEST_SUPPORTED)); + .value("value", DataType.uuid().serialize(putUuid, ProtocolVersion.NEWEST_SUPPORTED))); - session.execute(mapEntry); final int bucket = BUCKET_LOCATOR.getCurrentBucket( scope.getName() ); - Statement mapKey; - mapKey = QueryBuilder.insertInto(MAP_KEYS_TABLE) + batchStatement.add(QueryBuilder.insertInto(MAP_KEYS_TABLE) .value("key", getMapKeyPartitionKey(scope, bucket)) .value("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED)) - .value("value", DataType.serializeValue(null, ProtocolVersion.NEWEST_SUPPORTED)); + .value("value", DataType.serializeValue(null, ProtocolVersion.NEWEST_SUPPORTED))); + + session.execute(batchStatement); - session.execute(mapKey); } http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/TokenSerialization.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/TokenSerialization.java b/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/TokenSerialization.java new file mode 100644 index 0000000..22a93f0 --- /dev/null +++ b/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/TokenSerialization.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.usergrid.persistence.token; + + +import org.apache.usergrid.persistence.core.migration.schema.Migration; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import java.util.UUID; + + +/** + * Serialize token information to/from Cassandra. This was ported over to use a newer cassandra client from the old + * persistence code @ + * + * https://github.com/apache/usergrid/tree/3f819dc0679f84edb57c52e69b58622417cfd59f + * org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl + * + */ +public interface TokenSerialization extends Migration { + + void deleteTokens(List tokenUUIDs, ByteBuffer principalKeyBuffer); + + void revokeToken(UUID tokenUUID, ByteBuffer principalKeyBuffer); + + void updateTokenAccessTime(UUID tokenUUID, long accessedTime, long inactiveTime, int ttl ); + + Map getTokenInfo(UUID tokenUUID); + + void putTokenInfo(UUID tokenUUID, Map tokenInfo, ByteBuffer principalKeyBuffer, int ttl); + + List getTokensForPrincipal(ByteBuffer principalKeyBuffer); + +} http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/guice/TokenModule.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/guice/TokenModule.java b/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/guice/TokenModule.java index 13d6b43..a8fdd4c 100644 --- a/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/guice/TokenModule.java +++ b/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/guice/TokenModule.java @@ -20,11 +20,9 @@ package org.apache.usergrid.persistence.token.guice; import com.google.inject.AbstractModule; import com.google.inject.Key; import com.google.inject.multibindings.Multibinder; -import org.apache.usergrid.persistence.cache.CacheFactory; -import org.apache.usergrid.persistence.cache.impl.CacheFactoryImpl; -import org.apache.usergrid.persistence.cache.impl.TokenSerialization; -import org.apache.usergrid.persistence.cache.impl.TokenSerializationImpl; import org.apache.usergrid.persistence.core.migration.schema.Migration; +import org.apache.usergrid.persistence.token.TokenSerialization; +import org.apache.usergrid.persistence.token.impl.TokenSerializationImpl; /** @@ -35,12 +33,10 @@ public class TokenModule extends AbstractModule { @Override protected void configure() { - bind( CacheFactory.class ).to( CacheFactoryImpl.class ); - bind( TokenSerialization.class ).to( TokenSerializationImpl.class ); Multibinder migrationBinding = Multibinder.newSetBinder( binder(), Migration.class ); - migrationBinding.addBinding().to(Key.get(TokenSerialization.class)); + migrationBinding.addBinding().to( Key.get( TokenSerialization.class ) ); } } http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerialization.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerialization.java b/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerialization.java deleted file mode 100644 index bf9337f..0000000 --- a/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerialization.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.usergrid.persistence.token.impl; - - -import org.apache.usergrid.persistence.core.migration.schema.Migration; - -import java.nio.ByteBuffer; -import java.util.List; -import java.util.Map; -import java.util.UUID; - - -/** - * Serialize token information to/from Cassandra. This was ported over to use a newer cassandra client from the old - * persistence code @ - * - * https://github.com/apache/usergrid/tree/3f819dc0679f84edb57c52e69b58622417cfd59f - * org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl - * - */ -public interface TokenSerialization extends Migration { - - void deleteToken(UUID tokenUUID); - - void revokeToken(UUID tokenUUID, ByteBuffer principalKeyBuffer); - - void updateTokenAccessTime(UUID tokenUUID, int accessdTime, int inactiveTime ); - - Map getTokenInfo(UUID tokenUUID); - - void putTokenInfo(UUID tokenUUID, Map tokenInfo); - - List getTokensForPrincipal(ByteBuffer principalKeyBuffer); - -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerializationImpl.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerializationImpl.java b/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerializationImpl.java index b07472d..2207b99 100644 --- a/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerializationImpl.java +++ b/stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerializationImpl.java @@ -17,22 +17,18 @@ package org.apache.usergrid.persistence.token.impl; import com.datastax.driver.core.*; -import com.datastax.driver.core.querybuilder.Clause; -import com.datastax.driver.core.querybuilder.QueryBuilder; -import com.fasterxml.jackson.annotation.JsonAutoDetect; -import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.datastax.driver.core.querybuilder.*; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.google.common.base.Preconditions; -import com.google.common.hash.Funnel; import com.google.inject.Inject; +import com.google.inject.Singleton; import org.apache.usergrid.persistence.core.CassandraConfig; import org.apache.usergrid.persistence.core.astyanax.*; import org.apache.usergrid.persistence.core.datastax.CQLUtils; import org.apache.usergrid.persistence.core.datastax.TableDefinition; import org.apache.usergrid.persistence.core.datastax.impl.TableDefinitionImpl; -import org.apache.usergrid.persistence.core.shard.ExpandingShardLocator; -import org.apache.usergrid.persistence.core.shard.StringHashUtils; +import org.apache.usergrid.persistence.token.TokenSerialization; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,8 +37,9 @@ import java.util.*; /** - * Serialize cache to Cassandra. + * Serialize tokens and their details to Cassandra. */ +@Singleton public class TokenSerializationImpl implements TokenSerialization { public static final Logger logger = LoggerFactory.getLogger(TokenSerializationImpl.class); @@ -51,17 +48,18 @@ public class TokenSerializationImpl implements TokenSerialization { private ObjectMapper smileMapper = new ObjectMapper( smile ); - private static final String TOKEN_UUID = "uuid"; - private static final String TOKEN_TYPE = "type"; - private static final String TOKEN_CREATED = "created"; - private static final String TOKEN_ACCESSED = "accessed"; - private static final String TOKEN_INACTIVE = "inactive"; - private static final String TOKEN_DURATION = "duration"; - private static final String TOKEN_PRINCIPAL_TYPE = "principal"; - private static final String TOKEN_ENTITY = "entity"; - private static final String TOKEN_APPLICATION = "application"; - private static final String TOKEN_STATE = "state"; - private static final String TOKEN_WORKFLOW_ORG_ID = "workflowOrgId"; + public static final String TOKEN_UUID = "uuid"; + public static final String TOKEN_TYPE = "type"; + public static final String TOKEN_CREATED = "created"; + public static final String TOKEN_ACCESSED = "accessed"; + public static final String TOKEN_INACTIVE = "inactive"; + public static final String TOKEN_DURATION = "duration"; + public static final String TOKEN_PRINCIPAL_TYPE = "principal"; + public static final String TOKEN_ENTITY = "entity"; + public static final String TOKEN_APPLICATION = "application"; + public static final String TOKEN_STATE = "state"; + public static final String TOKEN_WORKFLOW_ORG_ID = "workflowOrgId"; + public static final String TOKEN_TYPE_ACCESS = "access"; private static final Set TOKEN_PROPERTIES; @@ -81,8 +79,7 @@ public class TokenSerializationImpl implements TokenSerialization { TOKEN_PROPERTIES = Collections.unmodifiableSet(set); } - private static final HashSet REQUIRED_TOKEN_PROPERTIES = new HashSet(); - + public static final HashSet REQUIRED_TOKEN_PROPERTIES = new HashSet(); static { REQUIRED_TOKEN_PROPERTIES.add( TOKEN_UUID ); @@ -93,8 +90,6 @@ public class TokenSerializationImpl implements TokenSerialization { REQUIRED_TOKEN_PROPERTIES.add( TOKEN_DURATION ); } - - private static final String TOKENS_TABLE = CQLUtils.quote("Tokens"); private static final Collection TOKENS_PARTITION_KEYS = Collections.singletonList("key"); private static final Collection TOKENS_COLUMN_KEYS = Collections.singletonList("column1"); @@ -118,14 +113,10 @@ public class TokenSerializationImpl implements TokenSerialization { new HashMap(){{ put( "column1", "ASC" ); }}; - - private final Session session; private final CassandraConfig cassandraConfig; - - @Inject public TokenSerializationImpl(final Session session, final CassandraConfig cassandraConfig ) { @@ -136,20 +127,106 @@ public class TokenSerializationImpl implements TokenSerialization { @Override - public void deleteToken(UUID tokenUUID){ + public void deleteTokens(final List tokenUUIDs, final ByteBuffer principalKeyBuffer){ + + Preconditions.checkNotNull(tokenUUIDs, "token UUID list is required"); + + final BatchStatement batchStatement = new BatchStatement(); + + tokenUUIDs.forEach( tokenUUID -> batchStatement.add( + QueryBuilder.delete() + .from(TOKENS_TABLE) + .where(QueryBuilder + .eq("key", DataType.uuid().serialize(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED))))); + + if(principalKeyBuffer != null){ + batchStatement.add( + QueryBuilder.delete() + .from(PRINCIPAL_TOKENS_TABLE) + .where(QueryBuilder + .eq("key", principalKeyBuffer))); + } + + session.execute(batchStatement); } + @Override - public void revokeToken(UUID tokenUUID, ByteBuffer principalKeyBuffer){ + public void revokeToken(final UUID tokenUUID, final ByteBuffer principalKeyBuffer){ + + Preconditions.checkNotNull(tokenUUID, "token UUID is required"); + + final BatchStatement batchStatement = new BatchStatement(); + + batchStatement.add( + QueryBuilder.delete() + .from(TOKENS_TABLE) + .where(QueryBuilder + .eq("key", DataType.uuid().serialize(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED)))); + + if(principalKeyBuffer != null){ + batchStatement.add( + QueryBuilder.delete() + .from(PRINCIPAL_TOKENS_TABLE) + .where(QueryBuilder + .eq("key", principalKeyBuffer)) + .and(QueryBuilder + .eq("column1", DataType.uuid().serialize(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED)))); + } + + session.execute(batchStatement); } + @Override - public void updateTokenAccessTime(UUID tokenUUID, int accessdTime, int inactiveTime ){ + public void updateTokenAccessTime(UUID tokenUUID, long accessedTime, long inactiveTime, int ttl ){ + + Preconditions.checkNotNull(tokenUUID, "token UUID is required"); + Preconditions.checkNotNull(accessedTime, "accessedTime is required"); + Preconditions.checkNotNull(inactiveTime, "inactiveTime is required"); + Preconditions.checkNotNull(ttl, "ttl is required"); + + + final BatchStatement batchStatement = new BatchStatement(); + final Clause inKey = + QueryBuilder.eq("key", DataType.uuid().serialize(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED)); + final Clause whereTokenAccessed = + QueryBuilder.eq("column1", DataType.serializeValue(TOKEN_ACCESSED, ProtocolVersion.NEWEST_SUPPORTED)); + final Clause whereTokenInactive = + QueryBuilder.eq("column1", DataType.serializeValue(TOKEN_INACTIVE, ProtocolVersion.NEWEST_SUPPORTED)); + + final Assignment setAccessedTime = + QueryBuilder.set("value", DataType.serializeValue(accessedTime, ProtocolVersion.NEWEST_SUPPORTED)); + final Assignment setInactiveTime = + QueryBuilder.set("value", DataType.serializeValue(inactiveTime, ProtocolVersion.NEWEST_SUPPORTED)); + + final Using usingTTL = QueryBuilder.ttl(ttl); + + if( inactiveTime != Long.MIN_VALUE){ + batchStatement.add( + QueryBuilder + .update(TOKENS_TABLE) + .with(setInactiveTime) + .where(inKey).and(whereTokenInactive) + .using(usingTTL) + ); + } + + batchStatement.add( + QueryBuilder + .update(TOKENS_TABLE) + .with(setAccessedTime) + .where(inKey).and(whereTokenAccessed) + .using(usingTTL) + ); + + session.execute(batchStatement); } + @Override public Map getTokenInfo(UUID tokenUUID){ @@ -159,7 +236,7 @@ public class TokenSerializationImpl implements TokenSerialization { TOKEN_PROPERTIES.forEach( prop -> tokenProperties.add(DataType.serializeValue(prop, ProtocolVersion.NEWEST_SUPPORTED))); - final ByteBuffer key = DataType.text().serialize(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED); + final ByteBuffer key = DataType.uuid().serialize(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED); final Clause inKey = QueryBuilder.eq("key", key); final Clause inColumn = QueryBuilder.in("column1", tokenProperties ); @@ -176,7 +253,8 @@ public class TokenSerializationImpl implements TokenSerialization { rows.forEach( row -> { - final String name = row.getString("column1"); + final String name = (String)DataType.text() + .deserialize(row.getBytes("column1"), ProtocolVersion.NEWEST_SUPPORTED); final Object value = deserializeColumnValue(name, row.getBytes("value")); if (value == null){ @@ -190,14 +268,71 @@ public class TokenSerializationImpl implements TokenSerialization { return tokenInfo; } + @Override - public void putTokenInfo(UUID tokenUUID, Map tokenInfo){ + public void putTokenInfo(final UUID tokenUUID, final Map tokenInfo, + final ByteBuffer principalKeyBuffer, final int ttl){ + + Preconditions.checkNotNull(tokenUUID, "tokenUUID is required"); + Preconditions.checkNotNull(tokenUUID, "tokenInfo is required"); + Preconditions.checkNotNull(ttl, "ttl is required"); + + + final BatchStatement batchStatement = new BatchStatement(); + final Using usingTTL = QueryBuilder.ttl(ttl); + + tokenInfo.forEach((key, value) -> { + + ByteBuffer valueBuffer; + if(key.equalsIgnoreCase(TOKEN_STATE)){ + valueBuffer = toByteBuffer(value); + }else{ + valueBuffer = DataType.serializeValue(value, ProtocolVersion.NEWEST_SUPPORTED); + } + + batchStatement.add( + QueryBuilder.insertInto(TOKENS_TABLE) + .value("key", DataType.serializeValue(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED)) + .value("column1", DataType.serializeValue(key, ProtocolVersion.NEWEST_SUPPORTED)) + .value("value", valueBuffer) + .using(usingTTL)); + + }); + + if(principalKeyBuffer != null){ + + batchStatement.add( + QueryBuilder.insertInto(PRINCIPAL_TOKENS_TABLE) + .value("key", principalKeyBuffer) + .value("column1", DataType.serializeValue(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED)) + .value("value", ByteBuffer.wrap( new byte[] { 0 } )) + .using(usingTTL)); + + } + + session.execute(batchStatement); } + @Override public List getTokensForPrincipal(ByteBuffer principalKeyBuffer){ - return new ArrayList<>(); + + Preconditions.checkNotNull(principalKeyBuffer, "principal key bytebuffer cannot be null"); + + Clause inPrincipal = QueryBuilder.eq("key", principalKeyBuffer); + Statement statement = QueryBuilder + .select() + .column("column1") + .from(PRINCIPAL_TOKENS_TABLE) + .where(inPrincipal); + + final List rows = session.execute(statement).all(); + final List tokenUUIDs = new ArrayList<>(rows.size()); + + rows.forEach(row -> tokenUUIDs.add(row.getUUID("column1"))); + + return tokenUUIDs; } @@ -219,13 +354,32 @@ public class TokenSerializationImpl implements TokenSerialization { case TOKEN_UUID: return DataType.uuid().deserialize(bb, ProtocolVersion.NEWEST_SUPPORTED); case TOKEN_STATE: - fromByteBuffer(bb, Object.class); + return fromByteBuffer(bb, Object.class); } return null; } + private ByteBuffer toByteBuffer( Object obj ) { + if ( obj == null ) { + return null; + } + + byte[] bytes = null; + try { + bytes = smileMapper.writeValueAsBytes( obj ); + } + catch ( Exception e ) { + logger.error( "Error getting SMILE bytes", e ); + } + if ( bytes != null ) { + return ByteBuffer.wrap( bytes ); + } + return null; + } + + private Object fromByteBuffer( ByteBuffer byteBuffer, Class clazz ) { if ( ( byteBuffer == null ) || !byteBuffer.hasRemaining() ) { return null; @@ -245,183 +399,14 @@ public class TokenSerializationImpl implements TokenSerialization { return obj; } -// @Override -// public V readValue(CacheScope scope, K key, TypeReference typeRef ) { -// -// return readValueCQL( scope, key, typeRef); -// -// } - - -// private V readValueCQL(CacheScope scope, K key, TypeReference typeRef){ -// -// Preconditions.checkNotNull(scope, "scope is required"); -// Preconditions.checkNotNull(key, "key is required"); -// -// final String rowKeyString = scope.getApplication().getUuid().toString(); -// final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString); -// -// // determine column name based on K key to string -// final String columnName = key.toString(); -// -// final Clause inKey = QueryBuilder.eq("key", getPartitionKey(scope, rowKeyString, bucket) ); -// final Clause inColumn = QueryBuilder.eq("column1", DataType.text().serialize(columnName, ProtocolVersion.NEWEST_SUPPORTED) ); -// -// final Statement statement = QueryBuilder.select().all().from(TOKENS_TABLE) -// .where(inKey) -// .and(inColumn) -// .setConsistencyLevel(cassandraConfig.getDataStaxReadCl()); -// -// final ResultSet resultSet = session.execute(statement); -// final com.datastax.driver.core.Row row = resultSet.one(); -// -// if (row == null){ -// -// if(logger.isDebugEnabled()){ -// logger.debug("Cache value not found for key {}", key ); -// } -// -// return null; -// } -// -// -// try { -// -// return MAPPER.readValue(row.getBytes("value").array(), typeRef); -// -// } catch (IOException ioe) { -// logger.error("Unable to read cached value", ioe); -// throw new RuntimeException("Unable to read cached value", ioe); -// } -// -// -// } - - -// @Override -// public V writeValue(CacheScope scope, K key, V value, Integer ttl) { -// -// return writeValueCQL( scope, key, value, ttl); -// -// } -// -// private V writeValueCQL(CacheScope scope, K key, V value, Integer ttl) { -// -// Preconditions.checkNotNull( scope, "scope is required"); -// Preconditions.checkNotNull( key, "key is required" ); -// Preconditions.checkNotNull( value, "value is required"); -// Preconditions.checkNotNull( ttl, "ttl is required"); -// -// -// final String rowKeyString = scope.getApplication().getUuid().toString(); -// final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString); -// -// // determine column name based on K key to string -// final String columnName = key.toString(); -// -// // serialize cache item -// byte[] cacheBytes; -// try { -// cacheBytes = MAPPER.writeValueAsBytes(value); -// } catch (JsonProcessingException jpe) { -// throw new RuntimeException("Unable to serialize cache value", jpe); -// } -// -// final Using timeToLive = QueryBuilder.ttl(ttl); -// -// -// // convert to ByteBuffer for the blob DataType in Cassandra -// final ByteBuffer bb = ByteBuffer.allocate(cacheBytes.length); -// bb.put(cacheBytes); -// bb.flip(); -// -// final Statement cacheEntry = QueryBuilder.insertInto(TOKENS_TABLE) -// .using(timeToLive) -// .value("key", getPartitionKey(scope, rowKeyString, bucket)) -// .value("column1", DataType.text().serialize(columnName, ProtocolVersion.NEWEST_SUPPORTED)) -// .value("value", bb); -// -// -// session.execute(cacheEntry); -// -// logger.debug("Wrote cache item to scope {}\n key/value types {}/{}\n key:value: {}:{}", -// scope.getApplication().getUuid(), -// key.getClass().getSimpleName(), -// value.getClass().getSimpleName(), -// key, -// value); -// -// return value; -// -// } -// -// -// -// @Override -// public void removeValue(CacheScope scope, K key) { -// -// removeValueCQL(scope, key); -// -// } -// -// -// private void removeValueCQL(CacheScope scope, K key) { -// -// Preconditions.checkNotNull( scope, "scope is required"); -// Preconditions.checkNotNull( key, "key is required" ); -// -// // determine bucketed row-key based application UUID -// -// final String rowKeyString = scope.getApplication().getUuid().toString(); -// final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString); -// -// // determine column name based on K key to string -// final String columnName = key.toString(); -// -// -// final Clause inKey = QueryBuilder.eq("key", getPartitionKey(scope, rowKeyString, bucket) ); -// final Clause inColumn = QueryBuilder.eq("column1", DataType.text().serialize(columnName, ProtocolVersion.NEWEST_SUPPORTED) ); -// -// final Statement statement = QueryBuilder.delete().from(TOKENS_TABLE) -// .where(inKey) -// .and(inColumn); -// -// session.execute(statement); -// -// } -// -// -// @Override -// public void invalidate(CacheScope scope) { -// -// invalidateCQL(scope); -// logger.debug("Invalidated scope {}", scope.getApplication().getUuid()); -// -// } -// -// private void invalidateCQL(CacheScope scope){ -// -// Preconditions.checkNotNull(scope, "scope is required"); -// -// // determine bucketed row-key based application UUID -// final String rowKeyString = scope.getApplication().getUuid().toString(); -// final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString); -// -// final Clause inKey = QueryBuilder.eq("key", getPartitionKey(scope, rowKeyString, bucket) ); -// -// final Statement statement = QueryBuilder.delete().from(TOKENS_TABLE) -// .where(inKey); -// -// session.execute(statement); -// -// } -// + @Override public Collection getColumnFamilies() { return Collections.emptyList(); } + @Override public Collection getTables() { @@ -447,90 +432,5 @@ public class TokenSerializationImpl implements TokenSerialization { return Arrays.asList(tokens, principalTokens); } -// -// -// -// private ByteBuffer getPartitionKey(CacheScope scope, String key, int bucketNumber){ -// -// return serializeKeys(scope.getApplication().getUuid(), -// scope.getApplication().getType(), bucketNumber, key); -// -// } - - private static ByteBuffer serializeTokenKey(UUID ownerUUID, String ownerType, int bucketNumber, String rowKeyString ){ - - List keys = new ArrayList<>(4); - keys.add(0, ownerUUID); - keys.add(1, ownerType); - keys.add(2, bucketNumber); - keys.add(3, rowKeyString); - - // UUIDs are 16 bytes, allocate the buffer accordingly - int size = 16+ownerType.getBytes().length+rowKeyString.getBytes().length; - - // ints are 4 bytes, add for the bucket - size += 4; - - - // we always need to add length for the 2 byte short and 1 byte equality - size += keys.size()*3; - - ByteBuffer stuff = ByteBuffer.allocate(size); - - for (Object key : keys) { - - ByteBuffer kb = DataType.serializeValue(key, ProtocolVersion.NEWEST_SUPPORTED); - if (kb == null) { - kb = ByteBuffer.allocate(0); - } - - stuff.putShort((short) kb.remaining()); - stuff.put(kb.slice()); - stuff.put((byte) 0); - - - } - stuff.flip(); - return stuff; - - } - - private static ByteBuffer serializeKeys(UUID ownerUUID, String ownerType, int bucketNumber, String rowKeyString ){ - - List keys = new ArrayList<>(4); - keys.add(0, ownerUUID); - keys.add(1, ownerType); - keys.add(2, bucketNumber); - keys.add(3, rowKeyString); - - // UUIDs are 16 bytes, allocate the buffer accordingly - int size = 16+ownerType.getBytes().length+rowKeyString.getBytes().length; - - // ints are 4 bytes, add for the bucket - size += 4; - - - // we always need to add length for the 2 byte short and 1 byte equality - size += keys.size()*3; - - ByteBuffer stuff = ByteBuffer.allocate(size); - - for (Object key : keys) { - - ByteBuffer kb = DataType.serializeValue(key, ProtocolVersion.NEWEST_SUPPORTED); - if (kb == null) { - kb = ByteBuffer.allocate(0); - } - - stuff.putShort((short) kb.remaining()); - stuff.put(kb.slice()); - stuff.put((byte) 0); - - - } - stuff.flip(); - return stuff; - - } } http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TestTokenModule.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TestTokenModule.java b/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TestTokenModule.java new file mode 100644 index 0000000..bfa0aa0 --- /dev/null +++ b/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TestTokenModule.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.usergrid.persistence.token; + + +import org.apache.usergrid.persistence.token.guice.TokenModule; +import org.apache.usergrid.persistence.token.impl.TokenSerializationImpl; +import org.apache.usergrid.persistence.core.guice.CommonModule; + + + +public class TestTokenModule extends org.apache.usergrid.persistence.core.guice.TestModule { + + @Override + protected void configure() { + + install( new CommonModule() ); + install( new TokenModule() ); + + } +} http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenSerializationTest.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenSerializationTest.java b/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenSerializationTest.java new file mode 100644 index 0000000..7beeb56 --- /dev/null +++ b/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenSerializationTest.java @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.usergrid.persistence.token; + +import net.jcip.annotations.NotThreadSafe; +import org.apache.usergrid.persistence.model.util.UUIDGenerator; +import org.apache.usergrid.persistence.token.impl.TokenSerializationImpl; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.apache.usergrid.persistence.core.guice.MigrationManagerRule; +import org.apache.usergrid.persistence.core.test.ITRunner; +import org.apache.usergrid.persistence.core.test.UseModules; + +import com.google.inject.Inject; + +import java.nio.ByteBuffer; +import java.util.*; + +import static org.junit.Assert.assertEquals; + + +@RunWith( ITRunner.class ) +@UseModules( { TestTokenModule.class } ) +@NotThreadSafe +public class TokenSerializationTest { + + + @Inject + @Rule + public MigrationManagerRule migrationManagerRule; + + @Inject + public TokenSerialization tokenSerialization; + + @Test + public void putTokenInfo() { + + UUID uuid = UUIDGenerator.newTimeUUID(); + Map tokenDetails = new HashMap<>(); + tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token"); + + ByteBuffer principalKeyBuffer = ByteBuffer.wrap("test-principal".getBytes()); + + tokenSerialization.putTokenInfo(uuid, tokenDetails, principalKeyBuffer, 60); + + } + + @Test + public void getTokenInfo() { + + UUID uuid = UUIDGenerator.newTimeUUID(); + Map tokenDetails = new HashMap<>(); + tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token"); + + ByteBuffer principalKeyBuffer = ByteBuffer.wrap("test-principal".getBytes()); + + tokenSerialization.putTokenInfo(uuid, tokenDetails, principalKeyBuffer, 60); + + Map returnedDetails = tokenSerialization.getTokenInfo(uuid); + + assertEquals( + tokenDetails.get(TokenSerializationImpl.TOKEN_TYPE), + returnedDetails.get(TokenSerializationImpl.TOKEN_TYPE) + ); + } + + @Test + public void updateAccessTokenTime() { + + UUID uuid = UUIDGenerator.newTimeUUID(); + Map tokenDetails = new HashMap<>(); + tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token"); + + long accessedTime = System.currentTimeMillis()-1000000; + long inactiveTime = accessedTime+2000000; + + tokenDetails.put(TokenSerializationImpl.TOKEN_ACCESSED, accessedTime); + tokenDetails.put(TokenSerializationImpl.TOKEN_INACTIVE, inactiveTime); + + ByteBuffer principalKeyBuffer = ByteBuffer.wrap("test-principal".getBytes()); + + tokenSerialization.putTokenInfo(uuid, tokenDetails, principalKeyBuffer, 60); + + Map returnedDetails = tokenSerialization.getTokenInfo(uuid); + + assertEquals( + accessedTime, + returnedDetails.get(TokenSerializationImpl.TOKEN_ACCESSED) + ); + + long newAccessedTime = System.currentTimeMillis(); + long newInactiveTime = newAccessedTime+1000000; + + tokenSerialization.updateTokenAccessTime(uuid, newAccessedTime,newInactiveTime, 1200); + + returnedDetails = tokenSerialization.getTokenInfo(uuid); + + assertEquals( + newAccessedTime, + returnedDetails.get(TokenSerializationImpl.TOKEN_ACCESSED) + ); + } + + @Test + public void deleteTokens() { + + UUID uuid = UUIDGenerator.newTimeUUID(); + Map tokenDetails = new HashMap<>(); + tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token"); + + ByteBuffer principalKeyBuffer = ByteBuffer.wrap("test-principal".getBytes()); + + tokenSerialization.putTokenInfo(uuid, tokenDetails, principalKeyBuffer, 60); + + Map returnedDetails = tokenSerialization.getTokenInfo(uuid); + + assertEquals( + tokenDetails.get(TokenSerializationImpl.TOKEN_TYPE), + returnedDetails.get(TokenSerializationImpl.TOKEN_TYPE) + ); + + tokenSerialization.deleteTokens(Collections.singletonList(uuid), principalKeyBuffer); + + returnedDetails = tokenSerialization.getTokenInfo(uuid); + + assertEquals( + 0, + returnedDetails.size() + ); + } + + @Test + public void revokeToken() { + + UUID uuid = UUIDGenerator.newTimeUUID(); + Map tokenDetails = new HashMap<>(); + tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token"); + + ByteBuffer principalKeyBuffer = ByteBuffer.wrap("test-principal".getBytes()); + + tokenSerialization.putTokenInfo(uuid, tokenDetails, principalKeyBuffer, 60); + + Map returnedDetails = tokenSerialization.getTokenInfo(uuid); + + assertEquals( + tokenDetails.get(TokenSerializationImpl.TOKEN_TYPE), + returnedDetails.get(TokenSerializationImpl.TOKEN_TYPE) + ); + + tokenSerialization.revokeToken(uuid, principalKeyBuffer); + + returnedDetails = tokenSerialization.getTokenInfo(uuid); + + assertEquals( + 0, + returnedDetails.size() + ); + } + + @Test + public void getTokensForPrincipal() { + + UUID uuid = UUIDGenerator.newTimeUUID(); + Map tokenDetails = new HashMap<>(); + tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token"); + + String principal = "test-principal-"+System.currentTimeMillis(); + + ByteBuffer principalKeyBuffer = ByteBuffer.wrap(principal.getBytes()); + + tokenSerialization.putTokenInfo(uuid, tokenDetails, principalKeyBuffer, 60); + + principalKeyBuffer = ByteBuffer.wrap(principal.getBytes()); + + List returnTokens = tokenSerialization.getTokensForPrincipal(principalKeyBuffer); + + assertEquals( + returnTokens.get(0), + uuid + ); + } + +} http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenTestModule.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenTestModule.java b/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenTestModule.java deleted file mode 100644 index b454d73..0000000 --- a/stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenTestModule.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.usergrid.persistence.token; - - -import org.apache.usergrid.persistence.token.guice.TokenModule; -import org.apache.usergrid.persistence.token.impl.TokenSerialization; -import org.apache.usergrid.persistence.token.impl.TokenSerializationImpl; -import org.apache.usergrid.persistence.core.guice.CommonModule; - - - -public class TokenTestModule extends org.apache.usergrid.persistence.core.guice.TestModule { - - @Override - protected void configure() { - - install( new CommonModule() ); - install( new TokenModule() ); - - bind(TokenSerialization.class).to(TokenSerializationImpl.class).asEagerSingleton(); - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/launcher/src/main/resources/usergrid-standalone.properties ---------------------------------------------------------------------- diff --git a/stack/launcher/src/main/resources/usergrid-standalone.properties b/stack/launcher/src/main/resources/usergrid-standalone.properties index 03d9b80..cd014e6 100644 --- a/stack/launcher/src/main/resources/usergrid-standalone.properties +++ b/stack/launcher/src/main/resources/usergrid-standalone.properties @@ -63,8 +63,6 @@ usergrid.version.properties=1.0.0 usergrid.version.build=${version} #usergrid.auth.token_secret_salt=super secret token value -#usergrid.auth.token_expires_from_last_use=false -#usergrid.auth.token_refresh_reuses_id=false # max time to persist tokens for (milliseconds) #usergrid.auth.token.persist.expires=0 http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java index 12e1270..48aec78 100644 --- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java +++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java @@ -41,7 +41,7 @@ import org.apache.usergrid.security.shiro.utils.SubjectUtils; import org.apache.usergrid.security.sso.ApigeeSSO2Provider; import org.apache.usergrid.security.sso.ExternalSSOProvider; import org.apache.usergrid.security.sso.SSOProviderFactory; -import org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl; +import org.apache.usergrid.security.tokens.impl.TokenServiceImpl; import org.apache.usergrid.security.tokens.exceptions.BadTokenException; import org.apache.usergrid.utils.JsonUtils; import org.glassfish.jersey.server.mvc.Viewable; @@ -62,7 +62,7 @@ import java.util.Map; import static javax.servlet.http.HttpServletResponse.*; import static javax.ws.rs.core.MediaType.*; import static org.apache.commons.lang.StringUtils.isNotBlank; -import static org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl.USERGRID_EXTERNAL_SSO_ENABLED; +import static org.apache.usergrid.security.tokens.impl.TokenServiceImpl.USERGRID_EXTERNAL_SSO_ENABLED; import static org.apache.usergrid.utils.JsonUtils.mapToJsonString; import static org.apache.usergrid.utils.StringUtils.stringOrSubstringAfterFirst; import static org.apache.usergrid.utils.StringUtils.stringOrSubstringBeforeFirst; http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UserResource.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UserResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UserResource.java index 066f734..4cbe9b2 100644 --- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UserResource.java +++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UserResource.java @@ -30,7 +30,7 @@ import org.apache.usergrid.rest.management.users.organizations.OrganizationsReso import org.apache.usergrid.rest.security.annotations.RequireAdminUserAccess; import org.apache.usergrid.security.shiro.principals.PrincipalIdentifier; import org.apache.usergrid.security.tokens.TokenInfo; -import org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl; +import org.apache.usergrid.security.tokens.impl.TokenServiceImpl; import org.apache.usergrid.security.tokens.exceptions.TokenException; import org.apache.usergrid.services.ServiceResults; import org.glassfish.jersey.server.mvc.Viewable; http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UsersResource.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UsersResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UsersResource.java index 6999841..634e5da 100644 --- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UsersResource.java +++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/users/UsersResource.java @@ -29,7 +29,7 @@ import org.apache.usergrid.rest.RootResource; import org.apache.usergrid.rest.exceptions.AuthErrorInfo; import org.apache.usergrid.rest.exceptions.RedirectionException; import org.apache.usergrid.security.shiro.utils.SubjectUtils; -import org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl; +import org.apache.usergrid.security.tokens.impl.TokenServiceImpl; import org.glassfish.jersey.server.mvc.Viewable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,7 +46,6 @@ import java.util.UUID; import static org.apache.commons.lang.StringUtils.isBlank; import static org.apache.usergrid.rest.exceptions.SecurityException.mappableSecurityException; import static org.apache.usergrid.security.shiro.utils.SubjectUtils.isServiceAdmin; -import static org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl.USERGRID_EXTERNAL_SSO_PROVIDER_URL; @Component( "org.apache.usergrid.rest.management.users.UsersResource" ) http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java index 0a80d73..8d18521f 100644 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java @@ -25,7 +25,7 @@ import org.apache.usergrid.rest.management.organizations.OrganizationsResource; import org.apache.usergrid.rest.test.resource.AbstractRestIT; import org.apache.usergrid.rest.test.resource.model.*; import org.apache.usergrid.rest.test.resource.model.Collection; -import org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl; +import org.apache.usergrid.security.tokens.impl.TokenServiceImpl; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; @@ -38,9 +38,9 @@ import javax.ws.rs.core.Response; import java.io.IOException; import java.util.*; -import static org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl.USERGRID_EXTERNAL_SSO_PROVIDER; -import static org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl.USERGRID_EXTERNAL_SSO_PROVIDER_URL; -import static org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl.USERGRID_EXTERNAL_SSO_ENABLED; +import static org.apache.usergrid.security.tokens.impl.TokenServiceImpl.USERGRID_EXTERNAL_SSO_PROVIDER; +import static org.apache.usergrid.security.tokens.impl.TokenServiceImpl.USERGRID_EXTERNAL_SSO_PROVIDER_URL; +import static org.apache.usergrid.security.tokens.impl.TokenServiceImpl.USERGRID_EXTERNAL_SSO_ENABLED; import static org.apache.usergrid.utils.MapUtils.hashMap; import static org.junit.Assert.*; http://git-wip-us.apache.org/repos/asf/usergrid/blob/146e47d6/stack/services/src/main/java/org/apache/usergrid/security/sso/SSOProviderFactory.java ---------------------------------------------------------------------- diff --git a/stack/services/src/main/java/org/apache/usergrid/security/sso/SSOProviderFactory.java b/stack/services/src/main/java/org/apache/usergrid/security/sso/SSOProviderFactory.java index 31e085e..b723d9e 100644 --- a/stack/services/src/main/java/org/apache/usergrid/security/sso/SSOProviderFactory.java +++ b/stack/services/src/main/java/org/apache/usergrid/security/sso/SSOProviderFactory.java @@ -18,7 +18,7 @@ package org.apache.usergrid.security.sso; import org.apache.usergrid.corepersistence.CpEntityManagerFactory; import org.apache.usergrid.persistence.EntityManagerFactory; -import org.apache.usergrid.security.tokens.cassandra.TokenServiceImpl; +import org.apache.usergrid.security.tokens.impl.TokenServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import java.util.List;