NSXの分散ファイアウォールやゲートウェイファイアウォールは、初期構築の際にポリシーやルールをたくさん作成すると思いますが、GUIで1つ1つ作成していくのは骨が折れます。
また、オンプレやNSX-Vからの移行などであれば、すでに情報はまとまっているので、今回はAPIによるポリシーとルールを作成してみたいと思います。
構成
ベースのネットワーク構成はこちらになります。
NSX:4.1.2.1.0.22667794
APIツール:Postman
ポリシー情報の取得
NSX Managerの各種設定は、JSON形式で保存されているため、決められたAPIを実行することによって取得することができます。
APIの実行は、URL形式で記載し行きます。
まずは、Tier1ゲートウェイのゲートウェイファイアウォールのポリシーとルールの情報を取得してみます。
NSX Managerに接続後、[セキュリティ]ー[ゲートウェイファイアウォール]ー[ゲートウェイ固有のルール]ー[t1gw01 | Tier-1]を選択すると、デフォルトで設定されているポリシーとルールが表示されます。
これを、APIで取得してみます。
以下のURLをPostmanの「GET」と表示されている右側の空欄に入力して、「送信」をクリックします。
IPアドレス部分は、自身のNSX Manager管理IPに置き換えてください。
https://192.168.10.61/policy/api/v1/infra/domains/
実行すると画面中央に「200 OK」と表示され、JSON形式の設定情報が取得できます。
"relative_path"の「default」と「/gateway-policies」を組み合わせた「default/gateway-policies」を先ほどのURLの後ろに追記し、「送信」をクリックします。
https://192.168.10.61/policy/api/v1/infra/domains/default/gateway-policies/
実行すると、以下のような結果が出力されます。
これは、デフォルトで設定されているゲートウェイファイアウォールのポリシーとルールになります。
この情報を参考にして、新規ルールを作成していきます。
{
"results": [
{
"resource_type": "GatewayPolicy",
"id": "Policy_Default_Infra-tier0-t0gw01",
"display_name": "Policy_Default_Infra-tier0-t0gw01",
"description": "Policy_Default_Infra-tier0-t0gw01",
"path": "/infra/domains/default/gateway-policies/Policy_Default_Infra-tier0-t0gw01",
"relative_path": "Policy_Default_Infra-tier0-t0gw01",
"parent_path": "/infra/domains/default",
"remote_path": "",
"unique_id": "e0e2235b-bcbf-4f81-954b-40087c8f243b",
"realization_id": "e0e2235b-bcbf-4f81-954b-40087c8f243b",
"owner_id": "f9d28748-e56d-4a1e-9528-92b6b8df6a9b",
"marked_for_delete": false,
"overridden": false,
"sequence_number": 90999999,
"internal_sequence_number": 90999999,
"category": "Default",
"stateful": false,
"locked": false,
"lock_modified_time": 0,
"is_default": true,
"_create_time": 1706919684486,
"_create_user": "system",
"_last_modified_time": 1706919684486,
"_last_modified_user": "system",
"_system_owned": false,
"_protection": "NOT_PROTECTED",
"_revision": 0
},
{
"resource_type": "GatewayPolicy",
"id": "Policy_Default_Infra-tier1-t1gw01",
"display_name": "Policy_Default_Infra-tier1-t1gw01",
"description": "Policy_Default_Infra-tier1-t1gw01",
"path": "/infra/domains/default/gateway-policies/Policy_Default_Infra-tier1-t1gw01",
"relative_path": "Policy_Default_Infra-tier1-t1gw01",
"parent_path": "/infra/domains/default",
"remote_path": "",
"unique_id": "ff76baab-e0b8-4d87-be84-fcadf979db93",
"realization_id": "ff76baab-e0b8-4d87-be84-fcadf979db93",
"owner_id": "f9d28748-e56d-4a1e-9528-92b6b8df6a9b",
"marked_for_delete": false,
"overridden": false,
"sequence_number": 90999999,
"internal_sequence_number": 90999999,
"category": "Default",
"stateful": true,
"locked": false,
"lock_modified_time": 0,
"is_default": true,
"_create_time": 1706929780696,
"_create_user": "system",
"_last_modified_time": 1706929780696,
"_last_modified_user": "system",
"_system_owned": false,
"_protection": "NOT_PROTECTED",
"_revision": 0
}
],
"result_count": 2,
"sort_by": "display_name",
"sort_ascending": true
}
ポリシーとルールの新規作成
ポリシーだけ作成してもGUIでは表示されないため、ルールも一緒に作成していきます。
GETで取得した情報を全て記載する必要はなく、最低限以下の情報があれば、ポリシーとルールの作成は可能となります。
ルールに関しては、rurles内で、送信元、送信先、サービス、アクション、対象のT1GWを記載します。
ポリシーについては、リソースタイプ、カテゴリを記載します。
以下のコードを[Body]から[raw]を選択し、表示形式を[JSON]として貼り付けます。
{
"rules": [
{
"action": "DROP",
"resource_type": "Rule",
"display_name": "GWRule02",
"source_groups": [
"ANY"
],
"destination_groups": [
"ANY"
],
"services": [
"ANY"
],
"scope": [
"/infra/tier-1s/t1gw01"
]
}
],
"resource_type": "GatewayPolicy",
"display_name": "GWPolicy02",
"category": "LocalGatewayRules"
}
APIの実行する種類を「PUT」に変更し、以下の URL を空欄に入力して、「送信」をクリックします。
この時に、URL の最後の文字列は、ポリシー名を入力します。
https://192.168.10.61/policy/api/v1/infra/domains/default/gateway-policies/GWPolicy02
実行すると、以下のような結果が出力されます。
{
"rules": [
{
"action": "DROP",
"resource_type": "Rule",
"id": "GWRule02",
"display_name": "GWRule02",
"path": "/infra/domains/default/gateway-policies/GWPolicy02/rules/GWRule02",
"relative_path": "GWRule02",
"parent_path": "/infra/domains/default/gateway-policies/GWPolicy02",
"remote_path": "",
"unique_id": "00000000-0000-0000-0000-000000003050",
"realization_id": "00000000-0000-0000-0000-000000003050",
"owner_id": "f9d28748-e56d-4a1e-9528-92b6b8df6a9b",
"marked_for_delete": false,
"overridden": false,
"rule_id": 3050,
"sequence_number": 0,
"sources_excluded": false,
"destinations_excluded": false,
"source_groups": [
"ANY"
],
"destination_groups": [
"ANY"
],
"services": [
"ANY"
],
"profiles": [
"ANY"
],
"logged": false,
"scope": [
"/infra/tier-1s/t1gw01"
],
"disabled": false,
"direction": "IN_OUT",
"ip_protocol": "IPV4_IPV6",
"is_default": false,
"_create_time": 1710252897558,
"_create_user": "admin",
"_last_modified_time": 1710252897558,
"_last_modified_user": "admin",
"_system_owned": false,
"_protection": "NOT_PROTECTED",
"_revision": 0
}
],
"resource_type": "GatewayPolicy",
"id": "GWPolicy02",
"display_name": "GWPolicy02",
"path": "/infra/domains/default/gateway-policies/GWPolicy02",
"relative_path": "GWPolicy02",
"parent_path": "/infra/domains/default",
"remote_path": "",
"unique_id": "854328ee-d98f-443f-b0af-8a22cfcad139",
"realization_id": "854328ee-d98f-443f-b0af-8a22cfcad139",
"owner_id": "f9d28748-e56d-4a1e-9528-92b6b8df6a9b",
"marked_for_delete": false,
"overridden": false,
"sequence_number": 0,
"internal_sequence_number": 50000000,
"category": "LocalGatewayRules",
"stateful": true,
"tcp_strict": true,
"locked": false,
"lock_modified_time": 0,
"rule_count": 1,
"is_default": false,
"_create_time": 1710252897550,
"_create_user": "admin",
"_last_modified_time": 1710252897550,
"_last_modified_user": "admin",
"_system_owned": false,
"_protection": "NOT_PROTECTED",
"_revision": 0
}
また、GUI上でも作成したポリシーとルールが反映されています。
ポリシーとルールの修正
基本的に新規作成と内容は一緒になりますが、一度作成したポリシーやルールは個別に revision 管理されています。
よって、編集を行う前は、いったん GET で revision 情報を取得し、その情報を付加して PUT する形になります。
再度、以下のURLとコードでPUTを行います。
https://192.168.10.61/policy/api/v1/infra/domains/default/gateway-policies/GWPolicy02
ポリシーとルールにそれぞれ、「"_revision": 0」を追記し実行します。
{
"rules": [
{
"action": "DROP",
"display_name": "GWRule02",
"source_groups": [
"192.168.1.1"
],
"destination_groups": [
"ANY"
],
"services": [
"ANY"
],
"scope": [
"/infra/tier-1s/t1gw01"
],
"_revision": 0 ←★この行が必要となる
}
],
"resource_type": "GatewayPolicy",
"display_name": "GWPolicy02",
"category": "LocalGatewayRules",
"_revision": 0 ←★この行が必要となる
}
実行すると、以下のような結果が出力されます。
{
"rules": [
{
"action": "DROP",
"resource_type": "Rule",
"id": "GWRule02",
"display_name": "GWRule02",
"path": "/infra/domains/default/gateway-policies/GWPolicy02/rules/GWRule02",
"relative_path": "GWRule02",
"parent_path": "/infra/domains/default/gateway-policies/GWPolicy02",
"remote_path": "",
"unique_id": "00000000-0000-0000-0000-000000003050",
"realization_id": "00000000-0000-0000-0000-000000003050",
"owner_id": "f9d28748-e56d-4a1e-9528-92b6b8df6a9b",
"marked_for_delete": false,
"overridden": false,
"rule_id": 3050,
"sequence_number": 0,
"sources_excluded": false,
"destinations_excluded": false,
"source_groups": [
"192.168.1.1"
],
"destination_groups": [
"ANY"
],
"services": [
"ANY"
],
"profiles": [
"ANY"
],
"logged": false,
"scope": [
"/infra/tier-1s/t1gw01"
],
"disabled": false,
"direction": "IN_OUT",
"ip_protocol": "IPV4_IPV6",
"is_default": false,
"_create_time": 1710252897558,
"_create_user": "admin",
"_last_modified_time": 1710253089846,
"_last_modified_user": "admin",
"_system_owned": false,
"_protection": "NOT_PROTECTED",
"_revision": 1
}
],
"resource_type": "GatewayPolicy",
"id": "GWPolicy02",
"display_name": "GWPolicy02",
"path": "/infra/domains/default/gateway-policies/GWPolicy02",
"relative_path": "GWPolicy02",
"parent_path": "/infra/domains/default",
"remote_path": "",
"unique_id": "854328ee-d98f-443f-b0af-8a22cfcad139",
"realization_id": "854328ee-d98f-443f-b0af-8a22cfcad139",
"owner_id": "f9d28748-e56d-4a1e-9528-92b6b8df6a9b",
"marked_for_delete": false,
"overridden": false,
"sequence_number": 0,
"internal_sequence_number": 50000000,
"category": "LocalGatewayRules",
"stateful": true,
"locked": false,
"lock_modified_time": 0,
"rule_count": 1,
"is_default": false,
"_create_time": 1710252897550,
"_create_user": "admin",
"_last_modified_time": 1710253089845,
"_last_modified_user": "admin",
"_system_owned": false,
"_protection": "NOT_PROTECTED",
"_revision": 1
}
また、GUI上でも修正したルールが反映されています。
ポリシーとルールの削除
最後に、削除を実施します。
削除は、APIを「DELETE」に変更し、削除したいポリシーを指定すると、ルール含めて削除されます。
以下のURLを貼り付けて、作成したポリシーとルールを削除します。この時ボディは「なし」としてください。
https://192.168.10.61/policy/api/v1/infra/domains/default/gateway-policies/GWPolicy02
実行すると、以下のようなに画面右側に、「ステータス 200 OK」と赤字で表示されます。
また、GUI上でもポリシーとルールが削除されました。
以上、APIによるゲートウェイファイアウォール操作の紹介でした。