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

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

                Author: ASF GitHub Bot
            Created on: 21/Apr/21 15:25
            Start Date: 21/Apr/21 15:25
    Worklog Time Spent: 10m 
      Work Description: lmccay commented on a change in pull request #437:
URL: https://github.com/apache/knox/pull/437#discussion_r617633095



##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
##########
@@ -28,7 +28,7 @@
 
 public class TokenStateDatabase {
   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 ADD_TOKEN_SQL = "INSERT INTO " + 
TOKENS_TABLE_NAME + "(id, token_id, issue_time, expiration, max_lifetime) 
VALUES(?, ?, ?, ?, ?)";

Review comment:
       I was thinking that we would leave this as token.id and add a passcode 
col that was a hash.

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenMAC.java
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Mac;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+
+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.AliasServiceException;
+
+public class TokenMAC {
+  static final String KNOX_TOKEN_HASH_KEY_ALIAS_NAME = "knox.token.hash.key";
+
+  private final Mac tokedMac;
+
+  TokenMAC(GatewayConfig config, AliasService aliasService) throws 
ServiceLifecycleException {
+    if (aliasService == null) {
+      throw new ServiceLifecycleException("Missing alias service while 
configuring Knox Token MAC.");
+    }
+
+    try {
+      final String algorithm = config.getKnoxTokenHashAlgorithm();
+      final char[] knoxTokenHashKey = 
aliasService.getPasswordFromAliasForGateway(KNOX_TOKEN_HASH_KEY_ALIAS_NAME);
+      if (knoxTokenHashKey != null) {
+        final SecretKey key = new SecretKeySpec(new 
String(knoxTokenHashKey).getBytes(StandardCharsets.UTF_8), algorithm);
+        tokedMac = Mac.getInstance(algorithm);
+        tokedMac.init(key);
+      } else {
+        throw new ServiceLifecycleException("Missing " + 
KNOX_TOKEN_HASH_KEY_ALIAS_NAME + " alias from Gateway's credential store");
+      }
+    } catch (AliasServiceException | NoSuchAlgorithmException | 
InvalidKeyException e) {
+      throw new ServiceLifecycleException("Error while initiating Knox Token 
MAC: " + e, e);
+    }
+  }
+
+  String hash(String toBeHashed) {
+    return new 
String(tokedMac.doFinal(toBeHashed.getBytes(StandardCharsets.UTF_8)), 
StandardCharsets.UTF_8);

Review comment:
       Possible NPE?

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenMAC.java
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Mac;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+
+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.AliasServiceException;
+
+public class TokenMAC {

Review comment:
       It seems like this should be something added to the CryptoService rather 
than a separate class.

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenMAC.java
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Mac;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+
+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.AliasServiceException;
+
+public class TokenMAC {
+  static final String KNOX_TOKEN_HASH_KEY_ALIAS_NAME = "knox.token.hash.key";
+
+  private final Mac tokedMac;
+
+  TokenMAC(GatewayConfig config, AliasService aliasService) throws 
ServiceLifecycleException {
+    if (aliasService == null) {
+      throw new ServiceLifecycleException("Missing alias service while 
configuring Knox Token MAC.");
+    }
+
+    try {
+      final String algorithm = config.getKnoxTokenHashAlgorithm();
+      final char[] knoxTokenHashKey = 
aliasService.getPasswordFromAliasForGateway(KNOX_TOKEN_HASH_KEY_ALIAS_NAME);
+      if (knoxTokenHashKey != null) {
+        final SecretKey key = new SecretKeySpec(new 
String(knoxTokenHashKey).getBytes(StandardCharsets.UTF_8), algorithm);

Review comment:
       I think we need to use salt here as well. Is this implemented somewhere 
else?
   Otherwise, a hash of the same token will always result in the same hash 
value. Since these are UUID's that is less likely but if all the passcodes are 
hashed with the same key then the entire set can be attacked at once rather 
than individually with a random salt per passcode.

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenMAC.java
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Mac;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+
+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.AliasServiceException;
+
+public class TokenMAC {
+  static final String KNOX_TOKEN_HASH_KEY_ALIAS_NAME = "knox.token.hash.key";
+
+  private final Mac tokedMac;

Review comment:
       tokeDmac?




-- 
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: 586669)
    Time Spent: 0.5h  (was: 20m)

> Make token passcode secure in DB token state backend
> ----------------------------------------------------
>
>                 Key: KNOX-2579
>                 URL: https://issues.apache.org/jira/browse/KNOX-2579
>             Project: Apache Knox
>          Issue Type: New Feature
>          Components: Server
>    Affects Versions: 1.6.0
>            Reporter: Sandor Molnar
>            Assignee: Sandor Molnar
>            Priority: Major
>             Fix For: 1.6.0
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> With KNOX-2554, we now have the ability to store passcode tokens in 
> relational databases. However, it indicates poor security practice if 
> sensitive data is stored in plain text format. Since the {{token_id}} JWT 
> claim can be used as a passcode, we need to make sure it's saved in a hashed 
> format. To be able to do this, the following is going to be implemented:
>  * add a new column called {{id}} which will serve as the primary key of the 
> {{KNOX_TOKENS}} table (this is also going to be a UUID)
>  * keep the current {{token_id}} column as is, and store the {{token.id}} 
> claim in a hashed form in this column
> By default, {{HS256}} is going to be used as a hash algorithm, but end-users 
> can configure it via the {{gateway.database.hash.alg}} gateway level 
> configuration. A new pre-defined alias name is to be introduced too: 
> {{gateway_database_hash_key}}. End-users must save the desired key using this 
> alias if they use the new {{JDBCTokenStateService}} as the token management 
> backend. Please note that key size it's very important for hash-based 
> algorithms so using the {{master secret}} is not an option here.
> The token verification logic has to be changed too (need to hash {{token.id}} 
> before getting expiration from the database).



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

Reply via email to