smolnar82 commented on a change in pull request #440:
URL: https://github.com/apache/knox/pull/440#discussion_r623334551
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/AbstractJWTFilter.java
##########
@@ -444,41 +420,41 @@ protected boolean verifyTokenSignature(final JWT token) {
}
}
- if (verified && tokenId != null) { // If successful, record the
verification for future reference
- recordSignatureVerification(tokenId);
+ if (verified) { // If successful, record the verification for future
reference
+ recordSignatureVerification(serializedJWT);
}
}
return verified;
}
/**
- * Determine if the specified token signature has previously been
successfully verified.
+ * Determine if the specified JWT signature has previously been successfully
verified.
*
- * @param tokenId The unique identifier for a token.
+ * @param jwt A serialized JWT String.
*
* @return true, if the specified token has been previously verified;
Otherwise, false.
*/
- protected boolean hasSignatureBeenVerified(final String tokenId) {
- return (verifiedTokens.getIfPresent(tokenId) != null);
+ protected boolean hasSignatureBeenVerified(final String jwt) {
Review comment:
Do we still need this method? The delegated method name is quite
expressive too.
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/AbstractJWTFilter.java
##########
@@ -96,7 +92,7 @@
protected List<String> audiences;
protected JWTokenAuthority authority;
protected RSAPublicKey publicKey;
- protected Cache<String, Boolean> verifiedTokens;
+ protected SignatureVerificationCache verifiedTokens;
Review comment:
I think `signatureVerificationCache` would be a better name for this
class member.
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/AbstractJWTFilter.java
##########
@@ -444,41 +420,41 @@ protected boolean verifyTokenSignature(final JWT token) {
}
}
- if (verified && tokenId != null) { // If successful, record the
verification for future reference
- recordSignatureVerification(tokenId);
+ if (verified) { // If successful, record the verification for future
reference
+ recordSignatureVerification(serializedJWT);
}
}
return verified;
}
/**
- * Determine if the specified token signature has previously been
successfully verified.
+ * Determine if the specified JWT signature has previously been successfully
verified.
*
- * @param tokenId The unique identifier for a token.
+ * @param jwt A serialized JWT String.
*
* @return true, if the specified token has been previously verified;
Otherwise, false.
*/
- protected boolean hasSignatureBeenVerified(final String tokenId) {
- return (verifiedTokens.getIfPresent(tokenId) != null);
+ protected boolean hasSignatureBeenVerified(final String jwt) {
+ return verifiedTokens.hasSignatureBeenVerified(jwt);
}
/**
- * Record a successful token signature verification.
+ * Record a successful JWT signature verification.
*
- * @param tokenId The unique identifier for the token which has been
successfully verified.
+ * @param jwt The serialized String for a JWT which has been successfully
verified.
*/
- protected void recordSignatureVerification(final String tokenId) {
- verifiedTokens.put(tokenId, true);
+ protected void recordSignatureVerification(final String jwt) {
Review comment:
Do we still need this method? The delegated method name is quite
expressive too.
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/AbstractJWTFilter.java
##########
@@ -444,41 +420,41 @@ protected boolean verifyTokenSignature(final JWT token) {
}
}
- if (verified && tokenId != null) { // If successful, record the
verification for future reference
- recordSignatureVerification(tokenId);
+ if (verified) { // If successful, record the verification for future
reference
+ recordSignatureVerification(serializedJWT);
}
}
return verified;
}
/**
- * Determine if the specified token signature has previously been
successfully verified.
+ * Determine if the specified JWT signature has previously been successfully
verified.
*
- * @param tokenId The unique identifier for a token.
+ * @param jwt A serialized JWT String.
*
* @return true, if the specified token has been previously verified;
Otherwise, false.
*/
- protected boolean hasSignatureBeenVerified(final String tokenId) {
- return (verifiedTokens.getIfPresent(tokenId) != null);
+ protected boolean hasSignatureBeenVerified(final String jwt) {
+ return verifiedTokens.hasSignatureBeenVerified(jwt);
}
/**
- * Record a successful token signature verification.
+ * Record a successful JWT signature verification.
*
- * @param tokenId The unique identifier for the token which has been
successfully verified.
+ * @param jwt The serialized String for a JWT which has been successfully
verified.
*/
- protected void recordSignatureVerification(final String tokenId) {
- verifiedTokens.put(tokenId, true);
+ protected void recordSignatureVerification(final String jwt) {
+ verifiedTokens.recordSignatureVerification(jwt);
}
/**
- * Explicitly evict the signature verification record from the cache if it
exists.
+ * Explicitly evict the signature verification record for the specified JWT
from the cache if it exists.
*
- * @param tokenId The token whose signature verification record should be
evicted.
+ * @param jwt The serialized String for a JWT whose signature verification
record should be evicted.
*/
- protected void removeSignatureVerificationRecord(final String tokenId) {
- verifiedTokens.asMap().remove(tokenId);
+ protected void removeSignatureVerificationRecord(final String jwt) {
Review comment:
Do we still need this method? The delegated method name is quite
expressive too.
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/SignatureVerificationCache.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.provider.federation.jwt.filter;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.provider.federation.jwt.JWTMessages;
+
+import javax.servlet.FilterConfig;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A shared record of tokens for which the signature has been verified.
+ */
+public class SignatureVerificationCache {
+
+ public static final String JWT_VERIFIED_CACHE_MAX =
"jwt.verified.cache.max";
+ public static final int JWT_VERIFIED_CACHE_MAX_DEFAULT = 250;
+
+ static final String DEFAULT_CACHE_ID = "default-cache";
+
+ static JWTMessages log = MessagesFactory.get( JWTMessages.class );
+
+ private static final Map<String, SignatureVerificationCache> instances =
new HashMap<>();
Review comment:
Maybe using ConcurrentHashMap?
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/SignatureVerificationCache.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.provider.federation.jwt.filter;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.provider.federation.jwt.JWTMessages;
+
+import javax.servlet.FilterConfig;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A shared record of tokens for which the signature has been verified.
+ */
+public class SignatureVerificationCache {
+
+ public static final String JWT_VERIFIED_CACHE_MAX =
"jwt.verified.cache.max";
+ public static final int JWT_VERIFIED_CACHE_MAX_DEFAULT = 250;
+
+ static final String DEFAULT_CACHE_ID = "default-cache";
+
+ static JWTMessages log = MessagesFactory.get( JWTMessages.class );
+
+ private static final Map<String, SignatureVerificationCache> instances =
new HashMap<>();
+
+ /**
+ * Caches are topology-specific because the configuration is defined at
the provider level.
+ *
+ * @param topology The topology for which the cache is being requested, or
null if the default is sufficient.
+ * @param config The FilterConfig associated with the calling provider.
+ *
+ * @return A SignatureVerificationCache for the specified topology, or the
default one if no topology is specified.
+ */
+ @SuppressWarnings("PMD.SingletonClassReturningNewInstance")
+ public static SignatureVerificationCache getInstance(final String
topology, final FilterConfig config) {
+ SignatureVerificationCache instance;
+ String cacheId = topology != null ? topology : DEFAULT_CACHE_ID;
+ synchronized (instances) {
+ instance = instances.get(cacheId);
+ if (instance == null) {
Review comment:
If ConcurrentHashMap is used you may move this check above, and
lock/synchronize only if there is nothing found for the given topology.
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/SignatureVerificationCache.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.provider.federation.jwt.filter;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.provider.federation.jwt.JWTMessages;
+
+import javax.servlet.FilterConfig;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A shared record of tokens for which the signature has been verified.
+ */
+public class SignatureVerificationCache {
+
+ public static final String JWT_VERIFIED_CACHE_MAX =
"jwt.verified.cache.max";
+ public static final int JWT_VERIFIED_CACHE_MAX_DEFAULT = 250;
+
+ static final String DEFAULT_CACHE_ID = "default-cache";
+
+ static JWTMessages log = MessagesFactory.get( JWTMessages.class );
+
+ private static final Map<String, SignatureVerificationCache> instances =
new HashMap<>();
+
+ /**
+ * Caches are topology-specific because the configuration is defined at
the provider level.
+ *
+ * @param topology The topology for which the cache is being requested, or
null if the default is sufficient.
+ * @param config The FilterConfig associated with the calling provider.
+ *
+ * @return A SignatureVerificationCache for the specified topology, or the
default one if no topology is specified.
+ */
+ @SuppressWarnings("PMD.SingletonClassReturningNewInstance")
+ public static SignatureVerificationCache getInstance(final String
topology, final FilterConfig config) {
+ SignatureVerificationCache instance;
+ String cacheId = topology != null ? topology : DEFAULT_CACHE_ID;
+ synchronized (instances) {
+ instance = instances.get(cacheId);
+ if (instance == null) {
+ instance = new SignatureVerificationCache(config);
+ instances.put(cacheId, instance);
+ log.initializedSignatureVerificationCache(cacheId);
+ }
+ }
+ return instance;
+ }
+
+ private Cache<String, Boolean> verifiedTokens;
Review comment:
This is a class member, it'd be better if you moved it up where the rest
of the fields are declared.
##########
File path:
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/SignatureVerificationCache.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.provider.federation.jwt.filter;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.provider.federation.jwt.JWTMessages;
+
+import javax.servlet.FilterConfig;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A shared record of tokens for which the signature has been verified.
+ */
+public class SignatureVerificationCache {
+
+ public static final String JWT_VERIFIED_CACHE_MAX =
"jwt.verified.cache.max";
+ public static final int JWT_VERIFIED_CACHE_MAX_DEFAULT = 250;
+
+ static final String DEFAULT_CACHE_ID = "default-cache";
+
+ static JWTMessages log = MessagesFactory.get( JWTMessages.class );
+
+ private static final Map<String, SignatureVerificationCache> instances =
new HashMap<>();
+
+ /**
+ * Caches are topology-specific because the configuration is defined at
the provider level.
+ *
+ * @param topology The topology for which the cache is being requested, or
null if the default is sufficient.
+ * @param config The FilterConfig associated with the calling provider.
+ *
+ * @return A SignatureVerificationCache for the specified topology, or the
default one if no topology is specified.
+ */
+ @SuppressWarnings("PMD.SingletonClassReturningNewInstance")
+ public static SignatureVerificationCache getInstance(final String
topology, final FilterConfig config) {
+ SignatureVerificationCache instance;
+ String cacheId = topology != null ? topology : DEFAULT_CACHE_ID;
+ synchronized (instances) {
+ instance = instances.get(cacheId);
+ if (instance == null) {
+ instance = new SignatureVerificationCache(config);
+ instances.put(cacheId, instance);
+ log.initializedSignatureVerificationCache(cacheId);
+ }
+ }
+ return instance;
+ }
+
+ private Cache<String, Boolean> verifiedTokens;
+
+ private SignatureVerificationCache(final FilterConfig config) {
+ initializeVerifiedTokensCache(config);
+ }
+
+ /**
+ * Initialize the cache for token verification records.
+ *
+ * @param config The configuration of the provider employing this cache.
+ */
+ private void initializeVerifiedTokensCache(final FilterConfig config) {
+ int maxCacheSize = JWT_VERIFIED_CACHE_MAX_DEFAULT;
+
+ String configValue = config.getInitParameter(JWT_VERIFIED_CACHE_MAX);
+ if (configValue != null && !configValue.isEmpty()) {
+ try {
+ maxCacheSize = Integer.parseInt(configValue);
+ } catch (NumberFormatException e) {
+ log.invalidVerificationCacheMaxConfiguration(configValue);
+ }
+ }
+
+ verifiedTokens =
Caffeine.newBuilder().maximumSize(maxCacheSize).build();
+ }
+
+ /**
+ * Determine if the specified JWT's signature has previously been
successfully verified.
+ *
+ * @param jwt A serialized JWT.
+ *
+ * @return true, if the specified token has been previously verified;
Otherwise, false.
+ */
+ public boolean hasSignatureBeenVerified(final String jwt) {
+ return (verifiedTokens.getIfPresent(jwt) != null);
+ }
+
+ /**
+ * Record a successful token signature verification.
+ *
+ * @param jwt A serialized JWT for which the signature has been
successfully verified.
+ */
+ public void recordSignatureVerification(final String jwt) {
+ verifiedTokens.put(jwt, true);
+ }
+
+ /**
+ * Explicitly evict the signature verification record from the cache if it
exists.
+ *
+ * @param jwt The serialized JWT for which the associated signature
verification record should be evicted.
+ */
+ public void removeSignatureVerificationRecord(final String jwt) {
+ verifiedTokens.asMap().remove(jwt);
+ }
+
+ /**
+ * @return The size of the cache.
+ */
+ public long getSize() {
+ return verifiedTokens.estimatedSize();
+ }
+
+ /**
+ * Explicitly clean up any records which should be evicted from the cache.
+ */
+ public void cleanUp() {
Review comment:
If I understood well, this is a `run maintenance now` (aka. evict now)
method, right? It'd be better to have a name indicating this. I'd check the
underlying cache implementation to understand the difference between this
method and `clear()`
##########
File path:
gateway-provider-security-jwt/src/test/java/org/apache/knox/gateway/provider/federation/jwt/filter/SignatureVerificationCacheTest.java
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.provider.federation.jwt.filter;
+
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.knox.gateway.provider.federation.TestFilterConfig;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.interfaces.RSAPrivateKey;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class SignatureVerificationCacheTest {
+
+ private static RSAPrivateKey privateKey;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
+ kpg.initialize(2048);
+ KeyPair KPair = kpg.generateKeyPair();
+ privateKey = (RSAPrivateKey) KPair.getPrivate();
+ }
+
+ /**
+ * Verify that the default SignatureVerificationCache instance is returned
when no topology is specified.
+ */
+ @Test
+ public void testSignatureVerificationCacheWithoutTopology() throws
Exception {
+ SignatureVerificationCache cache =
SignatureVerificationCache.getInstance(null, new TestFilterConfig());
+ assertNotNull(cache);
+ SignatureVerificationCache defaultCache =
+
SignatureVerificationCache.getInstance(SignatureVerificationCache.DEFAULT_CACHE_ID,
new TestFilterConfig());
+ assertNotNull(defaultCache);
+ assertEquals("Expected the default cache when no topology is
specified.", defaultCache, cache);
+ }
+
+ /**
+ * Verify that the topology-specific SignatureVerificationCache instance
is returned when a topology is specified.
+ */
+ @Test
+ public void testSignatureVerificationCacheForTopology() throws Exception {
+ final String topologyName = "test-topology-explicit";
+ final Properties filterProps = new Properties();
+ filterProps.setProperty(TestFilterConfig.TOPOLOGY_NAME_PROP,
topologyName);
+ final TestFilterConfig filterConfig = new
TestFilterConfig(filterProps);
+
+ SignatureVerificationCache ref1 =
SignatureVerificationCache.getInstance(topologyName, filterConfig);
+ assertNotNull(ref1);
+
+ SignatureVerificationCache ref2 =
SignatureVerificationCache.getInstance(topologyName, filterConfig);
+ assertNotNull(ref2);
+ assertEquals("Expected the same cache when the topology is explicitly
specified.", ref1, ref2);
Review comment:
Maybe adding another ref with a different topology name and assert they
are not the same?
--
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]