-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Service Accounts - cache clear API #71605
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb15146
Service Accounts - add Rest API for service account cache invalidation
ywangd 3e4690c
do not cache failed auth result
ywangd 2fe4535
Address feedback on handling token names
ywangd e68a84a
Merge remote-tracking branch 'origin/master' into service-account-cac…
ywangd 8ebfdd5
Merge remote-tracking branch 'origin/master' into service-account-cac…
ywangd 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 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 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 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 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 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
65 changes: 65 additions & 0 deletions
65
...arch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheAction.java
This file contains 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,65 @@ | ||
/* | ||
* 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.security.rest.action.service; | ||
|
||
import org.elasticsearch.client.node.NodeClient; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.action.RestActions; | ||
import org.elasticsearch.xpack.core.security.action.ClearSecurityCacheAction; | ||
import org.elasticsearch.xpack.core.security.action.ClearSecurityCacheRequest; | ||
import org.elasticsearch.xpack.core.security.support.Validation; | ||
import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.elasticsearch.rest.RestRequest.Method.POST; | ||
|
||
public class RestClearServiceAccountTokenStoreCacheAction extends SecurityBaseRestHandler { | ||
|
||
public RestClearServiceAccountTokenStoreCacheAction(Settings settings, XPackLicenseState licenseState) { | ||
super(settings, licenseState); | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return List.of(new Route(POST, "/_security/service/{namespace}/{service}/credential/token/{name}/_clear_cache")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "xpack_security_clear_service_account_token_store_cache"; | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
final String namespace = request.param("namespace"); | ||
final String service = request.param("service"); | ||
String[] tokenNames = request.paramAsStringArrayOrEmptyIfAll("name"); | ||
|
||
ClearSecurityCacheRequest req = new ClearSecurityCacheRequest().cacheName("service"); | ||
if (tokenNames.length == 0) { | ||
// This is the wildcard case for tokenNames | ||
req.keys(namespace + "/" + service + "/"); | ||
} else { | ||
final Set<String> qualifiedTokenNames = new HashSet<>(tokenNames.length); | ||
for (String name: tokenNames) { | ||
if (false == Validation.isValidServiceAccountTokenName(name)) { | ||
throw new IllegalArgumentException(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE + " got: [" + name + "]"); | ||
} | ||
qualifiedTokenNames.add(namespace + "/" + service + "/" + name); | ||
} | ||
req.keys(qualifiedTokenNames.toArray(String[]::new)); | ||
} | ||
return channel -> client.execute(ClearSecurityCacheAction.INSTANCE, req, new RestActions.NodesResponseRestListener<>(channel)); | ||
} | ||
} |
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.
I made this change so the logic is mostly the same as the one of
CachingUsernamePasswordRealm
. Basically the cache now does not cache negative results. I think this is a better choice since service account will be well known and it is easy to cause cache thrashing if negative results are cached. This is probably the similar argument whyCachingUsernamePasswordRealm
does not cache negative results as well. Note thatApiKeyService
cache is different in that it does cache negative results. But it can afford to do that because the ApiKey ID, unlike username/service account, is not well known.