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

Azure CLI Task: Added support for MSI based ARM service connections #9419

Merged
merged 6 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -16,7 +16,7 @@
"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 endpoint you chose to the script's execution environment. You can use these variables: `$servicePrincipalId` and `$servicePrincipalKey` in your 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.\n\nThis is honored only when the Azure endpoint has Service Principal authentication scheme.",
"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",
Expand Down
68 changes: 38 additions & 30 deletions Tasks/AzureCLIV1/azureclitask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ export class azureclitask {
// set az cli config dir
this.setConfigDirectory();
this.setAzureCloudBasedOnServiceEndpoint();
this.loginAzure();
var connectedService: string = tl.getInput("connectedServiceNameARM", true);
this.loginAzureRM(connectedService);

tool.line(args); // additional args should always call line. line() parses quoted arg strings

var addSpnToEnvironment = tl.getBoolInput("addSpnToEnvironment", false);
if (!!addSpnToEnvironment) {
if (!!addSpnToEnvironment && tl.getEndpointAuthorizationScheme(connectedService, true) == "ServicePrincipal") {
await tool.exec({
failOnStdErr: failOnStdErr,
env: { ...process.env, ...{ servicePrincipalId: this.servicePrincipalId, servicePrincipalKey: this.servicePrincipalKey } }
Expand Down Expand Up @@ -116,39 +117,46 @@ export class azureclitask {
private static servicePrincipalId: string = null;
private static servicePrincipalKey: string = null;

private static loginAzure() {
var connectedService: string = tl.getInput("connectedServiceNameARM", true);
this.loginAzureRM(connectedService);
}

private static loginAzureRM(connectedService: string): void {
var servicePrincipalId: string = tl.getEndpointAuthorizationParameter(connectedService, "serviceprincipalid", false);
let authType: string = tl.getEndpointAuthorizationParameter(connectedService, 'authenticationType', true);
let cliPassword: string = null;
if (authType == "spnCertificate") {
tl.debug('certificate based endpoint');
let certificateContent: string = tl.getEndpointAuthorizationParameter(connectedService, "servicePrincipalCertificate", false);
cliPassword = path.join(tl.getVariable('Agent.TempDirectory') || tl.getVariable('system.DefaultWorkingDirectory'), 'spnCert.pem');
fs.writeFileSync(cliPassword, certificateContent);
this.cliPasswordPath = cliPassword;
var authScheme: string = tl.getEndpointAuthorizationScheme(connectedService, true);
var subscriptionID: string = tl.getEndpointDataParameter(connectedService, "SubscriptionID", true);

}
else {
tl.debug('key based endpoint');
cliPassword = tl.getEndpointAuthorizationParameter(connectedService, "serviceprincipalkey", false);
this.servicePrincipalId = servicePrincipalId;
this.servicePrincipalKey = cliPassword;
}
if(authScheme == "ServicePrincipal") {
kuvinodms marked this conversation as resolved.
Show resolved Hide resolved
let authType: string = tl.getEndpointAuthorizationParameter(connectedService, 'authenticationType', true);
let cliPassword: string = null;
var servicePrincipalId: string = tl.getEndpointAuthorizationParameter(connectedService, "serviceprincipalid", false);
if (authType == "spnCertificate") {
tl.debug('certificate based endpoint');
let certificateContent: string = tl.getEndpointAuthorizationParameter(connectedService, "servicePrincipalCertificate", false);
cliPassword = path.join(tl.getVariable('Agent.TempDirectory') || tl.getVariable('system.DefaultWorkingDirectory'), 'spnCert.pem');
fs.writeFileSync(cliPassword, certificateContent);
this.cliPasswordPath = cliPassword;

var tenantId: string = tl.getEndpointAuthorizationParameter(connectedService, "tenantid", false);
var subscriptionID: string = tl.getEndpointDataParameter(connectedService, "SubscriptionID", true);
}
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);

//login using svn
this.throwIfError(tl.execSync("az", "login --service-principal -u \"" + servicePrincipalId + "\" -p \"" + cliPassword + "\" --tenant \"" + tenantId + "\""), tl.loc("LoginFailed"));
this.isLoggedIn = true;
//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"));
//set the subscription imported to the current subscription
this.throwIfError(tl.execSync("az", "account set --subscription \"" + subscriptionID + "\""), tl.loc("ErrorInSettingUpSubscription"));
kuvinodms marked this conversation as resolved.
Show resolved Hide resolved
}
else if(authScheme == "ManagedServiceIdentity") {
//login using msi
this.throwIfError(tl.execSync("az", "login --identity"), tl.loc("LoginFailed"));
kuvinodms marked this conversation as resolved.
Show resolved Hide resolved
this.isLoggedIn = true;

//set the subscription imported to the current subscription
this.throwIfError(tl.execSync("az", "account set --subscription \"" + subscriptionID + "\""), tl.loc("ErrorInSettingUpSubscription"));
kuvinodms marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static setConfigDirectory(): void {
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureCLIV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"demands": [],
"version": {
"Major": 1,
"Minor": 144,
"Minor": 147,
"Patch": 4
kuvinodms marked this conversation as resolved.
Show resolved Hide resolved
},
"minimumAgentVersion": "2.0.0",
Expand Down Expand Up @@ -97,7 +97,7 @@
"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",
"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.\n\nThis is honored only when the Azure endpoint has Service Principal authentication scheme.",
"groupName": "advanced"
},
{
Expand Down
3 changes: 2 additions & 1 deletion Tasks/AzureCLIV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"friendlyName": "ms-resource:loc.friendlyName",
"description": "ms-resource:loc.description",
"author": "Microsoft Corporation",
"helpUrl": "http://go.microsoft.com/fwlink/?LinkID=827160",
"helpMarkDown": "ms-resource:loc.helpMarkDown",
"releaseNotes": "ms-resource:loc.releaseNotes",
"category": "Deploy",
Expand All @@ -18,7 +19,7 @@
"demands": [],
"version": {
"Major": 1,
"Minor": 144,
"Minor": 147,
"Patch": 4
},
"minimumAgentVersion": "2.0.0",
Expand Down