Skip to content

Commit 063c6b5

Browse files
authored
Fix tfu (#27)
1 parent f0121d1 commit 063c6b5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

common/functions/terraform.ps1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,16 @@ function Get-Blobs (
391391
Write-Verbose "Environment variable ARM_ACCESS_KEY or ARM_SAS_TOKEN not set, trying az auth"
392392
$blobs = az storage blob list -c $BackendStorageContainerName --account-name $BackendStorageAccountName --auth-mode login --query $JmesPath | ConvertFrom-Json
393393
if (!$blobs) {
394-
Write-Verbose "No access to storage using KEY, SAS or SSO. Trying to obtain key..."
395-
$storageKey = az storage account keys list -n $BackendStorageAccountName --query "[?keyName=='key1'].value" -o tsv
396-
if ($storageKey) {
394+
az storage account show -n $BackendStorageAccountName --query allowSharedKeyAccess -o json | ConvertFrom-Json | Set-Variable allowSharedKeyAccess
395+
Write-Verbose "No access to storage using KEY or SAS"
396+
if ($allowSharedKeyAccess) {
397+
Write-Verbose "Trying to obtain key..."
398+
$storageKey = az storage account keys list -n $BackendStorageAccountName --query "[?keyName=='key1'].value" -o tsv
397399
$blobs = az storage blob list -c $BackendStorageContainerName --account-name $BackendStorageAccountName --account-key $storageKey --query $JmesPath | ConvertFrom-Json
398400
} else {
399-
Write-Error "Insufficient permissions (set environment variable ARM_SAS_TOKEN or ARM_ACCESS_KEY)"
400-
return
401+
Write-Verbose "Trying with Azure RBAC permission..."
402+
Write-Debug "az storage blob list -c $BackendStorageContainerName --account-name $BackendStorageAccountName --query $JmesPath"
403+
$blobs = az storage blob list -c $BackendStorageContainerName --account-name $BackendStorageAccountName --auth-mode login --query $JmesPath | ConvertFrom-Json
401404
}
402405
}
403406
}
@@ -630,6 +633,7 @@ function Unlock-TerraformState (
630633
if (!$ticks) {
631634
Write-Verbose "No access to storage using KEY, SAS or SSO. Trying to obtain key..."
632635
$storageKey = az storage account keys list -n $BackendStorageAccountName --query "[?keyName=='key1'].value" -o tsv
636+
Write-Debug "Storage Key: $storageKey"
633637
if ($storageKey) {
634638
$ticks = az storage blob lease break -b $blobName -c $backendStorageContainerName --account-name $BackendStorageAccountName --account-key $storageKey
635639
} else {

0 commit comments

Comments
 (0)