From 2661b09a6375bac7b00b566016ef3366d62532ab Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 5 May 2022 15:58:25 -0400 Subject: [PATCH 1/3] Display rule owner for users who can manage API keys --- .../public/application/lib/capabilities.ts | 2 ++ .../components/rule_details.test.tsx | 23 ++++++++++++++++- .../rule_details/components/rule_details.tsx | 25 ++++++++++++++++++- .../apps/triggers_actions_ui/details.ts | 3 +++ .../page_objects/rule_details.ts | 3 +++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/capabilities.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/capabilities.ts index 74b8243519428..bc4957b65b1bc 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/capabilities.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/capabilities.ts @@ -29,3 +29,5 @@ export function hasAllPrivilege(rule: InitialRule, ruleType?: RuleType): boolean export function hasReadPrivilege(rule: InitialRule, ruleType?: RuleType): boolean { return ruleType?.authorizedConsumers[rule.consumer]?.read ?? false; } +export const hasManageApiKeysCapability = (capabilities: Capabilities) => + capabilities?.management?.security?.api_keys; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx index fe17dde8c1282..0bd390ad7fb29 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx @@ -48,6 +48,7 @@ jest.mock('../../../lib/capabilities', () => ({ hasAllPrivilege: jest.fn(() => true), hasSaveRulesCapability: jest.fn(() => true), hasExecuteActionsCapability: jest.fn(() => true), + hasManageApiKeysCapability: jest.fn(() => true), })); const useKibanaMock = useKibana as jest.Mocked; const ruleTypeRegistry = ruleTypeRegistryMock.create(); @@ -100,6 +101,26 @@ describe('rule_details', () => { ).toBeTruthy(); }); + it('renders the owner badge when user can manage API keys', () => { + const rule = mockRule(); + expect( + shallow( + + ).find({rule.apiKeyOwner}) + ).toBeTruthy(); + }); + + it(`doesn't render the owner badge when user can't manage API keys`, () => { + const { hasManageApiKeysCapability } = jest.requireMock('../../../lib/capabilities'); + hasManageApiKeysCapability.mockReturnValueOnce(false); + const rule = mockRule(); + expect( + shallow() + .find({rule.apiKeyOwner}) + .exists() + ).toBeFalsy(); + }); + it('renders the rule error banner with error message, when rule has a license error', () => { const rule = mockRule({ enabled: true, @@ -871,7 +892,7 @@ function mockRule(overloads: Partial = {}): Rule { updatedBy: null, createdAt: new Date(), updatedAt: new Date(), - apiKeyOwner: null, + apiKeyOwner: 'bob', throttle: null, notifyWhen: null, muteAll: false, diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx index b3363159851d0..195f0c4000cad 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx @@ -27,7 +27,11 @@ import { import { FormattedMessage } from '@kbn/i18n-react'; import { toMountPoint } from '@kbn/kibana-react-plugin/public'; import { RuleExecutionStatusErrorReasons, parseDuration } from '@kbn/alerting-plugin/common'; -import { hasAllPrivilege, hasExecuteActionsCapability } from '../../../lib/capabilities'; +import { + hasAllPrivilege, + hasExecuteActionsCapability, + hasManageApiKeysCapability, +} from '../../../lib/capabilities'; import { getAlertingSectionBreadcrumb, getRuleDetailsBreadcrumb } from '../../../lib/breadcrumb'; import { getCurrentDocTitle } from '../../../lib/doc_title'; import { @@ -310,6 +314,25 @@ export const RuleDetails: React.FunctionComponent = ({ + {hasManageApiKeysCapability(capabilities) ? ( + + + + +

+ +

+
+
+ + {rule.apiKeyOwner} + +
+
+ ) : null} {uniqueActions && uniqueActions.length ? ( diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts index 1127a7423a3aa..fb6978011f954 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts @@ -168,6 +168,9 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { const ruleType = await pageObjects.ruleDetailsUI.getRuleType(); expect(ruleType).to.be(`Always Firing`); + const owner = await pageObjects.ruleDetailsUI.getOwner(); + expect(owner).to.be('elastic'); + const { connectorType } = await pageObjects.ruleDetailsUI.getActionsLabels(); expect(connectorType).to.be(`Slack`); }); diff --git a/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts b/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts index 01d7c24be2f41..374d809b3bc5b 100644 --- a/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts +++ b/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts @@ -21,6 +21,9 @@ export function RuleDetailsPageProvider({ getService }: FtrProviderContext) { async getRuleType() { return await testSubjects.getVisibleText('ruleTypeLabel'); }, + async getOwner() { + return await testSubjects.getVisibleText('ownerLabel'); + }, async getActionsLabels() { return { connectorType: await testSubjects.getVisibleText('actionTypeLabel'), From b56fe5fbec3b2859bd27bf85513100f77f09dc04 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 5 May 2022 16:03:48 -0400 Subject: [PATCH 2/3] Rename owner to API key owner --- .../sections/rule_details/components/rule_details.test.tsx | 4 ++-- .../sections/rule_details/components/rule_details.tsx | 6 +++--- .../apps/triggers_actions_ui/details.ts | 2 +- .../functional_with_es_ssl/page_objects/rule_details.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx index 0bd390ad7fb29..7857eb172eedb 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.test.tsx @@ -101,7 +101,7 @@ describe('rule_details', () => { ).toBeTruthy(); }); - it('renders the owner badge when user can manage API keys', () => { + it('renders the API key owner badge when user can manage API keys', () => { const rule = mockRule(); expect( shallow( @@ -110,7 +110,7 @@ describe('rule_details', () => { ).toBeTruthy(); }); - it(`doesn't render the owner badge when user can't manage API keys`, () => { + it(`doesn't render the API key owner badge when user can't manage API keys`, () => { const { hasManageApiKeysCapability } = jest.requireMock('../../../lib/capabilities'); hasManageApiKeysCapability.mockReturnValueOnce(false); const rule = mockRule(); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx index 195f0c4000cad..e03e25ce4f522 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx @@ -321,14 +321,14 @@ export const RuleDetails: React.FunctionComponent = ({

- {rule.apiKeyOwner} + {rule.apiKeyOwner} diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts index fb6978011f954..56dfa17ef6268 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts @@ -168,7 +168,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { const ruleType = await pageObjects.ruleDetailsUI.getRuleType(); expect(ruleType).to.be(`Always Firing`); - const owner = await pageObjects.ruleDetailsUI.getOwner(); + const owner = await pageObjects.ruleDetailsUI.getAPIKeyOwner(); expect(owner).to.be('elastic'); const { connectorType } = await pageObjects.ruleDetailsUI.getActionsLabels(); diff --git a/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts b/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts index 374d809b3bc5b..cff396276eefd 100644 --- a/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts +++ b/x-pack/test/functional_with_es_ssl/page_objects/rule_details.ts @@ -21,8 +21,8 @@ export function RuleDetailsPageProvider({ getService }: FtrProviderContext) { async getRuleType() { return await testSubjects.getVisibleText('ruleTypeLabel'); }, - async getOwner() { - return await testSubjects.getVisibleText('ownerLabel'); + async getAPIKeyOwner() { + return await testSubjects.getVisibleText('apiKeyOwnerLabel'); }, async getActionsLabels() { return { From c96f5dc1f658aab90b9eab3dc95d750db6468b6f Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Fri, 6 May 2022 09:33:06 -0400 Subject: [PATCH 3/3] Make owner bold instead of a badge --- .../sections/rule_details/components/rule_details.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx index e03e25ce4f522..0389e6b0d9b30 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_details/components/rule_details.tsx @@ -328,7 +328,9 @@ export const RuleDetails: React.FunctionComponent = ({ - {rule.apiKeyOwner} + + {rule.apiKeyOwner} +