Fixes issue with Keystore's stdin option (#15221)

Fixes issue preventing stdin option from being used
Using create to overwrite should reset keystore
This commit is contained in:
Tyler Smalley 2017-11-29 15:59:17 -08:00 committed by Tyler Smalley
parent efcf8737e7
commit 7df8dce8d8
8 changed files with 33 additions and 11 deletions

View file

@ -60,7 +60,7 @@ through stdin, use the `--stdin` flag:
[source,sh]
----------------------------------------------------------------
cat /file/containing/setting/value | bin/kibana-keystore add --stdin the.setting.name.to.set
cat /file/containing/setting/value | bin/kibana-keystore add the.setting.name.to.set --stdin
----------------------------------------------------------------
[float]

View file

@ -126,7 +126,7 @@ describe('Kibana keystore', () => {
stdin.end();
});
await add(keystore, 'foo', { stdin });
await add(keystore, 'foo', { stdin: true, stdinStream: stdin });
expect(keystore.data.foo).to.eql('kibana');
});

View file

@ -41,8 +41,8 @@ describe('Kibana keystore', () => {
const keystore = new Keystore('/data/nonexistent.keystore');
list(keystore);
sinon.assert.calledOnce(Logger.prototype.log);
sinon.assert.calledWith(Logger.prototype.log, '');
sinon.assert.calledOnce(Logger.prototype.error);
sinon.assert.calledWith(Logger.prototype.error, 'ERROR: Kibana keystore not found. Use \'create\' command to create one.');
});
});
});

View file

@ -7,7 +7,8 @@ import { createPromiseFromStreams, createConcatStream } from '../utils';
* @param {String} key
* @param {Object|null} options
* @property {Boolean} options.force - if true, will overwrite without prompting
* @property {Stream|Boolean} options.stdin - if true, uses process.stdin
* @property {Stream} options.stdinStream - defaults to process.stdin
* @property {Boolean} options.stdin - if true, uses options.stdin to read value
*/
export async function add(keystore, key, options = {}) {
@ -19,16 +20,20 @@ export async function add(keystore, key, options = {}) {
}
if (!options.force && keystore.has(key)) {
const overwrite = await confirm(`Setting ${key} already exists. Overwrite?`);
if (options.stdin) {
return logger.log(`Setting ${key} already exists, exiting without modifying keystore.`);
} else {
const overwrite = await confirm(`Setting ${key} already exists. Overwrite?`);
if (!overwrite) {
return logger.log('Exiting without modifying keystore.');
if (!overwrite) {
return logger.log('Exiting without modifying keystore.');
}
}
}
if (options.stdin) {
value = await createPromiseFromStreams([
options.stdin || process.stdin,
options.stdinStream || process.stdin,
createConcatStream('')
]);
} else {

View file

@ -12,6 +12,7 @@ export async function create(keystore, command, options) {
}
}
keystore.reset();
keystore.save();
logger.log(`Created Kibana keystore in ${keystore.path}`);

View file

@ -2,8 +2,12 @@ import Logger from '../cli_plugin/lib/logger';
export function list(keystore, command, options = {}) {
const logger = new Logger(options);
const keys = keystore.keys();
if (!keystore.exists()) {
return logger.error('ERROR: Kibana keystore not found. Use \'create\' command to create one.');
}
const keys = keystore.keys();
logger.log(keys.join('\n'));
}

View file

@ -91,6 +91,14 @@ describe('Keystore', () => {
});
});
describe('reset', () => {
it('clears the data', () => {
const keystore = new Keystore('/data/protected.keystore', 'changeme');
keystore.reset();
expect(keystore.data).to.eql([]);
});
});
describe('keys', () => {
it('lists object keys', () => {
const keystore = new Keystore('/data/unprotected.keystore');

View file

@ -9,9 +9,9 @@ const ITERATIONS = 10000;
export class Keystore {
constructor(path, password = '') {
this.path = path;
this.data = {};
this.password = password;
this.reset();
this.load();
}
@ -81,6 +81,10 @@ export class Keystore {
}
}
reset() {
this.data = {};
}
exists() {
return existsSync(this.path);
}