Skip to content

Commit

Permalink
Display rule owner for users who can manage API keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed May 5, 2022
1 parent 896b0e2 commit 2661b09
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof useKibana>;
const ruleTypeRegistry = ruleTypeRegistryMock.create();
Expand Down Expand Up @@ -100,6 +101,26 @@ describe('rule_details', () => {
).toBeTruthy();
});

it('renders the owner badge when user can manage API keys', () => {
const rule = mockRule();
expect(
shallow(
<RuleDetails rule={rule} ruleType={ruleType} actionTypes={[]} {...mockRuleApis} />
).find(<EuiBadge>{rule.apiKeyOwner}</EuiBadge>)
).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(<RuleDetails rule={rule} ruleType={ruleType} actionTypes={[]} {...mockRuleApis} />)
.find(<EuiBadge>{rule.apiKeyOwner}</EuiBadge>)
.exists()
).toBeFalsy();
});

it('renders the rule error banner with error message, when rule has a license error', () => {
const rule = mockRule({
enabled: true,
Expand Down Expand Up @@ -871,7 +892,7 @@ function mockRule(overloads: Partial<Rule> = {}): Rule {
updatedBy: null,
createdAt: new Date(),
updatedAt: new Date(),
apiKeyOwner: null,
apiKeyOwner: 'bob',
throttle: null,
notifyWhen: null,
muteAll: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -310,6 +314,25 @@ export const RuleDetails: React.FunctionComponent<RuleDetailsProps> = ({
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
{hasManageApiKeysCapability(capabilities) ? (
<EuiFlexItem grow={false}>
<EuiFlexGroup responsive={false} gutterSize="s" alignItems="center">
<EuiFlexItem grow={false}>
<EuiText size="s">
<p>
<FormattedMessage
id="xpack.triggersActionsUI.sections.rulesList.rulesListTable.columns.ownerTitle"
defaultMessage="Owner"
/>
</p>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiBadge data-test-subj="ownerLabel">{rule.apiKeyOwner}</EuiBadge>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
) : null}
<EuiFlexItem grow={false}>
{uniqueActions && uniqueActions.length ? (
<EuiFlexGroup responsive={false} gutterSize="xs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 2661b09

Please sign in to comment.