在Azure API管理中,有一个选项可以通过引用keyvault中的证书从门户添加证书:
使用az cli、powershell或terraform可以做到这一点吗?
我已经浏览了文档,我找到的唯一示例(包括Terraform)似乎涉及到上传证书字节的副本,而不是引用它。我希望能够引用它,这样当证书更改时,APIM将自动重新加载。
发布于 2021-03-02 06:55:51
您可以使用以下命令直接从Azure CLI
调用API Management
REST API:
az rest --method put --url "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/certificates/{certificateId}?api-version=2020-06-01-preview" --body @body.json
其中this describes the URI parameters和文件body.json
将反映request body defined here
{
"properties": {
"keyVault": {
"identityClientId": "{SystemAssignedIdentity or UserAssignedIdentity Client Id which will be used to access key vault secret.}",
"secretIdentifier" : "{Key vault secret identifier for fetching secret. Providing a versioned secret will prevent auto-refresh. This requires Api Management service to be configured with aka.ms/apimmsi}"
}
}
}
发布于 2021-03-02 03:11:57
我已经阅读了官方的Azure CLI和Azure PowerShell APIM参考,正如您所说的,它们不提供从keyVault设置证书引用的方法。但我认为我们可以从keyVault中导出.pfx,然后将其导入到APIM中作为一种解决办法。只需尝试PS命令:
$apimName = ""
$apimSresourceGroup = ""
$keyVaultName = ""
$certName = ""
$password = ""
#export pfx
$cert = Get-AzKeyVaultCertificate -VaultName $keyVaultName -Name $certName
$secret = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $cert.Name
$secretByte = [Convert]::FromBase64String($secret.SecretValueText)
$x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($secretByte, "", "Exportable,PersistKeySet")
$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx
$pfxFileByte = $x509Cert.Export($type, $password)
#import to APIM
$apim_context = New-AzApiManagementContext -ResourceGroupName $apimSresourceGroup -ServiceName $apimName
New-AzApiManagementCertificate -Context $apim_context -CertificateId 'testcert' -PfxBytes $pfxFileByte -PfxPassword $password
结果:
https://stackoverflow.com/questions/66425816
复制