[ 
https://issues.apache.org/jira/browse/KNOX-2554?focusedWorklogId=582042&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-582042
 ]

ASF GitHub Bot logged work on KNOX-2554:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 13/Apr/21 20:16
            Start Date: 13/Apr/21 20:16
    Worklog Time Spent: 10m 
      Work Description: moresandeep commented on a change in pull request #433:
URL: https://github.com/apache/knox/pull/433#discussion_r612741505



##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateService.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.services.token.impl;
+
+import java.sql.SQLException;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.ServiceLifecycleException;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+import org.apache.knox.gateway.services.security.token.UnknownTokenException;
+import org.apache.knox.gateway.util.JDBCUtils;
+import org.apache.knox.gateway.util.Tokens;
+
+public class JDBCTokenStateService extends DefaultTokenStateService {
+  private AliasService aliasService; // connection username/pw is stored here
+  private TokenStateDatabase tokenDatabase;
+
+  public void setAliasService(AliasService aliasService) {
+    this.aliasService = aliasService;
+  }
+
+  @Override
+  public void init(GatewayConfig config, Map<String, String> options) throws 
ServiceLifecycleException {
+    super.init(config, options);
+    if (aliasService == null) {
+      throw new ServiceLifecycleException("The required AliasService reference 
has not been set.");
+    }
+    try {
+      this.tokenDatabase = new 
TokenStateDatabase(JDBCUtils.getDataSource(config, aliasService));
+    } catch (Exception e) {
+      throw new ServiceLifecycleException("Error while initiating 
JDBCTokenStateService: " + e, e);
+    }
+  }
+
+  @Override
+  public void addToken(String tokenId, long issueTime, long expiration, long 
maxLifetimeDuration) {
+    super.addToken(tokenId, issueTime, expiration, maxLifetimeDuration);
+    try {
+      final boolean added = tokenDatabase.addToken(tokenId, issueTime, 
expiration, maxLifetimeDuration);
+      if (added) {
+        log.savedTokenInDatabase(Tokens.getTokenIDDisplayText(tokenId));
+      } else {
+        log.failedToSaveTokenInDatabase(Tokens.getTokenIDDisplayText(tokenId));
+      }
+    } catch (SQLException e) {
+      log.errorSavingTokenInDatabase(Tokens.getTokenIDDisplayText(tokenId), 
e.getMessage(), e);
+    }
+  }
+
+  @Override
+  protected void removeTokens(Set<String> tokenIds) {
+    try {
+      boolean removed = tokenDatabase.removeTokens(tokenIds);
+      if (removed) {
+        log.removedTokensFromDatabase(tokenIds.size());
+      } else {
+        log.failedToRemoveTokensFromDatabase(tokenIds.size());
+      }
+    } catch (SQLException e) {
+      log.errorRemovingTokensFromDatabase(tokenIds.size(), e.getMessage(), e);
+    }
+    super.removeTokens(tokenIds);
+  }
+
+  @Override
+  public long getTokenExpiration(String tokenId, boolean validate) throws 
UnknownTokenException {
+    try {
+      // check the in-memory cache, then
+      return super.getTokenExpiration(tokenId, validate);
+    } catch (UnknownTokenException e) {
+      // It's not in memory
+    }
+
+    long expiration = 0;
+    try {
+      expiration = tokenDatabase.getTokenExpiration(tokenId);
+      log.fetchedExpirationFromDatabase(Tokens.getTokenIDDisplayText(tokenId), 
expiration);
+    } catch (SQLException e) {
+      
log.errorFetchingExpirationFromDatabase(Tokens.getTokenIDDisplayText(tokenId), 
e.getMessage(), e);
+    }
+    return expiration;
+  }
+
+  @Override
+  protected void updateExpiration(String tokenId, long expiration) {
+    // Update in-memory
+    super.updateExpiration(tokenId, expiration);
+
+    try {
+      final boolean updated = tokenDatabase.updateExpiration(tokenId, 
expiration);
+      if (updated) {
+        log.updatedExpirationInDatabase(Tokens.getTokenIDDisplayText(tokenId), 
expiration);
+      } else {
+        
log.failedToUpdateExpirationInDatabase(Tokens.getTokenIDDisplayText(tokenId), 
expiration);
+      }
+    } catch (SQLException e) {
+      
log.errorUpdatingExpirationInDatabase(Tokens.getTokenIDDisplayText(tokenId), 
e.getMessage(), e);
+    }
+  }
+
+  @Override
+  protected long getMaxLifetime(String tokenId) {
+    long maxLifetime = super.getMaxLifetime(tokenId);
+
+    // If there is no result from the in-memory collection, proceed to check 
the Database
+    if (maxLifetime < 1L) {
+      try {
+        maxLifetime = tokenDatabase.getMaxLifetime(tokenId);
+        
log.fetchedMaxLifetimeFromDatabase(Tokens.getTokenIDDisplayText(tokenId), 
maxLifetime);
+      } catch (SQLException e) {
+        
log.errorFetchingMaxLifetimeFromDatabase(Tokens.getTokenIDDisplayText(tokenId), 
e.getMessage(), e);
+      }
+    }
+    return maxLifetime;
+  }
+
+  @Override
+  protected boolean isUnknown(String tokenId) {
+    boolean isUnknown = super.isUnknown(tokenId);
+
+    // If it's not in the cache, then check in the Database
+    if (isUnknown) {
+      try {
+        isUnknown = tokenDatabase.getMaxLifetime(tokenId) < 0;
+      } catch (SQLException e) {
+        
log.errorFetchingMaxLifetimeFromDatabase(Tokens.getTokenIDDisplayText(tokenId), 
e.getMessage(), e);
+      }
+    }
+    return isUnknown;
+  }
+
+  @Override
+  protected List<String> getTokenIds() {
+    List<String> tokenIds = new LinkedList<>();
+    try {
+      tokenIds = tokenDatabase.getTokenIds();

Review comment:
       This seems like an expensive operation and can potentially block a lot 
of CPU

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.services.token.impl;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import javax.sql.DataSource;
+
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+
+public class TokenStateDatabase {
+  private static final String TOKENS_TABLE_NAME = "KNOX_TOKENS";
+  private static final String ADD_TOKEN_SQL = "INSERT INTO " + 
TOKENS_TABLE_NAME + "(token_id, issue_time, expiration, max_lifetime) VALUES(?, 
?, ?, ?)";
+  private static final String REMOVE_TOKENS_SQL_PREFIX = "DELETE FROM " + 
TOKENS_TABLE_NAME + " WHERE token_id IN (";

Review comment:
       Just a thought, why not list them under resources as a *.sql statements 
so all of them are in one place and easy to manage and look at.

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.services.token.impl;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import javax.sql.DataSource;
+
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+
+public class TokenStateDatabase {
+  private static final String TOKENS_TABLE_NAME = "KNOX_TOKENS";
+  private static final String ADD_TOKEN_SQL = "INSERT INTO " + 
TOKENS_TABLE_NAME + "(token_id, issue_time, expiration, max_lifetime) VALUES(?, 
?, ?, ?)";
+  private static final String REMOVE_TOKENS_SQL_PREFIX = "DELETE FROM " + 
TOKENS_TABLE_NAME + " WHERE token_id IN (";
+  private static final String GET_TOKEN_EXPIRATION_SQL = "SELECT expiration 
FROM " + TOKENS_TABLE_NAME + " WHERE token_id = ?";
+  private static final String UPDATE_TOKEN_EXPIRATION_SQL = "UPDATE " + 
TOKENS_TABLE_NAME + " SET expiration = ? WHERE token_id = ?";
+  private static final String GET_MAX_LIFETIME_SQL = "SELECT max_lifetime FROM 
" + TOKENS_TABLE_NAME + " WHERE token_id = ?";
+  private static final String GET_ALL_TOKEN_IDS_SQL = "SELECT token_id FROM " 
+ TOKENS_TABLE_NAME;
+  private static final String ADD_METADATA_SQL = "UPDATE " + TOKENS_TABLE_NAME 
+ " SET username = ?, comment = ? WHERE token_id = ?";
+  private static final int DELETE_BATCH_SIZE = 500;
+
+  private final DataSource dataSource;
+
+  TokenStateDatabase(DataSource dataSource) throws Exception {
+    this.dataSource = dataSource;
+  }
+
+  boolean addToken(String tokenId, long issueTime, long expiration, long 
maxLifetimeDuration) throws SQLException {
+    try (Connection connection = dataSource.getConnection(); PreparedStatement 
addTokenStatement = connection.prepareStatement(ADD_TOKEN_SQL)) {
+      addTokenStatement.setString(1, tokenId);
+      addTokenStatement.setLong(2, issueTime);
+      addTokenStatement.setLong(3, expiration);
+      addTokenStatement.setLong(4, issueTime + maxLifetimeDuration);
+      return addTokenStatement.executeUpdate() == 1;
+    }
+  }
+
+  // This needs to be done in batches as many DB vendors have limitations on 
the number of items within the 'IN' clause
+  boolean removeTokens(Set<String> tokenIds) throws SQLException {
+    int removed = 0;

Review comment:
       Looks unsafe should be synchronized

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.services.token.impl;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import javax.sql.DataSource;
+
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+
+public class TokenStateDatabase {
+  private static final String TOKENS_TABLE_NAME = "KNOX_TOKENS";
+  private static final String ADD_TOKEN_SQL = "INSERT INTO " + 
TOKENS_TABLE_NAME + "(token_id, issue_time, expiration, max_lifetime) VALUES(?, 
?, ?, ?)";
+  private static final String REMOVE_TOKENS_SQL_PREFIX = "DELETE FROM " + 
TOKENS_TABLE_NAME + " WHERE token_id IN (";
+  private static final String GET_TOKEN_EXPIRATION_SQL = "SELECT expiration 
FROM " + TOKENS_TABLE_NAME + " WHERE token_id = ?";
+  private static final String UPDATE_TOKEN_EXPIRATION_SQL = "UPDATE " + 
TOKENS_TABLE_NAME + " SET expiration = ? WHERE token_id = ?";
+  private static final String GET_MAX_LIFETIME_SQL = "SELECT max_lifetime FROM 
" + TOKENS_TABLE_NAME + " WHERE token_id = ?";
+  private static final String GET_ALL_TOKEN_IDS_SQL = "SELECT token_id FROM " 
+ TOKENS_TABLE_NAME;
+  private static final String ADD_METADATA_SQL = "UPDATE " + TOKENS_TABLE_NAME 
+ " SET username = ?, comment = ? WHERE token_id = ?";
+  private static final int DELETE_BATCH_SIZE = 500;
+
+  private final DataSource dataSource;
+
+  TokenStateDatabase(DataSource dataSource) throws Exception {
+    this.dataSource = dataSource;
+  }
+
+  boolean addToken(String tokenId, long issueTime, long expiration, long 
maxLifetimeDuration) throws SQLException {
+    try (Connection connection = dataSource.getConnection(); PreparedStatement 
addTokenStatement = connection.prepareStatement(ADD_TOKEN_SQL)) {
+      addTokenStatement.setString(1, tokenId);
+      addTokenStatement.setLong(2, issueTime);
+      addTokenStatement.setLong(3, expiration);
+      addTokenStatement.setLong(4, issueTime + maxLifetimeDuration);
+      return addTokenStatement.executeUpdate() == 1;
+    }
+  }
+
+  // This needs to be done in batches as many DB vendors have limitations on 
the number of items within the 'IN' clause
+  boolean removeTokens(Set<String> tokenIds) throws SQLException {
+    int removed = 0;
+    if (tokenIds.size() <= DELETE_BATCH_SIZE) {
+      removed += doRemoveTokens(tokenIds);
+    } else {
+      Set<String> tokenIdBatch = new HashSet<>();
+      for (String tokenId : tokenIds) {
+        tokenIdBatch.add(tokenId);
+        if (tokenIdBatch.size() == DELETE_BATCH_SIZE) {
+          removed += doRemoveTokens(tokenIdBatch);
+          tokenIdBatch.clear();
+        }
+      }
+      // one more round of removal if the last batch has less items than the 
configured batch size
+      removed += doRemoveTokens(tokenIdBatch);
+    }
+    return removed == tokenIds.size();
+  }
+
+  private int doRemoveTokens(Set<String> tokenIds) throws SQLException {
+    final StringBuilder statementPostFixBuilder = new 
StringBuilder(REMOVE_TOKENS_SQL_PREFIX);
+    for (int i = 0; i < tokenIds.size(); i++) {
+      if (statementPostFixBuilder.length() > 
REMOVE_TOKENS_SQL_PREFIX.length()) {
+        statementPostFixBuilder.append(", ");
+      }
+      statementPostFixBuilder.append('?');
+    }
+    statementPostFixBuilder.append(')');
+    try (Connection connection = dataSource.getConnection(); PreparedStatement 
removeTokensStatement = 
connection.prepareStatement(statementPostFixBuilder.toString())) {
+      int i = 0;
+      for (String tokenId : tokenIds) {
+        removeTokensStatement.setString(++i, tokenId);
+      }
+      return removeTokensStatement.executeUpdate();
+    }
+  }
+
+  long getTokenExpiration(String tokenId) throws SQLException {
+    try (Connection connection = dataSource.getConnection(); PreparedStatement 
getTokenExpirationStatement = 
connection.prepareStatement(GET_TOKEN_EXPIRATION_SQL)) {
+      getTokenExpirationStatement.setString(1, tokenId);
+      try (ResultSet rs = getTokenExpirationStatement.executeQuery()) {
+        return rs.next() ? rs.getLong(1) : 0;
+      }
+    }
+  }
+
+  boolean updateExpiration(final String tokenId, long expiration) throws 
SQLException {
+    try (Connection connection = dataSource.getConnection(); PreparedStatement 
updateTokenExpirationStatement = 
connection.prepareStatement(UPDATE_TOKEN_EXPIRATION_SQL)) {
+      updateTokenExpirationStatement.setLong(1, expiration);
+      updateTokenExpirationStatement.setString(2, tokenId);
+      return updateTokenExpirationStatement.executeUpdate() == 1;
+    }
+  }
+
+  long getMaxLifetime(String tokenId) throws SQLException {
+    try (Connection connection = dataSource.getConnection(); PreparedStatement 
getMaxLifetimeStatement = connection.prepareStatement(GET_MAX_LIFETIME_SQL)) {
+      getMaxLifetimeStatement.setString(1, tokenId);
+      try (ResultSet rs = getMaxLifetimeStatement.executeQuery()) {
+        return rs.next() ? rs.getLong(1) : -1;
+      }
+    }
+  }
+
+  List<String> getTokenIds() throws SQLException {

Review comment:
       This looks risky :) we might overwhelm Knox depending on number of 
tokens, could end up DOSing




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 582042)
    Time Spent: 20m  (was: 10m)

> Implement JDBC TokenStateService
> --------------------------------
>
>                 Key: KNOX-2554
>                 URL: https://issues.apache.org/jira/browse/KNOX-2554
>             Project: Apache Knox
>          Issue Type: Task
>            Reporter: Sandor Molnar
>            Assignee: Sandor Molnar
>            Priority: Major
>             Fix For: 1.6.0
>
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> Add a new TokenStateService implementation that stores token metadata in a 
> relational database.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to