Skip to content

Enhance API Key Querying#103192

Closed
albertzaharovits wants to merge 23 commits intoelastic:mainfrom
albertzaharovits:query-api-key-api-improvements
Closed

Enhance API Key Querying#103192
albertzaharovits wants to merge 23 commits intoelastic:mainfrom
albertzaharovits:query-api-key-api-improvements

Conversation

@albertzaharovits
Copy link
Contributor

@albertzaharovits albertzaharovits commented Dec 8, 2023

Adds support for the following to the Query API Key API:

Closes: #101691

@albertzaharovits albertzaharovits added >enhancement :Security/Security Security issues without another label labels Dec 8, 2023
@albertzaharovits albertzaharovits self-assigned this Dec 8, 2023
@elasticsearchmachine
Copy link
Collaborator

Hi @albertzaharovits, I've created a changelog YAML for you.

@albertzaharovits albertzaharovits changed the title Query api key api improvements Enhance API Key Querying Dec 8, 2023
Comment on lines +89 to +90
ApiKeyAggregationsBuilder.verifyRequested(aggsBuilder, filteringAuthentication);
searchSourceBuilder.aggregationsBuilder(aggsBuilder);
Copy link
Contributor Author

@albertzaharovits albertzaharovits Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above verifies & modifies the requested aggregation to:

  • prohib scripts that can directly access the API Key docs
  • rewrite the field names (because the query & agg use different field names from the document ones, in order to reflect the terminology that we use in the returned API Keys)
  • rewrite queries to scope them to API Key docs only (and sometimes to the keys owned by the calling user only)

this.format = clone.format;
this.missing = clone.missing;
this.timeZone = clone.timeZone;
this.config = clone.config;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this unused bit, as it looked like one way to change the value source for the agg, and the point of this PR is to restrict that for API Keys aggs.


public static class KeyedFilter implements Writeable, ToXContentFragment {
private final String key;
private final QueryBuilder filter;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I had to make the filter here mutable, because the query for API Key aggs has to be modified to go only over API Key docs, and also optionally of a single user.

public class FilterAggregationBuilder extends AbstractAggregationBuilder<FilterAggregationBuilder> {
public static final String NAME = "filter";

private final QueryBuilder filter;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +38 to +42
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(doc['type'].value ?: \"rest\");"))
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keys created before 8.9 (#95714) don't have a type field and they effectively work as "rest" types do post 8.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>enhancement :Security/Security Security issues without another label v8.14.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance Query API key information API

2 participants