Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BRE-342 Implement retry mechanism with error handling to get secrets from KV #329

Merged
merged 7 commits into from
Oct 3, 2024
49 changes: 39 additions & 10 deletions get-keyvault-secrets/lib/KeyVaultClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,25 @@ class KeyVaultClient extends AzureRestClient_1.ServiceClient {
}
})).then((apiResult) => callback(apiResult.error, apiResult.result), (error) => callback(error));
}
getSecretValue(secretName, callback) {
getSecretValue(secretName, callback, attempt = 1) {
const MAX_RETRY_ATTEMPTS = 3; // Define the maximum number of retry attempts
const RETRY_DELAY = 3000; // Define the delay between retries in milliseconds
if (!callback) {
core.debug("Callback Cannot Be Null");
throw new Error("Callback Cannot Be Null");
}
// Helper function to handle retries
const retryRequest = (reason) => {
if (attempt < MAX_RETRY_ATTEMPTS) {
core.debug(`Retrying... Attempt ${attempt + 1} due to: ${reason}`);
setTimeout(() => {
this.getSecretValue(secretName, callback, attempt + 1); // Retry the request
}, RETRY_DELAY);
} else {
callback(new Error(`${reason} after max retries`), null); // If max retries reached, pass the error
}
};

// Create HTTP transport objects
var httpRequest = {
method: 'GET',
Expand All @@ -136,17 +150,32 @@ class KeyVaultClient extends AzureRestClient_1.ServiceClient {
}, [], this.apiVersion)
};
this.invokeRequest(httpRequest).then((response) => __awaiter(this, void 0, void 0, function* () {
if (response.statusCode == 200) {
var result = response.body.value;
return new AzureRestClient_1.ApiResult(null, result);
}
else if (response.statusCode == 400) {
return new AzureRestClient_1.ApiResult('Get Secret Failed Because Of Invalid Characters', secretName);
try {
if (!response || response.statusCode == null) {
throw new Error("Response or statusCode is null");
}
if (response.statusCode == 200) {
var result = response.body.value;
return new AzureRestClient_1.ApiResult(null, result);
} else if (response.statusCode == 400) {
return new AzureRestClient_1.ApiResult('Get Secret Failed Because Of Invalid Characters', secretName);
} else {
return new AzureRestClient_1.ApiResult((0, AzureRestClient_1.ToError)(response));
}
} catch (error) {
retryRequest(error.message); // Retry on error
}
else {
return new AzureRestClient_1.ApiResult((0, AzureRestClient_1.ToError)(response));
})).then((apiResult) => {
if (apiResult && apiResult.error) {
retryRequest(apiResult.error.message); // Retry on apiResult error
} else if (apiResult && typeof apiResult.result !== 'undefined') {
callback(null, apiResult.result); // No error, pass the result
} else {
retryRequest("Unexpected result format"); // Retry on unexpected result format
}
})).then((apiResult) => callback(apiResult.error, apiResult.result), (error) => callback(error));
}, (error) => {
retryRequest(error.message); // Retry on promise rejection
});
}
convertToAzureKeyVaults(result) {
var listOfSecrets = [];
Expand Down
1 change: 1 addition & 0 deletions get-keyvault-secrets/lib/KeyVaultHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class KeyVaultHelper {
return new Promise((resolve, reject) => {
this.keyVaultClient.getSecretValue(secretName, (error, secretValue) => {
if (error) {
console.log(util.format("Error: %s", this.getError(error)));
core.setFailed(util.format("Could not download the secret %s", secretName));
}
else {
Expand Down