[7.x] [kbn/es] auto-retry all requests in native realm setup (#36868)

* [kbn/es] auto-retry all requests in native realm setup

* run x-pack-ciGroup5 40 times

* fix argument to autoRetry

(cherry picked from commit 9c2ff83ba1)

* Revert "run x-pack-ciGroup5 40 times"

This reverts commit ebd179c52d.
This commit is contained in:
Spencer 2019-05-22 10:25:53 -07:00 committed by GitHub
parent ef6a38c2b1
commit 18c3eecb8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,12 +30,12 @@ exports.NativeRealm = class NativeRealm {
}
async setPassword(username, password = this._elasticPassword, { attempt = 1 } = {}) {
this._log.info(
(attempt > 1 ? `attempt ${attempt}: ` : '') +
`setting ${chalk.bold(username)} password to ${chalk.bold(password)}`
);
await this._autoRetry(async () => {
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',
@ -43,20 +43,7 @@ exports.NativeRealm = class NativeRealm {
password,
},
});
} catch (error) {
if (attempt < 3) {
this._log.warning(
'assuming that [elastic] user not available yet, waiting 1.5 seconds and trying again'
);
this._log.info(' -- error.meta', error.meta);
await new Promise(resolve => setTimeout(resolve, 1500));
return await this.setPassword(username, password, {
attempt: attempt + 1,
});
}
throw error;
}
});
}
async setPasswords(options) {
@ -74,7 +61,9 @@ exports.NativeRealm = class NativeRealm {
}
async getReservedUsers() {
const users = await this._client.security.getUser();
const users = await this._autoRetry(async () => {
return await this._client.security.getUser();
});
return Object.keys(users.body).reduce((acc, user) => {
if (users.body[user].metadata._reserved === true) {
@ -86,10 +75,12 @@ exports.NativeRealm = class NativeRealm {
async isSecurityEnabled() {
try {
const {
body: { features },
} = await this._client.xpack.info({ categories: 'features' });
return features.security && features.security.enabled && features.security.available;
return await this._autoRetry(async () => {
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;
@ -98,4 +89,18 @@ exports.NativeRealm = class NativeRealm {
throw error;
}
}
async _autoRetry(fn, attempt = 1) {
try {
return await fn(attempt);
} catch (error) {
if (attempt >= 3 || !error.meta || error.meta.statusCode !== 401) {
throw error;
}
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._autoRetry(fn, attempt + 1);
}
}
};