😺

Bastion を共有するときの権限設定

2023/02/10に公開

何がしたいか?

以下のようにハブアンドスポーク構成でハブとスポークで権限を分けて、かつ Bastion はハブで共有したい場合に、どのような権限設定が必要か考えます。

やりたいこと:

  • 各スポークへの仮想マシンのアクセスは、ハブの Bastion からのみ可能としたい
  • サブスクリプション A の管理者は、サブスクリプション A のリソースの作成、変更をさせたい
  • サブスクリプション A の管理者は、サブスクリプション A の仮想マシン以外にはアクセスさせたくない

設定方法

FAQ に権限に関する記載があります。
https://learn.microsoft.com/ja-jp/azure/bastion/bastion-faq#roles

仮想マシンにアクセスするには、なんらかのロールが必要ですか?
接続を作成するには、次のロールが必要です。

  • 仮想マシンに対する閲覧者ロール。
  • 仮想マシンのプライベート IP を使用する NIC に対する閲覧者ロール。
  • Azure Bastion リソースに対する閲覧者ロール。
  • ターゲット仮想マシンの仮想ネットワークに対する閲覧者ロール (Bastion デプロイが、ピアリングされた仮想ネットワーク内の場合)。

つまり最低限、サブスクリプションAの管理者に Bastion と仮想マシン(とそれに関するリソース)の閲覧者ロールを割り当てることで、Bastion から仮想マシンにアクセスできます。
逆に、それらの権限を落とすことでアクセスが出来なくなります。

今回のシナリオでは以下のように権限を設定することで実現できます。
※スコープについて、必ずしもサブスクリプション全体に権限設定する必要は無いのであくまで一例です。

ただし、リソースの権限でアクセス先を決定したい場合、IP ベースの接続は無効にしておかなければいけません。
IP ベースの接続を有効にしたままでは、権限に関係なく、IP アドレスを知っていればどこでも接続できてしまいます。

どうなるか

IP ベースの接続 を無効にすると Azure ポータルから Bastion のメニューを見ると、[接続] が表示されなくなります。従って、各仮想マシンの [Bastion] から接続することになります。

ちなみにこちらはIP ベースの接続が有効時の画面です。

コマンドラインから接続する場合も、権限がない仮想マシンへは接続できません。以下のように 403 エラーになります。

az network bastion ssh  --auth-type password --username tsunomur --name bastion -g rg-vm-0209-bastion --target-resource-id /subscriptions/dummy/resourceGroups/RG-VM-0209-A/providers/Microsoft.Compute/virtualMachines/vmubuntu
Command group 'network bastion' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Exception in thread Thread-1 (_start_tunnel):
Traceback (most recent call last):
  File "/opt/az/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/az/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/az/lib/python3.10/site-packages/azure/cli/command_modules/network/custom.py", line 8393, in _start_tunnel
    tunnel_server.start_server()
  File "/opt/az/lib/python3.10/site-packages/azure/cli/command_modules/network/tunnel.py", line 184, in start_server
    self._listen()
  File "/opt/az/lib/python3.10/site-packages/azure/cli/command_modules/network/tunnel.py", line 117, in _listen
    auth_token = self._get_auth_token()
  File "/opt/az/lib/python3.10/site-packages/azure/cli/command_modules/network/tunnel.py", line 104, in _get_auth_token
    raise exp
msrestazure.azure_exceptions.CloudError: com.microsoft.azure.CloudException: Status code 403, {"error":{"code":"AuthorizationFailed","message":"The client 'account@example.com' with object id '45eb4975-8dd4-410e-9aca-2a6a7d933947' does not have authorization to perform action 'Microsoft.Compute/virtualMachines/read' over scope '/subscriptions/dummy/resourceGroups/RG-VM-0209-A/providers/Microsoft.Compute/virtualMachines/vmubuntu' or the scope is invalid. If access was recently granted, please refresh your credentials."}}: The client 'account@example.com' with object id '45eb4975-8dd4-410e-9aca-2a6a7d933947' does not have authorization to perform action 'Microsoft.Compute/virtualMachines/read' over scope '/subscriptions/dummy/resourceGroups/RG-VM-0209-A/providers/Microsoft.Compute/virtualMachines/vmubuntu' or the scope is invalid. If access was recently granted, please refresh your credentials.
Microsoft (有志)

Discussion