[Azure] WebAppのアクセス制限設定をazurecliで操作する


Azure WebAppにはアクセス制限機能があり、WebAppへのアクセスをIPアドレス指定で制限することが可能です。

今回はこちらの設定をazurecliで操作する方法を調査しました。

設定に関する注意事項

  • 上記のスクリーンショットにもあるように、WebApp自体とSCMサイト(WebデプロイやKuduなどで使う管理用のサイト)は別々に制限設定をすることが可能です。これにより、WebサイトはIntranet全体に公開し、管理用のサイトはより限定した範囲にアクセスを許可するような使い方が可能になっています。オプションで「WebApp本体と同じ」に設定することも可能です。
  • WebAppのスロットを作成した場合、この設定は現在 ( 2020/6/25確認 ) スワップ対象になっていません。スロットの新規作成時のみ、オプションで設定をコピーすることができますが、その後は別管理になります 1
  • この後の実行例にも表示されているように、対象のCLIコマンドはプレビュー中のため今後変更される可能性があります。

ポータルでの手順

設定自体はストレージアカウント等と同様なので特に記載するほどの手順は無いのですが、入り口が少し分かりにくいところにあります。
WebApp → ネットワーク → アクセス制限を構成する で設定画面に遷移することができます。

azurecli手順

CLIでは az webapp config access-restriction コマンドで操作 します。順に手順を見ていきます。

設定の参照(WebApp)

az webapp config access-restriction show コマンドで設定が取得できます。リソースグループ名とWebApp名を引数として指定します。

$ az webapp config access-restriction show -g <リソースグループ名> -n <WebApp名>

実行例:

$ az webapp config access-restriction show -g test_resource_group -n xxxxxxxtest01
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
{
  "ipSecurityRestrictions": [
    {
      "action": "Allow",
      "additional_properties": {},
      "description": null,
      "ip_address": "192.168.0.0/16",
      "name": "allow1",
      "priority": 100,
      "subnet_mask": null,
      "subnet_traffic_tag": null,
      "tag": "Default",
      "vnet_subnet_resource_id": null,
      "vnet_traffic_tag": null
    },
    {
      ※デフォルトDenyルールのため省略※
    }
  ],
  "scmIpSecurityRestrictions": [
    {
      "action": "Allow",
      "additional_properties": {},
      "description": null,
      "ip_address": "192.168.1.0/24",
      "name": "allow2",
      "priority": 100,
      "subnet_mask": null,
      "subnet_traffic_tag": null,
      "tag": "Default",
      "vnet_subnet_resource_id": null,
      "vnet_traffic_tag": null
    },
    {
      ※デフォルトDenyルールのため省略※
    }
  ],
  "scmIpSecurityRestrictionsUseMain": true
}

結果中、ipSecurityRestrictions がWebAppの制限設定、scmIpSecurityRestrictions および scmIpSecurityRestrictionsUseMain がSCMサイトのアクセス制限設定です。

SCMサイトについては、scmIpSecurityRestrictionsUseMain が「WebApp本体と同じにする」設定であることに注意して下さい。

  • scmIpSecurityRestrictionsUseMain が false の場合は、 scmIpSecurityRestrictions の内容がそのまま制限設定として適用されます。
  • scmIpSecurityRestrictionsUseMain が true の場合は、 ipSecurityRestrictions の方が制限設定として適用されます。過去に false に設定して別の制限をしていた場合には、scmIpSecurityRestrictions はその時の設定であり、現在設定されている ipSecurityRestrictions の内容とは異なる場合があります。

設定の参照(スロット)

スロットの場合、-s オプションでスロット名を追加指定することにより、設定が取得できます。

$ az webapp config access-restriction show -g <リソースグループ名> -n <WebApp名> -s <スロット名>

実行例:

# スロット一覧の取得
$ az webapp deployment slot list -g test_resource_group -n xxxxxxxtest01 | jq -r '.[] .name'
dev
dev2
devtest

$ az webapp config access-restriction show -g test_resource_group -n xxxxxxxtest01 -s dev | jq -r '.ipSecurityRestrictions[] | [.name, .action, .priority, .ip_address] | @text'
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
["Allow1","Allow",100,"192.168.1.0/24"]
["Allow2","Allow",200,"192.168.2.0/24"]
["Allow3","Allow",300,"192.168.3.0/24"]
["Allow4","Allow",400,"192.168.4.0/24"]
["Allow5","Allow",500,"192.168.5.0/24"]
["Deny1","Allow",600,"192.168.6.0/24"]
["Deny2","Allow",700,"192.168.7.0/24"]
["Deny all","Deny",2147483647,"Any"]

ルールが増えると結果が長くなるので、適宜フィルタして下さい。scmIpSecurityRestrictionsUseMainscmIpSecurityRestrictions の内容はWebApp本体と同じです。

設定の追加(WebApp)

az webapp config access-restriction add コマンドで設定が追加できます。引数はリソースグループ名、WebApp名に加えてルール名、Action(AllowまたはDeny)、対象のIPレンジと優先度を引数として指定します。

$ az webapp config access-restriction add -g <リソースグループ名> -n <WebApp名> \
  --rule-name <ルール名> --action <Allow または Deny> \
  --ip-address <対象のIPレンジ> --priority <優先度>

