我的 Azure 应用程序 registration/service 主体凭据何时到期?
When are my Azure application registration/service principal credentials going to expire?
为了让我们的 Azure Web 应用程序可以访问 Azure Key Vault,我们使用服务主体的证书和应用程序注册。
生成证书后,我们使用以下 Azure PowerShell 创建应用程序注册和服务主体,然后授予服务主体访问 Azure Key Vault 的权限。然后,Web 应用加载此证书并使用它在 Azure Key Vault 中进行身份验证。一切正常。
$subscriptionId = Read-Host -Prompt 'SubscriptionId'
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$resourceGroupName = Read-Host -Prompt 'Resource group name'
$vaultName = Read-Host -Prompt 'Vault name'
$certificateName = Read-Host -Prompt 'Certificate name'
$applicationName = Read-Host -Prompt 'Application name'
$certificatePath = Join-Path (Get-Location) "$certificateName.cer"
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificate.Import($certificatePath)
$rawCertData = [System.Convert]::ToBase64String($certificate.GetRawCertData())
$now = [System.DateTime]::UtcNow
$application = New-AzureRmADApplication -DisplayName $applicationName -HomePage "https://$applicationName" -IdentifierUris "https://$applicationName" -CertValue $rawCertData -StartDate $now -EndDate $now.AddYears(1)
$servicePrincipal = New-AzureRmADServicePrincipal -ApplicationId $application.ApplicationId
Set-AzureRmKeyVaultAccessPolicy -ResourceGroupName $resourceGroupName -VaultName $vaultName -ServicePrincipalName "https://$applicationName" -PermissionsToSecrets get
问题出在这一行:
$application = New-AzureRmADApplication -DisplayName $applicationName -HomePage "https://$applicationName" -IdentifierUris "https://$applicationName" -CertValue $rawCertData -StartDate $now -EndDate $now.AddYears(1)
它将StartDate
和EndDate
设置为当前日期和当前日期加1年。事后看来,我认为应该是证书开始和结束日期:
$application = New-AzureRmADApplication -DisplayName $applicationName -HomePage "https://$applicationName" -IdentifierUris "https://$applicationName" -CertValue $rawCertData -StartDate` $certificate.NotBefore -EndDate $certificate.NotAfter
我的问题是 - $now.AddYears(1)
之后会发生什么? 创建的证书有效期为 3 年,但创建应用程序 registration/service 主体时使用较早的 EndDate
- 但这是什么意思?
根据文档,这是凭证的有效结束日期,因此我认为凭证将在那时停止工作。
您可以在此之前使用 New-AzureRmADAppCredential 来滚动密钥。
为了让我们的 Azure Web 应用程序可以访问 Azure Key Vault,我们使用服务主体的证书和应用程序注册。
生成证书后,我们使用以下 Azure PowerShell 创建应用程序注册和服务主体,然后授予服务主体访问 Azure Key Vault 的权限。然后,Web 应用加载此证书并使用它在 Azure Key Vault 中进行身份验证。一切正常。
$subscriptionId = Read-Host -Prompt 'SubscriptionId'
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$resourceGroupName = Read-Host -Prompt 'Resource group name'
$vaultName = Read-Host -Prompt 'Vault name'
$certificateName = Read-Host -Prompt 'Certificate name'
$applicationName = Read-Host -Prompt 'Application name'
$certificatePath = Join-Path (Get-Location) "$certificateName.cer"
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificate.Import($certificatePath)
$rawCertData = [System.Convert]::ToBase64String($certificate.GetRawCertData())
$now = [System.DateTime]::UtcNow
$application = New-AzureRmADApplication -DisplayName $applicationName -HomePage "https://$applicationName" -IdentifierUris "https://$applicationName" -CertValue $rawCertData -StartDate $now -EndDate $now.AddYears(1)
$servicePrincipal = New-AzureRmADServicePrincipal -ApplicationId $application.ApplicationId
Set-AzureRmKeyVaultAccessPolicy -ResourceGroupName $resourceGroupName -VaultName $vaultName -ServicePrincipalName "https://$applicationName" -PermissionsToSecrets get
问题出在这一行:
$application = New-AzureRmADApplication -DisplayName $applicationName -HomePage "https://$applicationName" -IdentifierUris "https://$applicationName" -CertValue $rawCertData -StartDate $now -EndDate $now.AddYears(1)
它将StartDate
和EndDate
设置为当前日期和当前日期加1年。事后看来,我认为应该是证书开始和结束日期:
$application = New-AzureRmADApplication -DisplayName $applicationName -HomePage "https://$applicationName" -IdentifierUris "https://$applicationName" -CertValue $rawCertData -StartDate` $certificate.NotBefore -EndDate $certificate.NotAfter
我的问题是 - $now.AddYears(1)
之后会发生什么? 创建的证书有效期为 3 年,但创建应用程序 registration/service 主体时使用较早的 EndDate
- 但这是什么意思?
根据文档,这是凭证的有效结束日期,因此我认为凭证将在那时停止工作。
您可以在此之前使用 New-AzureRmADAppCredential 来滚动密钥。