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

[AzureCLI] Loading service principal profile on request in azure config #8819

Merged
merged 8 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"loc.input.help.inlineScript": "You can write your scripts inline here. For batch files use the prefix \"call\" before every azure command. You can also pass predefined and custom variables to this script using arguments \n\n example for shell: az --version || az account show \n\n example for batch: call az --version || call az account show",
"loc.input.label.args": "Arguments",
"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 subscription endpoint you chose, to the script's execution environment. You can use these variables: `$servicePrincipalId` and `$servicePrincipalKey` in your script",
thesattiraju marked this conversation as resolved.
Show resolved Hide resolved
"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.failOnStandardError": "Fail on Standard Error",
Expand Down
19 changes: 18 additions & 1 deletion Tasks/AzureCLIV1/azureclitask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ export class azureclitask {
this.loginAzure();

tool.line(args); // additional args should always call line. line() parses quoted arg strings
await tool.exec({ failOnStdErr: failOnStdErr });

var addSpnToEnvironment = tl.getBoolInput("addSpnToEnvironment", false);
if (!!addSpnToEnvironment) {
await tool.exec({
failOnStdErr: failOnStdErr,
env: { ...process.env, ...{ servicePrincipalId: this.servicePrincipalId, servicePrincipalKey: this.servicePrincipalKey } }
});
}
else {
await tool.exec({ failOnStdErr: failOnStdErr });
}

}
catch (err) {
if (err.stderr) {
Expand Down Expand Up @@ -104,6 +115,9 @@ export class azureclitask {
private static cliPasswordPath: string = null;
private static azCliConfigPath: string;

private static servicePrincipalId: string = null;
private static servicePrincipalKey: string = null;

private static loginAzure() {
var connectedService: string = tl.getInput("connectedServiceNameARM", true);
this.loginAzureRM(connectedService);
Expand All @@ -124,6 +138,8 @@ export class azureclitask {
else {
tl.debug('key based endpoint');
cliPassword = tl.getEndpointAuthorizationParameter(connectedService, "serviceprincipalkey", false);
this.servicePrincipalId = servicePrincipalId;
this.servicePrincipalKey = cliPassword;
}

var tenantId: string = tl.getEndpointAuthorizationParameter(connectedService, "tenantid", false);
Expand All @@ -135,6 +151,7 @@ export class azureclitask {
//login using svn
this.throwIfError(tl.execSync("az", "login --service-principal -u \"" + servicePrincipalId + "\" -p \"" + cliPassword + "\" --tenant \"" + tenantId + "\""), tl.loc("LoginFailed"));
this.isLoggedIn = true;

//set the subscription imported to the current subscription
this.throwIfError(tl.execSync("az", "account set --subscription \"" + subscriptionID + "\""), tl.loc("ErrorInSettingUpSubscription"));
}
Expand Down
9 changes: 9 additions & 0 deletions Tasks/AzureCLIV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@
"editorExtension": "ms.vss-services-azure.parameters-grid"
}
},
{
"name": "addSpnToEnvironment",
"type": "boolean",
"label": "Access service principal details in script",
"defaultValue": "false",
"required": false,
"helpMarkDown": "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",
"groupName": "advanced"
},
{
"name": "cwd",
"aliases": [
Expand Down
11 changes: 10 additions & 1 deletion Tasks/AzureCLIV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"version": {
"Major": 1,
"Minor": 143,
"Patch": 1
"Patch": 2
},
"minimumAgentVersion": "2.0.0",
"instanceNameFormat": "ms-resource:loc.instanceNameFormat",
Expand Down Expand Up @@ -90,6 +90,15 @@
"editorExtension": "ms.vss-services-azure.parameters-grid"
}
},
{
"name": "addSpnToEnvironment",
"type": "boolean",
"label": "ms-resource:loc.input.label.addSpnToEnvironment",
"defaultValue": "false",
"required": false,
"helpMarkDown": "ms-resource:loc.input.help.addSpnToEnvironment",
"groupName": "advanced"
},
{
"name": "cwd",
"aliases": [
Expand Down