-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add Support for Providing a custom ServiceAccountTokenStore through SecurityExtensions #126612
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
jfreden
merged 17 commits into
elastic:main
from
jfreden:add_project_service_account_auth
May 7, 2025
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3dec54b
Add Project Service Account Auth
jfreden fcab6e2
fixup! Token source
jfreden 0d027db
fixup! Bug
jfreden 6a64ba5
fixup!
jfreden cd95dc1
Merge remote-tracking branch 'upstream/main' into add_project_service…
jfreden c679f7d
Update docs/changelog/126612.yaml
jfreden 35e379e
fixup! Code reiew comments
jfreden 5340793
Merge remote-tracking branch 'upstream/main' into add_project_service…
jfreden 5628f9e
fixup! Remov log
jfreden 6ff864f
[CI] Auto commit changes from spotless
cbe9cec
fixup! binding
jfreden 91ab630
fixup! Test - change interface
jfreden 4e2b7cf
fixup! Code review comments
jfreden 9da5400
fixup! Code review comments
jfreden 8dee42d
[CI] Auto commit changes from spotless
1375e1c
fixup! checkstyle
jfreden cebaa8a
Merge remote-tracking branch 'upstream/main' into add_project_service…
jfreden 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 126612 | ||
| summary: Add Support for Providing a custom `ServiceAccountTokenStore` through `SecurityExtensions` | ||
| area: Authentication | ||
| type: enhancement | ||
| issues: [] |
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
18 changes: 18 additions & 0 deletions
18
...rg/elasticsearch/xpack/core/security/authc/service/NodeLocalServiceAccountTokenStore.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,18 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.security.authc.service; | ||
|
|
||
| import org.elasticsearch.xpack.core.security.action.service.TokenInfo; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface NodeLocalServiceAccountTokenStore extends ServiceAccountTokenStore { | ||
| default List<TokenInfo> findNodeLocalTokensFor(ServiceAccount.ServiceAccountId accountId) { | ||
| throw new IllegalStateException("Find node local tokens not supported by [" + this.getClass() + "]"); | ||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,6 +208,8 @@ | |
| import org.elasticsearch.xpack.core.security.authc.RealmConfig; | ||
| import org.elasticsearch.xpack.core.security.authc.RealmSettings; | ||
| import org.elasticsearch.xpack.core.security.authc.Subject; | ||
| import org.elasticsearch.xpack.core.security.authc.service.NodeLocalServiceAccountTokenStore; | ||
| import org.elasticsearch.xpack.core.security.authc.service.ServiceAccountTokenStore; | ||
| import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; | ||
| import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; | ||
| import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine; | ||
|
|
@@ -310,6 +312,7 @@ | |
| import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm; | ||
| import org.elasticsearch.xpack.security.authc.jwt.JwtRealm; | ||
| import org.elasticsearch.xpack.security.authc.service.CachingServiceAccountTokenStore; | ||
| import org.elasticsearch.xpack.security.authc.service.CompositeServiceAccountTokenStore; | ||
| import org.elasticsearch.xpack.security.authc.service.FileServiceAccountTokenStore; | ||
| import org.elasticsearch.xpack.security.authc.service.IndexServiceAccountTokenStore; | ||
| import org.elasticsearch.xpack.security.authc.service.ServiceAccountService; | ||
|
|
@@ -915,12 +918,34 @@ Collection<Object> createComponents( | |
| this.realms.set(realms); | ||
|
|
||
| systemIndices.getMainIndexManager().addStateListener(nativeRoleMappingStore::onSecurityIndexStateChange); | ||
|
|
||
| final CacheInvalidatorRegistry cacheInvalidatorRegistry = new CacheInvalidatorRegistry(); | ||
| cacheInvalidatorRegistry.registerAlias("service", Set.of("file_service_account_token", "index_service_account_token")); | ||
| components.add(cacheInvalidatorRegistry); | ||
| systemIndices.getMainIndexManager().addStateListener(cacheInvalidatorRegistry::onSecurityIndexStateChange); | ||
|
|
||
| ServiceAccountService serviceAccountService = createServiceAccountService( | ||
| components, | ||
| cacheInvalidatorRegistry, | ||
| extensionComponents, | ||
| () -> new IndexServiceAccountTokenStore( | ||
| settings, | ||
| threadPool, | ||
| getClock(), | ||
| client, | ||
| systemIndices.getMainIndexManager(), | ||
| clusterService, | ||
| cacheInvalidatorRegistry | ||
| ), | ||
| () -> new FileServiceAccountTokenStore( | ||
| environment, | ||
| resourceWatcherService, | ||
| threadPool, | ||
| clusterService, | ||
| cacheInvalidatorRegistry | ||
| ) | ||
| ); | ||
|
|
||
| components.add(serviceAccountService); | ||
|
|
||
| systemIndices.getMainIndexManager().addStateListener(cacheInvalidatorRegistry::onSecurityIndexStateChange); | ||
| final NativePrivilegeStore privilegeStore = new NativePrivilegeStore( | ||
| settings, | ||
| client, | ||
|
|
@@ -1004,33 +1029,6 @@ Collection<Object> createComponents( | |
| ); | ||
| components.add(apiKeyService); | ||
|
|
||
| final IndexServiceAccountTokenStore indexServiceAccountTokenStore = new IndexServiceAccountTokenStore( | ||
| settings, | ||
| threadPool, | ||
| getClock(), | ||
| client, | ||
| systemIndices.getMainIndexManager(), | ||
| clusterService, | ||
| cacheInvalidatorRegistry | ||
| ); | ||
| components.add(indexServiceAccountTokenStore); | ||
|
|
||
| final FileServiceAccountTokenStore fileServiceAccountTokenStore = new FileServiceAccountTokenStore( | ||
| environment, | ||
| resourceWatcherService, | ||
| threadPool, | ||
| clusterService, | ||
| cacheInvalidatorRegistry | ||
| ); | ||
| components.add(fileServiceAccountTokenStore); | ||
|
|
||
| final ServiceAccountService serviceAccountService = new ServiceAccountService( | ||
| client, | ||
| fileServiceAccountTokenStore, | ||
| indexServiceAccountTokenStore | ||
| ); | ||
| components.add(serviceAccountService); | ||
|
|
||
| final RoleProviders roleProviders = new RoleProviders( | ||
| reservedRolesStore, | ||
| fileRolesStore.get(), | ||
|
|
@@ -1250,6 +1248,74 @@ Collection<Object> createComponents( | |
| return components; | ||
| } | ||
|
|
||
| private ServiceAccountService createServiceAccountService( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test for the logic in this method?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added testing for this. Thanks! |
||
| List<Object> components, | ||
| CacheInvalidatorRegistry cacheInvalidatorRegistry, | ||
| SecurityExtension.SecurityComponents extensionComponents, | ||
| Supplier<IndexServiceAccountTokenStore> indexServiceAccountTokenStoreSupplier, | ||
| Supplier<FileServiceAccountTokenStore> fileServiceAccountTokenStoreSupplier | ||
| ) { | ||
| Map<String, ServiceAccountTokenStore> accountTokenStoreByExtension = new HashMap<>(); | ||
|
|
||
| for (var extension : securityExtensions) { | ||
| var serviceAccountTokenStore = extension.getServiceAccountTokenStore(extensionComponents); | ||
| if (serviceAccountTokenStore != null) { | ||
| if (isInternalExtension(extension) == false) { | ||
| throw new IllegalStateException( | ||
| "The [" | ||
| + extension.getClass().getName() | ||
| + "] extension tried to install a custom ServiceAccountTokenStore. This functionality is not available to " | ||
| + "external extensions." | ||
| ); | ||
| } | ||
| accountTokenStoreByExtension.put(extension.extensionName(), serviceAccountTokenStore); | ||
| } | ||
| } | ||
|
|
||
| if (accountTokenStoreByExtension.size() > 1) { | ||
| throw new IllegalStateException( | ||
| "More than one extension provided a ServiceAccountTokenStore override: " + accountTokenStoreByExtension.keySet() | ||
| ); | ||
| } | ||
|
|
||
| if (accountTokenStoreByExtension.isEmpty()) { | ||
| var fileServiceAccountTokenStore = fileServiceAccountTokenStoreSupplier.get(); | ||
| var indexServiceAccountTokenStore = indexServiceAccountTokenStoreSupplier.get(); | ||
|
|
||
| components.add(new PluginComponentBinding<>(NodeLocalServiceAccountTokenStore.class, fileServiceAccountTokenStore)); | ||
| components.add(fileServiceAccountTokenStore); | ||
| components.add(indexServiceAccountTokenStore); | ||
| cacheInvalidatorRegistry.registerAlias("service", Set.of("file_service_account_token", "index_service_account_token")); | ||
|
|
||
| return new ServiceAccountService( | ||
| client.get(), | ||
| new CompositeServiceAccountTokenStore( | ||
| List.of(fileServiceAccountTokenStore, indexServiceAccountTokenStore), | ||
| client.get().threadPool().getThreadContext() | ||
| ), | ||
| indexServiceAccountTokenStore | ||
| ); | ||
| } | ||
| // Completely handover service account token management to the extension if provided, | ||
| // this will disable the index managed | ||
| // service account tokens managed through the service account token API | ||
| var extensionStore = accountTokenStoreByExtension.values().stream().findFirst(); | ||
| components.add(new PluginComponentBinding<>(NodeLocalServiceAccountTokenStore.class, (token, listener) -> { | ||
| throw new IllegalStateException("Node local config not supported by [" + extensionStore.get().getClass() + "]"); | ||
| })); | ||
| components.add(extensionStore); | ||
| logger.debug("Service account authentication handled by extension, disabling file and index token stores"); | ||
| return new ServiceAccountService(client.get(), extensionStore.get()); | ||
| } | ||
|
|
||
| private static boolean isInternalExtension(SecurityExtension extension) { | ||
| final String canonicalName = extension.getClass().getCanonicalName(); | ||
| if (canonicalName == null) { | ||
| return false; | ||
| } | ||
| return canonicalName.startsWith("org.elasticsearch.xpack.") || canonicalName.startsWith("co.elastic.elasticsearch."); | ||
| } | ||
|
|
||
| @FixForMultiProject | ||
| // TODO : The migration task needs to be project aware | ||
| private void applyPendingSecurityMigrations(ProjectId projectId, SecurityIndexManager.IndexState newState) { | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These had to move since they're now part of the
SecurityExtensioninterface throughServiceAccountTokenStore.