Skip to content

Commit

Permalink
[ResponseOps][Connectors] Fix bug in PagerDuty Connector (elastic#175507
Browse files Browse the repository at this point in the history
)

Fixes elastic#175342

## Summary

The Pagerduty action UI does not allow users to save when there are
linting errors in the `Custom Details` field.

Previously the validation was skipped if there were Mustache templates
in the field.

(cherry picked from commit 8fe7433)
  • Loading branch information
adcoelho committed Jan 25, 2024
1 parent b4d153e commit 5270372
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('pagerduty action params validation', () => {
});
});

test('action params validation fails when customDetails are not valid JSON', async () => {
test('action params validation fails when customDetails are not valid JSON object', async () => {
const actionParams = {
eventAction: 'trigger',
dedupKey: 'test',
Expand All @@ -95,7 +95,7 @@ describe('pagerduty action params validation', () => {
component: 'test',
group: 'group',
class: 'test class',
customDetails: '{foo:bar}',
customDetails: '{foo:bar, "customFields": "{{contex.foo}}"}',
links: [],
};

Expand All @@ -105,12 +105,12 @@ describe('pagerduty action params validation', () => {
summary: [],
timestamp: [],
links: [],
customDetails: ['Custom details must be a valid JSON.'],
customDetails: ['Custom details must be a valid JSON object.'],
},
});
});

test('action params validation does not fail when customDetails are not JSON but have mustache templates inside', async () => {
test('action params validation fails when customDetails are a valid JSON but not an object', async () => {
const actionParams = {
eventAction: 'trigger',
dedupKey: 'test',
Expand All @@ -121,7 +121,59 @@ describe('pagerduty action params validation', () => {
component: 'test',
group: 'group',
class: 'test class',
customDetails: '{"details": {{alert.flapping}}}',
customDetails: '1234',
links: [],
};

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: {
dedupKey: [],
summary: [],
timestamp: [],
links: [],
customDetails: ['Custom details must be a valid JSON object.'],
},
});
});

test('action params validation fails when customDetails are an array of objects', async () => {
const actionParams = {
eventAction: 'trigger',
dedupKey: 'test',
summary: '2323',
source: 'source',
severity: 'critical',
timestamp: new Date().toISOString(),
component: 'test',
group: 'group',
class: 'test class',
customDetails: '[{"details": "Foo Bar"}, {"details": "{{alert.flapping}}"}]',
links: [],
};

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: {
dedupKey: [],
summary: [],
timestamp: [],
links: [],
customDetails: ['Custom details must be a valid JSON object.'],
},
});
});

test('action params validation does not fail when customDetails are a valid JSON object', async () => {
const actionParams = {
eventAction: 'trigger',
dedupKey: 'test',
summary: '2323',
source: 'source',
severity: 'critical',
timestamp: new Date().toISOString(),
component: 'test',
group: 'group',
class: 'test class',
customDetails: '{"details": "{{alert.flapping}}"}',
links: [],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AlertProvidedActionVariables,
hasMustacheTokens,
} from '@kbn/triggers-actions-ui-plugin/public';
import { isPlainObject } from 'lodash';
import {
PagerDutyConfig,
PagerDutySecrets,
Expand Down Expand Up @@ -92,18 +93,22 @@ export function getConnectorType(): ConnectorTypeModel<
}
});
}
if (actionParams.customDetails?.length && !hasMustacheTokens(actionParams.customDetails)) {
if (actionParams.customDetails?.length) {
const errorMessage = i18n.translate(
'xpack.stackConnectors.components.pagerDuty.error.invalidCustomDetails',
{
defaultMessage: 'Custom details must be a valid JSON object.',
}
);

try {
JSON.parse(actionParams.customDetails);
const parsedJSON = JSON.parse(actionParams.customDetails);

if (!isPlainObject(parsedJSON)) {
errors.customDetails.push(errorMessage);
}
} catch {
errors.customDetails.push(
i18n.translate(
'xpack.stackConnectors.components.pagerDuty.error.invalidCustomDetails',
{
defaultMessage: 'Custom details must be a valid JSON.',
}
)
);
errors.customDetails.push(errorMessage);
}
}
return validationResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ const PagerDutyParamsFields: React.FunctionComponent<ActionParamsProps<PagerDuty
}}
onBlur={() => {
if (!customDetails) {
editAction('customDetails', '', index);
editAction('customDetails', '{}', index);
}
}}
data-test-subj="customDetailsJsonEditor"
Expand Down

0 comments on commit 5270372

Please sign in to comment.