-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kbn/es] await native realm setup, error if there are failures (#36475)
* Revert "Revert "[kbn-es] await native realm setup, error if there are failures (#36290)"" This reverts commit a102ca1. * [kbn/es] retry setPassword call if it fails
- Loading branch information
Spencer
authored
May 20, 2019
1 parent
70c4074
commit 508424e
Showing
4 changed files
with
231 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,98 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
const { Client } = require('@elastic/elasticsearch'); | ||
const chalk = require('chalk'); | ||
|
||
const { log: defaultLog } = require('./log'); | ||
|
||
exports.NativeRealm = class NativeRealm { | ||
constructor(elasticPassword, port, log = defaultLog) { | ||
this._client = new Client({ node: `http://elastic:${elasticPassword}@localhost:${port}` }); | ||
this._elasticPassword = elasticPassword; | ||
this._log = log; | ||
} | ||
|
||
async setPassword(username, password = this._elasticPassword) { | ||
this._log.info(`setting ${chalk.bold(username)} password to ${chalk.bold(password)}`); | ||
|
||
try { | ||
await this._client.security.changePassword({ | ||
username, | ||
refresh: 'wait_for', | ||
body: { | ||
password, | ||
}, | ||
}); | ||
} catch (e) { | ||
this._log.error( | ||
chalk.red(`unable to set password for ${chalk.bold(username)}: ${e.message}`) | ||
); | ||
} | ||
} | ||
|
||
async setPasswords(options) { | ||
if (!(await this.isSecurityEnabled())) { | ||
this._log.info('security is not enabled, unable to set native realm passwords'); | ||
return; | ||
} | ||
|
||
(await this.getReservedUsers()).forEach(user => { | ||
this.setPassword(user, options[`password.${user}`]); | ||
}); | ||
} | ||
|
||
async getReservedUsers() { | ||
const users = await this._client.security.getUser(); | ||
|
||
return Object.keys(users.body).reduce((acc, user) => { | ||
if (users.body[user].metadata._reserved === true) { | ||
acc.push(user); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
|
||
async isSecurityEnabled() { | ||
try { | ||
const { | ||
body: { features }, | ||
} = await this._client.xpack.info({ categories: 'features' }); | ||
return features.security && features.security.enabled && features.security.available; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
}; | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
const { Client } = require('@elastic/elasticsearch'); | ||
const chalk = require('chalk'); | ||
|
||
const { log: defaultLog } = require('./log'); | ||
|
||
exports.NativeRealm = class NativeRealm { | ||
constructor(elasticPassword, port, log = defaultLog) { | ||
this._client = new Client({ node: `http://elastic:${elasticPassword}@localhost:${port}` }); | ||
this._elasticPassword = elasticPassword; | ||
this._log = log; | ||
} | ||
|
||
async setPassword(username, password = this._elasticPassword, { attempt = 1 } = {}) { | ||
this._log.info( | ||
(attempt > 1 ? `attempt ${attempt}: ` : '') + | ||
`setting ${chalk.bold(username)} password to ${chalk.bold(password)}` | ||
); | ||
|
||
try { | ||
await this._client.security.changePassword({ | ||
username, | ||
refresh: 'wait_for', | ||
body: { | ||
password, | ||
}, | ||
}); | ||
} catch (error) { | ||
if (attempt < 3 && error.meta && error.meta.statusCode === 401) { | ||
this._log.warning('[elastic] user not available yet, waiting 1.5 seconds and trying again'); | ||
await new Promise(resolve => setTimeout(resolve, 1500)); | ||
return await this.setPassword(username, password, { | ||
attempt: attempt + 1, | ||
}); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
async setPasswords(options) { | ||
if (!(await this.isSecurityEnabled())) { | ||
this._log.info('security is not enabled, unable to set native realm passwords'); | ||
return; | ||
} | ||
|
||
const reservedUsers = await this.getReservedUsers(); | ||
await Promise.all( | ||
reservedUsers.map(async user => { | ||
await this.setPassword(user, options[`password.${user}`]); | ||
}) | ||
); | ||
} | ||
|
||
async getReservedUsers() { | ||
const users = await this._client.security.getUser(); | ||
|
||
return Object.keys(users.body).reduce((acc, user) => { | ||
if (users.body[user].metadata._reserved === true) { | ||
acc.push(user); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
|
||
async isSecurityEnabled() { | ||
try { | ||
const { | ||
body: { features }, | ||
} = await this._client.xpack.info({ categories: 'features' }); | ||
return features.security && features.security.enabled && features.security.available; | ||
} catch (error) { | ||
if (error.meta && error.meta.statusCode === 400) { | ||
return false; | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
}; |
Oops, something went wrong.