Private/Authentication/Curanet/Get-CuranetCredentials.ps1
function Get-CuranetCredentials { [CmdletBinding()] param ( [Parameter(Mandatory)] [ValidateSet("3370", "3850")] [string]$Account ) # Start by connecting to Azure Key Vault $AzContext = Get-AzContext if (!$AzContext -or $AzContext.Tenant.Id -ne $script:Config.PartnerTenantId) { try { # Try certificate auth first $Certificate = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq $script:Config.CertificateThumbprint } if ($Certificate) { Write-Verbose "Using certificate authentication for Azure Key Vault access" Connect-AzAccount -ApplicationId $script:Config.ApplicationId ` -CertificateThumbprint $Certificate.Thumbprint ` -Tenant $script:Config.PartnerTenantId ` -SubscriptionName $script:Config.SubscriptionName | Out-Null } else { Write-Verbose "Using interactive authentication for Azure Key Vault access" Connect-AzAccount -Tenant $script:Config.PartnerTenantId ` -SubscriptionName $script:Config.SubscriptionName | Out-Null } } catch { Write-ModuleLog -Message "Failed to connect to Azure" -Level Error -Component 'AzureConnection' -ErrorRecord $_ } } try { # Retrieve secrets from Key Vault $CuranetClientId = Get-AzKeyVaultSecret -VaultName $script:Config.KeyVaultName -Name "curanet$($Account)apiclient" -AsPlainText $CuranetClientSecret = Get-AzKeyVaultSecret -VaultName $script:Config.KeyVaultName -Name "curanet$($Account)apisecret" -AsPlainText return [PSCustomObject]@{ Account = $Account ClientId = $CuranetClientId ClientSecret = $CuranetClientSecret } } catch { Write-ModuleLog -Message "Failed to retrieve credentials from Key Vault" -Level Error -Component 'CredentialRetrieval' -ErrorRecord $_ } } |