From 51ab0674e146767cbc9816050d89fec8306bb3da Mon Sep 17 00:00:00 2001 From: Alexandra Doak Date: Wed, 29 Mar 2023 09:54:33 -0700 Subject: [PATCH] Fixing more functional tests --- .../alerting/public/lib/common_transformations.ts | 2 ++ x-pack/plugins/alerting/server/routes/clone_rule.ts | 2 ++ x-pack/plugins/alerting/server/routes/create_rule.ts | 2 ++ x-pack/plugins/alerting/server/routes/get_rule.ts | 2 ++ .../plugins/alerting/server/routes/lib/rewrite_rule.ts | 2 ++ x-pack/plugins/alerting/server/routes/resolve_rule.ts | 2 ++ x-pack/plugins/alerting/server/routes/update_rule.ts | 2 ++ .../common/api_key_as_alert_attributes.test.ts | 9 ++++++--- .../rules_client/common/api_key_as_alert_attributes.ts | 3 ++- .../server/rules_client/methods/update_api_key.ts | 2 +- .../plugins/alerting/server/rules_client_factory.test.ts | 4 ++++ .../security_and_spaces/group1/tests/alerting/get.ts | 1 + .../security_and_spaces/group2/tests/alerting/update.ts | 5 +++++ 13 files changed, 33 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/alerting/public/lib/common_transformations.ts b/x-pack/plugins/alerting/public/lib/common_transformations.ts index b123962bb4ea7..e873d56b8649f 100644 --- a/x-pack/plugins/alerting/public/lib/common_transformations.ts +++ b/x-pack/plugins/alerting/public/lib/common_transformations.ts @@ -109,6 +109,7 @@ export function transformRule(input: ApiRule): Rule { updated_at: updatedAt, api_key: apiKey, api_key_owner: apiKeyOwner, + api_key_created_by_user: apiKeyCreatedByUser, notify_when: notifyWhen, mute_all: muteAll, muted_alert_ids: mutedInstanceIds, @@ -130,6 +131,7 @@ export function transformRule(input: ApiRule): Rule { updatedAt: new Date(updatedAt), apiKey, apiKeyOwner, + apiKeyCreatedByUser, notifyWhen, muteAll, mutedInstanceIds, diff --git a/x-pack/plugins/alerting/server/routes/clone_rule.ts b/x-pack/plugins/alerting/server/routes/clone_rule.ts index 11819953cf9d6..fa775e626d903 100644 --- a/x-pack/plugins/alerting/server/routes/clone_rule.ts +++ b/x-pack/plugins/alerting/server/routes/clone_rule.ts @@ -36,6 +36,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ createdAt, updatedAt, apiKeyOwner, + apiKeyCreatedByUser, notifyWhen, muteAll, mutedInstanceIds, @@ -75,6 +76,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ : {}), ...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}), ...(nextRun ? { next_run: nextRun } : {}), + ...(apiKeyCreatedByUser !== undefined ? { api_key_created_by_user: apiKeyCreatedByUser } : {}), }); export const cloneRuleRoute = ( diff --git a/x-pack/plugins/alerting/server/routes/create_rule.ts b/x-pack/plugins/alerting/server/routes/create_rule.ts index 04c205e936da2..7a70ad4e94e3c 100644 --- a/x-pack/plugins/alerting/server/routes/create_rule.ts +++ b/x-pack/plugins/alerting/server/routes/create_rule.ts @@ -64,6 +64,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ createdAt, updatedAt, apiKeyOwner, + apiKeyCreatedByUser, notifyWhen, muteAll, mutedInstanceIds, @@ -93,6 +94,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ actions: rewriteActionsRes(actions), ...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}), ...(nextRun ? { next_run: nextRun } : {}), + ...(apiKeyCreatedByUser !== undefined ? { api_key_created_by_user: apiKeyCreatedByUser } : {}), }); export const createRuleRoute = ({ router, licenseState, usageCounter }: RouteOptions) => { diff --git a/x-pack/plugins/alerting/server/routes/get_rule.ts b/x-pack/plugins/alerting/server/routes/get_rule.ts index 06bb7716f7f66..10d91c80b07a7 100644 --- a/x-pack/plugins/alerting/server/routes/get_rule.ts +++ b/x-pack/plugins/alerting/server/routes/get_rule.ts @@ -29,6 +29,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ createdAt, updatedAt, apiKeyOwner, + apiKeyCreatedByUser, notifyWhen, muteAll, mutedInstanceIds, @@ -77,6 +78,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ ...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}), ...(nextRun ? { next_run: nextRun } : {}), ...(viewInAppRelativeUrl ? { view_in_app_relative_url: viewInAppRelativeUrl } : {}), + ...(apiKeyCreatedByUser !== undefined ? { api_key_created_by_user: apiKeyCreatedByUser } : {}), }); interface BuildGetRulesRouteParams { diff --git a/x-pack/plugins/alerting/server/routes/lib/rewrite_rule.ts b/x-pack/plugins/alerting/server/routes/lib/rewrite_rule.ts index eef33bb793d4a..a2075418d8fc5 100644 --- a/x-pack/plugins/alerting/server/routes/lib/rewrite_rule.ts +++ b/x-pack/plugins/alerting/server/routes/lib/rewrite_rule.ts @@ -25,6 +25,7 @@ export const rewriteRule = ({ createdAt, updatedAt, apiKeyOwner, + apiKeyCreatedByUser, notifyWhen, muteAll, mutedInstanceIds, @@ -75,4 +76,5 @@ export const rewriteRule = ({ })), ...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}), ...(nextRun ? { next_run: nextRun } : {}), + ...(apiKeyCreatedByUser !== undefined ? { api_key_created_by_user: apiKeyCreatedByUser } : {}), }); diff --git a/x-pack/plugins/alerting/server/routes/resolve_rule.ts b/x-pack/plugins/alerting/server/routes/resolve_rule.ts index 27128850d9e82..e42dd6795f14a 100644 --- a/x-pack/plugins/alerting/server/routes/resolve_rule.ts +++ b/x-pack/plugins/alerting/server/routes/resolve_rule.ts @@ -33,6 +33,7 @@ const rewriteBodyRes: RewriteResponseCase> createdAt, updatedAt, apiKeyOwner, + apiKeyCreatedByUser, notifyWhen, muteAll, mutedInstanceIds, @@ -62,6 +63,7 @@ const rewriteBodyRes: RewriteResponseCase> actions: rewriteActionsRes(actions), ...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}), ...(nextRun ? { next_run: nextRun } : {}), + ...(apiKeyCreatedByUser !== undefined ? { api_key_created_by_user: apiKeyCreatedByUser } : {}), }); export const resolveRuleRoute = ( diff --git a/x-pack/plugins/alerting/server/routes/update_rule.ts b/x-pack/plugins/alerting/server/routes/update_rule.ts index 006a83f0e30ce..23926c24d1742 100644 --- a/x-pack/plugins/alerting/server/routes/update_rule.ts +++ b/x-pack/plugins/alerting/server/routes/update_rule.ts @@ -64,6 +64,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ createdAt, updatedAt, apiKeyOwner, + apiKeyCreatedByUser, notifyWhen, muteAll, mutedInstanceIds, @@ -103,6 +104,7 @@ const rewriteBodyRes: RewriteResponseCase> = ({ : {}), ...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}), ...(nextRun ? { next_run: nextRun } : {}), + ...(apiKeyCreatedByUser !== undefined ? { api_key_created_by_user: apiKeyCreatedByUser } : {}), }); export const updateRuleRoute = ( diff --git a/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.test.ts b/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.test.ts index 593f191334021..495dab4f81487 100644 --- a/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.test.ts +++ b/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.test.ts @@ -19,12 +19,13 @@ describe('apiKeyAsAlertAttributes', () => { api_key: 'abc', }, }, - 'test' + 'test', + false ) ).toEqual({ apiKey: 'MTIzOmFiYw==', apiKeyOwner: 'test', - apiKeyCreatedByUser: undefined, + apiKeyCreatedByUser: false, }); }); @@ -34,11 +35,13 @@ describe('apiKeyAsAlertAttributes', () => { { apiKeysEnabled: false, }, - 'test' + 'test', + false ) ).toEqual({ apiKey: null, apiKeyOwner: null, + apiKeyCreatedByUser: null, }); }); diff --git a/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.ts b/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.ts index 6d6eef902dcdf..841ffdd1703a7 100644 --- a/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.ts +++ b/x-pack/plugins/alerting/server/rules_client/common/api_key_as_alert_attributes.ts @@ -11,7 +11,7 @@ import { CreateAPIKeyResult } from '../types'; export function apiKeyAsAlertAttributes( apiKey: CreateAPIKeyResult | null, username: string | null, - createdByUser?: boolean + createdByUser: boolean ): Pick { return apiKey && apiKey.apiKeysEnabled ? { @@ -22,5 +22,6 @@ export function apiKeyAsAlertAttributes( : { apiKeyOwner: null, apiKey: null, + apiKeyCreatedByUser: null, }; } diff --git a/x-pack/plugins/alerting/server/rules_client/methods/update_api_key.ts b/x-pack/plugins/alerting/server/rules_client/methods/update_api_key.ts index d423a5a1948ad..fce6edb925078 100644 --- a/x-pack/plugins/alerting/server/rules_client/methods/update_api_key.ts +++ b/x-pack/plugins/alerting/server/rules_client/methods/update_api_key.ts @@ -88,7 +88,7 @@ async function updateApiKeyWithOCC(context: RulesClientContext, { id }: { id: st const updateAttributes = updateMeta(context, { ...attributes, - ...apiKeyAsAlertAttributes(createdAPIKey, username), + ...apiKeyAsAlertAttributes(createdAPIKey, username, false), updatedAt: new Date().toISOString(), updatedBy: username, }); diff --git a/x-pack/plugins/alerting/server/rules_client_factory.test.ts b/x-pack/plugins/alerting/server/rules_client_factory.test.ts index 14363fcabf440..18364256ce4a9 100644 --- a/x-pack/plugins/alerting/server/rules_client_factory.test.ts +++ b/x-pack/plugins/alerting/server/rules_client_factory.test.ts @@ -122,6 +122,8 @@ test('creates a rules client with proper constructor arguments when security is encryptedSavedObjectsClient: rulesClientFactoryParams.encryptedSavedObjectsClient, kibanaVersion: '7.10.0', minimumScheduleInterval: { value: '1m', enforce: false }, + isAuthenticationTypeAPIKey: expect.any(Function), + getAuthenticationAPIKey: expect.any(Function), }); }); @@ -160,6 +162,8 @@ test('creates a rules client with proper constructor arguments', async () => { getEventLogClient: expect.any(Function), kibanaVersion: '7.10.0', minimumScheduleInterval: { value: '1m', enforce: false }, + isAuthenticationTypeAPIKey: expect.any(Function), + getAuthenticationAPIKey: expect.any(Function), }); }); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/group1/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/security_and_spaces/group1/tests/alerting/get.ts index d0d93c0154745..d3117ed4ce209 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/group1/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/group1/tests/alerting/get.ts @@ -79,6 +79,7 @@ const getTestUtils = ( notify_when: 'onThrottleInterval', updated_by: 'elastic', api_key_owner: 'elastic', + api_key_created_by_user: false, mute_all: false, muted_alert_ids: [], execution_status: response.body.execution_status, diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/alerting/update.ts b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/alerting/update.ts index ba3963a00ddd1..b6972730cc780 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/alerting/update.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/alerting/update.ts @@ -119,6 +119,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { enabled: true, updated_by: user.username, api_key_owner: user.username, + api_key_created_by_user: false, mute_all: false, muted_alert_ids: [], actions: [ @@ -219,6 +220,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { enabled: true, updated_by: user.username, api_key_owner: user.username, + api_key_created_by_user: false, mute_all: false, muted_alert_ids: [], scheduled_task_id: createdAlert.scheduled_task_id, @@ -321,6 +323,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { enabled: true, updated_by: user.username, api_key_owner: user.username, + api_key_created_by_user: false, mute_all: false, muted_alert_ids: [], scheduled_task_id: createdAlert.scheduled_task_id, @@ -423,6 +426,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { enabled: true, updated_by: user.username, api_key_owner: user.username, + api_key_created_by_user: false, mute_all: false, muted_alert_ids: [], scheduled_task_id: createdAlert.scheduled_task_id, @@ -523,6 +527,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { enabled: true, updated_by: user.username, api_key_owner: user.username, + api_key_created_by_user: false, mute_all: false, muted_alert_ids: [], scheduled_task_id: createdAlert.scheduled_task_id,