Skip to content

Commit

Permalink
Fix normalizers to not parse list values if they are already parsed b…
Browse files Browse the repository at this point in the history
…y js-yaml. (#133563) (#133212)
  • Loading branch information
awahab07 committed Jun 7, 2022
1 parent 45099d3 commit 552c1ea
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { NewPackagePolicyInput } from '@kbn/fleet-plugin/common';
import { parseJsonIfString } from '../helpers/parsers';
import { CommonFields, ConfigKey, DataStream } from '../types';
import {
DEFAULT_COMMON_FIELDS,
Expand All @@ -27,7 +28,8 @@ export type CommonNormalizerMap = Record<keyof CommonFields | ConfigKey.NAME, No
export const cronToSecondsNormalizer = (value: string) =>
value ? value.slice(0, value.length - 1) : null;

export const jsonToJavascriptNormalizer = (value: string) => (value ? JSON.parse(value) : null);
export const jsonToJavascriptNormalizer = (value: string) =>
value ? parseJsonIfString(value) : null;

export function getNormalizer<Fields>(key: string, defaultValues: Fields): Normalizer {
return (fields: NewPackagePolicyInput['vars']) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export function parseJsonIfString<T>(value: T) {
if (typeof value === 'string') {
return JSON.parse(value);
}

return value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { parseJsonIfString } from '../helpers/parsers';
import { HTTPFields, ConfigKey, ContentType, contentTypesToMode } from '../types';
import {
Normalizer,
Expand Down Expand Up @@ -56,7 +57,7 @@ export const httpNormalizers: HTTPNormalizerMap = {
const requestHeaders = fields?.[ConfigKey.REQUEST_HEADERS_CHECK]?.value;
if (requestBody) {
const headers = requestHeaders
? JSON.parse(fields?.[ConfigKey.REQUEST_HEADERS_CHECK]?.value)
? parseJsonIfString(fields?.[ConfigKey.REQUEST_HEADERS_CHECK]?.value)
: defaultHTTPAdvancedFields[ConfigKey.REQUEST_HEADERS_CHECK];
const requestBodyValue =
requestBody !== null && requestBody !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { parseJsonIfString } from '../helpers/parsers';
import { TLSFields, ConfigKey } from '../types';
import { Normalizer } from '../common/normalizers';
import { defaultTLSFields } from '../contexts';
Expand Down Expand Up @@ -41,4 +42,4 @@ export const tlsNormalizers: TLSNormalizerMap = {
export const tlsStringToObjectNormalizer = (value: string = '', key: keyof TLSFields) =>
value ?? defaultTLSFields[key];
export const tlsJsonToObjectNormalizer = (value: string = '', key: keyof TLSFields) =>
value ? JSON.parse(value) : defaultTLSFields[key];
value ? parseJsonIfString(value) : defaultTLSFields[key];

0 comments on commit 552c1ea

Please sign in to comment.