mirror of
https://github.com/hashicorp/vault-action.git
synced 2025-11-07 15:16:56 +00:00
adding generic async retry function and retry core.getIDToken
This commit is contained in:
parent
a1b77a0929
commit
dc470acfea
1 changed files with 30 additions and 1 deletions
31
src/auth.js
31
src/auth.js
|
|
@ -5,6 +5,8 @@ const fs = require('fs');
|
|||
const { default: got } = require('got');
|
||||
|
||||
const defaultKubernetesTokenPath = '/var/run/secrets/kubernetes.io/serviceaccount/token'
|
||||
const retries = 5
|
||||
const retries_delay = 3000
|
||||
/***
|
||||
* Authenticate with Vault and retrieve a Vault token that can be used for requests.
|
||||
* @param {string} method
|
||||
|
|
@ -35,7 +37,10 @@ async function retrieveToken(method, client) {
|
|||
const githubAudience = core.getInput('jwtGithubAudience', { required: false });
|
||||
|
||||
if (!privateKey) {
|
||||
jwt = await core.getIDToken(githubAudience)
|
||||
jwt = await retryAsyncFunction(retries, retries_delay, core.getIDToken, githubAudience)
|
||||
.then((result) => {
|
||||
return result;
|
||||
});
|
||||
} else {
|
||||
jwt = generateJwt(privateKey, keyPassword, Number(tokenTtl));
|
||||
}
|
||||
|
|
@ -142,6 +147,30 @@ async function getClientToken(client, method, path, payload) {
|
|||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* Generic function for retrying an async function
|
||||
* @param {number} retries
|
||||
* @param {number} delay
|
||||
* @param {Function} func
|
||||
* @param {any[]} args
|
||||
*/
|
||||
async function retryAsyncFunction(retries, delay, func, ...args) {
|
||||
let attempt = 0;
|
||||
while (attempt < retries) {
|
||||
try {
|
||||
const result = await func(...args);
|
||||
return result;
|
||||
} catch (error) {
|
||||
attempt++;
|
||||
if (attempt < retries) {
|
||||
await new Promise(resolve => setTimeout(resolve, delay));
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* @typedef {Object} VaultLoginResponse
|
||||
* @property {{
|
||||
|
|
|
|||
Loading…
Reference in a new issue