Skip to content

Commit

Permalink
[Defend Workflows] [Fix] Response console privilege error (#149185)
Browse files Browse the repository at this point in the history
  • Loading branch information
szwarckonrad authored Jan 23, 2023
1 parent 378866e commit d614aff
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import type { CommandExecutionComponentProps } from '../../types';
/**
* Builtin component that handles the output of command's `--help` argument
*/
export const HelpCommandArgument = memo<CommandExecutionComponentProps>((props) => {
export const HelpCommandArgument = memo<
CommandExecutionComponentProps<{}, { errorMessage?: string }>
>((props) => {
const CustomCommandHelp = props.command.commandDefinition.HelpComponent;

useEffect(() => {
Expand All @@ -37,7 +39,10 @@ export const HelpCommandArgument = memo<CommandExecutionComponentProps>((props)
}
)}
>
<CommandUsage commandDef={props.command.commandDefinition} />
<CommandUsage
commandDef={props.command.commandDefinition}
errorMessage={props.store.errorMessage}
/>
</HelpOutput>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
* 2.0.
*/

import React, { memo, useMemo } from 'react';
import React, { memo, useCallback, useMemo } from 'react';
import { EuiDescriptionList, EuiPanel, EuiSpacer } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { ConsoleCodeBlock } from './console_code_block';
import { getArgumentsForCommand } from '../service/parsed_command_input';
import type { CommandDefinition } from '../types';
import { useTestIdGenerator } from '../../../hooks/use_test_id_generator';
import { useDataTestSubj } from '../hooks/state_selectors/use_data_test_subj';
import { UnsupportedMessageCallout } from './unsupported_message_callout';

const additionalProps = {
className: 'euiTruncateText',
Expand Down Expand Up @@ -80,9 +82,10 @@ CommandInputUsage.displayName = 'CommandInputUsage';

export interface CommandUsageProps {
commandDef: CommandDefinition;
errorMessage?: string;
}

export const CommandUsage = memo<CommandUsageProps>(({ commandDef }) => {
export const CommandUsage = memo<CommandUsageProps>(({ commandDef, errorMessage }) => {
const getTestId = useTestIdGenerator(useDataTestSubj());
const hasArgs = useMemo(() => Object.keys(commandDef.args ?? []).length > 0, [commandDef.args]);

Expand Down Expand Up @@ -156,8 +159,31 @@ export const CommandUsage = memo<CommandUsageProps>(({ commandDef }) => {
);
};

const renderErrorMessage = useCallback(() => {
if (!errorMessage) {
return null;
}
return (
<UnsupportedMessageCallout
header={
<ConsoleCodeBlock textColor="danger">
<FormattedMessage
id="xpack.securitySolution.console.validationError.title"
defaultMessage="Unsupported action"
/>
</ConsoleCodeBlock>
}
data-test-subj={getTestId('validationError')}
>
<div data-test-subj={getTestId('badArgument-message')}>{errorMessage}</div>
<EuiSpacer size="s" />
</UnsupportedMessageCallout>
);
}, [errorMessage, getTestId]);

return (
<EuiPanel paddingSize="none" color="transparent" data-test-subj={getTestId('commandUsage')}>
{renderErrorMessage()}
<EuiDescriptionList
compressed
type="column"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,21 @@ export const handleExecuteCommand: ConsoleStoreReducer<
)
);
}

if (commandDefinition?.validate) {
const validationResult = commandDefinition.validate(command);
if (validationResult !== true) {
return updateStateWithNewCommandHistoryItem(
state,
createCommandHistoryEntry(
cloneCommandDefinitionWithNewRenderComponent(command, HelpCommandArgument),
createCommandExecutionState({
errorMessage: validationResult,
}),
false
)
);
}
}
return updateStateWithNewCommandHistoryItem(
state,
createCommandHistoryEntry(
Expand Down

0 comments on commit d614aff

Please sign in to comment.