-
-
Notifications
You must be signed in to change notification settings - Fork 18
Add CzertainlyAuthenticationCache #1435
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
18b4d56
Add CzertainlyAuthenticationCache with caching for authentication met…
LukasNajman 90bfb9f
Compare UUIDs not strings
LukasNajman 1d5bf6c
Add SessionTableHelper to create session tables in the database
LukasNajman 50a6748
Handle malformed certificate headers in CzertainlyAuthenticationFilte…
LukasNajman 3647d22
Replace null checks with `Objects.requireNonNull` in CzertainlyAuthen…
LukasNajman cd3ef37
Extract `TokenJtiIndex` for user-to-JTI mapping and integrate it into…
LukasNajman a56422e
Validate `userUuid` is non-blank in `TokenJtiIndex.add` to ensure dat…
LukasNajman 52630ee
Fix failing tests
LukasNajman 1d06752
Handle `IllegalStateException` in `CzertainlyAuthenticationClient` to…
LukasNajman 9383a94
Address code review notes
LukasNajman 985efbc
Refactor `AuthenticationCache` to remove certificate fingerprint depe…
LukasNajman f575127
Introduce configurable authentication cache properties with TTL and m…
LukasNajman d5bf756
Enable cache statistics tracking for enhanced monitoring
LukasNajman 8a549c3
Add integration tests for `AuthenticationCache` eviction logic
LukasNajman d908ec1
Update `revokeCertificate` test to correct cache eviction assertions
LukasNajman 28ff286
Refactor to use `UUID` objects instead of `String`
LukasNajman eee94d9
Merge branch 'main' into Authn-Cache
LukasNajman 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
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/czertainly/core/config/AuthCacheProperties.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,15 @@ | ||
| package com.czertainly.core.config; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| @ConfigurationProperties(prefix = "caching.authentication") | ||
| public record AuthCacheProperties( | ||
| int ttlMinutes, | ||
| int maxSize | ||
| ) { | ||
|
|
||
| public AuthCacheProperties { | ||
| if (ttlMinutes <= 0) ttlMinutes = 5; | ||
| if (maxSize <= 0) maxSize = 500; | ||
| } | ||
| } |
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,52 @@ | ||
| package com.czertainly.core.config; | ||
|
|
||
| import com.czertainly.core.security.authn.client.TokenJtiIndex; | ||
| import com.czertainly.core.security.authn.client.UserCertificateIndex; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.annotation.EnableCaching; | ||
| import org.springframework.cache.caffeine.CaffeineCacheManager; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.Ordered; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Configuration | ||
| @EnableCaching(order = Ordered.HIGHEST_PRECEDENCE) | ||
| @EnableConfigurationProperties(AuthCacheProperties.class) | ||
| public class CacheConfig { | ||
|
|
||
| public static final String SIGNING_PROFILES_CACHE = "signingProfiles"; | ||
| public static final String TSP_PROFILES_CACHE = "tspProfiles"; | ||
| public static final String CERTIFICATE_CHAIN_CACHE = "certificateChain"; | ||
| public static final String SYSTEM_USER_AUTH_CACHE = "systemUserAuth"; | ||
| public static final String USER_UUID_AUTH_CACHE = "userUuidAuth"; | ||
| public static final String CERTIFICATE_AUTH_CACHE = "certificateAuth"; | ||
| public static final String TOKEN_AUTH_CACHE = "tokenAuth"; | ||
| public static final String FORMATTER_CONNECTOR_CACHE = "formatterConnector"; | ||
| public static final String CRYPTOGRAPHIC_KEY_ITEM_CACHE = "cryptographicKeyItem"; | ||
|
|
||
| @Bean | ||
| public CacheManager cacheManager(AuthCacheProperties cacheProperties, TokenJtiIndex tokenJtiIndex, UserCertificateIndex userCertificateIndex) { | ||
| CaffeineCacheManager mgr = new CaffeineCacheManager(SIGNING_PROFILES_CACHE, TSP_PROFILES_CACHE, CERTIFICATE_CHAIN_CACHE, SYSTEM_USER_AUTH_CACHE, USER_UUID_AUTH_CACHE, FORMATTER_CONNECTOR_CACHE, CRYPTOGRAPHIC_KEY_ITEM_CACHE); | ||
| mgr.setCaffeine(Caffeine.newBuilder() | ||
| .expireAfterWrite(cacheProperties.ttlMinutes(), TimeUnit.MINUTES) | ||
| .maximumSize(cacheProperties.maxSize()) | ||
| .recordStats()); | ||
| mgr.registerCustomCache(CERTIFICATE_AUTH_CACHE, Caffeine.newBuilder() | ||
| .expireAfterWrite(cacheProperties.ttlMinutes(), TimeUnit.MINUTES) | ||
| .maximumSize(cacheProperties.maxSize()) | ||
| .recordStats() | ||
| .removalListener(userCertificateIndex) | ||
| .build()); | ||
| mgr.registerCustomCache(TOKEN_AUTH_CACHE, Caffeine.newBuilder() | ||
| .expireAfterWrite(cacheProperties.ttlMinutes(), TimeUnit.MINUTES) | ||
| .maximumSize(cacheProperties.maxSize()) | ||
| .recordStats() | ||
| .removalListener(tokenJtiIndex) | ||
| .build()); | ||
| return mgr; | ||
| } | ||
| } |
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
78 changes: 78 additions & 0 deletions
78
src/main/java/com/czertainly/core/security/authn/client/AuthenticationCache.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,78 @@ | ||
| package com.czertainly.core.security.authn.client; | ||
|
|
||
| import java.util.UUID; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public interface AuthenticationCache { | ||
|
|
||
| /** | ||
| * Returns cached authentication for a system user, or invokes {@code loader} and caches the result. | ||
| * Cached by username. System usernames (superadmin, acme, scep, …) are stable identifiers | ||
| * that never change, so the cache entry remains valid for its full TTL. | ||
| * | ||
| * @param username system username used as the cache key | ||
| * @param loader called on a cache miss to produce the {@link AuthenticationInfo} | ||
| * @return the cached or freshly loaded {@link AuthenticationInfo} | ||
| */ | ||
| AuthenticationInfo getOrAuthenticateSystemUser(String username, Supplier<AuthenticationInfo> loader); | ||
|
LukasNajman marked this conversation as resolved.
Dismissed
|
||
|
|
||
| /** | ||
| * Returns cached authentication for a user identified by UUID, or invokes {@code loader} and caches the result. | ||
| * Effective for repeated internal impersonation calls within the TTL window. | ||
| * | ||
| * @param userUuid UUID of the user, used as the cache key | ||
| * @param loader called on a cache miss to produce the {@link AuthenticationInfo} | ||
| * @return the cached or freshly loaded {@link AuthenticationInfo} | ||
| */ | ||
| AuthenticationInfo getOrAuthenticateByUserUuid(UUID userUuid, Supplier<AuthenticationInfo> loader); | ||
|
LukasNajman marked this conversation as resolved.
Dismissed
|
||
|
|
||
| /** | ||
| * Returns cached authentication for a client-certificate request, or invokes {@code loader} and caches the result. | ||
| * Cached by SHA-256 of the DER-encoded certificate bytes (computed in CzertainlyAuthenticationFilter), | ||
| * matching the DB {@code certificate.fingerprint} field. All requests carrying the same client certificate | ||
| * share one cache entry. The entry is evicted immediately on revocation via CertificateServiceImpl. | ||
| * | ||
| * @param thumbprint SHA-256 fingerprint of the client certificate, used as the cache key | ||
| * @param loader called on a cache miss to produce the {@link AuthenticationInfo} | ||
| * @return the cached or freshly loaded {@link AuthenticationInfo} | ||
| */ | ||
| AuthenticationInfo getOrAuthenticateByCertificate(String thumbprint, Supplier<AuthenticationInfo> loader); | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| /** | ||
| * Returns cached authentication for a bearer-token request, or invokes {@code loader} and caches the result. | ||
| * Cached by the {@code jti} claim, which is unique per token issuance. All requests that carry the same | ||
| * access token share one cache entry for its lifetime. When the token is refreshed, a new {@code jti} | ||
| * causes a cache miss, triggering a fresh auth-service call. Tokens without a {@code jti} are never cached. | ||
| * | ||
| * @param jti the {@code jti} claim of the access token, used as the cache key; {@code null} skips caching | ||
| * @param loader called on a cache miss to produce the {@link AuthenticationInfo} | ||
| * @return the cached or freshly loaded {@link AuthenticationInfo} | ||
| */ | ||
| AuthenticationInfo getOrAuthenticateByToken(String jti, Supplier<AuthenticationInfo> loader); | ||
|
|
||
| /** | ||
| * Evicts all auth cache entries for a single user: their UUID entry, all token entries tracked | ||
| * in the jti index, and their certificate entry tracked in the certificate index. | ||
| * Use this for user-scoped changes (role assignment, disable, delete, certificate revocation) | ||
| * where only one user is affected and the system-user cache can be left untouched. | ||
| * | ||
| * @param userUuid UUID of the user whose cache entries should be evicted | ||
| */ | ||
| void evictByUserUuid(UUID userUuid); | ||
|
|
||
| /** | ||
| * Evicts only the certificate-based auth cache entry for the given fingerprint. | ||
| * Use this when a certificate is disassociated from a user but the user's identity and | ||
| * roles are unchanged — their UUID and token cache entries remain valid. | ||
| * | ||
| * @param certFingerprint SHA-256 fingerprint of the certificate to evict | ||
| */ | ||
| void evictByCertificateFingerprint(String certFingerprint); | ||
|
|
||
| /** | ||
| * Clears all four auth caches and the jti index (the per-user map of {@code jti} claims used to | ||
| * find and evict token cache entries when a user is modified). Use this for role-level mutations | ||
| * (permission changes, role deletion) that may affect any user, including system accounts. | ||
| */ | ||
| void evictAll(); | ||
| } | ||
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.