你当前正在访问 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'
}
$mycert = New-SelfSignedCertificate @codeSigningParams

# Export the certificates
$mypwd = ConvertTo-SecureString -String "Password1234" -Force -AsPlainText
$mycert | Export-PfxCertificate -FilePath C:\demo\GCPrivateKey.pfx -Password $mypwd
$mycert | Export-Certificate -FilePath "C:\demo\GCPublicKey.cer" -Force

# Import the certificate
$importParams = @{
    FilePath          = 'C:\demo\GCPrivateKey.pfx'
    Password          = $mypwd
    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        = 'C:\demo\AuditWindowsService.zip'
    Certificate = $certToSignThePackage
    Verbose     = $true
}
Protect-GuestConfigurationPackage @protectParams

Linux 签名验证

# generate gpg key
gpg --gen-key

# export public key
gpg --output public.gpg --export <email-id-used-to-generate-gpg-key>

# export private key
gpg --output private.gpg --export-secret-key <email-id-used-to-generate-gpg-key>

# Sign linux policy package
Import-Module GuestConfiguration
$protectParams = @{
    Path              = './not_installed_application_linux.zip'
    PrivateGpgKeyPath = './private.gpg'
    PublicGpgKeyPath  = './public.gpg'
    Verbose           = $true
}
Protect-GuestConfigurationPackage

Protect-GuestConfigurationPackage cmdlet 的参数:

  • 路径:计算机配置包的完整路径。
  • Certificate:用于对包进行签名的代码签名证书。 只有在对 Windows 内容进行签名时,才支持此参数。

证书要求

计算机配置代理要求证书公钥存在于 Windows 计算机上的“受信任发布者”和 Linux 计算机上的 /usr/local/share/ca-certificates/gc 路径中。 为了让节点能够验证已签名的内容,请先在计算机上安装证书公钥,再应用自定义策略。 可以使用 VM 内的任何技术或使用 Azure Policy 来完成此过程。 示例模板可用于部署具有证书的计算机。 Key Vault 访问策略必须允许计算资源提供程序在部署过程中访问证书。 有关详细步骤,请参阅在 Azure 资源管理器中为虚拟机设置 Key Vault

下面是从签名证书导出公钥以导入计算机的示例。

$Cert = Get-ChildItem -Path cert:\LocalMachine\My |
    Where-Object { $_.Subject-eq "CN=mycert3" } |
    Select-Object -First 1
$Cert | Export-Certificate -FilePath "$env:temp\DscPublicKey.cer" -Force

标记要求

在内容发布后,将名为 GuestConfigPolicyCertificateValidation 且值为 enabled 的标记追加到所有应需要进行代码签名的虚拟机。 请参阅标记示例,了解如何使用 Azure Policy 大规模传递标记。 在此标记就位后,使用 New-GuestConfigurationPolicy cmdlet 生成的策略定义通过计算机配置扩展启用要求。