Skip to content

Commit 525b3ea

Browse files
authored
BRE-342 Implement retry mechanism with error handling to get secrets from KV (#329)
* BRE-342 DEBUG: Print error message for debuging purposes * BRE-342 REFACTOR: Error message while downloading secret * BRE-342 ADD: retry mechanism with error handling for null/undefined responses in getSecretValue to prevent failures and retry up to 3 times with a delay * BRE-342 REFACTOR: Increase RETRY_DELAY * BRE-342 REFACTOR: Added retry logic to getSecretValue to handle both response and apiResult errors * BRE-342 REFACTOR: retry logic in getSecretValue by extracting repetitive code into a helper function for cleaner retries
1 parent d6235cf commit 525b3ea

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

get-keyvault-secrets/lib/KeyVaultClient.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,25 @@ class KeyVaultClient extends AzureRestClient_1.ServiceClient {
122122
}
123123
})).then((apiResult) => callback(apiResult.error, apiResult.result), (error) => callback(error));
124124
}
125-
getSecretValue(secretName, callback) {
125+
getSecretValue(secretName, callback, attempt = 1) {
126+
const MAX_RETRY_ATTEMPTS = 3; // Define the maximum number of retry attempts
127+
const RETRY_DELAY = 3000; // Define the delay between retries in milliseconds
126128
if (!callback) {
127129
core.debug("Callback Cannot Be Null");
128130
throw new Error("Callback Cannot Be Null");
129131
}
132+
// Helper function to handle retries
133+
const retryRequest = (reason) => {
134+
if (attempt < MAX_RETRY_ATTEMPTS) {
135+
core.debug(`Retrying... Attempt ${attempt + 1} due to: ${reason}`);
136+
setTimeout(() => {
137+
this.getSecretValue(secretName, callback, attempt + 1); // Retry the request
138+
}, RETRY_DELAY);
139+
} else {
140+
callback(new Error(`${reason} after max retries`), null); // If max retries reached, pass the error
141+
}
142+
};
143+
130144
// Create HTTP transport objects
131145
var httpRequest = {
132146
method: 'GET',
@@ -136,17 +150,32 @@ class KeyVaultClient extends AzureRestClient_1.ServiceClient {
136150
}, [], this.apiVersion)
137151
};
138152
this.invokeRequest(httpRequest).then((response) => __awaiter(this, void 0, void 0, function* () {
139-
if (response.statusCode == 200) {
140-
var result = response.body.value;
141-
return new AzureRestClient_1.ApiResult(null, result);
142-
}
143-
else if (response.statusCode == 400) {
144-
return new AzureRestClient_1.ApiResult('Get Secret Failed Because Of Invalid Characters', secretName);
153+
try {
154+
if (!response || response.statusCode == null) {
155+
throw new Error("Response or statusCode is null");
156+
}
157+
if (response.statusCode == 200) {
158+
var result = response.body.value;
159+
return new AzureRestClient_1.ApiResult(null, result);
160+
} else if (response.statusCode == 400) {
161+
return new AzureRestClient_1.ApiResult('Get Secret Failed Because Of Invalid Characters', secretName);
162+
} else {
163+
return new AzureRestClient_1.ApiResult((0, AzureRestClient_1.ToError)(response));
164+
}
165+
} catch (error) {
166+
retryRequest(error.message); // Retry on error
145167
}
146-
else {
147-
return new AzureRestClient_1.ApiResult((0, AzureRestClient_1.ToError)(response));
168+
})).then((apiResult) => {
169+
if (apiResult && apiResult.error) {
170+
retryRequest(apiResult.error.message); // Retry on apiResult error
171+
} else if (apiResult && typeof apiResult.result !== 'undefined') {
172+
callback(null, apiResult.result); // No error, pass the result
173+
} else {
174+
retryRequest("Unexpected result format"); // Retry on unexpected result format
148175
}
149-
})).then((apiResult) => callback(apiResult.error, apiResult.result), (error) => callback(error));
176+
}, (error) => {
177+
retryRequest(error.message); // Retry on promise rejection
178+
});
150179
}
151180
convertToAzureKeyVaults(result) {
152181
var listOfSecrets = [];

get-keyvault-secrets/lib/KeyVaultHelper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class KeyVaultHelper {
106106
return new Promise((resolve, reject) => {
107107
this.keyVaultClient.getSecretValue(secretName, (error, secretValue) => {
108108
if (error) {
109+
console.log(util.format("Error: %s", this.getError(error)));
109110
core.setFailed(util.format("Could not download the secret %s", secretName));
110111
}
111112
else {

0 commit comments

Comments
 (0)