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

Retry Logic For AzureKeyVault based on status code #19701

46 changes: 32 additions & 14 deletions Tasks/AzureKeyVaultV1/operations/azure-arm-keyvault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import util = require("util");
import tl = require('azure-pipelines-task-lib/task');
import webClient = require("azure-pipelines-tasks-azure-arm-rest/webClient");

let retriesCount: number;
Deekshitha981 marked this conversation as resolved.
Show resolved Hide resolved
let retryWait: number;

export class AzureKeyVaultSecret {
name: string;
enabled: boolean;
Expand Down Expand Up @@ -176,23 +179,38 @@ export class KeyVaultClient extends azureServiceClient.ServiceClient {
'{secretName}': secretName
}
);

console.log(tl.loc("DownloadingSecretValue", secretName));
this.invokeRequest(httpRequest).then(async (response: webClient.WebResponse) => {
if (response.statusCode == 200) {
var result = response.body.value;
return new azureServiceClientBase.ApiResult(null, result);
}
else if (response.statusCode == 400) {
return new azureServiceClientBase.ApiResult(tl.loc('GetSecretFailedBecauseOfInvalidCharacters', secretName));
}
else {
return new azureServiceClientBase.ApiResult(azureServiceClientBase.ToError(response));
}
}).then((apiResult: azureServiceClientBase.ApiResult) => callback(apiResult.error, apiResult.result),
(error) => callback(error));
this.invokeRequestWithRetry(httpRequest,secretName)
.then((apiResult: azureServiceClientBase.ApiResult) => callback(apiResult.error, apiResult.result))
.catch(error => callback(error));

}

async invokeRequestWithRetry(httpRequest, secretName, retriesCount: number = 3, retryWait: number = 2000) {
return this.invokeRequest(httpRequest).then(async (response: webClient.WebResponse) => {
try {
// Check response status code
if (response.statusCode === 200) {
var result = response.body.value;
return new azureServiceClientBase.ApiResult(null, result);
} else if (response.statusCode === 400) {
return new azureServiceClientBase.ApiResult(tl.loc('GetSecretFailedBecauseOfInvalidCharacters', secretName));
} else {
return new azureServiceClientBase.ApiResult(azureServiceClientBase.ToError(response));
}
} catch (error) {
// Retry logic
if (retriesCount > 0) {
console.error(tl.loc(`failed with error: ${error} - remaining attempts: ${retriesCount}`));
console.log(tl.loc(`Retrying ...`));
await new Promise(r => setTimeout(r, retryWait));
return await this.invokeRequestWithRetry(httpRequest, secretName, retriesCount - 1, retryWait)
}
}
throw new Error(tl.loc(`Failed after ${4 - retriesCount} attempts`));
});
}

