Skip to content

Commit 336132b

Browse files
committed
improve test
1 parent 814f301 commit 336132b

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ public static User randomUser() {
9292
);
9393
}
9494

95+
public static User randomCloudApiKeyUser() {
96+
return randomCloudApiKeyUser(null);
97+
}
98+
99+
public static User randomCloudApiKeyUser(String principal) {
100+
final Map<String, Object> metadata = ESTestCase.randomBoolean()
101+
? null
102+
: Map.ofEntries(
103+
Map.entry(AuthenticationField.API_KEY_NAME_KEY, ESTestCase.randomAlphanumericOfLength(64)),
104+
Map.entry(AuthenticationField.API_KEY_INTERNAL_KEY, ESTestCase.randomBoolean())
105+
);
106+
return new User(
107+
principal == null ? ESTestCase.randomAlphanumericOfLength(64) : principal,
108+
ESTestCase.randomArray(1, 3, String[]::new, () -> "role_" + ESTestCase.randomAlphaOfLengthBetween(3, 8)),
109+
null,
110+
null,
111+
metadata,
112+
true
113+
);
114+
}
115+
95116
public static InternalUser randomInternalUser() {
96117
return ESTestCase.randomFrom(InternalUsers.get());
97118
}
@@ -260,27 +281,14 @@ public static Authentication randomCloudApiKeyAuthentication(User user, String a
260281
if (apiKeyId == null) {
261282
apiKeyId = user != null ? user.principal() : ESTestCase.randomAlphanumericOfLength(64);
262283
}
263-
final Map<String, Object> metadata = ESTestCase.randomBoolean()
264-
? null
265-
: Map.ofEntries(
266-
Map.entry(AuthenticationField.API_KEY_NAME_KEY, ESTestCase.randomAlphanumericOfLength(64)),
267-
Map.entry(AuthenticationField.API_KEY_INTERNAL_KEY, ESTestCase.randomBoolean())
268-
);
269284
if (user == null) {
270-
user = new User(
271-
apiKeyId,
272-
ESTestCase.randomArray(1, 3, String[]::new, () -> "role_" + ESTestCase.randomAlphaOfLengthBetween(3, 8)),
273-
null,
274-
null,
275-
metadata,
276-
true
277-
);
285+
user = randomCloudApiKeyUser(apiKeyId);
278286
}
279287

280288
assert user.principal().equals(apiKeyId) : "user principal must match cloud API key ID";
281289

282290
return Authentication.newCloudApiKeyAuthentication(
283-
AuthenticationResult.success(user, metadata),
291+
AuthenticationResult.success(user, user.metadata()),
284292
"node_" + ESTestCase.randomAlphaOfLengthBetween(3, 8)
285293
);
286294

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.stream.Collectors;
2525

2626
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_ID_KEY;
27+
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_INTERNAL_KEY;
2728
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_NAME_KEY;
2829
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.CROSS_CLUSTER_ACCESS_AUTHENTICATION_KEY;
2930
import static org.hamcrest.Matchers.containsString;
@@ -64,10 +65,18 @@ public void testAddAuthorizationInfoWithApiKey() throws IOException {
6465
}
6566

6667
public void testAddAuthorizationInfoWithCloudApiKey() throws IOException {
67-
String apiKeyId = randomAlphaOfLength(20);
68-
Authentication authentication = AuthenticationTestHelper.randomCloudApiKeyAuthentication(apiKeyId);
68+
User user = AuthenticationTestHelper.randomCloudApiKeyUser();
69+
Authentication authentication = AuthenticationTestHelper.randomCloudApiKeyAuthentication(user);
6970
String json = generateJson(Map.of(AuthenticationField.AUTHENTICATION_KEY, authentication.encode()));
70-
assertThat(json, containsString("{\"authorization\":{\"cloud_api_key\":{\"id\":\"" + apiKeyId + "\""));
71+
assertThat(json, containsString("{\"authorization\":{\"cloud_api_key\":{\"id\":\"" + user.principal()));
72+
assertThat(json, containsString("\"internal\":" + user.metadata().getOrDefault(API_KEY_INTERNAL_KEY, null)));
73+
if (user.metadata().containsKey(API_KEY_NAME_KEY)) {
74+
assertThat(json, containsString("\"name\":\"" + user.metadata().getOrDefault(API_KEY_NAME_KEY, null) + "\""));
75+
}
76+
for (String role : user.roles()) {
77+
assertThat(json, containsString(role));
78+
}
79+
7180
}
7281

7382
public void testAddAuthorizationInfoWithServiceAccount() throws IOException {

0 commit comments

Comments
 (0)