Skip to content

Commit

Permalink
Display rule API key owner to users who can manage API keys (#131662)
Browse files Browse the repository at this point in the history
* Display rule owner for users who can manage API keys

* Rename owner to API key owner

* Make owner bold instead of a badge
  • Loading branch information
mikecote authored May 6, 2022
1 parent 39ae1d0 commit 3303999
Show file tree
Hide file tree
Showing 5 changed files with 56 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 API key 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 API key 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,27 @@ 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.apiKeyOwnerTitle"
defaultMessage="API key owner"
/>
</p>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText size="s" data-test-subj="apiKeyOwnerLabel">
<b>{rule.apiKeyOwner}</b>
</EuiText>
</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.getAPIKeyOwner();
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 getAPIKeyOwner() {
return await testSubjects.getVisibleText('apiKeyOwnerLabel');
},
async getActionsLabels() {
return {
connectorType: await testSubjects.getVisibleText('actionTypeLabel'),
Expand Down

0 comments on commit 3303999

Please sign in to comment.