Skip to content

Commit f325088

Browse files
committed
Improve validation and remove set sub id
1 parent f6e345e commit f325088

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,74 @@ function Test-Tooling {
4242
}
4343

4444
# Check if using Service Principal Auth
45+
Write-Verbose "Checking Azure environment variables"
4546
$nonAzCliEnvVars = @(
4647
"ARM_CLIENT_ID",
4748
"ARM_SUBSCRIPTION_ID",
4849
"ARM_TENANT_ID"
4950
)
5051

5152
$envVarsSet = $true
53+
$envVarValid = $true
54+
$envVarUnique = $true
55+
$envVarAtLeastOneSet = $false
56+
$envVarsWithValue = @()
57+
$checkedEnvVars = @()
5258
foreach($envVar in $nonAzCliEnvVars) {
5359
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
54-
if($envVarValue -eq $null -or $envVarValue -eq "") {
60+
if($envVarValue -eq $null -or $envVarValue -eq "" ) {
5561
$envVarsSet = $false
5662
break
5763
}
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+
break
69+
}
70+
if($checkedEnvVars -contains $envVarValue) {
71+
$envVarUnique = $false
72+
break
73+
}
74+
$checkedEnvVars += $envVarValue
5875
}
5976

6077
if($envVarsSet) {
61-
Write-InformationColored "Using Service Principal Authentication, skipping Azure CLI checks" -ForegroundColor Yellow -NewLineBefore -InformationAction Continue
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 valid GUIDs."
89+
result = "Failure"
90+
}
91+
}
92+
93+
if (-not $envVarUnique) {
94+
$envVarValidationOutput = ""
95+
foreach($envVar in $nonAzCliEnvVars) {
96+
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
97+
$envVarValidationOutput += " $envVar ($envVarValue)"
98+
}
99+
$checkResults += @{
100+
message = "Azure environment variables are set, but are not unique GUIDs. There is at least one duplicate:$envVarValidationOutput."
101+
result = "Failure"
102+
}
103+
}
104+
$hasFailure = $true
62105
} else {
106+
if($envVarAtLeastOneSet) {
107+
$checkResults += @{
108+
message = "At least one environment variables is set, but the other expected environment variables are not set. This could cause Terraform to fail in unexpected ways. Set environment variables: $($envVarsWithValue -join " ")."
109+
result = "Warning"
110+
}
111+
}
112+
63113
# Check if Azure CLI is installed
64114
Write-Verbose "Checking Azure CLI installation"
65115
$azCliPath = Get-Command az -ErrorAction SilentlyContinue

0 commit comments

Comments
 (0)