From 2661b09a6375bac7b00b566016ef3366d62532ab Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 5 May 2022 15:58:25 -0400 Subject: [PATCH] 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'),