Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -112,9 +112,7 @@ public static void addAuthorizationInfo(final XContentBuilder builder, final Map
private static void addSubjectInfo(XContentBuilder builder, Subject subject) throws IOException {
switch (subject.getType()) {
case USER -> builder.array(User.Fields.ROLES.getPreferredName(), subject.getUser().roles());
case API_KEY -> {
addApiKeyInfo(builder, subject);
}
case API_KEY -> addApiKeyInfo(builder, subject);
case SERVICE_ACCOUNT -> builder.field("service_account", subject.getUser().principal());
case CROSS_CLUSTER_ACCESS -> {
builder.startObject("cross_cluster_access");
Expand All @@ -129,7 +127,16 @@ private static void addSubjectInfo(XContentBuilder builder, Subject subject) thr
builder.endObject();
}
case CLOUD_API_KEY -> {
// TODO Add cloud API key information here
builder.startObject("cloud_api_key");
Map<String, Object> metadata = subject.getUser().metadata();
builder.field("id", subject.getUser().principal());
Object name = metadata.get(AuthenticationField.API_KEY_NAME_KEY);
if (name instanceof String) {
builder.field("name", name);
}
builder.field("internal", metadata.get(AuthenticationField.API_KEY_INTERNAL_KEY));
builder.array(User.Fields.ROLES.getPreferredName(), subject.getUser().roles());
builder.endObject();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_ID_KEY;
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_NAME_KEY;
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.CROSS_CLUSTER_ACCESS_AUTHENTICATION_KEY;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class XContentUtilsTests extends ESTestCase {
Expand Down Expand Up @@ -62,6 +63,13 @@ public void testAddAuthorizationInfoWithApiKey() throws IOException {
assertThat(json, equalTo("{\"authorization\":{\"api_key\":{\"id\":\"" + apiKeyId + "\",\"name\":\"" + apiKeyName + "\"}}}"));
}

public void testAddAuthorizationInfoWithCloudApiKey() throws IOException {
String apiKeyId = randomAlphaOfLength(20);
Authentication authentication = AuthenticationTestHelper.randomCloudApiKeyAuthentication(apiKeyId);
String json = generateJson(Map.of(AuthenticationField.AUTHENTICATION_KEY, authentication.encode()));
assertThat(json, containsString("{\"authorization\":{\"cloud_api_key\":{\"id\":\"" + apiKeyId + "\""));
Copy link
Contributor

Choose a reason for hiding this comment

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

Test only asserts id field. Using randomCloudApiKeyAuthentication(apiKeyId) may not be quite suitable here. Probably it's better to randomize User then call randomCloudApiKeyAuthentication(user). This way you can assert all fields as you control user's principal (API key id), metadata (name and internal) and roles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactored the test and some helper methods to assert on a fully randomized user

}

public void testAddAuthorizationInfoWithServiceAccount() throws IOException {
String account = "elastic/" + randomFrom("kibana", "fleet-server");
User user = new User(account);
Expand Down