你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
计算机配置自定义策略使用 SHA256 哈希来验证策略包是否没有更改。 客户还可以选择使用证书对包进行签名,并强制计算机配置扩展只允许已签名的内容。
若要启用此方案,需要完成两个步骤:
- 运行 cmdlet 对内容包进行签名。
- 将一个标记追加到要求对代码进行签名的计算机。
使用代码签名证书的签名验证
若要使用签名验证功能,请运行 Protect-GuestConfigurationPackage cmdlet,以在发布前对包进行签名。 此 cmdlet 需要“代码签名”证书。 如果没有“代码签名”证书,请使用下面的脚本创建自签名证书,以按照该示例进行测试。
Windows 签名验证
# How to create a self sign cert and use it to sign Machine Configuration
# custom policy package
# Create Code signing cert
$codeSigningParams = @{
    Type          = 'CodeSigningCert'
    DnsName       = 'GCEncryptionCertificate'
    HashAlgorithm = 'SHA256'
}
$certificate = New-SelfSignedCertificate @codeSigningParams
# Export the certificates
$privateKey = @{
    Cert     = $certificate
    Password = Read-Host "Enter password for private key" -AsSecureString
    FilePath = '<full-path-to-export-private-key-pfx-file>'
}
$publicKey = @{
    Cert     = $certificate
    FilePath = '<full-path-to-export-public-key-cer-file>'
    Force    = $true
}
Export-PfxCertificate @privateKey
Export-Certificate    @publicKey
# Import the certificate
$importParams = @{
    FilePath          = $privateKey.FilePath
    Password          = $privateKey.Password
    CertStoreLocation = 'Cert:\LocalMachine\My'
}
Import-PfxCertificate @importParams
# Sign the policy package
$certToSignThePackage = Get-ChildItem -Path Cert:\LocalMachine\My |
    Where-Object { $_.Subject -eq "CN=GCEncryptionCertificate" }
$protectParams = @{
    Path        = '<path-to-package-to-sign>'
    Certificate = $certToSignThePackage
    Verbose     = $true
}
Protect-GuestConfigurationPackage @protectParams
Linux 签名验证
# generate gpg key
gpg --gen-key
$emailAddress      = '<email-id-used-to-generate-gpg-key>'
$publicGpgKeyPath  = '<full-path-to-export-public-key-gpg-file>'
$privateGpgKeyPath = '<full-path-to-export-private-key-gpg-file>'
# export public key
gpg --output $publicGpgKeyPath --export $emailAddress
# export private key
gpg --output $privateGpgKeyPath --export-secret-key $emailAddress
# Sign linux policy package
Import-Module GuestConfiguration
$protectParams = @{
    Path              = '<path-to-package-to-sign>'
    PrivateGpgKeyPath = $privateGpgKeyPath
    PublicGpgKeyPath  = $publicGpgKeyPath
    Verbose           = $true
}
Protect-GuestConfigurationPackage
              Protect-GuestConfigurationPackage cmdlet 的参数:
- 路径:计算机配置包的完整路径。
- Certificate:用于对包进行签名的代码签名证书。 只有在对 Windows 内容进行签名时,才支持此参数。
- 
              PrivateGpgKeyPath:私钥 .gpg文件的完整路径。 只有在对 Linux 内容进行签名时,才支持此参数。
- 
              PublicGpgKeyPath:公钥 .gpg文件的完整路径。 只有在对 Linux 内容进行签名时,才支持此参数。
证书要求
计算机配置代理要求证书公钥存在于 Windows 计算机上的“受信任发布者”和 Linux 计算机上的 /usr/local/share/ca-certificates/gc 路径中。 为了让节点能够验证已签名的内容,请先在计算机上安装证书公钥,再应用自定义策略。
可以使用 VM 中的一般工具或使用 Azure Policy 安装证书公钥。 使用 Azure Policy 的示例模板演示了如何使用证书部署计算机。 Key Vault 访问策略必须允许计算资源提供程序在部署过程中访问证书。 有关详细步骤,请参阅在 Azure 资源管理器中为虚拟机设置 Key Vault。
下面是从签名证书导出公钥以导入计算机的示例。
$Cert = Get-ChildItem -Path Cert:\LocalMachine\My |
    Where-Object { $_.Subject-eq 'CN=<CN-of-your-signing-certificate>' } |
    Select-Object -First 1
$Cert | Export-Certificate -FilePath '<path-to-export-public-key-cer-file>' -Force
标记要求
在内容发布后,将名为 GuestConfigPolicyCertificateValidation 且值为 enabled 的标记追加到所有应需要进行代码签名的虚拟机。 请参阅标记示例,了解如何使用 Azure Policy 大规模传递标记。 在此标记就位后,使用 New-GuestConfigurationPolicy cmdlet 生成的策略定义通过计算机配置扩展启用要求。
相关内容
- 使用 GuestConfiguration模块创建 Azure Policy 定义,用于对环境进行大规模管理。
- 使用 Azure 门户分配自定义策略定义。
- 了解如何查看计算机配置策略分配的合规性详细信息。