From 936033acb474705d35d1ee5ebe6754cd2c4493e4 Mon Sep 17 00:00:00 2001 From: Vineet Date: Wed, 5 Dec 2018 17:14:04 +0530 Subject: [PATCH] Setting up Azure Active cloud corresponding to service endpoint (#9003) * Setting up Azure Active cloud corresponding to service endpoint * Fix review comment --- .../resources.resjson/en-US/resources.resjson | 7 +++--- Tasks/AzureCLIV1/azureclitask.ts | 19 ++++++++++----- Tasks/AzureCLIV1/task.json | 7 +++--- Tasks/AzureCLIV1/task.loc.json | 23 ++++++++++--------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/Tasks/AzureCLIV1/Strings/resources.resjson/en-US/resources.resjson b/Tasks/AzureCLIV1/Strings/resources.resjson/en-US/resources.resjson index 9c1d4703b5c2..c1e868902301 100644 --- a/Tasks/AzureCLIV1/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/AzureCLIV1/Strings/resources.resjson/en-US/resources.resjson @@ -17,10 +17,10 @@ "loc.input.help.args": "Arguments passed to the script", "loc.input.label.addSpnToEnvironment": "Access service principal details in script", "loc.input.help.addSpnToEnvironment": "Adds service principal id and key of the Azure endpoint you chose to the script's execution environment. You can use these variables: `$servicePrincipalId` and `$servicePrincipalKey` in your script", + "loc.input.label.useGlobalConfig": "Use global Azure CLI configuration", + "loc.input.help.useGlobalConfig": "If this is false, this task will use its own separate [Azure CLI configuration directory](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest#cli-configuration-file). This can be used to run Azure CLI tasks in *parallel* releases", "loc.input.label.cwd": "Working Directory", "loc.input.help.cwd": "Current working directory where the script is run. Empty is the root of the repo (build) or artifacts (release), which is $(System.DefaultWorkingDirectory)", - "loc.input.label.useGlobalConfig": "Use global Azure CLI configuration", - "loc.input.help.useGlobalConfig": "If this is false, this task will use its own separate [*AzCLI configuration directory*](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest#cli-configuration-file). This can be used to run Azure CLI tasks in *parallel* releases", "loc.input.label.failOnStandardError": "Fail on Standard Error", "loc.input.help.failOnStandardError": "If this is true, this task will fail when any errors are written to the StandardError stream. Unselect the checkbox to ignore standard errors and rely on exit codes to determine the status", "loc.messages.ScriptReturnCode": "Script exited with return code: %d", @@ -30,5 +30,6 @@ "loc.messages.FailedToLogout": "The following error occurred while logging out: %s", "loc.messages.LoginFailed": "Azure login failed", "loc.messages.ErrorInSettingUpSubscription": "Error in setting up subscription", - "loc.messages.SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s" + "loc.messages.SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s", + "loc.messages.SettingAzureCloud": "Setting active cloud to: %s" } \ No newline at end of file diff --git a/Tasks/AzureCLIV1/azureclitask.ts b/Tasks/AzureCLIV1/azureclitask.ts index 3af183bb7acb..7a768bf0a384 100644 --- a/Tasks/AzureCLIV1/azureclitask.ts +++ b/Tasks/AzureCLIV1/azureclitask.ts @@ -57,7 +57,9 @@ export class azureclitask { tool = tl.tool(tl.which(scriptPath, true)); } this.throwIfError(tl.execSync("az", "--version")); - this.useGlobalConfig = tl.getBoolInput("useGlobalConfig"); + // set az cli config dir + this.setConfigDirectory(); + this.setAzureCloudBasedOnServiceEndpoint(); this.loginAzure(); tool.line(args); // additional args should always call line. line() parses quoted arg strings @@ -115,7 +117,6 @@ export class azureclitask { private static isLoggedIn: boolean = false; private static cliPasswordPath: string = null; private static azCliConfigPath: string; - private static useGlobalConfig: boolean = true; private static servicePrincipalId: string = null; private static servicePrincipalKey: string = null; @@ -146,9 +147,6 @@ export class azureclitask { var tenantId: string = tl.getEndpointAuthorizationParameter(connectedService, "tenantid", false); var subscriptionID: string = tl.getEndpointDataParameter(connectedService, "SubscriptionID", true); - // set az cli config dir - this.setConfigDirectory(); - //login using svn this.throwIfError(tl.execSync("az", "login --service-principal -u \"" + servicePrincipalId + "\" -p \"" + cliPassword + "\" --tenant \"" + tenantId + "\""), tl.loc("LoginFailed")); this.isLoggedIn = true; @@ -158,7 +156,7 @@ export class azureclitask { } private static setConfigDirectory(): void { - if (this.useGlobalConfig) { + if (tl.getBoolInput("useGlobalConfig")) { return; } @@ -178,6 +176,15 @@ export class azureclitask { } } + private static setAzureCloudBasedOnServiceEndpoint(): void { + var connectedService: string = tl.getInput("connectedServiceNameARM", true); + var environment = tl.getEndpointDataParameter(connectedService, 'environment', true); + if(!!environment) { + console.log(tl.loc('SettingAzureCloud', environment)); + this.throwIfError(tl.execSync("az", "cloud set -n " + environment)); + } + } + private static logoutAzure() { try { tl.execSync("az", " account clear"); diff --git a/Tasks/AzureCLIV1/task.json b/Tasks/AzureCLIV1/task.json index 73d8bf9fce70..b3c537ea7fcb 100644 --- a/Tasks/AzureCLIV1/task.json +++ b/Tasks/AzureCLIV1/task.json @@ -19,7 +19,7 @@ "version": { "Major": 1, "Minor": 144, - "Patch": 2 + "Patch": 3 }, "minimumAgentVersion": "2.0.0", "instanceNameFormat": "Azure CLI $(scriptPath)", @@ -103,7 +103,7 @@ "name": "useGlobalConfig", "type": "boolean", "label": "Use global Azure CLI configuration", - "defaultValue": "true", + "defaultValue": "false", "required": false, "helpMarkDown": "If this is false, this task will use its own separate [Azure CLI configuration directory](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest#cli-configuration-file). This can be used to run Azure CLI tasks in *parallel* releases", "groupName": "advanced" @@ -144,6 +144,7 @@ "FailedToLogout": "The following error occurred while logging out: %s", "LoginFailed": "Azure login failed", "ErrorInSettingUpSubscription": "Error in setting up subscription", - "SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s" + "SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s", + "SettingAzureCloud": "Setting active cloud to: %s" } } \ No newline at end of file diff --git a/Tasks/AzureCLIV1/task.loc.json b/Tasks/AzureCLIV1/task.loc.json index 4f457163a84c..c50386921090 100644 --- a/Tasks/AzureCLIV1/task.loc.json +++ b/Tasks/AzureCLIV1/task.loc.json @@ -19,7 +19,7 @@ "version": { "Major": 1, "Minor": 144, - "Patch": 2 + "Patch": 3 }, "minimumAgentVersion": "2.0.0", "instanceNameFormat": "ms-resource:loc.instanceNameFormat", @@ -99,6 +99,15 @@ "helpMarkDown": "ms-resource:loc.input.help.addSpnToEnvironment", "groupName": "advanced" }, + { + "name": "useGlobalConfig", + "type": "boolean", + "label": "ms-resource:loc.input.label.useGlobalConfig", + "defaultValue": "false", + "required": false, + "helpMarkDown": "ms-resource:loc.input.help.useGlobalConfig", + "groupName": "advanced" + }, { "name": "cwd", "aliases": [ @@ -111,15 +120,6 @@ "helpMarkDown": "ms-resource:loc.input.help.cwd", "groupName": "advanced" }, - { - "name": "useGlobalConfig", - "type": "boolean", - "label": "ms-resource:loc.input.label.useGlobalConfig", - "defaultValue": "true", - "required": false, - "helpMarkDown": "ms-resource:loc.input.help.useGlobalConfig", - "groupName": "advanced" - }, { "name": "failOnStandardError", "type": "boolean", @@ -144,6 +144,7 @@ "FailedToLogout": "ms-resource:loc.messages.FailedToLogout", "LoginFailed": "ms-resource:loc.messages.LoginFailed", "ErrorInSettingUpSubscription": "ms-resource:loc.messages.ErrorInSettingUpSubscription", - "SettingAzureConfigDir": "ms-resource:loc.messages.SettingAzureConfigDir" + "SettingAzureConfigDir": "ms-resource:loc.messages.SettingAzureConfigDir", + "SettingAzureCloud": "ms-resource:loc.messages.SettingAzureCloud" } } \ No newline at end of file