実行例:

$ az webapp config access-restriction add -g test_resource_group -n xxxxxxxtest01 \
> --rule-name 'new-allow-rule' --action Allow \
> --ip-address '192.168.0.0/16'--priority 900
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

設定の追加(SCMサイト)

--scm-site true オプションを追加するとSCMサイトの設定が追加できます。それ以外はWebAppと同様です。

$ az webapp config access-restriction add -g <リソースグループ名> -n <WebApp名> \
  --rule-name <ルール名> --action <Allow または Deny> \
  --ip-address <対象のIPレンジ> --priority <優先度> --scm-site true

実行例:

$ az webapp config access-restriction add -g test_resource_group -n xxxxxxxtest01 \
> --rule-name 'new-allow-rule' --action Allow \
> --ip-address '192.168.0.0/16' --priority 900 --scm-site true
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

設定の追加(スロット)

参照と同様に、-s オプションでスロット名を追加指定する以外はWebAppと同様です。

$ az webapp config access-restriction add -g <リソースグループ名> -n <WebApp名> -s <スロット名> \
  --rule-name <ルール名> --action <Allow または Deny> \
  --ip-address <対象のIPレンジ> --priority <優先度>

実行例:

$ az webapp config access-restriction add -g test_resource_group -n xxxxxxxtest01 -s dev \
> --rule-name 'new-allow-rule' --action Allow \
> --ip-address '192.168.0.0/16'--priority 900
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

設定の追加(スロットのSCMサイト)

スロットと同様に-s オプションでスロット名を指定、さらに --scm-site true を付けるとスロットのSCMサイトの設定が追加できます。

$ az webapp config access-restriction add -g <リソースグループ名> -n <WebApp名> -s <スロット名> \
  --rule-name <ルール名> --action <Allow または Deny> \
  --ip-address <対象のIPレンジ> --priority <優先度> --scm-site true

実行例:

$ az webapp config access-restriction add -g test_resource_group -n xxxxxxxtest01 -s dev \
> --rule-name 'new-allow-rule' --action Allow \
> --ip-address '192.168.0.0/16'--priority 900 --scm-site true
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

SCMサイトの設定をWebAppと同じにする/別にする

参照設定で出てきた scmIpSecurityRestrictionsUseMain の設定方法です。az webapp config access-restriction set コマンドで設定できます。

$ az webapp config access-restriction set -g <リソースグループ名> -n <WebApp名> \
  --use-same-restrictions-for-scm-site <true or false>

スロットの場合は

$ az webapp config access-restriction set -g <リソースグループ名> -n <WebApp名> -s <スロット名> \
  --use-same-restrictions-for-scm-site <true or false>

実行例:

## WebAppのSCMサイト設定を同じに設定
$ az webapp config access-restriction set -g test_resource_group -n xxxxxxxtest01 \
> --use-same-restrictions-for-scm-site true
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
{
  "scmIpSecurityRestrictionsUseMain": true
}

## スロットのSCMサイト設定を別に設定
$ az webapp config access-restriction set -g test_resource_group -n xxxxxxxtest01 -s dev \
> --use-same-restrictions-for-scm-site false
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
{
  "scmIpSecurityRestrictionsUseMain": false
}

設定の削除

az webapp config access-restriction remove コマンドで設定できます。削除対象のルール名を指定します。

$ az webapp config access-restriction remove -g <リソースグループ名> -n <WebApp名> \
  --rule-name <ルール名>

スロットの場合は

$ az webapp config access-restriction remove -g <リソースグループ名> -n <WebApp名> -s <スロット名> \
  --rule-name <ルール名>

それぞれに --scm-site true オプションを追加するとSCMサイトの設定ができる点も参照・追加と同じです。

実行例:

## WebAppのルールを削除
$ az webapp config access-restriction remove -g test_resource_group -n xxxxxxxtest01 \
> --rule-name 'allow-rule'
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

## WebApp、SCMサイトのルールを削除
$ az webapp config access-restriction remove -g test_resource_group -n xxxxxxxtest01 \
> --rule-name 'allow-rule' --scm-site true
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

## スロットのルールを削除
$ az webapp config access-restriction remove -g test_resource_group -n xxxxxxxtest01 -s dev \
> --rule-name allow-rule'
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

## スロット、SCMサイトのルールを削除
$ az webapp config access-restriction remove -g test_resource_group -n xxxxxxxtest01 -s dev \
> --rule-name allow-rule' --scm-site true
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
[
  ※変更後のルールが表示される※
]

変更等の設定

現在、az webapp config access-restriction コマンドではルールの変更ができないようです。削除 → 追加をおこなって変更することになります。

ただし、現在WebAppのアクセス制限ルールではルール名が一意に制約されておらず、またNULLでも登録できてしまいます。ルール名を指定して削除した場合は、どれか1つ(おそらく最初に作った方から)削除されるようです。CLIでルールの管理を行う場合は、ルール名が一意になるよう運用ルールを決めておくのがよさそうです。

以上で、azurecliによるWebAppのアクセス制限設定が確認できました。

参考資料


  1. これが本記事を書こうと思った主な理由です。