Skip to content

Remove "magic" Service Principal support #112

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

Merged
merged 1 commit into from
Dec 11, 2018
Merged
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
38 changes: 2 additions & 36 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ internal PowerShellManager(ILogger logger)
internal void AuthenticateToAzure()
{
// Check if Az.Profile is available
Collection<PSModuleInfo> azprofile = _pwsh.AddCommand("Get-Module")
Collection<PSModuleInfo> azProfile = _pwsh.AddCommand("Get-Module")
.AddParameter("ListAvailable")
.AddParameter("Name", "Az.Profile")
.InvokeAndClearCommands<PSModuleInfo>();

if (azprofile.Count == 0)
if (azProfile.Count == 0)
{
_logger.Log(LogLevel.Trace, "Required module to automatically authenticate with Azure `Az.Profile` was not found in the PSModulePath.");
return;
Expand Down Expand Up @@ -111,40 +111,6 @@ internal void AuthenticateToAzure()
{
_logger.Log(LogLevel.Trace, "Skip authentication to Azure via MSI. Environment variables for authenticating to Azure are not present.");
}

// Try to authenticate to Azure using Service Principal
string applicationId = Environment.GetEnvironmentVariable("SERVICE_PRINCIPAL_APP_ID");
string applicationSecret = Environment.GetEnvironmentVariable("SERVICE_PRINCIPAL_APP_PASSWORD");
string tenantId = Environment.GetEnvironmentVariable("SERVICE_PRINCIPAL_TENANT_ID");

if (string.IsNullOrEmpty(applicationId) ||
string.IsNullOrEmpty(applicationSecret) ||
string.IsNullOrEmpty(tenantId))
{
_logger.Log(LogLevel.Trace, "Skip authentication to Azure via Service Principal. Environment variables for authenticating to Azure are not present.");
return;
}

// Build SecureString
var secureString = new SecureString();
foreach (char item in applicationSecret)
{
secureString.AppendChar(item);
}

using (ExecutionTimer.Start(_logger, "Authentication to Azure"))
{
_pwsh.AddCommand("Az.Profile\\Connect-AzAccount")
.AddParameter("Credential", new PSCredential(applicationId, secureString))
.AddParameter("ServicePrincipal")
.AddParameter("TenantId", tenantId)
.InvokeAndClearCommands();

if(_pwsh.HadErrors)
{
_logger.Log(LogLevel.Warning, "Failed to Authenticate to Azure via Service Principal. Check the logs for the errors generated.");
}
}
}

internal void InitializeRunspace()
Expand Down