Skip to content

Commit 66f0e17

Browse files
authored
Remove synthetic role names of API keys as they confuse users (#56039)
* Remove synthetic role names of API keys as they confuse users The synthetic role names of API key add confusion to users. This happens to API responses as well as audit logs. This PR removes them for clarity. * Fix typo
1 parent ce1aaec commit 66f0e17

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,7 @@ private void validateApiKeyExpiration(Map<String, Object> source, ApiKeyCredenti
537537
final Map<String, Object> metadata = (Map<String, Object>) creator.get("metadata");
538538
final Map<String, Object> roleDescriptors = (Map<String, Object>) source.get("role_descriptors");
539539
final Map<String, Object> limitedByRoleDescriptors = (Map<String, Object>) source.get("limited_by_role_descriptors");
540-
final String[] roleNames = (roleDescriptors != null) ? roleDescriptors.keySet().toArray(Strings.EMPTY_ARRAY)
541-
: limitedByRoleDescriptors.keySet().toArray(Strings.EMPTY_ARRAY);
542-
final User apiKeyUser = new User(principal, roleNames, null, null, metadata, true);
540+
final User apiKeyUser = new User(principal, Strings.EMPTY_ARRAY, null, null, metadata, true);
543541
final Map<String, Object> authResultMetadata = new HashMap<>();
544542
authResultMetadata.put(API_KEY_ROLE_DESCRIPTORS_KEY, roleDescriptors);
545543
authResultMetadata.put(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, limitedByRoleDescriptors);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
import java.util.concurrent.atomic.AtomicInteger;
7575

7676
import static org.elasticsearch.xpack.core.security.authz.store.ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR;
77-
import static org.hamcrest.Matchers.arrayContaining;
7877
import static org.hamcrest.Matchers.containsString;
78+
import static org.hamcrest.Matchers.emptyArray;
7979
import static org.hamcrest.Matchers.equalTo;
8080
import static org.hamcrest.Matchers.hasKey;
8181
import static org.hamcrest.Matchers.instanceOf;
@@ -374,7 +374,7 @@ public void testValidateApiKey() throws Exception {
374374
assertNotNull(result);
375375
assertTrue(result.isAuthenticated());
376376
assertThat(result.getUser().principal(), is("test_user"));
377-
assertThat(result.getUser().roles(), arrayContaining("a role"));
377+
assertThat(result.getUser().roles(), is(emptyArray()));
378378
assertThat(result.getUser().metadata(), is(Collections.emptyMap()));
379379
assertThat(result.getMetadata().get(ApiKeyService.API_KEY_ROLE_DESCRIPTORS_KEY), equalTo(sourceMap.get("role_descriptors")));
380380
assertThat(result.getMetadata().get(ApiKeyService.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY),
@@ -387,7 +387,7 @@ public void testValidateApiKey() throws Exception {
387387
assertNotNull(result);
388388
assertTrue(result.isAuthenticated());
389389
assertThat(result.getUser().principal(), is("test_user"));
390-
assertThat(result.getUser().roles(), arrayContaining("a role"));
390+
assertThat(result.getUser().roles(), is(emptyArray()));
391391
assertThat(result.getUser().metadata(), is(Collections.emptyMap()));
392392
assertThat(result.getMetadata().get(ApiKeyService.API_KEY_ROLE_DESCRIPTORS_KEY), equalTo(sourceMap.get("role_descriptors")));
393393
assertThat(result.getMetadata().get(ApiKeyService.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY),

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ teardown:
119119
xpack.security.authenticate: {}
120120

121121
- match: { username: "api_key_user" }
122-
- match: { roles.0: "role-b" }
123-
- match: { roles.1: "role-a" }
122+
- length: { roles: 0 }
124123
- match: { authentication_realm.name: "_es_api_key" }
125124
- match: { authentication_realm.type: "_es_api_key" }
126125

0 commit comments

Comments
 (0)