Skip to content

Commit 56d9afe

Browse files
committed
support built-in roles in apiTest
1 parent 453a248 commit 56d9afe

5 files changed

Lines changed: 40 additions & 8 deletions

File tree

src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/api_key.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ export interface RequestAuthFixture {
6464
* Elasticsearch projects, `editor` for all other deployments and project types.
6565
*/
6666
getApiKeyForPrivilegedUser: () => Promise<RoleApiCredentials>;
67+
/**
68+
* Fetches the descriptor of the named ES role and creates an API key scoped
69+
* to those privileges. Works for built-in ES roles (e.g. `'kibana_admin'`,
70+
* `'superuser'`) without requiring an entry in `roles.yml`.
71+
*
72+
* The descriptor is embedded inline in the API key — no separate role is
73+
* created in Elasticsearch.
74+
*
75+
* @example
76+
* const { apiKeyHeader } = await requestAuth.getApiKeyForBuiltinRole('kibana_admin');
77+
*/
78+
getApiKeyForBuiltinRole: (roleName: string) => Promise<RoleApiCredentials>;
6779
}
6880

6981
export const requestAuthFixture = coreWorkerFixtures.extend<
@@ -204,12 +216,20 @@ export const requestAuthFixture = coreWorkerFixtures.extend<
204216
);
205217
};
206218

219+
const getApiKeyForBuiltinRole = async (roleName: string): Promise<RoleApiCredentials> => {
220+
const descriptor = await samlAuth.setBuiltinRole(roleName);
221+
return createApiKeyWithAdminCredentials(samlAuth.customRoleName, {
222+
[samlAuth.customRoleName]: descriptor,
223+
});
224+
};
225+
207226
await use({
208227
getApiKey,
209228
getApiKeyForCustomRole,
210229
getApiKeyForAdmin,
211230
getApiKeyForViewer,
212231
getApiKeyForPrivilegedUser,
232+
getApiKeyForBuiltinRole,
213233
});
214234

215235
// Invalidate all API Keys after tests

src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/core_fixtures.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface RoleSessionCredentials {
3838
cookieHeader: CookieHeader;
3939
}
4040

41-
export interface BaseCoreWorkerFixtures {
41+
export interface BaseWorkerFixtures {
4242
log: ScoutLogger;
4343
config: ScoutTestConfig;
4444
kbnUrl: KibanaUrl;
@@ -69,7 +69,7 @@ export interface BaseCoreWorkerFixtures {
6969
* Note: `samlAuth` is added by the `samlAuthFixture` in `./saml_auth/index.ts`, which
7070
* extends this base. The combined fixture (with samlAuth) is what `worker/index.ts` exports.
7171
*/
72-
export const coreWorkerFixtures = base.extend<{}, BaseCoreWorkerFixtures>({
72+
export const coreWorkerFixtures = base.extend<{}, BaseWorkerFixtures>({
7373
// Provides a scoped logger instance for each worker to use in fixtures and tests.
7474
// For parallel workers logger context is matching worker index+1, e.g. '[scout-worker-1]', '[scout-worker-2]', etc.
7575
log: [

src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/saml_auth/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import type { SamlSessionManager } from '@kbn/test-saml-auth';
1111
import { createSamlSessionManager } from '../../../../../common/services';
1212
import type { ElasticsearchRoleDescriptor, KibanaRole } from '../../../../../common/services';
13-
import type { RoleSessionCredentials, BaseCoreWorkerFixtures } from '../core_fixtures';
13+
import type { RoleSessionCredentials, BaseWorkerFixtures } from '../core_fixtures';
1414
import { coreWorkerFixtures } from '../core_fixtures';
1515
import { SamlAuthManager } from './saml_auth_manager';
1616

@@ -65,7 +65,7 @@ export interface SamlAuth {
6565
*
6666
* @param roleName - The name of the role to look up in Elasticsearch.
6767
*/
68-
setBuiltinRole(roleName: string): Promise<void>;
68+
setBuiltinRole(roleName: string): Promise<ElasticsearchRoleDescriptor>;
6969
/**
7070
* Generates a SAML session cookie for an interactive user with the specified role.
7171
*
@@ -104,7 +104,7 @@ export interface SamlAuth {
104104
* Full worker fixture set: base fixtures + samlAuth.
105105
* Use this type when you need to reference the complete worker fixture surface.
106106
*/
107-
export interface CoreWorkerFixtures extends BaseCoreWorkerFixtures {
107+
export interface CoreWorkerFixtures extends BaseWorkerFixtures {
108108
samlAuth: SamlAuth;
109109
}
110110

src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/saml_auth/saml_auth_manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class SamlAuthManager {
6767
*
6868
* @param roleName - The name of the role to look up in Elasticsearch.
6969
*/
70-
async setBuiltinRole(roleName: string): Promise<void> {
70+
async setBuiltinRole(roleName: string): Promise<ElasticsearchRoleDescriptor> {
7171
const response = await this.esClient.security.getRole({ name: roleName });
7272
const roleData = response[roleName];
7373
if (!roleData) {
@@ -76,6 +76,7 @@ export class SamlAuthManager {
7676
// Strip non-privilege metadata before delegating to the generic custom-role path
7777
const { metadata: _metadata, transient_metadata: _transient, ...descriptor } = roleData;
7878
await this.setCustomRole(descriptor as ElasticsearchRoleDescriptor);
79+
return descriptor as ElasticsearchRoleDescriptor;
7980
}
8081

8182
async asInteractiveUser(

src/platform/packages/shared/kbn-scout/test/scout/api/parallel_tests/auth/saml_login.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ apiTest.describe(
2626
});
2727

2828
apiTest(
29-
`should create a session for a built-in ES role via setBuiltinRole`,
29+
`setBuiltinRole should provision the custom role slot and return the descriptor`,
3030
async ({ samlAuth }) => {
31-
await samlAuth.setBuiltinRole('kibana_admin');
31+
const descriptor = await samlAuth.setBuiltinRole('kibana_admin');
32+
expect(descriptor).toBeDefined();
3233
const credentials = await samlAuth.asInteractiveUser(samlAuth.customRoleName);
3334
expect(credentials.cookieValue).toBeDefined();
3435
}
3536
);
37+
38+
apiTest(
39+
`getApiKeyForBuiltinRole should create an API key scoped to a built-in ES role`,
40+
async ({ requestAuth }) => {
41+
const { apiKey, apiKeyHeader } = await requestAuth.getApiKeyForBuiltinRole('kibana_admin');
42+
expect(apiKey.id).toBeDefined();
43+
expect(apiKey.name).toBeDefined();
44+
expect(apiKeyHeader.Authorization).toMatch(/^ApiKey /);
45+
}
46+
);
3647
}
3748
);

0 commit comments

Comments
 (0)