Skip to content

Commit 814f301

Browse files
committed
add change and test
1 parent 528bd9c commit 814f301

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ public static void addAuthorizationInfo(final XContentBuilder builder, final Map
112112
private static void addSubjectInfo(XContentBuilder builder, Subject subject) throws IOException {
113113
switch (subject.getType()) {
114114
case USER -> builder.array(User.Fields.ROLES.getPreferredName(), subject.getUser().roles());
115-
case API_KEY -> {
116-
addApiKeyInfo(builder, subject);
117-
}
115+
case API_KEY -> addApiKeyInfo(builder, subject);
118116
case SERVICE_ACCOUNT -> builder.field("service_account", subject.getUser().principal());
119117
case CROSS_CLUSTER_ACCESS -> {
120118
builder.startObject("cross_cluster_access");
@@ -129,7 +127,16 @@ private static void addSubjectInfo(XContentBuilder builder, Subject subject) thr
129127
builder.endObject();
130128
}
131129
case CLOUD_API_KEY -> {
132-
// TODO Add cloud API key information here
130+
builder.startObject("cloud_api_key");
131+
Map<String, Object> metadata = subject.getUser().metadata();
132+
builder.field("id", subject.getUser().principal());
133+
Object name = metadata.get(AuthenticationField.API_KEY_NAME_KEY);
134+
if (name instanceof String) {
135+
builder.field("name", name);
136+
}
137+
builder.field("internal", metadata.get(AuthenticationField.API_KEY_INTERNAL_KEY));
138+
builder.array(User.Fields.ROLES.getPreferredName(), subject.getUser().roles());
139+
builder.endObject();
133140
}
134141
}
135142
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtilsTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_ID_KEY;
2727
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_NAME_KEY;
2828
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.CROSS_CLUSTER_ACCESS_AUTHENTICATION_KEY;
29+
import static org.hamcrest.Matchers.containsString;
2930
import static org.hamcrest.Matchers.equalTo;
3031

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

66+
public void testAddAuthorizationInfoWithCloudApiKey() throws IOException {
67+
String apiKeyId = randomAlphaOfLength(20);
68+
Authentication authentication = AuthenticationTestHelper.randomCloudApiKeyAuthentication(apiKeyId);
69+
String json = generateJson(Map.of(AuthenticationField.AUTHENTICATION_KEY, authentication.encode()));
70+
assertThat(json, containsString("{\"authorization\":{\"cloud_api_key\":{\"id\":\"" + apiKeyId + "\""));
71+
}
72+
6573
public void testAddAuthorizationInfoWithServiceAccount() throws IOException {
6674
String account = "elastic/" + randomFrom("kibana", "fleet-server");
6775
User user = new User(account);

0 commit comments

Comments
 (0)