Skip to content

Commit 156a5bd

Browse files
committed
[kbn/scout] move samlAuth call to create ES security indexes after servers start (#247630)
Originally we added this code as part of `preCreateSecurityIndexesFixture` called in `globalSetupHook`. It automatically enforces `global.setup.ts` creation even if the hook has no tests-specific logic: file existence triggers the hook call. This PR moves the logic to server start assuming it is only needed for the local cluster and it should simplify Scout/Playwright hook logic and make hook optional as it was originally intended. From CI logs we can see indexes were created before Playwright tests were started: ``` 2025-12-30 15:41:35 UTC | info [o.e.x.s.s.SecurityIndexManager] [scout] security index does not exist, creating [.security-tokens-7] with alias [.security-tokens] in project [default] 2025-12-30 15:41:35 UTC | info [o.e.c.m.MetadataCreateIndexService] [scout] creating index [.security-tokens-7] in project [default], cause [api], templates [], shards [1]/[1] 2025-12-30 15:41:35 UTC | info [o.e.c.r.a.AllocationService] [scout] in project [default] updating number_of_replicas to [0] for indices [.security-tokens-7] 2025-12-30 15:41:35 UTC | info [o.e.c.r.a.AllocationService] [scout] current.health="GREEN" message="Cluster health status changed from [YELLOW] to [GREEN] (reason: [shards started [[.security-tokens-7][0]]])." previous.health="YELLOW" reason="shards started [[.security-tokens-7][0]]" 2025-12-30 15:41:35 UTC | info [o.e.x.s.s.SecurityIndexManager] [scout] security index does not exist, creating [.security-profile-8] with alias [.security-profile] in project [default] 2025-12-30 15:41:35 UTC | info [o.e.c.m.MetadataCreateIndexService] [scout] creating index [.security-profile-8] in project [default], cause [api], templates [], shards [1]/[1] 2025-12-30 15:41:35 UTC | info [o.e.c.r.a.AllocationService] [scout] in project [default] updating number_of_replicas to [0] for indices [.security-profile-8] 2025-12-30 15:41:36 UTC | info [o.e.c.r.a.AllocationService] [scout] current.health="GREEN" message="Cluster health status changed from [YELLOW] to [GREEN] (reason: [shards started [[.security-profile-8][0]]])." previous.health="YELLOW" reason="shards started [[.security-profile-8][0]]" 2025-12-30 15:41:37 UTC | proc [kibana] [2025-12-30T15:41:37.010+00:00][INFO ][plugins.security.authentication] Login attempt with "saml" provider succeeded (requires redirect: true). {"service":{"node":{"roles":["background_tasks","ui"]}}} 2025-12-30 15:41:37 UTC | info starting [playwright] > /<redacted>kibana/node_modules/.bin/playwright test --config=x-pack/platform/plugins/private/discover_enhanced/test/scout/ui/parallel.playwright.config.ts --grep=@ess --project=local ``` (cherry picked from commit f109902)
1 parent 91b82aa commit 156a5bd

5 files changed

Lines changed: 46 additions & 28 deletions

File tree

src/platform/packages/shared/kbn-scout/src/playwright/runner/run_tests.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import type { ToolingLog } from '@kbn/tooling-log';
1616
import { pickLevelFromFlags } from '@kbn/tooling-log';
1717
import { resolve } from 'path';
1818
import { silence } from '../../common';
19-
import { runElasticsearch, runKibanaServer } from '../../servers';
19+
import {
20+
preCreateSecurityIndexesViaSamlAuth,
21+
runElasticsearch,
22+
runKibanaServer,
23+
} from '../../servers';
2024
import { getConfigRootDir, loadServersConfig } from '../../servers/configs';
2125
import { getExtraKbnOpts } from '../../servers/run_kibana_server';
2226
import type { ScoutPlaywrightProjects } from '../types';
@@ -125,6 +129,9 @@ async function runLocalServersAndTests(
125129
// wait for 5 seconds
126130
await silence(log, 5000);
127131

132+
// Pre-create Elasticsearch Security indexes after server startup
133+
await preCreateSecurityIndexesViaSamlAuth(config, log);
134+
128135
await runPlaywrightTest(procs, cmd, cmdArgs, env);
129136
} finally {
130137
try {

src/platform/packages/shared/kbn-scout/src/playwright/test/ui/parallel_run_fixtures.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
} from '../../fixtures/scope/worker';
1818
import type {
1919
ApiServicesFixture,
20-
CoreWorkerFixtures,
2120
EsClient,
2221
KbnClient,
2322
KibanaUrl,
@@ -61,34 +60,9 @@ export interface ScoutParallelWorkerFixtures {
6160
apiServices: ApiServicesFixture;
6261
}
6362

64-
/**
65-
* Pre-creates Elasticsearch Security indexes (.security-tokens, .security-profile)
66-
* during global setup to prevent race conditions when parallel tests perform their first SAML authentication.
67-
*/
68-
const preCreateSecurityIndexesFixture = coreWorkerFixtures.extend<
69-
{},
70-
{ samlAuth: CoreWorkerFixtures['samlAuth']; preCreateSecurityIndexes: void }
71-
>({
72-
preCreateSecurityIndexes: [
73-
async (
74-
{
75-
samlAuth,
76-
log,
77-
}: { samlAuth: CoreWorkerFixtures['samlAuth']; log: CoreWorkerFixtures['log'] },
78-
use: (arg: void) => Promise<void>
79-
) => {
80-
log.debug('Running SAML authentication to pre-create Elasticsearch .security indexes');
81-
await samlAuth.session.getInteractiveUserSessionCookieWithRoleScope('admin');
82-
await use();
83-
},
84-
{ scope: 'worker', auto: true },
85-
],
86-
});
87-
8863
export const globalSetupFixtures = mergeTests(
8964
coreWorkerFixtures,
9065
esArchiverFixture,
9166
synthtraceFixture,
92-
apiServicesFixture,
93-
preCreateSecurityIndexesFixture
67+
apiServicesFixture
9468
);

src/platform/packages/shared/kbn-scout/src/servers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export { parseServerFlags, SERVER_FLAG_OPTIONS } from './flags';
1111
export { startServers } from './start_servers';
1212
export { runKibanaServer } from './run_kibana_server';
1313
export { runElasticsearch } from './run_elasticsearch';
14+
export { preCreateSecurityIndexesViaSamlAuth } from './pre_create_security_indexes';
1415

1516
export type { StartServerOptions } from './flags';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
import type { ToolingLog } from '@kbn/tooling-log';
11+
import { createSamlSessionManager, ScoutLogger } from '../common';
12+
import type { Config } from './configs';
13+
14+
/**
15+
* Pre-creates Elasticsearch Security indexes (.security-tokens, .security-profile)
16+
* by performing SAML authentication. This prevents race conditions when parallel tests
17+
* perform their first SAML authentication, as the security indexes will already exist.
18+
*
19+
* @param config - The server configuration containing Scout test config
20+
* @param log - Logger instance for logging operations
21+
*/
22+
export async function preCreateSecurityIndexesViaSamlAuth(
23+
config: Config,
24+
log: ToolingLog
25+
): Promise<void> {
26+
const session = createSamlSessionManager(
27+
config.getScoutTestConfig(),
28+
new ScoutLogger('pre-create-security-indexes', 'info')
29+
);
30+
await session.getInteractiveUserSessionCookieWithRoleScope('admin');
31+
log.debug('Successfully pre-created Elasticsearch Security indexes');
32+
}

src/platform/packages/shared/kbn-scout/src/servers/start_servers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { silence } from '../common';
1515
import { getPlaywrightGrepTag } from '../playwright/utils';
1616
import { getConfigRootDir, loadServersConfig } from './configs';
1717
import type { StartServerOptions } from './flags';
18+
import { preCreateSecurityIndexesViaSamlAuth } from './pre_create_security_indexes';
1819
import { runElasticsearch } from './run_elasticsearch';
1920
import { getExtraKbnOpts, runKibanaServer } from './run_kibana_server';
2021

@@ -53,6 +54,9 @@ export async function startServers(log: ToolingLog, options: StartServerOptions)
5354
// success message so that it doesn't get buried
5455
await silence(log, 5000);
5556

57+
// Pre-create Elasticsearch Security indexes after server startup
58+
await preCreateSecurityIndexesViaSamlAuth(config, log);
59+
5660
log.success(
5761
'\n\n' +
5862
dedent`

0 commit comments

Comments
 (0)