-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Add support for the type
parameter to the Query API Key API
#103695
Changes from all commits
23c2053
1db1806
c84b841
ba575e1
6b3d7b6
c465cde
aace531
a1c7d61
e4d64b1
d13e199
e8dba56
8ae29f2
7d666ad
19a1e72
2673e61
6dc9189
05653c5
859fa4b
38458f1
5ed7840
3dfe94a
20727d1
c64b574
fb69b9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,11 +27,26 @@ | |
import org.elasticsearch.xpack.security.support.ApiKeyFieldNameTranslators; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static org.elasticsearch.xpack.security.support.SecuritySystemIndices.SECURITY_MAIN_ALIAS; | ||
|
||
public final class TransportQueryApiKeyAction extends HandledTransportAction<QueryApiKeyRequest, QueryApiKeyResponse> { | ||
|
||
// API keys with no "type" field are implicitly of type "rest" (this is the case for all API Keys created before v8.9). | ||
// The below runtime field ensures that the "type" field can be used by the {@link RestQueryApiKeyAction}, | ||
// while making the implicit "rest" type feature transparent to the caller (hence all keys are either "rest" | ||
// or "cross_cluster", and the "type" is always set). | ||
// This can be improved, to get rid of the runtime performance impact of the runtime field, by reindexing | ||
// the api key docs and setting the "type" to "rest" if empty. But the infrastructure to run such a maintenance | ||
// task on a system index (once the cluster version permits) is not currently available. | ||
public static final String API_KEY_TYPE_RUNTIME_MAPPING_FIELD = "runtime_key_type"; | ||
private static final Map<String, Object> API_KEY_TYPE_RUNTIME_MAPPING = Map.of( | ||
API_KEY_TYPE_RUNTIME_MAPPING_FIELD, | ||
Map.of("type", "keyword", "script", Map.of("source", "emit(field('type').get(\"rest\"));")) | ||
); | ||
|
||
private final ApiKeyService apiKeyService; | ||
private final SecurityContext securityContext; | ||
|
||
|
@@ -66,12 +81,19 @@ protected void doExecute(Task task, QueryApiKeyRequest request, ActionListener<Q | |
searchSourceBuilder.size(request.getSize()); | ||
} | ||
|
||
final ApiKeyBoolQueryBuilder apiKeyBoolQueryBuilder = ApiKeyBoolQueryBuilder.build( | ||
request.getQueryBuilder(), | ||
request.isFilterForCurrentUser() ? authentication : null | ||
); | ||
final AtomicBoolean accessesApiKeyTypeField = new AtomicBoolean(false); | ||
final ApiKeyBoolQueryBuilder apiKeyBoolQueryBuilder = ApiKeyBoolQueryBuilder.build(request.getQueryBuilder(), fieldName -> { | ||
if (API_KEY_TYPE_RUNTIME_MAPPING_FIELD.equals(fieldName)) { | ||
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. Wondering if we can skip the boolean ref here and do this directly, in the lambda: // only add the query-level runtime field to the search request if it's actually referring the "type" field
if (API_KEY_TYPE_RUNTIME_MAPPING_FIELD.equals(fieldName)) {
searchSourceBuilder.runtimeMappings(API_KEY_TYPE_RUNTIME_MAPPING);
} 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. Ah no that wouldn't work because we need this to happen only once 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. Well... I don't think it needs to happen once, |
||
accessesApiKeyTypeField.set(true); | ||
} | ||
}, request.isFilterForCurrentUser() ? authentication : null); | ||
searchSourceBuilder.query(apiKeyBoolQueryBuilder); | ||
|
||
// only add the query-level runtime field to the search request if it's actually referring the "type" field | ||
if (accessesApiKeyTypeField.get()) { | ||
searchSourceBuilder.runtimeMappings(API_KEY_TYPE_RUNTIME_MAPPING); | ||
} | ||
|
||
if (request.getFieldSortBuilders() != null) { | ||
translateFieldSortBuilders(request.getFieldSortBuilders(), searchSourceBuilder); | ||
} | ||
|
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.
Optional, but could also add a
NOT cross_cluster
query here since that's probably a query end-users might try.