Skip to content

Commit d3c167c

Browse files
authored
KNOX-3087 - Provide a way to validate JWT tokens that are missing typ header and Add letsencrypt staging cert to knox docker image (#985)
1 parent 8e4e343 commit d3c167c

File tree

8 files changed

+43
-5
lines changed

8 files changed

+43
-5
lines changed

gateway-docker/src/main/resources/docker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ ADD --chown=knox:knox \
4343
http://www.awstrust.com/repository/AmazonRootCA1.cer \
4444
http://www.awstrust.com/repository/AmazonRootCA2.cer \
4545
http://www.awstrust.com/repository/AmazonRootCA3.cer \
46-
http://www.awstrust.com/repository/AmazonRootCA4.cer /home/knox/cacrts/
46+
http://www.awstrust.com/repository/AmazonRootCA4.cer \
47+
http://letsencrypt.org/certs/staging/letsencrypt-stg-root-x1.pem /home/knox/cacrts/
4748

4849
WORKDIR /home/knox/knox
4950

gateway-docker/src/main/resources/docker/gateway-entrypoint.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,15 @@ fi
190190
-storepass "${ALIAS_PASSPHRASE}" \
191191
-noprompt || true
192192

193-
export KNOX_GATEWAY_DBG_OPTS="${KNOX_GATEWAY_DBG_OPTS}"
193+
# Add letsencrypt staging root CA
194+
/usr/bin/keytool -importcert \
195+
-keystore ${KEYSTORE_DIR}/truststore.jks \
196+
-alias letsencrypt-stg-root \
197+
-file /home/knox/cacrts/letsencrypt-stg-root-x1.pem \
198+
-storepass "${ALIAS_PASSPHRASE}" \
199+
-noprompt || true
200+
201+
export KNOX_GATEWAY_DBG_OPTS="${KNOX_GATEWAY_DBG_OPTS} -Djavax.net.ssl.trustStore=${KEYSTORE_DIR}/truststore.jks -Djavax.net.ssl.trustStorePassword=${ALIAS_PASSPHRASE}"
194202

195203
echo "Starting Knox gateway ..."
196204
/home/knox/knox/bin/gateway.sh start

gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,9 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig {
354354

355355
private static final String GATEWAY_HEALTH_CHECK_TOPOLOGIES = GATEWAY_CONFIG_FILE_PREFIX + ".health.check.topologies";
356356

357-
private static final String JWKS_OUTAGE_CACHE_TTL = GATEWAY_CONFIG_FILE_PREFIX + ".jwks.outage.cache.ttl";;
357+
private static final String JWKS_OUTAGE_CACHE_TTL = GATEWAY_CONFIG_FILE_PREFIX + ".jwks.outage.cache.ttl";
358358
private static final long JWKS_OUTAGE_CACHE_TTL_DEFAULT = TimeUnit.HOURS.toMillis(2);
359+
private static final String ISSUER_IGNORE_TYPE_VALIDATION = GATEWAY_CONFIG_FILE_PREFIX + ".token.issuers.ignore.type.validation";
359360

360361
public GatewayConfigImpl() {
361362
init();
@@ -1547,6 +1548,12 @@ public long getServiceDiscoveryWriteTimeoutMillis() {
15471548
return getLong(CLOUDERA_MANAGER_SERVICE_DISCOVERY_WRITE_TIMEOUT, CLOUDERA_MANAGER_SERVICE_DISCOVERY_WRITE_TIMEOUT_DEFAULT);
15481549
}
15491550

1551+
@Override
1552+
public Set<String> getIssuersWithIgnoredTypeHeader() {
1553+
final Collection<String> issuers = getTrimmedStringCollection(ISSUER_IGNORE_TYPE_VALIDATION);
1554+
return issuers == null ? Collections.emptySet() : new HashSet<>(issuers);
1555+
}
1556+
15501557
private Map<String, Collection<String>> getPathAliases(String qualifier) {
15511558
final String prefix = GATEWAY_CONFIG_FILE_PREFIX + qualifier + DEPLOYMENT_PATH_ALIAS;
15521559
final Map<String, Collection<String>> pathAliases = new HashMap<>();

gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/DefaultTokenAuthorityService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,13 @@ public boolean verifyToken(JWT token, String jwksurl, String algorithm, Set<JOSE
241241
JWTClaimsSetVerifier<SecurityContext> claimsVerifier = new DefaultJWTClaimsVerifier<>();
242242
jwtProcessor.setJWTClaimsSetVerifier(claimsVerifier);
243243
final JOSEObjectTypeVerifier<SecurityContext> objectTypeVerifier = new DefaultJOSEObjectTypeVerifier<>(allowedJwsTypes);
244-
jwtProcessor.setJWSTypeVerifier(objectTypeVerifier);
244+
/* See if we have a issuer for which we want to ignore type validation */
245+
if(!config.getIssuersWithIgnoredTypeHeader().contains(token.getIssuer())) {
246+
jwtProcessor.setJWSTypeVerifier(objectTypeVerifier);
247+
} else {
248+
/* no typ claim found in token, log and move on */
249+
LOG.ignoreTypeHeaderVerification();
250+
}
245251

246252
// Process the token
247253
SecurityContext ctx = null; // optional context parameter, not required here

gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenAuthorityServiceMessages.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ public interface TokenAuthorityServiceMessages {
2828

2929
@Message(level = MessageLevel.ERROR, text = "Failed to verify token using JWKS endpoint {0}, reason: {1}")
3030
void jwksVerificationFailed(String jwksUrl, String reason);
31+
32+
@Message(level = MessageLevel.WARN, text = "Ignoring typ header verification for token")
33+
void ignoreTypeHeaderVerification();
3134
}

gateway-spi-common/src/main/java/org/apache/knox/gateway/GatewayTestConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,4 +1133,9 @@ public long getJwksOutageCacheTTL() {
11331133
}
11341134

11351135

1136+
@Override
1137+
public Set<String> getIssuersWithIgnoredTypeHeader() {
1138+
return Collections.emptySet();
1139+
}
1140+
11361141
}

gateway-spi/src/main/java/org/apache/knox/gateway/config/GatewayConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,4 +961,12 @@ public interface GatewayConfig {
961961
* @return jwks outage cache TTL
962962
*/
963963
long getJwksOutageCacheTTL();
964+
965+
/**
966+
* Some JWT tokens could be missing typ header.
967+
* This config skips typ validation for tokens issued by
968+
* configured Issuers.
969+
* @return
970+
*/
971+
Set<String> getIssuersWithIgnoredTypeHeader();
964972
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292
<xml-jaxb.version>2.3.0</xml-jaxb.version>
293293
<xml-matchers.version>0.10</xml-matchers.version>
294294
<zookeeper.version>3.8.4</zookeeper.version>
295-
<docker-maven-plugin.version>0.43.4</docker-maven-plugin.version>
295+
<docker-maven-plugin.version>0.45.0</docker-maven-plugin.version>
296296
<docker.platforms>linux/amd64,linux/arm64</docker.platforms>
297297
</properties>
298298
<repositories>

0 commit comments

Comments
 (0)