Skip to content

Commit a9229ab

Browse files
authored
[Test] Service account tokens should work when TokenService is disabled (#72518)
Add a test to ensure that service account tokens always work with basic license for which oauth2 token service is disabled.
1 parent bf23f76 commit a9229ab

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityInBasicRestTestCase.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
import java.util.List;
1717

18-
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
19-
2018
public abstract class SecurityInBasicRestTestCase extends ESRestTestCase {
2119
private RestHighLevelClient highLevelAdminClient;
2220

x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public void testWithBasicLicense() throws Exception {
3939
assertAuthenticateWithApiKey(keyAndId, true);
4040

4141
assertFailToGetToken();
42+
// Service account token works independently to oauth2 token service
43+
final String bearerString = createServiceAccountToken();
44+
assertAuthenticateWithServiceAccountToken(bearerString);
45+
4246
assertAddRoleWithDLS(false);
4347
assertAddRoleWithFLS(false);
4448
}
@@ -222,6 +226,23 @@ private void assertAuthenticateWithApiKey(Tuple<String, String> keyAndId, boolea
222226
}
223227
}
224228

229+
private String createServiceAccountToken() throws IOException {
230+
final Request request = new Request("POST", "_security/service/elastic/fleet-server/credential/token/api-token-1");
231+
final Response response = adminClient().performRequest(request);
232+
assertOK(response);
233+
@SuppressWarnings("unchecked")
234+
final Map<String, ?> tokenMap = (Map<String, ?>) responseAsMap(response).get("token");
235+
return String.valueOf(tokenMap.get("value"));
236+
}
237+
238+
private void assertAuthenticateWithServiceAccountToken(String bearerString) throws IOException {
239+
Request request = new Request("GET", "/_security/_authenticate");
240+
request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + bearerString));
241+
final Response response = client().performRequest(request);
242+
assertOK(response);
243+
assertEquals("elastic/fleet-server", responseAsMap(response).get("username"));
244+
}
245+
225246
private void assertAddRoleWithDLS(boolean shouldSucceed) throws IOException {
226247
final Request addRole = new Request("POST", "/_security/role/dlsrole");
227248
addRole.setJsonEntity("{\n" +

0 commit comments

Comments
 (0)