Skip to content

Commit

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

# Backport

This will backport the following commits from `main` to `8.12`:
- [[ResponseOps][Connectors] Fix bug in PagerDuty Connector
(#175507)](#175507)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Antonio","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-01-25T13:32:38Z","message":"[ResponseOps][Connectors]
Fix bug in PagerDuty Connector (#175507)\n\nFixes #175342 \r\n\r\n##
Summary\r\n\r\nThe Pagerduty action UI does not allow users to save when
there are\r\nlinting errors in the `Custom Details`
field.\r\n\r\nPreviously the validation was skipped if there were
Mustache templates\r\nin the
field.","sha":"8fe74333a841817a93e329acc17c1b598d6d5688","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Feature:Actions","Team:ResponseOps","v8.12.1","v8.13.0"],"title":"[ResponseOps][Connectors]
Fix bug in PagerDuty
Connector","number":175507,"url":"https://github.com/elastic/kibana/pull/175507","mergeCommit":{"message":"[ResponseOps][Connectors]
Fix bug in PagerDuty Connector (#175507)\n\nFixes #175342 \r\n\r\n##
Summary\r\n\r\nThe Pagerduty action UI does not allow users to save when
there are\r\nlinting errors in the `Custom Details`
field.\r\n\r\nPreviously the validation was skipped if there were
Mustache templates\r\nin the
field.","sha":"8fe74333a841817a93e329acc17c1b598d6d5688"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","branchLabelMappingKey":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/175507","number":175507,"mergeCommit":{"message":"[ResponseOps][Connectors]
Fix bug in PagerDuty Connector (#175507)\n\nFixes #175342 \r\n\r\n##
Summary\r\n\r\nThe Pagerduty action UI does not allow users to save when
there are\r\nlinting errors in the `Custom Details`
field.\r\n\r\nPreviously the validation was skipped if there were
Mustache templates\r\nin the
field.","sha":"8fe74333a841817a93e329acc17c1b598d6d5688"}}]}]
BACKPORT-->

Co-authored-by: Antonio <[email protected]>
  • Loading branch information
kibanamachine and adcoelho authored Jan 25, 2024
1 parent 836d11c commit b281d4c
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 b281d4c

Please sign in to comment.