-
Notifications
You must be signed in to change notification settings - Fork 341
Usage of JWKS with JWT (without using OpenID connect) #5578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shikharj05
merged 46 commits into
opensearch-project:main
from
Rishav9852Kumar:jwt-auth-with-jwks
Sep 26, 2025
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
a3fe97a
initial-jwks-commit
d1329ff
code refactor
8b75ce6
code refactor
4d33e97
code refactor
452d5b3
code refactor
caaa8c6
Merge branch 'opensearch-project:main' into jwt-auth-with-jwks
Rishav9852Kumar 38077a7
added response size and jwks count limits
6a7a517
code refactor
37e0e51
Merge branch 'opensearch-project:main' into jwt-auth-with-jwks
Rishav9852Kumar c653789
Merge branch 'opensearch-project:main' into jwt-auth-with-jwks
Rishav9852Kumar 0071e3f
merged the jwks auth in config.yml
a8eca4e
code-refactor
6a1c9cb
Merge branch 'opensearch-project:main' into jwt-auth-with-jwks
Rishav9852Kumar 464a542
temp
7a31689
temp
a90c414
logic-flow-change
9153ffa
code-refactor
eb87375
code-refactor
669bcd8
code-refactor
caed0e2
removing comments
8ee0bc0
adding unit tests and mockjwks-server
e5dda13
code-refactor
eadab8f
temp
7772406
test-fix
a9a7109
test-refactor
62fa6d6
Merge branch 'opensearch-project:main' into jwt-auth-with-jwks
Rishav9852Kumar 41c5014
test-refactor
1901f17
test-refactor
e97ddcd
Merge branch 'opensearch-project:main' into jwt-auth-with-jwks
Rishav9852Kumar 8836e4d
code-refactor
60ddf61
indentations fix
2b7a022
Merge branch 'opensearch-project:main' into jwt-auth-with-jwks
Rishav9852Kumar a6cd72d
code-changes-done
0e0308d
test-refactor
115955f
code modification done
8262dbe
test-refactor
ac93d2b
test indentation refactor
3826416
test indentation refactor
126f8c4
Merge branch 'main' into jwt-auth-with-jwks
Rishav9852Kumar f54af46
Merge branch 'main' into jwt-auth-with-jwks
Rishav9852Kumar b1c41e2
removing changelog to resolve conflict
03d206b
Merge branch 'main' into jwt-auth-with-jwks
Rishav9852Kumar b6262ce
resolving comments
f61c902
added parsing checks around jwks
68adef6
test-refactor
9845012
logging-fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...n/java/org/opensearch/security/auth/http/jwt/keybyjwks/HTTPJwtKeyByJWKSAuthenticator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| * Modifications Copyright OpenSearch Contributors. See | ||
| * GitHub history for details. | ||
| */ | ||
|
|
||
| package org.opensearch.security.auth.http.jwt.keybyjwks; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import org.opensearch.OpenSearchSecurityException; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.common.util.concurrent.ThreadContext; | ||
| import org.opensearch.core.common.Strings; | ||
| import org.opensearch.security.auth.http.jwt.AbstractHTTPJwtAuthenticator; | ||
| import org.opensearch.security.auth.http.jwt.HTTPJwtAuthenticator; | ||
| import org.opensearch.security.auth.http.jwt.keybyoidc.KeyProvider; | ||
| import org.opensearch.security.auth.http.jwt.keybyoidc.KeySetRetriever; | ||
| import org.opensearch.security.auth.http.jwt.keybyoidc.SelfRefreshingKeySet; | ||
| import org.opensearch.security.filter.SecurityRequest; | ||
| import org.opensearch.security.user.AuthCredentials; | ||
| import org.opensearch.security.util.SettingsBasedSSLConfigurator; | ||
|
|
||
| /** | ||
| * JWT authenticator that uses JWKS (JSON Web Key Set) endpoints for key retrieval. | ||
| * | ||
| * This authenticator extends AbstractHTTPJwtAuthenticator and provides JWKS-specific | ||
| * key provider initialization. It supports direct JWKS endpoint access with caching, | ||
| * SSL configuration, automatic key refresh, and enhanced security features to protect | ||
| * against malicious JWKS endpoints. | ||
| * | ||
| * Security Features: | ||
| * - Response size validation before parsing to prevent memory exhaustion | ||
| * - Hard key count limit after parsing to reject oversized JWKS | ||
| * - Configurable timeouts and rate limiting | ||
| * | ||
| * Configuration: | ||
| * - jwks_uri: Direct JWKS endpoint URL (required) | ||
| * - cache_jwks_endpoint: Enable/disable caching (default: true) | ||
Rishav9852Kumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * - jwks_request_timeout_ms: Request timeout in milliseconds (default: 5000) | ||
| * - jwks_queued_thread_timeout_ms: Queued thread timeout (default: 2500) | ||
| * - refresh_rate_limit_time_window_ms: Rate limit window (default: 10000) | ||
| * - refresh_rate_limit_count: Max refreshes per window (default: 10) | ||
| * - max_jwks_keys: HARD LIMIT - Rejects JWKS if exceeded (default: 10) | ||
| * - max_jwks_response_size_bytes: Max HTTP response size (default: 1MB) | ||
| */ | ||
| public class HTTPJwtKeyByJWKSAuthenticator extends AbstractHTTPJwtAuthenticator { | ||
|
|
||
| private final static Logger log = LogManager.getLogger(HTTPJwtKeyByJWKSAuthenticator.class); | ||
|
|
||
| // Fallback to static JWT authenticator if jwks_uri is null | ||
| private final HTTPJwtAuthenticator staticJwtAuthenticator; | ||
| private final boolean useJwks; | ||
| private final String jwtUrlParameter; | ||
|
|
||
| public HTTPJwtKeyByJWKSAuthenticator(Settings settings, Path configPath) { | ||
| super(settings, configPath); | ||
|
|
||
| String jwksUri = settings.get("jwks_uri"); | ||
| this.useJwks = !Strings.isNullOrEmpty(jwksUri); | ||
| this.jwtUrlParameter = settings.get("jwt_url_parameter"); | ||
|
|
||
| // Initialize static JWT authenticator as fallback if jwks_uri is not configured | ||
| if (!useJwks) { | ||
| log.warn("jwks_uri is not configured, falling back to static JWT authentication"); | ||
| this.staticJwtAuthenticator = new HTTPJwtAuthenticator(settings, configPath); | ||
| } else { | ||
| this.staticJwtAuthenticator = null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected KeyProvider initKeyProvider(Settings settings, Path configPath) throws Exception { | ||
| String jwksUri = settings.get("jwks_uri"); | ||
|
|
||
| // If jwks_uri is not configured, return null (will use static JWT fallback) | ||
| if (jwksUri == null || jwksUri.isBlank()) { | ||
| log.warn("jwks_uri is not configured, will use static JWT authentication fallback"); | ||
| return null; | ||
| } | ||
|
|
||
| log.debug("Initializing JWKS key provider with endpoint: {}", jwksUri); | ||
|
|
||
| // Initialize configuration parameters | ||
| int jwksRequestTimeoutMs = settings.getAsInt("jwks_request_timeout_ms", 5000); | ||
| int jwksQueuedThreadTimeoutMs = settings.getAsInt("jwks_queued_thread_timeout_ms", 2500); | ||
| int refreshRateLimitTimeWindowMs = settings.getAsInt("refresh_rate_limit_time_window_ms", 10000); | ||
| int refreshRateLimitCount = settings.getAsInt("refresh_rate_limit_count", 10); | ||
| boolean cacheJwksEndpoint = settings.getAsBoolean("cache_jwks_endpoint", true); | ||
| int maxJwksKeys = settings.getAsInt("max_jwks_keys", -1); | ||
|
|
||
| log.warn("Initializing JWKS key provider with endpoint: {} (max keys: {})", jwksUri, maxJwksKeys); | ||
|
|
||
| // Add security configuration parameters | ||
| long maxJwksResponseSizeBytes = settings.getAsLong("max_jwks_response_size_bytes", 1024L * 1024L); // 1MB default | ||
|
|
||
| // Create secure key set retriever with HARD LIMIT enforcement using maxJwksKeys | ||
| KeySetRetriever keySetRetriever = KeySetRetriever.createForJwksUri( | ||
| getSSLConfig(settings, configPath), | ||
| cacheJwksEndpoint, | ||
| jwksUri, | ||
| maxJwksResponseSizeBytes, | ||
| maxJwksKeys | ||
| ); | ||
| keySetRetriever.setRequestTimeoutMs(jwksRequestTimeoutMs); | ||
|
|
||
| // Create self-refreshing key set with caching and rate limiting | ||
| SelfRefreshingKeySet selfRefreshingKeySet = new SelfRefreshingKeySet(keySetRetriever); | ||
| selfRefreshingKeySet.setRequestTimeoutMs(jwksRequestTimeoutMs); | ||
| selfRefreshingKeySet.setQueuedThreadTimeoutMs(jwksQueuedThreadTimeoutMs); | ||
| selfRefreshingKeySet.setRefreshRateLimitTimeWindowMs(refreshRateLimitTimeWindowMs); | ||
| selfRefreshingKeySet.setRefreshRateLimitCount(refreshRateLimitCount); | ||
|
|
||
| return selfRefreshingKeySet; | ||
| } | ||
|
|
||
| @Override | ||
| public AuthCredentials extractCredentials(final SecurityRequest request, final ThreadContext context) | ||
| throws OpenSearchSecurityException { | ||
|
|
||
| // If jwks_uri is not configured, delegate to static JWT authenticator | ||
| if (!useJwks && staticJwtAuthenticator != null) { | ||
| log.debug("Delegating to static JWT authenticator since jwks_uri is not configured"); | ||
| return staticJwtAuthenticator.extractCredentials(request, context); | ||
| } | ||
|
|
||
| // Use the standard JWKS authentication flow | ||
| return super.extractCredentials(request, context); | ||
| } | ||
|
|
||
| private static SettingsBasedSSLConfigurator.SSLConfig getSSLConfig(Settings settings, Path configPath) throws Exception { | ||
| return new SettingsBasedSSLConfigurator(settings, configPath, "jwks").buildSSLConfig(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return "jwt"; | ||
| } | ||
Rishav9852Kumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public Set<String> getSensitiveUrlParams() { | ||
| if (jwtUrlParameter != null) { | ||
| return Set.of(jwtUrlParameter); | ||
| } | ||
| return Collections.emptySet(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.