Skip to content

Commit 324ee42

Browse files
authored
Add "manage_api_key" cluster privilege (#43728)
This adds a new cluster privilege for manage_api_key. Users with this privilege are able to create new API keys (as a child of their own user identity) and may also get and invalidate any/all API keys (including those owned by other users).
1 parent dfa882b commit 324ee42

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilege.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public final class ClusterPrivilege extends Privilege {
3939
InvalidateTokenAction.NAME, RefreshTokenAction.NAME);
4040
private static final Automaton MANAGE_OIDC_AUTOMATON = patterns("cluster:admin/xpack/security/oidc/*");
4141
private static final Automaton MANAGE_TOKEN_AUTOMATON = patterns("cluster:admin/xpack/security/token/*");
42+
private static final Automaton MANAGE_API_KEY_AUTOMATON = patterns("cluster:admin/xpack/security/api_key/*");
4243
private static final Automaton MONITOR_AUTOMATON = patterns("cluster:monitor/*");
4344
private static final Automaton MONITOR_ML_AUTOMATON = patterns("cluster:monitor/xpack/ml/*");
4445
private static final Automaton MONITOR_DATA_FRAME_AUTOMATON = patterns("cluster:monitor/data_frame/*");
@@ -84,6 +85,7 @@ public final class ClusterPrivilege extends Privilege {
8485
public static final ClusterPrivilege MANAGE_SECURITY = new ClusterPrivilege("manage_security", MANAGE_SECURITY_AUTOMATON);
8586
public static final ClusterPrivilege MANAGE_SAML = new ClusterPrivilege("manage_saml", MANAGE_SAML_AUTOMATON);
8687
public static final ClusterPrivilege MANAGE_OIDC = new ClusterPrivilege("manage_oidc", MANAGE_OIDC_AUTOMATON);
88+
public static final ClusterPrivilege MANAGE_API_KEY = new ClusterPrivilege("manage_api_key", MANAGE_API_KEY_AUTOMATON);
8789
public static final ClusterPrivilege MANAGE_PIPELINE = new ClusterPrivilege("manage_pipeline", "cluster:admin/ingest/pipeline/*");
8890
public static final ClusterPrivilege MANAGE_CCR = new ClusterPrivilege("manage_ccr", MANAGE_CCR_AUTOMATON);
8991
public static final ClusterPrivilege READ_CCR = new ClusterPrivilege("read_ccr", READ_CCR_AUTOMATON);
@@ -112,6 +114,7 @@ public final class ClusterPrivilege extends Privilege {
112114
entry("manage_security", MANAGE_SECURITY),
113115
entry("manage_saml", MANAGE_SAML),
114116
entry("manage_oidc", MANAGE_OIDC),
117+
entry("manage_api_key", MANAGE_API_KEY),
115118
entry("manage_pipeline", MANAGE_PIPELINE),
116119
entry("manage_rollup", MANAGE_ROLLUP),
117120
entry("manage_ccr", MANAGE_CCR),

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -743,27 +743,29 @@ private void findApiKeys(final BoolQueryBuilder boolQuery, boolean filterOutInva
743743
expiredQuery.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("expiration_time")));
744744
boolQuery.filter(expiredQuery);
745745
}
746-
final SearchRequest request = client.prepareSearch(SECURITY_MAIN_ALIAS)
747-
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
748-
.setQuery(boolQuery)
749-
.setVersion(false)
750-
.setSize(1000)
751-
.setFetchSource(true)
752-
.request();
753-
securityIndex.checkIndexVersionThenExecute(listener::onFailure,
754-
() -> ScrollHelper.fetchAllByEntity(client, request, listener,
755-
(SearchHit hit) -> {
756-
Map<String, Object> source = hit.getSourceAsMap();
757-
String name = (String) source.get("name");
758-
String id = hit.getId();
759-
Long creation = (Long) source.get("creation_time");
760-
Long expiration = (Long) source.get("expiration_time");
761-
Boolean invalidated = (Boolean) source.get("api_key_invalidated");
762-
String username = (String) ((Map<String, Object>) source.get("creator")).get("principal");
763-
String realm = (String) ((Map<String, Object>) source.get("creator")).get("realm");
764-
return new ApiKey(name, id, Instant.ofEpochMilli(creation),
765-
(expiration != null) ? Instant.ofEpochMilli(expiration) : null, invalidated, username, realm);
766-
}));
746+
try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) {
747+
final SearchRequest request = client.prepareSearch(SECURITY_MAIN_ALIAS)
748+
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
749+
.setQuery(boolQuery)
750+
.setVersion(false)
751+
.setSize(1000)
752+
.setFetchSource(true)
753+
.request();
754+
securityIndex.checkIndexVersionThenExecute(listener::onFailure,
755+
() -> ScrollHelper.fetchAllByEntity(client, request, listener,
756+
(SearchHit hit) -> {
757+
Map<String, Object> source = hit.getSourceAsMap();
758+
String name = (String) source.get("name");
759+
String id = hit.getId();
760+
Long creation = (Long) source.get("creation_time");
761+
Long expiration = (Long) source.get("expiration_time");
762+
Boolean invalidated = (Boolean) source.get("api_key_invalidated");
763+
String username = (String) ((Map<String, Object>) source.get("creator")).get("principal");
764+
String realm = (String) ((Map<String, Object>) source.get("creator")).get("realm");
765+
return new ApiKey(name, id, Instant.ofEpochMilli(creation),
766+
(expiration != null) ? Instant.ofEpochMilli(expiration) : null, invalidated, username, realm);
767+
}));
768+
}
767769
}
768770

769771
private void findApiKeyForApiKeyName(String apiKeyName, boolean filterOutInvalidatedKeys, boolean filterOutExpiredKeys,

x-pack/plugin/src/test/resources/rest-api-spec/test/api_key/10_basic.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ setup:
1212
name: "admin_role"
1313
body: >
1414
{
15-
"cluster": ["all"],
15+
"cluster": ["manage_api_key"],
1616
"indices": [
1717
{
1818
"names": "*",
@@ -166,6 +166,8 @@ teardown:
166166
- set: { name: api_key_name }
167167

168168
- do:
169+
headers:
170+
Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
169171
security.get_api_key:
170172
id: "$api_key_id"
171173
- match: { "api_keys.0.id": "$api_key_id" }
@@ -198,6 +200,8 @@ teardown:
198200
- transform_and_set: { login_creds: "#base64EncodeCredentials(id,api_key)" }
199201

200202
- do:
203+
headers:
204+
Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
201205
security.invalidate_api_key:
202206
body: >
203207
{

0 commit comments

Comments
 (0)