smolnar82 commented on code in PR #826: URL: https://github.com/apache/knox/pull/826#discussion_r1440274597
########## 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"; + private static final String TOKEN_MAX_LIFETIME_POSTFIX = TOKEN_ALIAS_SUFFIX_DELIM + "max"; + private static final String TOKEN_META_POSTFIX = TOKEN_ALIAS_SUFFIX_DELIM + "meta"; + private static final TokenStateServiceMessages LOG = MessagesFactory.get(TokenStateServiceMessages.class); + + private final AliasService aliasService; + private final TokenStateService tokenStateService; + private final PrintStream out; + + private int progressCount = 10; + private boolean archiveMigratedTokens; + private boolean migrateExpiredTokens; + private boolean verbose; + + public TokenMigrationTool(AliasService aliasService, TokenStateService tokenStateService, PrintStream out) { + this.aliasService = aliasService; + this.tokenStateService = tokenStateService; + this.out = out; + } + + public void setProgressCount(int progressCount) { + this.progressCount = progressCount; + } + + public void setArchiveMigratedTokens(boolean archiveMigratedTokens) { + this.archiveMigratedTokens = archiveMigratedTokens; + } + + public void setMigrateExpiredTokens(boolean migrateExpiredTokens) { + this.migrateExpiredTokens = migrateExpiredTokens; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + public void migrateTokensFromGatewayCredentialStore() { + try { + final Map<String, TokenData> tokenDataMap = new ConcurrentHashMap<>(); + final long start = System.currentTimeMillis(); + String logMessage = "Loading token aliases from the __gateway credential store. This could take a while."; + log(logMessage); + final Map<String, char[]> passwordAliasMap = aliasService.getPasswordsForGateway(); + log("Token aliases loaded in " + (System.currentTimeMillis() - start) + " milliseconds"); + String alias; + for (Map.Entry<String, char[]> passwordAliasMapEntry : passwordAliasMap.entrySet()) { + alias = passwordAliasMapEntry.getKey(); + processAlias(passwordAliasMap, passwordAliasMapEntry, alias, tokenDataMap); + } + + final long migrationStart = System.currentTimeMillis(); + final AtomicInteger count = new AtomicInteger(0); Review Comment: No, there is no multi-threading here. I needed to use AtomicInteger because of the stream below accepts a final `count` variable. -- 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