Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@
"principal" : {
"type": "keyword"
},
"full_name" : {
"type" : "text"
},
"email" : {
"type" : "text",
"analyzer" : "email"
},
"metadata" : {
"type" : "object",
"dynamic" : false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ XContentBuilder newDocument(SecureString apiKey, String name, Authentication aut
.field("version", version.id)
.startObject("creator")
.field("principal", authentication.getUser().principal())
.field("full_name", authentication.getUser().fullName())
.field("email", authentication.getUser().email())
.field("metadata", authentication.getUser().metadata())
.field("realm", authentication.getSourceRealm().getName())
.field("realm_type", authentication.getSourceRealm().getType())
Expand Down Expand Up @@ -590,8 +592,10 @@ void validateApiKeyExpiration(ApiKeyDoc apiKeyDoc, ApiKeyCredentials credentials
ActionListener<AuthenticationResult> listener) {
if (apiKeyDoc.expirationTime == -1 || Instant.ofEpochMilli(apiKeyDoc.expirationTime).isAfter(clock.instant())) {
final String principal = Objects.requireNonNull((String) apiKeyDoc.creator.get("principal"));
final String fullName = (String) apiKeyDoc.creator.get("full_name");
final String email = (String) apiKeyDoc.creator.get("email");
Map<String, Object> metadata = (Map<String, Object>) apiKeyDoc.creator.get("metadata");
final User apiKeyUser = new User(principal, Strings.EMPTY_ARRAY, null, null, metadata, true);
final User apiKeyUser = new User(principal, Strings.EMPTY_ARRAY, fullName, email, metadata, true);
final Map<String, Object> authResultMetadata = new HashMap<>();
authResultMetadata.put(API_KEY_CREATOR_REALM_NAME, apiKeyDoc.creator.get("realm"));
authResultMetadata.put(API_KEY_CREATOR_REALM_TYPE, apiKeyDoc.creator.get("realm_type"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,20 @@ public void testAuthenticateWithApiKey() throws Exception {

final User user;
if (randomBoolean()) {
user = new User("hulk", new String[] { "superuser" }, new User("authenticated_user", new String[] { "other" }));
user = new User(
new User("hulk", new String[]{"superuser"}, "Bruce Banner", "[email protected]", Map.of(), true),
new User("authenticated_user", new String[]{"other"}));
} else {
user = new User("hulk", new String[] { "superuser" });
user = new User("hulk", new String[]{"superuser"}, "Bruce Banner", "[email protected]", Map.of(), true);
}
mockKeyDocument(service, id, key, user);

final AuthenticationResult auth = tryAuthenticate(service, id, key);
assertThat(auth.getStatus(), is(AuthenticationResult.Status.SUCCESS));
assertThat(auth.getUser(), notNullValue());
assertThat(auth.getUser().principal(), is("hulk"));
assertThat(auth.getUser().fullName(), is("Bruce Banner"));
assertThat(auth.getUser().email(), is("[email protected]"));
assertThat(auth.getMetadata().get(ApiKeyService.API_KEY_CREATOR_REALM_NAME), is("realm1"));
assertThat(auth.getMetadata().get(ApiKeyService.API_KEY_CREATOR_REALM_TYPE), is("native"));
assertThat(auth.getMetadata().get(ApiKeyService.API_KEY_ID_KEY), is(id));
Expand Down Expand Up @@ -377,6 +381,8 @@ public void testValidateApiKey() throws Exception {
assertNotNull(result);
assertTrue(result.isAuthenticated());
assertThat(result.getUser().principal(), is("test_user"));
assertThat(result.getUser().fullName(), is("test user"));
assertThat(result.getUser().email(), is("[email protected]"));
assertThat(result.getUser().roles(), is(emptyArray()));
assertThat(result.getUser().metadata(), is(Collections.emptyMap()));
assertThat(result.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY), equalTo(apiKeyDoc.roleDescriptorsBytes));
Expand All @@ -391,6 +397,8 @@ public void testValidateApiKey() throws Exception {
assertNotNull(result);
assertTrue(result.isAuthenticated());
assertThat(result.getUser().principal(), is("test_user"));
assertThat(result.getUser().fullName(), is("test user"));
assertThat(result.getUser().email(), is("[email protected]"));
assertThat(result.getUser().roles(), is(emptyArray()));
assertThat(result.getUser().metadata(), is(Collections.emptyMap()));
assertThat(result.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY), equalTo(apiKeyDoc.roleDescriptorsBytes));
Expand Down Expand Up @@ -923,6 +931,8 @@ private Map<String, Object> buildApiKeySourceDoc(char[] hash) {
sourceMap.put("limited_by_role_descriptors", Collections.singletonMap("limited role", Collections.singletonMap("cluster", "all")));
Map<String, Object> creatorMap = new HashMap<>();
creatorMap.put("principal", "test_user");
creatorMap.put("full_name", "test user");
creatorMap.put("email", "[email protected]");
creatorMap.put("metadata", Collections.emptyMap());
sourceMap.put("creator", creatorMap);
sourceMap.put("api_key_invalidated", false);
Expand Down Expand Up @@ -953,7 +963,14 @@ private ApiKeyDoc buildApiKeyDoc(char[] hash, long expirationTime, boolean inval
0,
new BytesArray("{\"a role\": {\"cluster\": [\"all\"]}}"),
new BytesArray("{\"limited role\": {\"cluster\": [\"all\"]}}"),
Map.of("principal", "test_user", "realm", "realm1", "realm_type", "realm_type1", "metadata", Map.of())
Map.of(
"principal", "test_user",
"full_name", "test user",
"email", "[email protected]",
"realm", "realm1",
"realm_type", "realm_type1",
"metadata", Map.of()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,8 @@ public void testApiKeyAuth() {
source.put("version", 0);
Map<String, Object> creatorMap = new HashMap<>();
creatorMap.put("principal", "johndoe");
creatorMap.put("full_name", "john doe");
creatorMap.put("email", "[email protected]");
creatorMap.put("metadata", Collections.emptyMap());
creatorMap.put("realm", "auth realm");
source.put("creator", creatorMap);
Expand All @@ -1438,6 +1440,8 @@ public void testApiKeyAuth() {
threadContext.putHeader("Authorization", headerValue);
final Authentication authentication = authenticateBlocking("_action", transportRequest, null);
assertThat(authentication.getUser().principal(), is("johndoe"));
assertThat(authentication.getUser().fullName(), is("john doe"));
assertThat(authentication.getUser().email(), is("[email protected]"));
assertThat(authentication.getAuthenticationType(), is(AuthenticationType.API_KEY));
}
}
Expand Down