pzampino commented on code in PR #826:
URL: https://github.com/apache/knox/pull/826#discussion_r1473463201


##########
gateway-server/src/main/java/org/apache/knox/gateway/services/factory/TokenStateServiceFactory.java:
##########
@@ -61,17 +62,32 @@ protected Service createService(GatewayServices 
gatewayServices, ServiceType ser
           service.init(gatewayConfig, options);
         } catch (ServiceLifecycleException e) {
           LOG.errorInitializingService(implementation, e.getMessage(), e);
-          service = new AliasBasedTokenStateService();
-          ((AliasBasedTokenStateService) 
service).setAliasService(getAliasService(gatewayServices));
+          service = useDerbyDatabaseTokenStateService(gatewayServices, 
gatewayConfig, options);
         }
+      } else if (matchesImplementation(implementation, 
DerbyDBTokenStateService.class, true)) {
+        service = useDerbyDatabaseTokenStateService(gatewayServices, 
gatewayConfig, options);
       }
 
-      logServiceUsage(isEmptyDefaultImplementation(implementation) ? 
AliasBasedTokenStateService.class.getName() : implementation, serviceType);
+      logServiceUsage(service.getClass().getName(), serviceType);
     }
 
     return service;
   }
 
+  private Service useDerbyDatabaseTokenStateService(GatewayServices 
gatewayServices, GatewayConfig gatewayConfig, Map<String, String> options) {
+    Service service;
+    try {
+      service = new DerbyDBTokenStateService();
+      ((DerbyDBTokenStateService) 
service).setAliasService(getAliasService(gatewayServices));

Review Comment:
   If the constructors accept GatewayServices, GatewayConfig, and options, the 
implementations can do all their own initialization in a self-contained manner, 
simplifying the factory implementation.



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateService.java:
##########
@@ -65,12 +76,33 @@ public void init(GatewayConfig config, Map<String, String> 
options) throws Servi
         } catch (Exception e) {
           throw new ServiceLifecycleException("Error while initiating 
JDBCTokenStateService: " + e, e);
         }
+
+        this.skipTokenMigration = config.skipTokenMigration();
+        this.archiveMigratedTokens = config.archiveMigratedTokens();
+        this.migrateExpiredTokens = config.migrateExpiredTokens();
+        this.verboseTokenMigration = 
config.printVerboseTokenMigrationMessages();
+        this.tokenMigrationProgressCount = 
config.getTokenMigrationProgressCount();
       } finally {
         initLock.unlock();
       }
     }
   }
 
+  @Override
+  public void start() throws ServiceLifecycleException {
+    super.start();
+    if (skipTokenMigration) {
+      log.skipTokenMigration();
+    } else {
+      final TokenMigrationTool tokenMigrationTool = new 
TokenMigrationTool(aliasService, this, null);

Review Comment:
   Token migration is an implicit action when a JDBC-based impl starts? I 
missed that part when I first read the PR description. Should 
skipTokenMigration default to true, such that an admin has to explicitly enable 
this implicit migration?



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/DefaultTokenStateService.java:
##########
@@ -457,4 +457,9 @@ private Collection<KnoxToken> fetchTokens(String userName, 
boolean createdBy) {
     });
     return tokens;
   }
+
+  @Override
+  public boolean isMigrationTarget() {

Review Comment:
   Instead of a method to override in every impl, you could have those 
implementations that support migration implement a MigrationTarget interface.



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/factory/TokenStateServiceFactory.java:
##########
@@ -61,17 +62,32 @@ protected Service createService(GatewayServices 
gatewayServices, ServiceType ser
           service.init(gatewayConfig, options);
         } catch (ServiceLifecycleException e) {
           LOG.errorInitializingService(implementation, e.getMessage(), e);
-          service = new AliasBasedTokenStateService();
-          ((AliasBasedTokenStateService) 
service).setAliasService(getAliasService(gatewayServices));
+          service = useDerbyDatabaseTokenStateService(gatewayServices, 
gatewayConfig, options);
         }
+      } else if (matchesImplementation(implementation, 
DerbyDBTokenStateService.class, true)) {

Review Comment:
   There is something about this logic that smells bad to me. From my point of 
view, someone should be able to implement TokenStateService with whatever impl 
they need, configure it in gateway-site, and drop their JAR in the ext dir. 
With this logic, that won't work. Can we consider refactoring this to keep Knox 
extensible?



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/factory/TokenStateServiceFactory.java:
##########
@@ -61,17 +62,32 @@ protected Service createService(GatewayServices 
gatewayServices, ServiceType ser
           service.init(gatewayConfig, options);
         } catch (ServiceLifecycleException e) {
           LOG.errorInitializingService(implementation, e.getMessage(), e);
-          service = new AliasBasedTokenStateService();
-          ((AliasBasedTokenStateService) 
service).setAliasService(getAliasService(gatewayServices));
+          service = useDerbyDatabaseTokenStateService(gatewayServices, 
gatewayConfig, options);
         }
+      } else if (matchesImplementation(implementation, 
DerbyDBTokenStateService.class, true)) {
+        service = useDerbyDatabaseTokenStateService(gatewayServices, 
gatewayConfig, options);
       }
 
-      logServiceUsage(isEmptyDefaultImplementation(implementation) ? 
AliasBasedTokenStateService.class.getName() : implementation, serviceType);
+      logServiceUsage(service.getClass().getName(), serviceType);
     }
 
     return service;
   }
 
+  private Service useDerbyDatabaseTokenStateService(GatewayServices 
gatewayServices, GatewayConfig gatewayConfig, Map<String, String> options) {
+    Service service;
+    try {
+      service = new DerbyDBTokenStateService();
+      ((DerbyDBTokenStateService) 
service).setAliasService(getAliasService(gatewayServices));
+      ((DerbyDBTokenStateService) 
service).setMasterService(getMasterService(gatewayServices));
+      service.init(gatewayConfig, options);
+    } catch (ServiceLifecycleException e) {
+      LOG.errorInitializingService(DerbyDBTokenStateService.class.getName(), 
e.getMessage(), e);
+      service = new DefaultTokenStateService();

Review Comment:
   Falling back to in-memory is ok with me, so long as we log some indication 
about that fact. If a user thinks their Knox is running against the DB they 
configured, but it's only in-memory, that could be a real problem.



##########
gateway-server/src/main/java/org/apache/knox/gateway/util/TokenMigrationTool.java:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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.knox.gateway.util;
+
+import java.io.PrintStream;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+import org.apache.knox.gateway.services.security.token.TokenStateService;
+import org.apache.knox.gateway.services.token.impl.TokenStateServiceMessages;
+
+public class TokenMigrationTool {
+
+  private static final String TOKEN_ALIAS_SUFFIX_DELIM = "--";
+  private static final String TOKEN_ISSUE_TIME_POSTFIX = 
TOKEN_ALIAS_SUFFIX_DELIM + "iss";

Review Comment:
   It would be ideal if these were referenced from the 
AliasBasedTokenStateService where they're defined.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@knox.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to