Skip to content

Commit 5e74b4c

Browse files
authored
[Scout] Remove custom role deletion the requestAuth fixture (so it is deleted just once in samlAuth) (#244462)
This PR removes the custom role deletion logic from the `requestAuth` fixture (used by Scout API tests to request an API key). Custom roles will still be deleted in the `samlAuth` fixture. We were attempting to delete the custom role in two places instead of one. ### Context Currently, a bug in the Scout API tests causes the custom role to be deleted twice: once [in the `requestAuth` fixture](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/api_key.ts#L169-L178) and again [in the `samlAuth` fixture](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/core_fixtures.ts#L186-L198). We should ensure the custom role is deleted only **once**, ideally within the `samlAuth` fixture, to align with the behavior in UI tests. This issue specifically affects Scout API tests that use the `requestAuth` fixture. ### How to test First, enable `debug` Scout logs in you local environment: ``` export SCOUT_LOG_LEVEL=debug ``` Start the servers: ``` node scripts/scout.js start-server --stateful ``` In a separate terminal run some Scout API tests: ``` npx playwright test x-pack/platform/plugins/private/painless_lab/test/scout/api/tests/execute_api_custom_cluster_privileges.spec.ts --config x-pack/platform/plugins/private/painless_lab/test/scout/api/playwright.config.ts --project local ``` Notice the custom role is deleted just once: ``` Running 4 tests using 1 worker [local] › x-pack/platform/plugins/private/painless_lab/test/scout/api/tests/execute_api_custom_cluster_privileges.spec.ts:15:12 › POST api/painless_lab/execute with specific cluster privileges › should execute a valid painless script using cluster:admin/scripts/painless/execute credentials @ess info [scout-worker] Created API key for custom_role_worker_1 role: myTestApiKey-0-custom_role_worker_1-worker-1 [local] › x-pack/platform/plugins/private/painless_lab/test/scout/api/tests/execute_api_custom_cluster_privileges.spec.ts:38:12 › POST api/painless_lab/execute with specific cluster privileges › should execute a valid painless script using cluster:admin credentials @ess info [scout-worker] Created API key for custom_role_worker_1 role: myTestApiKey-1-custom_role_worker_1-worker-1 [local] › x-pack/platform/plugins/private/painless_lab/test/scout/api/tests/execute_api_custom_cluster_privileges.spec.ts:60:12 › POST api/painless_lab/execute with specific cluster privileges › should return an unauthorized status code when using monitor cluster credentials @ess info [scout-worker] Created API key for custom_role_worker_1 role: myTestApiKey-2-custom_role_worker_1-worker-1 [local] › x-pack/platform/plugins/private/painless_lab/test/scout/api/tests/execute_api_custom_cluster_privileges.spec.ts:79:12 › POST api/painless_lab/execute with specific cluster privileges › should return an unauthorized status code when using both cluster and Kibana privileges @ess info [scout-worker] Created API key for custom_role_worker_1 role: myTestApiKey-3-custom_role_worker_1-worker-1 info [scout-worker] Invalidated API key: myTestApiKey-0-custom_role_worker_1-worker-1 info [scout-worker] Invalidated API key: myTestApiKey-1-custom_role_worker_1-worker-1 info [scout-worker] Invalidated API key: myTestApiKey-2-custom_role_worker_1-worker-1 info [scout-worker] Invalidated API key: myTestApiKey-3-custom_role_worker_1-worker-1 info [scout-worker] Deleting custom role custom_role_worker_1 info [scout-worker] Custom role 'custom_role_worker_1' deleted 4 passed (9.5s) ``` Without this change, you would see an error (because we were attempting to delete the custom role twice): ``` info [scout-worker] Deleted custom_role_worker_1 custom role ERROR [scout-worker] Failed to delete custom role 'custom_role_worker_1' during worker cleanup: {"found":false} 4 passed (11.0s) ```
1 parent a7f8bb6 commit 5e74b4c

File tree

1 file changed

+2
-15
lines changed
  • src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker

1 file changed

+2
-15
lines changed

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
import type { Client } from '@elastic/elasticsearch';
1110
import { coreWorkerFixtures } from './core_fixtures';
1211
import type { ApiClientFixture } from './api_client';
1312
import type { DefaultRolesFixture } from './default_roles';
@@ -43,13 +42,11 @@ export const requestAuthFixture = coreWorkerFixtures.extend<
4342
requestAuth: RequestAuthFixture;
4443
defaultRoles: DefaultRolesFixture;
4544
apiClient: ApiClientFixture;
46-
esClient: Client;
4745
}
4846
>({
4947
requestAuth: [
50-
async ({ log, samlAuth, defaultRoles, apiClient, esClient }, use, workerInfo) => {
48+
async ({ log, samlAuth, defaultRoles, apiClient }, use, workerInfo) => {
5149
const generatedApiKeys: ApiKey[] = [];
52-
let isCustomRoleCreated = false;
5350

5451
const createApiKeyPayload = (
5552
apiKeyName: string,
@@ -148,8 +145,6 @@ export const requestAuthFixture = coreWorkerFixtures.extend<
148145
const getApiKeyForCustomRole = async (
149146
roleDescriptor: KibanaRole | ElasticsearchRoleDescriptor
150147
): Promise<RoleApiCredentials> => {
151-
isCustomRoleCreated = true;
152-
153148
await samlAuth.setCustomRole(roleDescriptor);
154149

155150
const result = await createApiKeyWithAdminCredentials(samlAuth.customRoleName, {
@@ -166,15 +161,7 @@ export const requestAuthFixture = coreWorkerFixtures.extend<
166161
return invalidateApiKeys(generatedApiKeys);
167162
});
168163

169-
if (isCustomRoleCreated) {
170-
log.debug(`Deleting custom role with name ${samlAuth.customRoleName}`);
171-
try {
172-
await esClient.security.deleteRole({ name: samlAuth.customRoleName });
173-
log.info(`Deleted ${samlAuth.customRoleName} custom role`);
174-
} catch (error: any) {
175-
log.error(`Failed to delete custom role ${samlAuth.customRoleName}: ${error.message}`);
176-
}
177-
}
164+
// Note: the custom role will be deleted in the samlAuth fixture cleanup
178165
},
179166
{ scope: 'worker' },
180167
],

0 commit comments

Comments
 (0)