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

Conversation

thesattiraju
Copy link
Contributor

No description provided.

//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"));
}

private static loadServicePrincipalsIfRequired(servicePrincipalId, client_key, subscriptionID): void {
var loadFileName = tl.getVariable("LOAD_SERVICE_PRINCIPAL");
Copy link
Contributor

@bansalaseem bansalaseem Nov 13, 2018

Choose a reason for hiding this comment

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

We should not expect the user to specify the file name as the file name is kind of fixed in the az cli code and expect user to specify true/false only.

Secondly LOAD word does not look right. We should change it to CREATE_SERVICE_PRINCIPAL_FILE

Thirdly if there is a logout action in the task, we should delete the file as well.

Copy link
Member

Choose a reason for hiding this comment

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

What is the scenario for this requirement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required for supporting a scenario where the user wants to use the SPN details of the serviceEndpoint while creating a new AKS cluster, instead of creating a new service principal every time.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. It also addresses #8726 . Should the secrets rather be set as secret variables instead of this file for easier consumption? Also I believe this file will get deleted after the task exists (due to logout) - is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file is a convention followed by az cli which we figured out by following the source code.
I noticed az logout doesn't explicitly delete that file, so I'm adding logic for unlinking upon exit.

Copy link
Member

Choose a reason for hiding this comment

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

If this is the already saved by az cli - then why do we need to store it again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I meant, the file is a convention for the az aks module i.e. it searches for this file in the config path, otherwise tries to create a new service-principal for AKS cluster creation.
This file is not available by default.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Given that we have requests for use cases other than aks (as pointed above), and also this file syntax is an internal implementation of aks (subject to change) - should we consider the environment variables so that this feature is more usable for all scenarios.

Copy link
Member

Choose a reason for hiding this comment

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

Azure endpoint can take client key or a certificate. Az CLI today does no support certificate yet (Azure PS does). @Ajay-MS would likely enhance Az CLI as well. Want to know if the solution proposed will scale for both client key and certificate?

Copy link
Contributor

@hiyadav hiyadav left a comment

Choose a reason for hiding this comment

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

User will not be able to discover this feature, please update the documentation as well.


// create config.json
this.loadServicePrincipalsIfRequired(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 loadServicePrincipalsIfRequired(servicePrincipalId, client_key, subscriptionID): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

rename the function as well to createServicePrincipalsIfRequired

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.

//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"));
}

private static loadServicePrincipalsIfRequired(servicePrincipalId, client_key, subscriptionID): void {
var loadFileName = tl.getVariable("LOAD_SERVICE_PRINCIPAL");
Copy link
Member

Choose a reason for hiding this comment

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

If this is the already saved by az cli - then why do we need to store it again?

@thesattiraju thesattiraju merged commit d2e31fc into master Nov 19, 2018
@thesattiraju thesattiraju deleted the users/desattir/azureCLIstoreCreds branch November 19, 2018 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants