Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.11] [RAM] Enable read-only users to access rules (#167003) #168259

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -38307,7 +38307,6 @@
"xpack.triggersActionsUI.sections.rulesList.monthsLabel": "mois",
"xpack.triggersActionsUI.sections.rulesList.multipleTitle": "règles",
"xpack.triggersActionsUI.sections.rulesList.noPermissionToCreateDescription": "Contactez votre administrateur système.",
"xpack.triggersActionsUI.sections.rulesList.noPermissionToCreateTitle": "Aucune autorisation pour créer des règles",
"xpack.triggersActionsUI.sections.rulesList.previousSnooze": "Précédent",
"xpack.triggersActionsUI.sections.rulesList.refreshRulesButtonLabel": "Actualiser",
"xpack.triggersActionsUI.sections.rulesList.remainingSnoozeIndefinite": "Indéfiniment",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -38298,7 +38298,6 @@
"xpack.triggersActionsUI.sections.rulesList.monthsLabel": "月",
"xpack.triggersActionsUI.sections.rulesList.multipleTitle": "ルール",
"xpack.triggersActionsUI.sections.rulesList.noPermissionToCreateDescription": "システム管理者にお問い合わせください。",
"xpack.triggersActionsUI.sections.rulesList.noPermissionToCreateTitle": "ルールを作成する権限がありません",
"xpack.triggersActionsUI.sections.rulesList.previousSnooze": "前へ",
"xpack.triggersActionsUI.sections.rulesList.refreshRulesButtonLabel": "更新",
"xpack.triggersActionsUI.sections.rulesList.remainingSnoozeIndefinite": "永久",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -38292,7 +38292,6 @@
"xpack.triggersActionsUI.sections.rulesList.monthsLabel": "个月",
"xpack.triggersActionsUI.sections.rulesList.multipleTitle": "规则",
"xpack.triggersActionsUI.sections.rulesList.noPermissionToCreateDescription": "请联系您的系统管理员。",
"xpack.triggersActionsUI.sections.rulesList.noPermissionToCreateTitle": "没有创建规则的权限",
"xpack.triggersActionsUI.sections.rulesList.previousSnooze": "上一步",
"xpack.triggersActionsUI.sections.rulesList.refreshRulesButtonLabel": "刷新",
"xpack.triggersActionsUI.sections.rulesList.remainingSnoozeIndefinite": "无限期",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const NoPermissionPrompt = () => (
title={
<h1>
<FormattedMessage
id="xpack.triggersActionsUI.sections.rulesList.noPermissionToCreateTitle"
defaultMessage="No permissions to create rules"
id="xpack.triggersActionsUI.sections.rulesList.noPermissionToReadTitle"
defaultMessage="No permissions to read rules"
/>
</h1>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export const useLoadRuleTypesQuery = (props: UseLoadRuleTypesQueryProps) => {
const authorizedToCreateAnyRules = authorizedRuleTypes.some(
(ruleType) => ruleType.authorizedConsumers[ALERTS_FEATURE_ID]?.all
);
const authorizedToReadAnyRules =
authorizedToCreateAnyRules ||
authorizedRuleTypes.some((ruleType) => ruleType.authorizedConsumers[ALERTS_FEATURE_ID]?.read);

return {
ruleTypesState: {
Expand All @@ -73,6 +76,7 @@ export const useLoadRuleTypesQuery = (props: UseLoadRuleTypesQueryProps) => {
},
hasAnyAuthorizedRuleType,
authorizedRuleTypes,
authorizedToReadAnyRules,
authorizedToCreateAnyRules,
isSuccess,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isEmpty } from 'lodash';
import { RulesListFilters } from '../../types';

interface UseUiProps {
authorizedToReadAnyRules: boolean;
authorizedToCreateAnyRules: boolean;
filters: RulesListFilters;
hasDefaultRuleTypesFiltersOn: boolean;
Expand Down Expand Up @@ -37,6 +38,7 @@ const getFilterApplied = ({ hasEmptyTypesFilter, filters }: GetFilterAppliedProp
};

export const useRulesListUiState = ({
authorizedToReadAnyRules,
authorizedToCreateAnyRules,
filters,
hasDefaultRuleTypesFiltersOn,
Expand All @@ -56,8 +58,9 @@ export const useRulesListUiState = ({
const isInitialLoading = isInitialLoadingRuleTypes || isInitialLoadingRules;
const isLoading = isLoadingRuleTypes || isLoadingRules;

const showNoAuthPrompt = !isInitialLoadingRuleTypes && !authorizedToCreateAnyRules;
const showCreateFirstRulePrompt = !isLoading && !hasData && !isFilterApplied;
const showNoAuthPrompt = !isInitialLoadingRuleTypes && !authorizedToReadAnyRules;
const showCreateFirstRulePrompt =
!isLoading && !hasData && !isFilterApplied && authorizedToCreateAnyRules;
const showSpinner =
isInitialLoading && (isLoadingRuleTypes || (!showNoAuthPrompt && isLoadingRules));
const showRulesList = !showSpinner && !showCreateFirstRulePrompt && !showNoAuthPrompt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jest.mock('./rule_actions', () => ({
jest.mock('../../../lib/capabilities', () => ({
hasAllPrivilege: jest.fn(() => true),
hasSaveRulesCapability: jest.fn(() => true),
hasShowActionsCapability: jest.fn(() => true),
hasExecuteActionsCapability: jest.fn(() => true),
hasManageApiKeysCapability: jest.fn(() => true),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import { formatDuration } from '@kbn/alerting-plugin/common';
import { RuleDefinitionProps } from '../../../../types';
import { RuleType, useLoadRuleTypes } from '../../../..';
import { useKibana } from '../../../../common/lib/kibana';
import { hasAllPrivilege, hasExecuteActionsCapability } from '../../../lib/capabilities';
import {
hasAllPrivilege,
hasExecuteActionsCapability,
hasShowActionsCapability,
} from '../../../lib/capabilities';
import { RuleActions } from './rule_actions';
import { RuleEdit } from '../../rule_form';

Expand Down Expand Up @@ -60,6 +64,7 @@ export const RuleDefinition: React.FunctionComponent<RuleDefinitionProps> = ({
values: { numberOfConditions },
});
};
const canReadActions = hasShowActionsCapability(capabilities);
const canExecuteActions = hasExecuteActionsCapability(capabilities);
const canSaveRule =
rule &&
Expand Down Expand Up @@ -209,11 +214,21 @@ export const RuleDefinition: React.FunctionComponent<RuleDefinitionProps> = ({
})}
</ItemTitleRuleSummary>
<EuiFlexItem grow={3}>
<RuleActions
ruleActions={rule.actions}
actionTypeRegistry={actionTypeRegistry}
legacyNotifyWhen={rule.notifyWhen}
/>
{canReadActions ? (
<RuleActions
ruleActions={rule.actions}
actionTypeRegistry={actionTypeRegistry}
legacyNotifyWhen={rule.notifyWhen}
/>
) : (
<EuiFlexItem>
<EuiText size="s">
{i18n.translate('xpack.triggersActionsUI.ruleDetails.cannotReadActions', {
defaultMessage: 'Connector feature privileges are required to view actions',
})}
</EuiText>
</EuiFlexItem>
)}
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export const RulesList = ({
ruleTypesState,
hasAnyAuthorizedRuleType,
authorizedRuleTypes,
authorizedToReadAnyRules,
authorizedToCreateAnyRules,
isSuccess: isLoadRuleTypesSuccess,
} = useLoadRuleTypesQuery({ filteredRuleTypes });
Expand Down Expand Up @@ -285,6 +286,7 @@ export const RulesList = ({
});

const { showSpinner, showRulesList, showNoAuthPrompt, showCreateFirstRulePrompt } = useUiState({
authorizedToReadAnyRules,
authorizedToCreateAnyRules,
filters,
hasDefaultRuleTypesFiltersOn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,20 +297,56 @@ export default ({ getService, getPageObjects }: FtrProviderContext) => {
'Create rule button',
async () => await testSubjects.exists('createRuleButton')
);
await retry.waitFor(
'Create rule button is enabled',
async () => await testSubjects.isEnabled('createRuleButton')
);
});

it(`shows the no permission prompt when the user has no permissions`, async () => {
// We kept this test to make sure that the stack management rule page
// is showing the right prompt corresponding to the right privileges.
// Knowing that o11y alert page won't come up if you do not have any
// kind of privileges to o11y
await observability.users.setTestUserRole({
elasticsearch: {
cluster: [],
indices: [],
run_as: [],
},
kibana: [
{
base: [],
feature: {
discover: ['read'],
},
spaces: ['*'],
},
],
});
await observability.alerts.common.navigateToRulesPage();
await retry.waitFor(
'No permissions prompt',
async () => await testSubjects.exists('noPermissionPrompt')
);
await observability.users.restoreDefaultTestUserRole();
});

it(`shows the rules list in read-only mode when the user only has read permissions`, async () => {
await observability.users.setTestUserRole(
observability.users.defineBasicObservabilityRole({
logs: ['read'],
})
);
await observability.alerts.common.navigateToRulesPage();
await retry.waitFor(
'No permissions prompt',
async () => await testSubjects.exists('noPermissionPrompt')
'Read-only rules list is visible',
async () => await testSubjects.exists('rulesList')
);
await retry.waitFor(
'Create rule button is disabled',
async () => !(await testSubjects.isEnabled('createRuleButton'))
);

await observability.users.restoreDefaultTestUserRole();
});
});
Expand Down
Loading