-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathGet-CippKeyVaultSecret.ps1
More file actions
93 lines (80 loc) · 3.38 KB
/
Copy pathGet-CippKeyVaultSecret.ps1
File metadata and controls
93 lines (80 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function Get-CippKeyVaultSecret {
<#
.SYNOPSIS
Retrieves a secret from Azure Key Vault using REST API (no Az.KeyVault module required)
.DESCRIPTION
Lightweight replacement for Get-AzKeyVaultSecret that uses REST API directly.
Significantly faster as it doesn't require loading the Az.KeyVault module.
.PARAMETER VaultName
Name of the Key Vault. If not provided, derives from WEBSITE_DEPLOYMENT_ID environment variable.
.PARAMETER Name
Name of the secret to retrieve.
.PARAMETER AsPlainText
Returns the secret value as plain text instead of SecureString.
.EXAMPLE
Get-CippKeyVaultSecret -Name 'ApplicationID' -AsPlainText
.EXAMPLE
Get-CippKeyVaultSecret -VaultName 'mykeyvault' -Name 'RefreshToken' -AsPlainText
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$VaultName,
[Parameter(Mandatory = $true)]
[string]$Name,
[Parameter(Mandatory = $false)]
[switch]$AsPlainText
)
try {
# Derive vault name if not provided
if (-not $VaultName) {
$VaultName = Get-CippKeyVaultName
if (-not $VaultName) {
throw 'VaultName not provided and could not be derived (WEBSITE_SITE_NAME / WEBSITE_DEPLOYMENT_ID not set)'
}
}
# Get access token for Key Vault
$token = Get-CIPPAzIdentityToken -ResourceUrl 'https://vault.azure.net'
# Call Key Vault REST API with retry logic
$uri = "https://$VaultName.vault.azure.net/secrets/$Name`?api-version=7.4"
$maxRetries = 3
$retryDelay = 2
$response = $null
for ($i = 0; $i -lt $maxRetries; $i++) {
try {
$response = Invoke-CIPPRestMethod -Uri $uri -Headers @{
Authorization = "Bearer $token"
} -Method Get -ErrorAction Stop
break
} catch {
$lastError = $_
# 404 is definitive - the secret does not exist and retrying cannot change that
if ($_.Exception.Message -match '404|SecretNotFound') {
throw "Failed to retrieve secret '$Name' from vault '$VaultName': $($_.Exception.Message)"
}
if ($i -lt ($maxRetries - 1)) {
Start-Sleep -Seconds $retryDelay
$retryDelay *= 2 # Exponential backoff
} else {
Write-Error "Failed to retrieve secret '$Name' from vault '$VaultName' after $maxRetries attempts: $($_.Exception.Message)"
throw "Failed to retrieve secret '$Name' from vault '$VaultName' after $maxRetries attempts: $($_.Exception.Message)"
}
}
}
# Return based on AsPlainText switch
if ($AsPlainText) {
return $response.value
} else {
# Return object similar to Get-AzKeyVaultSecret for compatibility
return @{
SecretValue = ($response.value | ConvertTo-SecureString -AsPlainText -Force)
Name = $Name
VaultName = $VaultName
}
}
} catch {
# Error already handled in retry loop, just rethrow
Write-Error "CRITICAL: Key Vault secret retrieval failed: $($_.Exception.Message)"
throw "Key Vault secret retrieval failed: $($_.Exception.Message)"
}
}