Skip to content

Commit

Permalink
Fix password field showing as null instead of empty string (#65183)
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang committed May 5, 2020
1 parent 6d78489 commit 86332e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ describe('createStream', () => {
exclude_files: [".gz$"]
processors:
- add_locale: ~
password: {{password}}
{{#if password}}
hidden_password: {{password}}
{{/if}}
`;
const vars = {
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
password: { type: 'password', value: '' },
};

const output = createStream(vars, streamTemplate);
Expand All @@ -28,6 +33,7 @@ describe('createStream', () => {
paths: ['/usr/local/var/log/nginx/access.log'],
exclude_files: ['.gz$'],
processors: [{ add_locale: null }],
password: '',
});
});

Expand All @@ -36,6 +42,7 @@ describe('createStream', () => {
input: redis/metrics
metricsets: ["key"]
test: null
password: {{password}}
{{#if key.patterns}}
key.patterns: {{key.patterns}}
{{/if}}
Expand All @@ -48,6 +55,7 @@ describe('createStream', () => {
pattern: '*'
`,
},
password: { type: 'password', value: '' },
};

const output = createStream(vars, streamTemplate);
Expand All @@ -61,6 +69,7 @@ describe('createStream', () => {
pattern: '*',
},
],
password: '',
});
});
});
13 changes: 12 additions & 1 deletion x-pack/plugins/ingest_manager/server/services/epm/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,16 @@ export function createStream(variables: DatasourceConfigRecord, streamTemplate:
const stream = template(vars);
const yamlFromStream = safeLoad(stream, {});

return replaceVariablesInYaml(yamlValues, yamlFromStream);
// Hack to keep empty string ('') values around in the end yaml because
// `safeLoad` replaces empty strings with null
const patchedYamlFromStream = Object.entries(yamlFromStream).reduce((acc, [key, value]) => {
if (value === null && typeof vars[key] === 'string' && vars[key].trim() === '') {
acc[key] = '';
} else {
acc[key] = value;
}
return acc;
}, {} as { [k: string]: any });

return replaceVariablesInYaml(yamlValues, patchedYamlFromStream);
}

0 comments on commit 86332e2

Please sign in to comment.