Skip to content

Commit 9a32965

Browse files
fix(auth): resolve domain TenantID to GUID on load
If the TenantID secret is set to a domain name instead of a GUID, resolve it via the OpenID metadata endpoint and update the stored secret (DevSecrets table or Key Vault) so subsequent loads use the GUID directly. Synced from CyberDrain/CIPP@6d65d86
1 parent 5f4631f commit 9a32965

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

Modules/CIPPCore/Public/Get-CIPPAuthentication.ps1

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function Get-CIPPAuthentication {
88
$Variables = @('ApplicationID', 'ApplicationSecret', 'TenantID', 'RefreshToken')
99

1010
try {
11-
if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true' -or $env:NonLocalHostAzurite -eq 'true') {
11+
$IsDevMode = $env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true' -or $env:NonLocalHostAzurite -eq 'true'
12+
if ($IsDevMode) {
1213
$Table = Get-CIPPTable -tablename 'DevSecrets'
1314
$Secret = Get-AzDataTableEntity @Table -Filter "PartitionKey eq 'Secret' and RowKey eq 'Secret'"
1415
if (!$Secret) {
@@ -26,6 +27,45 @@ function Get-CIPPAuthentication {
2627
Set-Item -Path env:$_ -Value (Get-CippKeyVaultSecret -VaultName $keyvaultname -Name $_ -AsPlainText -ErrorAction Stop) -Force
2728
}
2829
}
30+
# TenantID must be the tenant GUID: a domain name (contoso.onmicrosoft.com)
31+
# works for token requests but breaks API integrations that compare or store
32+
# tenant ids. Resolve a domain to its GUID via the unauthenticated OpenID
33+
# metadata endpoint.
34+
$GuidPattern = '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$'
35+
if ($env:TenantID -and $env:TenantID -notmatch $GuidPattern) {
36+
$StoredTenantID = $env:TenantID
37+
try {
38+
$OpenIdConfig = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$StoredTenantID/v2.0/.well-known/openid-configuration" -ErrorAction Stop
39+
$ResolvedTenantID = ($OpenIdConfig.issuer -split '/')[3]
40+
if ($ResolvedTenantID -notmatch $GuidPattern) {
41+
throw "OpenID metadata for '$StoredTenantID' did not contain a tenant GUID (issuer: $($OpenIdConfig.issuer))"
42+
}
43+
$env:TenantID = $ResolvedTenantID
44+
Write-LogMessage -message "The TenantID secret is set to domain name '$StoredTenantID' - resolved to tenant GUID $ResolvedTenantID." -Sev 'Warning' -API 'CIPP Authentication'
45+
46+
# Fix the stored secret so every future load gets the GUID directly.
47+
# Best-effort: this session already has the resolved value.
48+
if ($IsDevMode) {
49+
try {
50+
$Secret | Add-Member -MemberType NoteProperty -Name 'TenantID' -Value $ResolvedTenantID -Force
51+
$null = Add-AzDataTableEntity @Table -Entity $Secret -Force
52+
Write-LogMessage -message "Updated the TenantID in the DevSecrets table from '$StoredTenantID' to tenant GUID $ResolvedTenantID." -Sev 'Info' -API 'CIPP Authentication'
53+
} catch {
54+
Write-LogMessage -message 'Could not update the TenantID in the DevSecrets table - it will be re-resolved on every authentication load.' -Sev 'Warning' -API 'CIPP Authentication' -LogData (Get-CippException -Exception $_)
55+
}
56+
} elseif ($keyvaultname) {
57+
try {
58+
$null = Set-CippKeyVaultSecret -VaultName $keyvaultname -Name 'TenantID' -SecretValue (ConvertTo-SecureString -String $ResolvedTenantID -AsPlainText -Force) -ErrorAction Stop
59+
Write-LogMessage -message "Updated the 'TenantID' Key Vault secret from '$StoredTenantID' to tenant GUID $ResolvedTenantID." -Sev 'Info' -API 'CIPP Authentication'
60+
} catch {
61+
Write-LogMessage -message "Could not update the 'TenantID' Key Vault secret to the tenant GUID - it will be re-resolved on every authentication load until the secret is updated manually." -Sev 'Warning' -API 'CIPP Authentication' -LogData (Get-CippException -Exception $_)
62+
}
63+
}
64+
} catch {
65+
Write-LogMessage -message "The TenantID secret ('$StoredTenantID') is not a GUID and could not be resolved to one. API integrations may misbehave until the 'tenantid' Key Vault secret is set to the tenant GUID." -Sev 'Error' -API 'CIPP Authentication' -LogData (Get-CippException -Exception $_)
66+
}
67+
}
68+
2969
# Set before certificate handling: Update-CIPPSAMCertificate goes through
3070
# Get-GraphToken, which re-enters this function when SetFromProfile is unset
3171
$env:SetFromProfile = $true
@@ -34,7 +74,7 @@ function Get-CIPPAuthentication {
3474
# when it does not exist yet. Non-fatal: auth must succeed even when certificate
3575
# handling fails; the weekly token update retries provisioning.
3676
try {
37-
if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true' -or $env:NonLocalHostAzurite -eq 'true') {
77+
if ($IsDevMode) {
3878
if ($Secret.SAMCertificate) {
3979
$env:SAMCertificate = $Secret.SAMCertificate
4080
}

0 commit comments

Comments
 (0)