Skip to content

Commit

Permalink
[APM] ensure rum_allow_origins setting only saves valid YAML strings (e…
Browse files Browse the repository at this point in the history
…lastic#128703) (elastic#128707)

(cherry picked from commit 79039db)
  • Loading branch information
ogupte committed Mar 29, 2022
1 parent d5853fe commit aac0d2a
Showing 1 changed file with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import yaml from 'js-yaml';
import {
POLICY_ELASTIC_AGENT_ON_CLOUD,
SUPPORTED_APM_PACKAGE_VERSION,
Expand Down Expand Up @@ -89,7 +89,11 @@ function getApmPackageInputVars(options: GetApmPackagePolicyDefinitionOptions) {

const inputVars: Record<string, { type: string; value: any }> =
apmServerConfigs.reduce((acc, { key, name, type, getValue }) => {
const value = (getValue ? getValue(options) : apmServerSchema[key]) ?? ''; // defaults to an empty string to be edited in Fleet UI
const apmServerSchemaValue = apmServerSchema[key];
const value =
(getValue
? getValue(options, apmServerSchemaValue)
: apmServerSchemaValue) ?? ''; // defaults to an empty string to be edited in Fleet UI
return {
...acc,
[name]: { type, value },
Expand All @@ -103,7 +107,10 @@ export const apmConfigMapping: Record<
{
name: string;
type: string;
getValue?: (options: GetApmPackagePolicyDefinitionOptions) => any;
getValue?: (
options: GetApmPackagePolicyDefinitionOptions,
value?: any
) => any;
}
> = {
'apm-server.host': {
Expand All @@ -126,6 +133,8 @@ export const apmConfigMapping: Record<
'apm-server.rum.allow_origins': {
name: 'rum_allow_origins',
type: 'text',
getValue: (options, apmServerSchemaValue) =>
ensureValidMultiText(apmServerSchemaValue as string[]) ?? '',
},
'apm-server.rum.allow_headers': {
name: 'rum_allow_headers',
Expand Down Expand Up @@ -252,3 +261,20 @@ export const apmConfigMapping: Record<
type: 'integer',
},
};

function ensureValidMultiText(textMultiValue: string[] | undefined) {
if (!textMultiValue) {
return undefined;
}
return textMultiValue.map(escapeInvalidYamlString);
}
function escapeInvalidYamlString(yamlString: string) {
try {
yaml.load(yamlString);
} catch (error) {
if (error instanceof yaml.YAMLException) {
return `"${yamlString}"`;
}
}
return yamlString;
}

0 comments on commit aac0d2a

Please sign in to comment.