|
| 1 | +function Get-CippKeyVaultName { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Returns the name of the CIPP Azure Key Vault for the current instance. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + The Key Vault is named after the main App Service instance, so its name equals |
| 8 | + $env:WEBSITE_SITE_NAME on the main app. |
| 9 | +
|
| 10 | + Two things have to be handled: |
| 11 | +
|
| 12 | + 1. Dashed instance names. Earlier code derived the vault name as |
| 13 | + ($env:WEBSITE_DEPLOYMENT_ID -split '-')[0], which keeps only the segment before |
| 14 | + the first dash. That silently truncated any instance name containing a dash - |
| 15 | + e.g. 'compaction-01-z2ir2' became 'compaction' - so every secret call was pointed |
| 16 | + at a vault that does not exist (404). The full name must be kept intact. |
| 17 | +
|
| 18 | + 2. Offloaded function apps. When function offloading is enabled, extra Function Apps |
| 19 | + are deployed alongside the main app and share its Key Vault. They are named |
| 20 | + '<mainname>-<suffix>' (e.g. 'compaction-01-z2ir2-standards'). Those apps must |
| 21 | + resolve the SAME vault as the main app, so a known offload suffix is stripped from |
| 22 | + the end of the site name. Only the fixed offload suffixes are stripped, never an |
| 23 | + arbitrary trailing segment, so a legitimate dashed vault name is left intact. |
| 24 | +
|
| 25 | + This is the single source of truth for the vault name so those bugs cannot reappear |
| 26 | + per call site. |
| 27 | +
|
| 28 | + .EXAMPLE |
| 29 | + $VaultName = Get-CippKeyVaultName |
| 30 | + #> |
| 31 | + [CmdletBinding()] |
| 32 | + [OutputType([string])] |
| 33 | + param() |
| 34 | + |
| 35 | + # Primary: the App Service site name IS the vault name (on the main app). |
| 36 | + # Fallback: the full deployment id. Never split on '-' (that is the truncation bug this |
| 37 | + # helper exists to prevent); a dashed vault name must be kept whole. |
| 38 | + $Name = if (-not [string]::IsNullOrWhiteSpace($env:WEBSITE_SITE_NAME)) { |
| 39 | + $env:WEBSITE_SITE_NAME |
| 40 | + } elseif (-not [string]::IsNullOrWhiteSpace($env:WEBSITE_DEPLOYMENT_ID)) { |
| 41 | + $env:WEBSITE_DEPLOYMENT_ID |
| 42 | + } else { |
| 43 | + return $null |
| 44 | + } |
| 45 | + |
| 46 | + # If running on an offloaded app ('<mainname>-<suffix>'), strip the known suffix so it |
| 47 | + # resolves the SAME vault as the main app. Get-CippOffloadSuffix is the single source of |
| 48 | + # truth for the suffix list. |
| 49 | + $Suffix = Get-CippOffloadSuffix -SiteName $Name |
| 50 | + if ($Suffix) { |
| 51 | + $Name = $Name -replace "-$([regex]::Escape($Suffix))$", '' |
| 52 | + } |
| 53 | + |
| 54 | + return $Name |
| 55 | +} |
0 commit comments