Skip to content

Commit

Permalink
[keystore_cli] parse values as JSON before adding to keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Oct 16, 2020
1 parent ca8b038 commit 494fefb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cli_keystore/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ export async function add(keystore, key, options = {}) {
value = await question(`Enter value for ${key}`, { mask: '*' });
}

keystore.add(key, value.trim());
const parsedValue = value.trim();
let parsedJsonValue;
try {
parsedJsonValue = JSON.parse(parsedValue);
} catch {
// noop, only treat value as json if it parses as JSON
}

keystore.add(key, parsedJsonValue ?? parsedValue);
keystore.save();
}

Expand Down
11 changes: 11 additions & 0 deletions src/cli_keystore/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ describe('Kibana keystore', () => {
expect(keystore.data.foo).toEqual('bar');
});

it('parses JSON values', async () => {
prompt.question.returns(Promise.resolve('["bar"]\n'));

const keystore = new Keystore('/data/test.keystore');
sandbox.stub(keystore, 'save');

await add(keystore, 'foo');

expect(keystore.data.foo).toEqual(['bar']);
});

it('persists updated keystore', async () => {
prompt.question.returns(Promise.resolve('bar\n'));

Expand Down
2 changes: 2 additions & 0 deletions src/legacy/server/keystore/keystore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ describe('Keystore', () => {
it('adds a key/value pair', () => {
const keystore = new Keystore('/data/unprotected.keystore');
keystore.add('a3', 'baz');
keystore.add('a4', [1, 'a', 2, 'b']);

expect(keystore.data).toEqual({
'a1.b2.c3': 'foo',
a2: 'bar',
a3: 'baz',
a4: [1, 'a', 2, 'b'],
});
});
});
Expand Down

0 comments on commit 494fefb

Please sign in to comment.