NEXTSCAPE blog

株式会社ネクストスケープの社員による会社公式ブログです。ネスケラボでは、社員が日頃どのようなことに興味をもっているのか、仕事を通してどのような面白いことに取り組んでいるのかなど、会社や技術に関する情報をマイペースに紹介しています。

MENU

Azure Application Gateway に AppService 証明書 を適用時にはまったこと

こんにちは。

クライアントビジネス推進部の開發(@mamikaihatsu)です。
この記事は「NEXTSCAPE Advent Calendar 2021」の2日目です。

qiita.com

Azureで各リソースで使用するSSL証明書を、AppService 証明書 を使用して
Azure Application Gateway に適用する際に 躓いた部分があったので、記載いたします。

f:id:mamikaihatsu:20211201202730p:plain

Azure AppService 証明書

AppService マネージド 証明書 と AppService証明書がありますが
本記事では AppService 証明書 を使用します。
AppService 証明書は、ワイルドカードやサブドメインの証明書をAzureから購入する事ができ、
Azureのリソースへの適用はもちろんのこと、証明書をダウンロードして他のサービスでも利用する事が出来ます。

AppServie 証明書 の使い方

AppService証明書をKeyVaultシークレットに格納して、KeyVault経由でAzureの他リソースで証明書を使用するか
証明書をダウンロードして、Azureもしくは他サービスでアップロードして使用する
という使い方になるかと思います。

Azure Application Gateway に AppService証明書を適用

AppService証明書をKeyVaultに格納後、
他Azureリソースであれば、KeyVaultアクセスポリシーにサービスのプリンシパルを追加しますが
Application Gatewayの場合は、マネージドIDを作成しApplicationGatewayに紐づける必要があります。

マネージドIDはポータルから作成で可能です。
f:id:mamikaihatsu:20211201204758p:plain


マネージドIDを作成後、ApplicationGatewayに紐づけます。

identityName="identity-mi"
identityRg="identity-rg"
identityID=$(az identity show -n $identityName -g $identityRg -o tsv --query "id")

appgwRg="sample-rg"
appgwName="sample-apgw"

az network application-gateway identity assign \
  --gateway-name $appgwName \
  --resource-group $appgwRg \
  --identity $identityID

紐づけ後、KeyVaultに先ほど追加したマネージドIDをアクセスポリシーに
シークレットのアクセス許可:取得
の権限を付与して追加します。
f:id:mamikaihatsu:20211201194054p:plain
これで、Azure Application GatewayからKeyVaultへのアクセスが可能となりました。

問題発生

そして、Azure Application Gateway のリスナーに以下のように
AppService証明書が格納されたKeyVaultシークレットを割り当てたところ
アクセス時にブラウザ上では問題ないように見えますが、
SSLチェッカーサイトで確認すると証明書エラーがでている事が発覚しました。

## AppService証明書を取得
$kv = "samplekeyvault"
$cerName = "xxxxxx-xxxxxx"
$secret = Get-AzKeyVaultSecret -VaultName $kv -Name $cerName
$secretId = $secret.Id.Replace($secret.Version, "")

## ApplicationGateway取得
appgwRg="sample-rg"
appgwName="sample-apgw"
$appgw = Get-AzApplicationGateway -ResourceGroupName $appgwRgName -Name $appgwName

## ApplicationGatewayに証明書を追加
$gwCerName = "samplecername"
Add-AzApplicationGatewaySslCertificate -ApplicationGateway $appgw -Name $gwCerName -KeyVaultSecretId $secretId
$cert = Get-AzApplicationGatewaySSLCertificate -ApplicationGateway $appgw -Name $gwCerName

##FrontendIPConfig取得
$frontendIPConfig = "Frontend_ip_sample"
$FEC= Get-AzApplicationGatewayFrontendIPConfig -ApplicationGateway $appgw -Name $frontendIPConfig

## 443のPortの名前確認
Get-AzApplicationGatewayFrontendPort -ApplicationGateway $appgw

##FrontendPort取得
$portName = "port_443"
$port = Get-AzApplicationGatewayFrontendPort -ApplicationGateway $appgw -Name $portName

## リスナーに設定
$gwListnerName = "Listner_sample"
Set-AzApplicationGatewayHttpListener -ApplicationGateway $appgw -Name $gwListnerName -FrontendIPConfiguration $FEC -FrontendPort $port -Protocol Https -SslCertificate $cert

## 設定を更新
Set-AzApplicationGateway -ApplicationGateway $appgw

原因

SSL証明書のChainエラー。
Azure CDNなどのサービスや、ブラウザや一般的なWebサーバーにおいて、
SSL証明書の内部的な順番が異なっていても、自動的にSSL証明書のChainを入れ替える機能があるが、
Application Gateway の場合は その機能が無いため、証明書のChainを入れ替える必要があるとのこと。

解決方法

1. AppService証明書から格納されたAzure Key Vault のシークレットをダウンロード
2.PFX から PEM に変換してテキストでChain入れ替え
3.PEM から PFX に変換
4.KeyVaultシークレットは他サービスで使用していた為、変換後のPFXはKeyVault証明書にアップロードしApplication Gateway のリスナーには、KeyVault証明書で指定(ポータルから可能)。

まとめ

各サービスの証明書をAppService証明書を使用し、KeyVaultで一元管理をする予定でした。
しかしながら、Azure AppService や Azure CDN では問題はありませんが、
Application Gatewayの場合は一度ダウンロードしないといけないのは少し不便ですね。
証明書の自動更新の事も考えると、期限前にアラートを出して証明書を加工する運用は続けないといけないですが
今後のApplication GatewayのUpdateでこの辺りがクリアになるといいですね。