若要保護(hù) Web 服務(wù)器,可以使用安全套接字層 (SSL) 證書來加密 Web 流量。 這些 SSL 證書可存儲(chǔ)在 Azure Key Vault 中,并可安全部署到 Azure 中的 Windows 虛擬機(jī) (VM)。 本教程介紹如何執(zhí)行下列操作:
創(chuàng)建 Azure Key Vault
生成證書或?qū)⑵渖蟼鞯?Key Vault
創(chuàng)建 VM 并安裝 IIS Web 服務(wù)器
將證書注入 VM 并使用 SSL 綁定配置 IIS
本教程需要 Azure PowerShell 模塊 3.6 或更高版本。 運(yùn)行Get-Module -ListAvailable AzureRM即可查找版本。 如果需要進(jìn)行升級(jí),請參閱Install Azure PowerShell module(安裝 Azure PowerShell 模塊)。
概述
Azure Key Vault 保護(hù)加密密鑰和機(jī)密、此類證書或密碼。 Key Vault 有助于簡化證書管理過程,讓我們持續(xù)掌控用于訪問這些證書的密鑰。 可以在 Key Vault 中創(chuàng)建自簽名證書,或者上傳已擁有的現(xiàn)有受信任證書。
無需使用包含植入證書的自定義 VM 映像,而可將證書直接注入正在運(yùn)行的 VM。 此過程可確保在部署過程中,在 Web 服務(wù)器上安裝最新的證書。 如果續(xù)訂或替換了證書,也不需要?jiǎng)?chuàng)建新的自定義 VM 映像。 創(chuàng)建附加的 VM 時(shí),會(huì)自動(dòng)注入最新證書。 在整個(gè)過程中,證書永遠(yuǎn)不會(huì)離開 Azure 平臺(tái),也不會(huì)在腳本、命令行歷史記錄或模板中公開。
創(chuàng)建 Azure Key Vault
創(chuàng)建 Key Vault 和證書之前,需使用New-AzureRmResourceGroup創(chuàng)建資源組。 以下示例在“中國北部”位置創(chuàng)建名為myResourceGroupSecureWeb的資源組。
PowerShell復(fù)制
$resourceGroup="myResourceGroupSecureWeb"$location="China North"New-AzureRmResourceGroup-ResourceGroupName$resourceGroup-Location$location
接下來,使用New-AzureRmKeyVault創(chuàng)建 Key Vault。 每個(gè) Key Vault 均需具備唯一名稱且全部小寫。 將下例中的替換為自己唯一的 Key Vault 名稱:
PowerShell復(fù)制
$keyvaultName=""New-AzureRmKeyVault-VaultName$keyvaultName`-ResourceGroup$resourceGroup`-Location$location`-EnabledForDeployment
生成證書并存儲(chǔ)在 Key Vault 中
針對生產(chǎn)用途,應(yīng)使用Import-AzureKeyVaultCertificate導(dǎo)入由受信任提供程序簽名的有效證書。 在本教程中,以下示例演示了如何使用Add-AzureKeyVaultCertificate生成一個(gè)自簽名證書,該證書使用New-AzureKeyVaultCertificatePolicy指定的默認(rèn)證書策略:
PowerShell復(fù)制
$policy=New-AzureKeyVaultCertificatePolicy`-SubjectName"CN=www.contoso.com"`-SecretContentType"application/x-pkcs12"`-IssuerNameSelf `-ValidityInMonths12Add-AzureKeyVaultCertificate`-VaultName$keyvaultName`-Name"mycert"`-CertificatePolicy$policy
創(chuàng)建虛擬機(jī)
使用Get-Credential設(shè)置 VM 的管理員用戶名和密碼:
PowerShell復(fù)制
$cred=Get-Credential
現(xiàn)在,可使用New-AzureRmVM創(chuàng)建 VM。 以下示例創(chuàng)建所需的虛擬網(wǎng)絡(luò)組件、OS 配置,然后創(chuàng)建名為“myVM”的 VM:
PowerShell復(fù)制
# Create a subnet configuration$subnetConfig=New-AzureRmVirtualNetworkSubnetConfig`-NamemySubnet `-AddressPrefix192.168.1.0/24# Create a virtual network$vnet=New-AzureRmVirtualNetwork`-ResourceGroupName$resourceGroup`-Location$location`-Name"myVnet"`-AddressPrefix192.168.0.0/16`-Subnet$subnetConfig# Create a public IP address and specify a DNS name$publicIP=New-AzureRmPublicIpAddress`-ResourceGroupName$resourceGroup`-Location$location`-AllocationMethodStatic `-IdleTimeoutInMinutes4`-Name"myPublicIP"# Create an inbound network security group rule for port 3389$nsgRuleRDP=New-AzureRmNetworkSecurityRuleConfig`-Name"myNetworkSecurityGroupRuleRDP"`-Protocol"Tcp"`-Direction"Inbound"`-Priority1000`-SourceAddressPrefix* `-SourcePortRange* `-DestinationAddressPrefix* `-DestinationPortRange3389`-AccessAllow# Create an inbound network security group rule for port 443$nsgRuleWeb=New-AzureRmNetworkSecurityRuleConfig`-Name"myNetworkSecurityGroupRuleWWW"`-Protocol"Tcp"`-Direction"Inbound"`-Priority1001`-SourceAddressPrefix* `-SourcePortRange* `-DestinationAddressPrefix* `-DestinationPortRange443`-AccessAllow# Create a network security group$nsg=New-AzureRmNetworkSecurityGroup`-ResourceGroupName$resourceGroup`-Location$location`-Name"myNetworkSecurityGroup"`-SecurityRules$nsgRuleRDP,$nsgRuleWeb# Create a virtual network card and associate with public IP address and NSG$nic=New-AzureRmNetworkInterface`-Name"myNic"`-ResourceGroupName$resourceGroup`-Location$location`-SubnetId$vnet.Subnets[0].Id `-PublicIpAddressId$publicIP.Id `-NetworkSecurityGroupId$nsg.Id# Create a virtual machine configuration$vmConfig=New-AzureRmVMConfig-VMName"myVM"-VMSize"Standard_DS2"| `Set-AzureRmVMOperatingSystem-Windows-ComputerName"myVM"-Credential$cred| `Set-AzureRmVMSourceImage-PublisherName"MicrosoftWindowsServer"`-Offer"WindowsServer"-Skus"2016-Datacenter"-Version"latest"| `Add-AzureRmVMNetworkInterface-Id$nic.Id# Create virtual machineNew-AzureRmVM-ResourceGroupName$resourceGroup-Location$location-VM$vmConfig# Use the Custom Script Extension to install IISSet-AzureRmVMExtension-ResourceGroupName$resourceGroup`-ExtensionName"IIS"`-VMName"myVM"`-Location$location`-Publisher"Microsoft.Compute"`-ExtensionType"CustomScriptExtension"`-TypeHandlerVersion1.8`-SettingString'{"commandToExecute":"powershell Add-WindowsFeature Web-Server -IncludeManagementTools"}'
創(chuàng)建 VM 需要幾分鐘時(shí)間。 最后一個(gè)步驟通過Set-AzureRmVmExtension使用 Azure 自定義腳本擴(kuò)展來安裝 IIS Web 服務(wù)器。
將 Key Vault 中的證書添加到 VM
若要將 Key Vault 中的證書添加到 VM,請使用Get-AzureKeyVaultSecret獲取證書的 ID。 使用Add-AzureRmVMSecret將證書添加到 VM:
PowerShell復(fù)制
$certURL=(Get-AzureKeyVaultSecret-VaultName$keyvaultName-Name"mycert").id$vm=Get-AzureRMVM-ResourceGroupName$resourceGroup-Name"myVM"$vaultId=(Get-AzureRmKeyVault-ResourceGroupName$resourceGroup-VaultName$keyVaultName).ResourceId$vm=Add-AzureRmVMSecret-VM$vm-SourceVaultId$vaultId-CertificateStore"My"-CertificateUrl$certURLUpdate-AzureRmVM-ResourceGroupName$resourceGroup-VM$vm
將 IIS 配置為使用該證書
再次通過Set-AzureRmVMExtension使用自定義腳本擴(kuò)展來更新 IIS 配置。 此項(xiàng)更新會(huì)應(yīng)用從 Key Vault 注入 IIS 的證書,并配置 Web 綁定:
PowerShell復(fù)制
$PublicSettings='{
"fileUris":["https://raw.githubusercontent.com/iainfoulds/azure-samples/master/secure-iis.ps1"],
"commandToExecute":"powershell -ExecutionPolicy Unrestricted -File secure-iis.ps1"
}'Set-AzureRmVMExtension-ResourceGroupName$resourceGroup`-ExtensionName"IIS"`-VMName"myVM"`-Location$location`-Publisher"Microsoft.Compute"`-ExtensionType"CustomScriptExtension"`-TypeHandlerVersion1.8`-SettingString$publicSettings
測試 Web 應(yīng)用是否安全
使用Get-AzureRmPublicIPAddress獲取 VM 的公共 IP 地址。 以下示例獲取前面創(chuàng)建的myPublicIP的 IP 地址:
PowerShell復(fù)制
Get-AzureRmPublicIPAddress-ResourceGroupName$resourceGroup-Name"myPublicIP"| select"IpAddress"
現(xiàn)可打開 Web 瀏覽器,并在地址欄中輸入https://。 若要接受有關(guān)使用自簽名證書的安全警告,請依次選擇“詳細(xì)信息”和“繼續(xù)轉(zhuǎn)到網(wǎng)頁”:

隨即顯示受保護(hù)的 IIS 網(wǎng)站,如下例所示:

后續(xù)步驟
本教程已介紹如何使用 Azure Key Vault 中存儲(chǔ)的 SSL 證書保護(hù) IIS Web 服務(wù)器。 你已了解如何:
創(chuàng)建 Azure Key Vault
生成證書或?qū)⑵渖蟼鞯?Key Vault
創(chuàng)建 VM 并安裝 IIS Web 服務(wù)器
將證書注入 VM 并使用 SSL 綁定配置 IIS
請?jiān)L問以下鏈接,查看預(yù)先生成的虛擬機(jī)腳本示例。
Windows 虛擬機(jī)腳本示例? ? ? ? ?立即訪問http://market.azure.cn