This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new e089646bf1f4 CAMEL-26920: fail at startup when the JWT keystore has no
key Vert.x can use (#30151)
e089646bf1f4 is described below
commit e089646bf1f4192576d045e24d60eed7eb2162e5
Author: Urmila Unni <[email protected]>
AuthorDate: Mon Nov 16 19:03:37 2026 +0530
CAMEL-26920: fail at startup when the JWT keystore has no key Vert.x can
use (#30151)
Vert.x only loads the keys of a JWT keystore whose alias is the name of a
JWT algorithm (HS256, RS256, ES256, ...), read with the keystore password.
Without such a key it runs in unsecured mode and rejects every signed token
with HTTP 401, which is hard to diagnose.
The HTTP server and the management server now fail to start with an
`IllegalArgumentException` in that case:
- the keystore is checked as Vert.x loads it: `getEntry(<algorithm>,
PasswordProtection(password))` for the algorithms of `JWK.load` in
vertx-auth-common 5.2.3
- the default keystore type is used when `jwtKeystoreType` is not set, as
Vert.x does
- a keystore that cannot be found or loaded also fails, instead of being
skipped
- the message names the keystore, the aliases found, and how to rename the
alias with keytool
Co-authored-by: Claude <[email protected]>
---
.../camel/catalog/docs/platform-http-main.adoc | 14 +++
.../src/main/docs/platform-http-main.adoc | 14 +++
.../JWTAuthenticationConfigurer.java | 98 ++++++++++++++++++
.../JWTAuthenticationMainHttpServerTest.java | 3 +-
...erAudienceAuthenticationMainHttpServerTest.java | 2 +-
...WTIssuerAudienceRequiredMainHttpServerTest.java | 5 +-
.../authentication/JWTKeystoreAliasesTest.java | 114 +++++++++++++++++++++
.../test/resources/test-camel-main-auth-jwt.jks | Bin 2716 -> 2630 bytes
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 15 +++
9 files changed, 261 insertions(+), 4 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/platform-http-main.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/platform-http-main.adoc
index 45827b5a56ce..badb147a9726 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/platform-http-main.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/platform-http-main.adoc
@@ -139,6 +139,20 @@ without an issuer or an audience.
The same options are available on the management server as
`camel.management.jwtIssuer`,
`camel.management.jwtAudience` and
`camel.management.jwtAllowMissingIssuerAndAudience`.
+=== Keystore aliases
+
+Vert.x only uses the keys of the JWT keystore whose alias is the name of a JWT
algorithm: `HS256`, `HS384`, `HS512`,
+`RS256`, `RS384`, `RS512`, `ES256K`, `ES256`, `ES384` or `ES512`. A key stored
under another alias, such as the
+`keycloak` alias of a Keycloak realm export, is not used, and every signed
token is then rejected with HTTP 401
+(`JWT is in unsecured mode but token is signed`). The server therefore fails
to start when the keystore has no such
+alias, or when the key under it cannot be read with the keystore password
(`jwtKeystorePassword`).
+Rename the alias of the key to its algorithm, for example:
+
+[source,bash]
+----
+keytool -changealias -alias keycloak -destalias RS256 -keystore
keycloak-realm.p12 -storetype PKCS12
+----
+
== See More
- xref:platform-http-vertx.adoc[Platform HTTP Vert.x]
diff --git
a/components/camel-platform-http-main/src/main/docs/platform-http-main.adoc
b/components/camel-platform-http-main/src/main/docs/platform-http-main.adoc
index 45827b5a56ce..badb147a9726 100644
--- a/components/camel-platform-http-main/src/main/docs/platform-http-main.adoc
+++ b/components/camel-platform-http-main/src/main/docs/platform-http-main.adoc
@@ -139,6 +139,20 @@ without an issuer or an audience.
The same options are available on the management server as
`camel.management.jwtIssuer`,
`camel.management.jwtAudience` and
`camel.management.jwtAllowMissingIssuerAndAudience`.
+=== Keystore aliases
+
+Vert.x only uses the keys of the JWT keystore whose alias is the name of a JWT
algorithm: `HS256`, `HS384`, `HS512`,
+`RS256`, `RS384`, `RS512`, `ES256K`, `ES256`, `ES384` or `ES512`. A key stored
under another alias, such as the
+`keycloak` alias of a Keycloak realm export, is not used, and every signed
token is then rejected with HTTP 401
+(`JWT is in unsecured mode but token is signed`). The server therefore fails
to start when the keystore has no such
+alias, or when the key under it cannot be read with the keystore password
(`jwtKeystorePassword`).
+Rename the alias of the key to its algorithm, for example:
+
+[source,bash]
+----
+keytool -changealias -alias keycloak -destalias RS256 -keystore
keycloak-realm.p12 -storetype PKCS12
+----
+
== See More
- xref:platform-http-vertx.adoc[Platform HTTP Vert.x]
diff --git
a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationConfigurer.java
b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationConfigurer.java
index 982e1a27068a..4fcd1c7c7231 100644
---
a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationConfigurer.java
+++
b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationConfigurer.java
@@ -16,6 +16,16 @@
*/
package org.apache.camel.component.platform.http.main.authentication;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.GeneralSecurityException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.util.Collections;
+import java.util.List;
+
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.JWTOptions;
import io.vertx.ext.auth.authentication.AuthenticationProvider;
@@ -32,6 +42,11 @@ import org.apache.camel.util.ObjectHelper;
public class JWTAuthenticationConfigurer implements
MainAuthenticationConfigurer {
+ // the aliases Vert.x looks up in a JWT keystore, one per algorithm
+ // (io.vertx.ext.auth.impl.jose.JWK#load in vertx-auth-common 5.2.3: check
it when upgrading Vert.x)
+ static final List<String> JWT_KEYSTORE_ALIASES = List.of(
+ "HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256K",
"ES256", "ES384", "ES512");
+
@Override
public void configureAuthentication(
AuthenticationConfig authenticationConfig,
@@ -43,6 +58,9 @@ public class JWTAuthenticationConfigurer implements
MainAuthenticationConfigurer
assertIssuerOrAudienceConfigured(properties.getJwtAudience(),
properties.getJwtIssuer(),
properties.isJwtAllowMissingIssuerAndAudience(),
"camel.server");
+ assertJwtKeystoreHasKey(properties.getJwtKeystoreType(),
properties.getJwtKeystorePath(),
+ properties.getJwtKeystorePassword(), "camel.server");
+
AuthenticationConfigEntry entry = new AuthenticationConfigEntry();
entry.setPath(path);
entry.setAuthenticationHandlerFactory(new
AuthenticationHandlerFactory() {
@@ -83,6 +101,9 @@ public class JWTAuthenticationConfigurer implements
MainAuthenticationConfigurer
assertIssuerOrAudienceConfigured(properties.getJwtAudience(),
properties.getJwtIssuer(),
properties.isJwtAllowMissingIssuerAndAudience(),
"camel.management");
+ assertJwtKeystoreHasKey(properties.getJwtKeystoreType(),
properties.getJwtKeystorePath(),
+ properties.getJwtKeystorePassword(), "camel.management");
+
AuthenticationConfigEntry entry = new AuthenticationConfigEntry();
entry.setPath(path);
entry.setAuthenticationHandlerFactory(new
AuthenticationHandlerFactory() {
@@ -112,6 +133,83 @@ public class JWTAuthenticationConfigurer implements
MainAuthenticationConfigurer
authenticationConfig.setEnabled(true);
}
+ /**
+ * Vert.x only uses the keys whose alias is the name of a JWT algorithm,
such as RS256, and reads them with the
+ * keystore password. A keystore without such a key loads fine, but then
every signed token is rejected, so fail at
+ * startup.
+ */
+ static void assertJwtKeystoreHasKey(String type, String path, String
password, String configPrefix) {
+ if (path == null) {
+ return;
+ }
+ KeyStore keyStore = loadKeystore(type, path, password, configPrefix);
+ String error = checkJwtKeystoreKeys(keyStore, password, configPrefix,
path);
+ if (error != null) {
+ throw new IllegalArgumentException(error);
+ }
+ }
+
+ private static KeyStore loadKeystore(String type, String path, String
password, String configPrefix) {
+ try (InputStream is = openKeystore(path)) {
+ if (is == null) {
+ throw new IllegalArgumentException(
+ "The JWT keystore " + path + " (" + configPrefix +
".jwtKeystorePath) is not found as a file"
+ + " or on the classpath.");
+ }
+ // Vert.x uses the default keystore type when none is set
+ KeyStore keyStore = KeyStore.getInstance(type != null ? type :
KeyStore.getDefaultType());
+ keyStore.load(is, password != null ? password.toCharArray() :
null);
+ return keyStore;
+ } catch (IOException | GeneralSecurityException e) {
+ throw new IllegalArgumentException(
+ "Cannot load the JWT keystore " + path + " (" +
configPrefix + ".jwtKeystorePath, " + configPrefix
+ + ".jwtKeystoreType and " +
configPrefix + ".jwtKeystorePassword): "
+ + e.getMessage(),
+ e);
+ }
+ }
+
+ /**
+ * Returns the error for a keystore that has no key Vert.x can use, or
null when it has one.
+ */
+ static String checkJwtKeystoreKeys(KeyStore keyStore, String password,
String configPrefix, String path) {
+ KeyStore.PasswordProtection protection
+ = new KeyStore.PasswordProtection(password != null ?
password.toCharArray() : null);
+ for (String algorithm : JWT_KEYSTORE_ALIASES) {
+ try {
+ // as Vert.x does; the alias lookup of a JKS or PKCS12
keystore ignores case
+ if (keyStore.getEntry(algorithm, protection) != null) {
+ return null;
+ }
+ } catch (Exception e) {
+ // Vert.x skips an entry it cannot read, such as a key with
another password
+ }
+ }
+ String aliases;
+ try {
+ aliases = Collections.list(keyStore.aliases()).toString();
+ } catch (KeyStoreException e) {
+ aliases = "unknown";
+ }
+ return "The JWT keystore " + path + " (" + configPrefix +
".jwtKeystorePath) has no key that Vert.x can use:"
+ + " it only reads the keys whose alias is the name of a JWT
algorithm ("
+ + String.join(", ", JWT_KEYSTORE_ALIASES) + "), with the
keystore password (" + configPrefix
+ + ".jwtKeystorePassword). Aliases found: " + aliases + ".
Rename the alias of the key to the algorithm"
+ + " of the key, for example RS256 for an RSA key, with keytool
-changealias -alias <alias>"
+ + " -destalias RS256.";
+ }
+
+ private static InputStream openKeystore(String path) throws IOException {
+ Path file = Path.of(path);
+ if (Files.isRegularFile(file)) {
+ return Files.newInputStream(file);
+ }
+ // as Vert.x does: the thread context class loader first, then the own
one
+ ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+ InputStream is = tccl != null ? tccl.getResourceAsStream(path) : null;
+ return is != null ? is :
JWTAuthenticationConfigurer.class.getClassLoader().getResourceAsStream(path);
+ }
+
/**
* A JWT authenticator built without an issuer or an audience only
verifies the token signature and the exp/nbf
* claims, which is weaker than the configuration suggests. Fail closed
unless that was asked for explicitly.
diff --git
a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationMainHttpServerTest.java
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationMainHttpServerTest.java
index 3aee2e7c52cb..fe7e746bd6bd 100644
---
a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationMainHttpServerTest.java
+++
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTAuthenticationMainHttpServerTest.java
@@ -64,7 +64,8 @@ public class JWTAuthenticationMainHttpServerTest {
@Test
void testJWTAuth() {
- String validToken = jwtAuth.generateToken(new
JsonObject().put("admin", "camel"), new JWTOptions());
+ String validToken
+ = jwtAuth.generateToken(new JsonObject().put("admin",
"camel"), new JWTOptions().setAlgorithm("RS256"));
String invalidToken = validToken.substring(0, (validToken.length() -
2));
CamelContext camelContext = main.getCamelContext();
diff --git
a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceAuthenticationMainHttpServerTest.java
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceAuthenticationMainHttpServerTest.java
index fad7a23546c0..ccec42bf48fe 100644
---
a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceAuthenticationMainHttpServerTest.java
+++
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceAuthenticationMainHttpServerTest.java
@@ -153,7 +153,7 @@ public class
JWTIssuerAudienceAuthenticationMainHttpServerTest {
}
private static String mintToken(JWTOptions jwtOptions) {
- return jwtAuth.generateToken(new JsonObject().put("admin", "camel"),
jwtOptions);
+ return jwtAuth.generateToken(new JsonObject().put("admin", "camel"),
jwtOptions.setAlgorithm("RS256"));
}
private static class PlatformHttpRouteBuilder extends RouteBuilder {
diff --git
a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceRequiredMainHttpServerTest.java
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceRequiredMainHttpServerTest.java
index 921cb5d83f3d..56e49b62bc09 100644
---
a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceRequiredMainHttpServerTest.java
+++
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTIssuerAudienceRequiredMainHttpServerTest.java
@@ -81,7 +81,8 @@ class JWTIssuerAudienceRequiredMainHttpServerTest {
try {
main.start();
- String token = testKeystoreJwtAuth().generateToken(new
JsonObject().put("admin", "camel"), new JWTOptions());
+ String token = testKeystoreJwtAuth().generateToken(new
JsonObject().put("admin", "camel"),
+ new JWTOptions().setAlgorithm("RS256"));
given()
.header("Authorization", "Bearer " + token)
@@ -105,7 +106,7 @@ class JWTIssuerAudienceRequiredMainHttpServerTest {
String foreignToken = testKeystoreJwtAuth().generateToken(
new JsonObject().put("admin", "camel"),
new JWTOptions()
- .setIssuer("https://another.example")
+
.setAlgorithm("RS256").setIssuer("https://another.example")
.addAudience("another-application"));
given()
diff --git
a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTKeystoreAliasesTest.java
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTKeystoreAliasesTest.java
new file mode 100644
index 000000000000..70ba61cfc45c
--- /dev/null
+++
b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/authentication/JWTKeystoreAliasesTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.camel.component.platform.http.main.authentication;
+
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyStore;
+
+import javax.crypto.spec.SecretKeySpec;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class JWTKeystoreAliasesTest {
+
+ private static final String PASSWORD = "secret";
+
+ @TempDir
+ Path dir;
+
+ @Test
+ void validKeystoreStarts() throws Exception {
+ // a PKCS12 keystore stores the alias in lower case, as hs256
+ Path file = writeKeystore("PKCS12", PASSWORD, "HS256");
+
+ assertThatCode(() ->
JWTAuthenticationConfigurer.assertJwtKeystoreHasKey("PKCS12", file.toString(),
PASSWORD,
+ "camel.server")).doesNotThrowAnyException();
+ }
+
+ @Test
+ void oneAliasNamedAfterAnAlgorithmIsEnough() throws Exception {
+ Path file = writeKeystore("PKCS12", PASSWORD, "keycloak", "HS512");
+
+ assertThatCode(() ->
JWTAuthenticationConfigurer.assertJwtKeystoreHasKey("PKCS12", file.toString(),
PASSWORD,
+ "camel.server")).doesNotThrowAnyException();
+ }
+
+ @Test
+ void noAliasNamedAfterAnAlgorithmFails() throws Exception {
+ Path file = writeKeystore("PKCS12", PASSWORD, "keycloak");
+
+ assertThatThrownBy(() ->
JWTAuthenticationConfigurer.assertJwtKeystoreHasKey("PKCS12", file.toString(),
+ PASSWORD, "camel.management"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining(file.toString())
+ .hasMessageContaining("camel.management.jwtKeystorePath")
+ .hasMessageContaining("keycloak")
+ .hasMessageContaining("RS256");
+ }
+
+ @Test
+ void keyWithAnotherPasswordThanTheKeystoreFails() throws Exception {
+ // JCEKS keeps a password per key, so the key can have another
password than the keystore
+ Path file = writeKeystore("JCEKS", "another", "HS256");
+
+ assertThatThrownBy(() ->
JWTAuthenticationConfigurer.assertJwtKeystoreHasKey("JCEKS", file.toString(),
+ PASSWORD, "camel.server"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("has no key that Vert.x can use");
+ }
+
+ @Test
+ void typeNotSetIsStillChecked() throws Exception {
+ // the default keystore type of Java is PKCS12
+ Path file = writeKeystore("PKCS12", PASSWORD, "keycloak");
+
+ assertThatThrownBy(() ->
JWTAuthenticationConfigurer.assertJwtKeystoreHasKey(null, file.toString(),
PASSWORD,
+ "camel.server"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("keycloak");
+ }
+
+ @Test
+ void keystoreNotFoundFails() {
+ String missing = dir.resolve("missing.p12").toString();
+
+ assertThatThrownBy(() ->
JWTAuthenticationConfigurer.assertJwtKeystoreHasKey("PKCS12", missing, PASSWORD,
+ "camel.server"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("is not found");
+ }
+
+ private Path writeKeystore(String type, String keyPassword, String...
aliases) throws Exception {
+ KeyStore keyStore = KeyStore.getInstance(type);
+ keyStore.load(null, null);
+ for (String alias : aliases) {
+ keyStore.setEntry(alias, new KeyStore.SecretKeyEntry(new
SecretKeySpec(new byte[32], "HmacSHA256")),
+ new
KeyStore.PasswordProtection(keyPassword.toCharArray()));
+ }
+ Path file = dir.resolve("keystore." + type.toLowerCase());
+ try (OutputStream out = Files.newOutputStream(file)) {
+ keyStore.store(out, PASSWORD.toCharArray());
+ }
+ return file;
+ }
+}
diff --git
a/components/camel-platform-http-main/src/test/resources/test-camel-main-auth-jwt.jks
b/components/camel-platform-http-main/src/test/resources/test-camel-main-auth-jwt.jks
index 45a01afeea71..5301cf91d554 100644
Binary files
a/components/camel-platform-http-main/src/test/resources/test-camel-main-auth-jwt.jks
and
b/components/camel-platform-http-main/src/test/resources/test-camel-main-auth-jwt.jks
differ
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 37027c372c50..bb1c272dca26 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -8730,6 +8730,21 @@ A custom `EmbeddingModel` must implement
`embedAll(List<TextSegment>)` (or `doEm
the LangChain4j models do. A model that only overrides `embed(TextSegment)` is
no longer called, and the
default methods of LangChain4j then fail with a `StackOverflowError`.
+=== camel-platform-http-main - the server fails to start when Vert.x cannot
use a key of the JWT keystore
+
+Vert.x only uses the keys of a JWT keystore whose alias is the name of a JWT
algorithm (`HS256`, `RS256`, `ES256` and
+so on), read with the keystore password. Without such a key, Vert.x ran in
unsecured mode and rejected every signed
+token with HTTP 401. The HTTP server and the management server now fail to
start with an `IllegalArgumentException`
+when `camel.server.jwtKeystorePath` or `camel.management.jwtKeystorePath`
points to a keystore that has no such key,
+or that cannot be found or loaded.
+
+Rename the alias of the key to the algorithm of the key, for example `RS256`
for an RSA key:
+
+[source,bash]
+----
+keytool -changealias -alias keycloak -destalias RS256 -keystore
keycloak-realm.p12 -storetype PKCS12
+----
+
=== Jackson 3
Camel 4.24 moves its own code to Jackson 3 (`tools.jackson`, version 3.2). The
`camel-jackson`, `camel-jacksonxml`,