寻找一种自动化方法,在 1 个月前设置 Azure Keyvault 密钥过期通知,并根据 Azure Devops 管道端或自动化流程中的任何方式批准后续订过期日期。
如果您想使用Azure DevOps,您可以将其与事件网格和自动化帐户集成以满足您的需求。
对于第一步和第二步,请参阅文档中的详细步骤使用 Azure 事件网格接收和响应密钥保管库通知。
a.创建 Runbook 时,请将 Runbook 替换为以下脚本。另外,替换 {} 中的值。
param
(
[Parameter (Mandatory = $false)]
[object] $WebhookData
)
#If runbook was called from Webhook, WebhookData will not be null.
if ($WebhookData) {
#Run Azure DevOps REST API to trigger the pipeline
$token = "{Azure DevOps PAT}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/{Azure DevOps Org name}/{Project name}/_apis/pipelines/{Pipeline id}/runs?api-version=7.1-preview.1"
$body = @'
{
"stagesToSkip": [],
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/main"
}
}
},
"variables": {}
}
'@
$head = @{ Authorization =" Basic $token" }
Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $body -ContentType application/json
}
else
{
# Error
write-Error "No input data found."
}
b.当您在 KeyVault 中创建事件网格订阅时,请将“事件类型过滤器”设置为 SecretNearExpiry。
第三步:
a.创建一个没有任何资源的环境,并从批准和检查添加批准者。
b.使用以下 yaml 创建管道。
jobs:
- deployment: Deploy
displayName: deploy KeyVault
pool:
vmImage: 'windows-latest'
environment: 'KeyVault'
strategy:
runOnce:
deploy:
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: '{ARM service connection used to access your KeyVault}'
ScriptType: 'InlineScript'
Inline: |
$Expires = (Get-Date).AddYears(2).ToUniversalTime()
Update-AzKeyVaultSecret -VaultName '{Your KeyVault Name}' -Name '{Your Secret Name}' -Expires $Expires
azurePowerShellVersion: 'LatestVersion'
如果更新密钥的操作不需要批准,您可以直接使用自动化帐户的 Runbook 来更新过期时间。此外,您还可以考虑使用Azure Function应用程序。有关详细信息,请参阅自动轮换使用一组身份验证凭据的资源的密钥和自动轮换具有两组身份验证凭据的资源的密钥。