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 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
28 changes: 28 additions & 0 deletions Tasks/AzureCLIV1/azureclitask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,27 @@ 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;

// create config.json
this.createServicePrincipalsIfRequired(servicePrincipalId, cliPassword, subscriptionID);

//set the subscription imported to the current subscription
this.throwIfError(tl.execSync("az", "account set --subscription \"" + subscriptionID + "\""), tl.loc("ErrorInSettingUpSubscription"));
}

private static createServicePrincipalsIfRequired(servicePrincipalId, client_key, subscriptionID): void {
var shouldCreateServicePrincipalJSONfile = tl.getVariable("CREATE_SERVICE_PRINCIPAL_FILE");
if (!!shouldCreateServicePrincipalJSONfile) {
var fileName = "aksServicePrincipal.json";
fs.writeFileSync(
path.join(fileName),
JSON.stringify({ [subscriptionID]: { client_secret: client_key, service_principal: servicePrincipalId } })
);

fs.link(fileName, path.join(this.azCliConfigPath, fileName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you not creating the file in the eventual location path.join(this.azCliConfigPath, fileName) itself?

Suggested change
fs.link(fileName, path.join(this.azCliConfigPath, fileName));
fs.writeFileSync(path.join(this.azCliConfigPath, fileName), JSON.stringify..);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writeFileSync fails to create a new file if it is outside the application directory. And azCliConfigPath is outside it, so I had to create it here, and move it there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my knowledge, does link method move the file, or creates a link? if it just links, we would need to delete the original file as well.

}
}

private static setConfigDirectory(): void {
var configDirName: string = "c" + new Date().getTime(); // 'c' denotes config
if (tl.osType().match(/^Win/)) {
Expand All @@ -159,6 +176,17 @@ export class azureclitask {
// task should not fail if logout doesn`t occur
tl.warning(tl.loc("FailedToLogout"));
}
finally {
var createServicePrincipalJSONfile = tl.getVariable("CREATE_SERVICE_PRINCIPAL_FILE");
if (!!createServicePrincipalJSONfile) {
try {
fs.unlinkSync(path.join(this.azCliConfigPath, "aksServicePrincipal.json"))
}
catch (err) {
// no-op
}
}
}
}

private static throwIfError(resultOfToolExecution: IExecSyncResult, errormsg?: string): void {
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureCLIV1/task.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": "Azure CLI $(scriptPath)",
Expand Down
2 changes: 1 addition & 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