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.x] [ResponseOps][Connectors] Show a licensing message if the user does not have the sufficient license for system actions. (#201396) #201494

Merged
merged 1 commit into from
Nov 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { ActionTypeModel } from '../../common';
import { RuleActionsMessageProps } from './rule_actions_message';
import { RuleActionsSystemActionsItem } from './rule_actions_system_actions_item';
import { I18nProvider } from '@kbn/i18n-react';

jest.mock('../hooks', () => ({
useRuleFormState: jest.fn(),
Expand Down Expand Up @@ -81,7 +82,7 @@ const { validateParamsForWarnings } = jest.requireMock(
'../validation/validate_params_for_warnings'
);

const mockConnectors = [getConnector('1', { id: 'action-1' })];
const mockConnectors = [getConnector('1', { id: 'action-1', isSystemAction: true })];

const mockActionTypes = [getActionType('1')];

Expand Down Expand Up @@ -260,4 +261,59 @@ describe('ruleActionsSystemActionsItem', () => {

expect(screen.getByText('warning message!')).toBeInTheDocument();
});

describe('licensing', () => {
it('should render the licensing message if the user does not have the sufficient license', async () => {
const mockConnectorsWithLicensing = [
getConnector('1', { id: 'action-1', isSystemAction: true }),
];
const mockActionTypesWithLicensing = [
getActionType('1', {
enabledInLicense: false,
minimumLicenseRequired: 'platinum' as const,
}),
];

const actionTypeRegistry = new TypeRegistry<ActionTypeModel>();
actionTypeRegistry.register(
getActionTypeModel('1', {
id: 'actionType-1',
validateParams: mockValidate,
})
);
useRuleFormState.mockReturnValue({
plugins: {
actionTypeRegistry,
http: {
basePath: {
publicBaseUrl: 'publicUrl',
},
},
},
actionsParamsErrors: {},
selectedRuleType: {
...ruleType,
enabledInLicense: false,
minimumLicenseRequired: 'platinum' as const,
},
aadTemplateFields: [],
connectors: mockConnectorsWithLicensing,
connectorTypes: mockActionTypesWithLicensing,
});

render(
<I18nProvider>
<RuleActionsSystemActionsItem
action={getAction('1', { actionTypeId: 'actionType-1' })}
index={0}
producerId="stackAlerts"
/>
</I18nProvider>
);

expect(
await screen.findByText('This feature requires a Platinum license.')
).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { RuleActionParam, RuleSystemAction } from '@kbn/alerting-types';
import { SavedObjectAttribute } from '@kbn/core/types';
import { css } from '@emotion/react';
import { useRuleFormDispatch, useRuleFormState } from '../hooks';
import { RuleFormParamsErrors } from '../../common';
import { ActionConnector, RuleFormParamsErrors } from '../../common';
import {
ACTION_ERROR_TOOLTIP,
ACTION_WARNING_TITLE,
Expand All @@ -38,13 +38,76 @@ import {
import { RuleActionsMessage } from './rule_actions_message';
import { validateParamsForWarnings } from '../validation';
import { getAvailableActionVariables } from '../../action_variables';
import {
IsDisabledResult,
IsEnabledResult,
checkActionFormActionTypeEnabled,
} from '../utils/check_action_type_enabled';

interface RuleActionsSystemActionsItemProps {
action: RuleSystemAction;
index: number;
producerId: string;
}

interface SystemActionAccordionContentProps extends RuleActionsSystemActionsItemProps {
connector: ActionConnector;
checkEnabledResult?: IsEnabledResult | IsDisabledResult | null;
warning?: string | null;
onParamsChange: (key: string, value: RuleActionParam) => void;
}

const SystemActionAccordionContent: React.FC<SystemActionAccordionContentProps> = React.memo(
({ connector, checkEnabledResult, action, index, producerId, warning, onParamsChange }) => {
const { aadTemplateFields } = useRuleFormState();
const { euiTheme } = useEuiTheme();
const plain = useEuiBackgroundColor('plain');

if (!connector || !checkEnabledResult) {
return null;
}

if (!checkEnabledResult.isEnabled) {
return (
<EuiFlexGroup
direction="column"
style={{
padding: euiTheme.size.l,
backgroundColor: plain,
borderRadius: euiTheme.border.radius.medium,
}}
>
<EuiFlexItem>{checkEnabledResult.messageCard}</EuiFlexItem>
</EuiFlexGroup>
);
}

return (
<EuiFlexGroup
data-test-subj="ruleActionsSystemActionsItemAccordionContent"
direction="column"
style={{
padding: euiTheme.size.l,
backgroundColor: plain,
}}
>
<EuiFlexItem>
<RuleActionsMessage
useDefaultMessage
action={action}
index={index}
connector={connector}
producerId={producerId}
warning={warning}
templateFields={aadTemplateFields}
onParamsChange={onParamsChange}
/>
</EuiFlexItem>
</EuiFlexGroup>
);
}
);

export const RuleActionsSystemActionsItem = (props: RuleActionsSystemActionsItemProps) => {
const { action, index, producerId } = props;

Expand All @@ -54,7 +117,6 @@ export const RuleActionsSystemActionsItem = (props: RuleActionsSystemActionsItem
selectedRuleType,
connectorTypes,
connectors,
aadTemplateFields,
} = useRuleFormState();

const [isOpen, setIsOpen] = useState(true);
Expand All @@ -64,7 +126,6 @@ export const RuleActionsSystemActionsItem = (props: RuleActionsSystemActionsItem
const [warning, setWarning] = useState<string | null>(null);

const subdued = useEuiBackgroundColor('subdued');
const plain = useEuiBackgroundColor('plain');
const { euiTheme } = useEuiTheme();

const dispatch = useRuleFormDispatch();
Expand Down Expand Up @@ -156,6 +217,13 @@ export const RuleActionsSystemActionsItem = (props: RuleActionsSystemActionsItem
]
);

const checkEnabledResult = useMemo(() => {
if (!actionType) {
return null;
}
return checkActionFormActionTypeEnabled(actionType, []);
}, [actionType]);

return (
<EuiAccordion
data-test-subj="ruleActionsSystemActionsItem"
Expand Down Expand Up @@ -247,27 +315,15 @@ export const RuleActionsSystemActionsItem = (props: RuleActionsSystemActionsItem
</EuiPanel>
}
>
<EuiFlexGroup
data-test-subj="ruleActionsSystemActionsItemAccordionContent"
direction="column"
style={{
padding: euiTheme.size.l,
backgroundColor: plain,
}}
>
<EuiFlexItem>
<RuleActionsMessage
useDefaultMessage
action={action}
index={index}
connector={connector}
producerId={producerId}
warning={warning}
templateFields={aadTemplateFields}
onParamsChange={onParamsChange}
/>
</EuiFlexItem>
</EuiFlexGroup>
<SystemActionAccordionContent
action={action}
index={index}
producerId={producerId}
warning={warning}
connector={connector}
checkEnabledResult={checkEnabledResult}
onParamsChange={onParamsChange}
/>
</EuiAccordion>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,64 @@ describe('action_type_form', () => {

expect(setActionParamsProperty).toHaveBeenCalledWith('my-key', 'my-value', 1);
});

describe('licensing', () => {
const actionTypeIndexDefaultWithLicensing = {
...actionTypeIndexDefault,
'.test-system-action': {
...actionTypeIndexDefault['.test-system-action'],
enabledInLicense: false,
minimumLicenseRequired: 'platinum' as const,
},
};

beforeEach(() => {
const actionType = actionTypeRegistryMock.createMockActionTypeModel({
id: '.test-system-action-with-license',
iconClass: 'test',
selectMessage: 'test',
validateParams: (): Promise<GenericValidationResult<unknown>> => {
const validationResult = { errors: {} };
return Promise.resolve(validationResult);
},
actionConnectorFields: null,
actionParamsFields: mockedActionParamsFields,
defaultActionParams: {
dedupKey: 'test',
eventAction: 'resolve',
},
isSystemActionType: true,
});

actionTypeRegistry.get.mockReturnValue(actionType);

jest.clearAllMocks();
});

it('should render the licensing message if the user does not have the sufficient license', async () => {
render(
<I18nProvider>
<SystemActionTypeForm
actionConnector={actionConnector}
actionItem={actionItem}
connectors={connectors}
onDeleteAction={jest.fn()}
setActionParamsProperty={jest.fn()}
index={1}
actionTypesIndex={actionTypeIndexDefaultWithLicensing}
actionTypeRegistry={actionTypeRegistry}
messageVariables={{ context: [], state: [], params: [] }}
summaryMessageVariables={{ context: [], state: [], params: [] }}
producerId={AlertConsumers.INFRASTRUCTURE}
featureId={AlertConsumers.INFRASTRUCTURE}
ruleTypeId={'test'}
/>
</I18nProvider>
);

expect(
await screen.findByText('This feature requires a Platinum license.')
).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { isEmpty, partition, some } from 'lodash';
import { ActionVariable, RuleActionParam } from '@kbn/alerting-plugin/common';
import { ActionGroupWithMessageVariables } from '@kbn/triggers-actions-ui-types';
import { transformActionVariables } from '@kbn/alerts-ui-shared/src/action_variables/transforms';
import { checkActionFormActionTypeEnabled } from '@kbn/alerts-ui-shared/src/rule_form/utils/check_action_type_enabled';
import { TECH_PREVIEW_DESCRIPTION, TECH_PREVIEW_LABEL } from '../translations';
import {
IErrorObject,
Expand Down Expand Up @@ -167,8 +168,12 @@ export const SystemActionTypeForm = ({
};

const ParamsFieldsComponent = actionTypeRegistered.actionParamsFields;
const checkEnabledResult = checkActionFormActionTypeEnabled(
actionTypesIndex[actionConnector.actionTypeId],
[]
);

const accordionContent = (
const accordionContent = checkEnabledResult.isEnabled ? (
<>
<EuiSplitPanel.Inner color="plain">
{ParamsFieldsComponent ? (
Expand Down Expand Up @@ -212,6 +217,8 @@ export const SystemActionTypeForm = ({
) : null}
</EuiSplitPanel.Inner>
</>
) : (
checkEnabledResult.messageCard
);

return (
Expand Down