Skip to content

Commit b6bf165

Browse files
fix: support sp auth for requirements (#282)
# Pull Request ## Description Support SP auth and skip az cli checks. Issue spotted in e2e tests. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent 97345b6 commit b6bf165

File tree

2 files changed

+107
-23
lines changed

2 files changed

+107
-23
lines changed

src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function Invoke-Terraform {
2525

2626
if ($PSCmdlet.ShouldProcess("Apply Terraform", "modify")) {
2727
# Check and Set Subscription ID
28+
$removeSubscriptionId = $false
2829
if($null -eq $env:ARM_SUBSCRIPTION_ID -or $env:ARM_SUBSCRIPTION_ID -eq "") {
2930
Write-Verbose "Setting environment variable ARM_SUBSCRIPTION_ID"
3031
$subscriptionId = $(az account show --query id -o tsv)
@@ -33,6 +34,7 @@ function Invoke-Terraform {
3334
return
3435
}
3536
$env:ARM_SUBSCRIPTION_ID = $subscriptionId
37+
$removeSubscriptionId = $true
3638
Write-Verbose "Environment variable ARM_SUBSCRIPTION_ID set to $subscriptionId"
3739
}
3840

@@ -144,6 +146,11 @@ function Invoke-Terraform {
144146
$exitCode = $LASTEXITCODE
145147
}
146148

149+
if($removeSubscriptionId) {
150+
Write-Verbose "Removing environment variable ARM_SUBSCRIPTION_ID that was set prior to this run"
151+
Remove-Item $env:ARM_SUBSCRIPTION_ID = $null
152+
}
153+
147154
# Stop and display timer
148155
$StopWatch.Stop()
149156
if(!$silent) {

src/ALZ/Private/Tools/Test-Tooling.ps1

+100-23
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,112 @@ function Test-Tooling {
4141
$hasFailure = $true
4242
}
4343

44-
# Check if Azure CLI is installed
45-
Write-Verbose "Checking Azure CLI installation"
46-
$azCliPath = Get-Command az -ErrorAction SilentlyContinue
47-
if ($azCliPath) {
48-
$checkResults += @{
49-
message = "Azure CLI is installed."
50-
result = "Success"
44+
# Check if using Service Principal Auth
45+
Write-Verbose "Checking Azure environment variables"
46+
$nonAzCliEnvVars = @(
47+
"ARM_CLIENT_ID",
48+
"ARM_SUBSCRIPTION_ID",
49+
"ARM_TENANT_ID"
50+
)
51+
52+
$envVarsSet = $true
53+
$envVarValid = $true
54+
$envVarUnique = $true
55+
$envVarAtLeastOneSet = $false
56+
$envVarsWithValue = @()
57+
$checkedEnvVars = @()
58+
foreach($envVar in $nonAzCliEnvVars) {
59+
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
60+
if($envVarValue -eq $null -or $envVarValue -eq "" ) {
61+
$envVarsSet = $false
62+
continue
5163
}
52-
} else {
53-
$checkResults += @{
54-
message = "Azure CLI is not installed. Follow the instructions here: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli"
55-
result = "Failure"
64+
$envVarAtLeastOneSet = $true
65+
$envVarsWithValue += $envVar
66+
if($envVarValue -notmatch("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")) {
67+
$envVarValid = $false
68+
continue
5669
}
57-
$hasFailure = $true
70+
if($checkedEnvVars -contains $envVarValue) {
71+
$envVarUnique = $false
72+
continue
73+
}
74+
$checkedEnvVars += $envVarValue
5875
}
5976

60-
# Check if Azure CLI is logged in
61-
Write-Verbose "Checking Azure CLI login status"
62-
$azCliAccount = $(az account show -o json) | ConvertFrom-Json
63-
if ($azCliAccount) {
64-
$checkResults += @{
65-
message = "Azure CLI is logged in. Tenant ID: $($azCliAccount.tenantId), Subscription: $($azCliAccount.name) ($($azCliAccount.id))"
66-
result = "Success"
77+
if($envVarsSet) {
78+
Write-Verbose "Using Service Principal Authentication, skipping Azure CLI checks"
79+
if($envVarValid -and $envVarUnique) {
80+
$checkResults += @{
81+
message = "Azure environment variables are set and are valid unique GUIDs."
82+
result = "Success"
83+
}
84+
}
85+
86+
if(-not $envVarValid) {
87+
$checkResults += @{
88+
message = "Azure environment variables are set, but are not all valid GUIDs."
89+
result = "Failure"
90+
}
91+
$hasFailure = $true
92+
}
93+
94+
if (-not $envVarUnique) {
95+
$envVarValidationOutput = ""
96+
foreach($envVar in $nonAzCliEnvVars) {
97+
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
98+
$envVarValidationOutput += " $envVar ($envVarValue)"
99+
}
100+
$checkResults += @{
101+
message = "Azure environment variables are set, but are not unique GUIDs. There is at least one duplicate:$envVarValidationOutput."
102+
result = "Failure"
103+
}
104+
$hasFailure = $true
67105
}
68106
} else {
69-
$checkResults += @{
70-
message = "Azure CLI is not logged in. Please login to Azure CLI using 'az login -t `"00000000-0000-0000-0000-000000000000}`"', replacing the empty GUID with your tenant ID."
71-
result = "Failure"
107+
if($envVarAtLeastOneSet) {
108+
$envVarValidationOutput = ""
109+
foreach($envVar in $envVarsWithValue) {
110+
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
111+
$envVarValidationOutput += " $envVar ($envVarValue)"
112+
}
113+
$checkResults += @{
114+
message = "At least one environment variable is set, but the other expected environment variables are not set. This could cause Terraform to fail in unexpected ways. Set environment variables:$envVarValidationOutput."
115+
result = "Warning"
116+
}
117+
}
118+
119+
# Check if Azure CLI is installed
120+
Write-Verbose "Checking Azure CLI installation"
121+
$azCliPath = Get-Command az -ErrorAction SilentlyContinue
122+
if ($azCliPath) {
123+
$checkResults += @{
124+
message = "Azure CLI is installed."
125+
result = "Success"
126+
}
127+
} else {
128+
$checkResults += @{
129+
message = "Azure CLI is not installed. Follow the instructions here: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli"
130+
result = "Failure"
131+
}
132+
$hasFailure = $true
133+
}
134+
135+
# Check if Azure CLI is logged in
136+
Write-Verbose "Checking Azure CLI login status"
137+
$azCliAccount = $(az account show -o json) | ConvertFrom-Json
138+
if ($azCliAccount) {
139+
$checkResults += @{
140+
message = "Azure CLI is logged in. Tenant ID: $($azCliAccount.tenantId), Subscription: $($azCliAccount.name) ($($azCliAccount.id))"
141+
result = "Success"
142+
}
143+
} else {
144+
$checkResults += @{
145+
message = "Azure CLI is not logged in. Please login to Azure CLI using 'az login -t `"00000000-0000-0000-0000-000000000000}`"', replacing the empty GUID with your tenant ID."
146+
result = "Failure"
147+
}
148+
$hasFailure = $true
72149
}
73-
$hasFailure = $true
74150
}
75151

76152
# Check if latest ALZ module is installed
@@ -96,6 +172,7 @@ function Test-Tooling {
96172
switch ($_.result) {
97173
'Success' { $color = "92"; break }
98174
'Failure' { $color = "91"; break }
175+
'Warning' { $color = "93"; break }
99176
default { $color = "0" }
100177
}
101178
$e = [char]27

0 commit comments

Comments
 (0)