private convertToAzureKeyVaults(result: any[]): AzureKeyVaultSecret[] {
var listOfSecrets: AzureKeyVaultSecret[] = [];
result.forEach((value: any, index: number) => {
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureKeyVaultV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 237,
"Patch": 2
"Minor": 238,
"Patch": 0
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureKeyVaultV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 237,
"Patch": 2
"Minor": 238,
"Patch": 0
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions _generated/AzureKeyVaultV1.versionmap.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Default|1.237.2
Node20_229_2|1.237.3
Default|1.238.0
Node20_229_2|1.238.1
46 changes: 32 additions & 14 deletions _generated/AzureKeyVaultV1/operations/azure-arm-keyvault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import util = require("util");
import tl = require('azure-pipelines-task-lib/task');
import webClient = require("azure-pipelines-tasks-azure-arm-rest/webClient");

let retriesCount: number;
Deekshitha981 marked this conversation as resolved.
Show resolved Hide resolved
let retryWait: number;

export class AzureKeyVaultSecret {
name: string;
enabled: boolean;
Expand Down Expand Up @@ -176,23 +179,38 @@ export class KeyVaultClient extends azureServiceClient.ServiceClient {
'{secretName}': secretName
}
);

console.log(tl.loc("DownloadingSecretValue", secretName));
this.invokeRequest(httpRequest).then(async (response: webClient.WebResponse) => {
if (response.statusCode == 200) {
var result = response.body.value;
return new azureServiceClientBase.ApiResult(null, result);
}
else if (response.statusCode == 400) {
return new azureServiceClientBase.ApiResult(tl.loc('GetSecretFailedBecauseOfInvalidCharacters', secretName));
}
else {
return new azureServiceClientBase.ApiResult(azureServiceClientBase.ToError(response));
}
}).then((apiResult: azureServiceClientBase.ApiResult) => callback(apiResult.error, apiResult.result),
(error) => callback(error));
this.invokeRequestWithRetry(httpRequest,secretName)
.then((apiResult: azureServiceClientBase.ApiResult) => callback(apiResult.error, apiResult.result))
.catch(error => callback(error));

}

async invokeRequestWithRetry(httpRequest, secretName, retriesCount: number = 3, retryWait: number = 2000) {
return this.invokeRequest(httpRequest).then(async (response: webClient.WebResponse) => {
try {
// Check response status code
if (response.statusCode === 200) {
var result = response.body.value;
return new azureServiceClientBase.ApiResult(null, result);
} else if (response.statusCode === 400) {
return new azureServiceClientBase.ApiResult(tl.loc('GetSecretFailedBecauseOfInvalidCharacters', secretName));
} else {
return new azureServiceClientBase.ApiResult(azureServiceClientBase.ToError(response));
}
} catch (error) {
// Retry logic
if (retriesCount > 0) {
console.error(tl.loc(`failed with error: ${error} - remaining attempts: ${retriesCount}`));
console.log(tl.loc(`Retrying ...`));
await new Promise(r => setTimeout(r, retryWait));
return await this.invokeRequestWithRetry(httpRequest, secretName, retriesCount - 1, retryWait)
}
}
throw new Error(tl.loc(`Failed after ${4 - retriesCount} attempts`));
});
}

private convertToAzureKeyVaults(result: any[]): AzureKeyVaultSecret[] {
var listOfSecrets: AzureKeyVaultSecret[] = [];
result.forEach((value: any, index: number) => {
Expand Down
8 changes: 4 additions & 4 deletions _generated/AzureKeyVaultV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 237,
"Patch": 2
"Minor": 238,
"Patch": 0
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down Expand Up @@ -129,7 +129,7 @@
"ExpiredServicePrincipal": "Could not fetch access token for Azure. Verify if the Service Principal used is valid and not expired."
},
"_buildConfigMapping": {
"Default": "1.237.2",
"Node20_229_2": "1.237.3"
"Default": "1.238.0",
"Node20_229_2": "1.238.1"
}
}
8 changes: 4 additions & 4 deletions _generated/AzureKeyVaultV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 237,
"Patch": 2
"Minor": 238,
"Patch": 0
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down Expand Up @@ -129,7 +129,7 @@
"ExpiredServicePrincipal": "ms-resource:loc.messages.ExpiredServicePrincipal"
},
"_buildConfigMapping": {
"Default": "1.237.2",
"Node20_229_2": "1.237.3"
"Default": "1.238.0",
"Node20_229_2": "1.238.1"
}
}
46 changes: 32 additions & 14 deletions _generated/AzureKeyVaultV1_Node20/operations/azure-arm-keyvault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import util = require("util");
import tl = require('azure-pipelines-task-lib/task');
import webClient = require("azure-pipelines-tasks-azure-arm-rest/webClient");

let retriesCount: number;
let retryWait: number;

export class AzureKeyVaultSecret {
name: string;
enabled: boolean;
Expand Down Expand Up @@ -176,23 +179,38 @@ export class KeyVaultClient extends azureServiceClient.ServiceClient {
'{secretName}': secretName
}
);

console.log(tl.loc("DownloadingSecretValue", secretName));
this.invokeRequest(httpRequest).then(async (response: webClient.WebResponse) => {
if (response.statusCode == 200) {
var result = response.body.value;
return new azureServiceClientBase.ApiResult(null, result);
}
else if (response.statusCode == 400) {
return new azureServiceClientBase.ApiResult(tl.loc('GetSecretFailedBecauseOfInvalidCharacters', secretName));
}
else {
return new azureServiceClientBase.ApiResult(azureServiceClientBase.ToError(response));
}
}).then((apiResult: azureServiceClientBase.ApiResult) => callback(apiResult.error, apiResult.result),
(error) => callback(error));
this.invokeRequestWithRetry(httpRequest,secretName)
.then((apiResult: azureServiceClientBase.ApiResult) => callback(apiResult.error, apiResult.result))
.catch(error => callback(error));

}

async invokeRequestWithRetry(httpRequest, secretName, retriesCount: number = 3, retryWait: number = 2000) {
return this.invokeRequest(httpRequest).then(async (response: webClient.WebResponse) => {
try {
// Check response status code
if (response.statusCode === 200) {
var result = response.body.value;
return new azureServiceClientBase.ApiResult(null, result);
} else if (response.statusCode === 400) {
return new azureServiceClientBase.ApiResult(tl.loc('GetSecretFailedBecauseOfInvalidCharacters', secretName));
} else {
return new azureServiceClientBase.ApiResult(azureServiceClientBase.ToError(response));
}
} catch (error) {
// Retry logic
if (retriesCount > 0) {
console.error(tl.loc(`failed with error: ${error} - remaining attempts: ${retriesCount}`));
console.log(tl.loc(`Retrying ...`));
await new Promise(r => setTimeout(r, retryWait));
return await this.invokeRequestWithRetry(httpRequest, secretName, retriesCount - 1, retryWait)
}
}
throw new Error(tl.loc(`Failed after ${4 - retriesCount} attempts`));
});
}

private convertToAzureKeyVaults(result: any[]): AzureKeyVaultSecret[] {
var listOfSecrets: AzureKeyVaultSecret[] = [];
result.forEach((value: any, index: number) => {
Expand Down
8 changes: 4 additions & 4 deletions _generated/AzureKeyVaultV1_Node20/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 237,
"Patch": 3
"Minor": 238,
"Patch": 1
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down Expand Up @@ -137,7 +137,7 @@
"ExpiredServicePrincipal": "Could not fetch access token for Azure. Verify if the Service Principal used is valid and not expired."
},
"_buildConfigMapping": {
"Default": "1.237.2",
"Node20_229_2": "1.237.3"
"Default": "1.238.0",
"Node20_229_2": "1.238.1"
}
}
8 changes: 4 additions & 4 deletions _generated/AzureKeyVaultV1_Node20/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 237,
"Patch": 3
"Minor": 238,
"Patch": 1
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down Expand Up @@ -137,7 +137,7 @@
"ExpiredServicePrincipal": "ms-resource:loc.messages.ExpiredServicePrincipal"
},
"_buildConfigMapping": {
"Default": "1.237.2",
"Node20_229_2": "1.237.3"
"Default": "1.238.0",
"Node20_229_2": "1.238.1"
}
}