利尻島ペシ岬より礼文島を望む


Date/Time: 2017:09:22 13:52:38
Camera: PENTAX
Model: PENTAX K-5 II s
Exporsure Time: 1/800
FNumber: 7.1
Aperture Value: 5.7
Focal Length: 21.0

Close

y2blog » WordPress KUSANAGI 環境をカスタマイズする(その3)

10

29

2017

WordPress KUSANAGI 環境をカスタマイズする(その3)

Network APIを用いて仮想基盤側Firewallの設定を変更する(つづき)


2. セキュリティグループを作成する

セキュリティグループ作成 – Network API v2.0

IPV4でのSSHアクセスポートとして、TCP/65432 を許可する場合を例に示す.先ずはセキュリティグループの作成を行う.セキュリティグループの名前(“name”)と説明(description) を設定しておく.



curl  -X POST \
 -H "Accept: application/json" \
 -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
 -d '{"security_group": { "name": "SSH-IPv4-65432", "description": "My Custom SSH port 65432" }}' \
 https://networking.tyo1.conoha.io/v2.0/security-groups \
| python -mjson.tool

コマンド実行例


iMac27:~ yasuaki$ curl  -X POST \
>  -H "Accept: application/json" \
>  -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
>  -d '{"security_group": { "name": "SSH-IPv4-65432", "description": "My Custom SSH port 65432" }}' \
>  https://networking.tyo1.conoha.io/v2.0/security-groups \
> | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   890  100   799  100    91    787     89  0:00:01  0:00:01 --:--:--   787
{
    "security_group": {
        "description": "My Custom SSH port 65432",
        "id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
        "name": "SSH-IPv4-65432",
        "security_group_rules": [
            {
                "direction": "egress",
                "ethertype": "IPv4",
                "id": "1482a536-d1f2-4944-892e-05603a3ca079",
                "port_range_max": null,
                "port_range_min": null,
                "protocol": null,
                "remote_group_id": null,
                "remote_ip_prefix": null,
                "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
                "tenant_id": "70b------------------------5061c"
            },
            {
                "direction": "egress",
                "ethertype": "IPv6",
                "id": "00b2c815-57d8-4350-82a0-c9b43125c9a9",
                "port_range_max": null,
                "port_range_min": null,
                "protocol": null,
                "remote_group_id": null,
                "remote_ip_prefix": null,
                "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
                "tenant_id": "70b------------------------5061c"
            }
        ],
        "tenant_id": "70b------------------------5061c"
    }
}
iMac27:~ yasuaki$ 

セキュリティグループを作成すると、IPv4/IPv6のoutbound(egress)が “null” なセキュリティルールが追加された状態で作成される.作成されたセキュリティグループ ID : “a57fbf35-3472-40e6-9cee-7e9dd253c9f2” がこの後のセキュリティルールの追加の際に必要になるので、この値をメモしておく.



3. セキュリティグループにセキュリティルールを追加する

セキュリティグループ ルール作成 – Network API v2.0

前のステップで作成したセキュリティグループに対し、SSH(IPv4: TCP/65432)を許可するセキュリティルールを追加する.



curl -X POST \
 -H "Accept: application/json" \
 -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
 -d '{"security_group_rule": {"direction": "ingress", "ethertype": "IPv4", "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2", "port_range_min": "65432", "port_range_max": "65432", "protocol": "tcp"}}' \
 https://networking.tyo1.conoha.io/v2.0/security-group-rules \
| python -mjson.tool

コマンド実行例


iMac27:~ yasuaki$ curl -X POST \
> -H "Accept: application/json" \
> -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
> -d '{"security_group_rule": {"direction": "ingress", "ethertype": "IPv4", "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2", "port_range_min": "65432", "port_range_max": "65432", "protocol": "tcp"}}' \
> https://networking.tyo1.conoha.io/v2.0/security-group-rules \
> | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   530  100   326  100   204    253    158  0:00:01  0:00:01 --:--:--   253
{
    "security_group_rule": {
        "direction": "ingress",
        "ethertype": "IPv4",
        "id": "864f9fea-76b3-432b-aba4-55b98d0053f2",
        "port_range_max": 65432,
        "port_range_min": 65432,
        "protocol": "tcp",
        "remote_group_id": null,
        "remote_ip_prefix": null,
        "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
        "tenant_id": "70b------------------------5061c"
    }
}
iMac27:~ yasuaki$ 

同様に、IPv6環境用のセキュリティーグループ SSH(IPv6: TCP/65432)とセキュリティルールも作成しておく.一つのセキュリティグループに対して、IPv4/IPv6両方のセキュリティルールを記述する事も可能だ.


IPv4とIPv6で全く同じセキュリティールールであれば、一つのセキュリティグループ内でIPv4とIPv6のルールをまとめておいた方が管理上は楽かもしれない.また、1ユーザがセキュリティーグループを作成する際の上限は50個(標準で備わっている “gcn-xxxx”も含む)という事なので、セキュリティーグループの数を節約したい場合は一つにまとめた方が得策だ.


今回はIPv4,Ipv6それぞれに対して一つずつのセキュリティグループを作成している.尚、IPv4しか使わないのであれば IPv6はコンソール画面でIPv6の接続許可を全て閉じて置けば良いので、この作業は不要だ.


コマンド実行例


iMac27:~ yasuaki$ curl -X POST \
> -H "Accept: application/json" \
> -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
> -d '{"security_group_rule": {"direction": "ingress", "ethertype": "IPv6", "security_group_id": "bfba80e3-eb63-46f7-b7ea-e29e83911292", "port_range_min": "65432", "port_range_max": "65432", "protocol": "tcp"}}' \
> https://networking.tyo1.conoha.io/v2.0/security-group-rules \
> | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   530  100   326  100   204    283    177  0:00:01  0:00:01 --:--:--   284
{
    "security_group_rule": {
        "direction": "ingress",
        "ethertype": "IPv6",
        "id": "94d1ba3a-1986-4197-a3e4-99cbca3ebe67",
        "port_range_max": 65432,
        "port_range_min": 65432,
        "protocol": "tcp",
        "remote_group_id": null,
        "remote_ip_prefix": null,
        "security_group_id": "bfba80e3-eb63-46f7-b7ea-e29e83911292",
        "tenant_id": "70b------------------------5061c"
    }
}
iMac27:~ yasuaki$ 

4. セキュリティグループの設定を確認する

セキュリティグループ詳細取得 – Network API v2.0


curl -X GET \
 -H "Accept: application/json" \
 -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
 https://networking.tyo1.conoha.io/v2.0/security-groups/a57fbf35-3472-40e6-9cee-7e9dd253c9f2 \
 | python -mjson.tool

コマンド実行例


iMac27:~ yasuaki$ curl -X GET \
> -H "Accept: application/json" \
> -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
> https://networking.tyo1.conoha.io/v2.0/security-groups/a57fbf35-3472-40e6-9cee-7e9dd253c9f2 \
> | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1102  100  1102    0     0   2092      0 --:--:-- --:--:-- --:--:--  2091
{
    "security_group": {
        "description": "My Custom SSH port 65432",
        "id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
        "name": "SSH-IPv4-65432",
        "security_group_rules": [
            {
                "direction": "egress",
                "ethertype": "IPv6",
                "id": "00b2c815-57d8-4350-82a0-c9b43125c9a9",
                "port_range_max": null,
                "port_range_min": null,
                "protocol": null,
                "remote_group_id": null,
                "remote_ip_prefix": null,
                "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
                "tenant_id": "70b------------------------5061c"
            },
            {
                "direction": "egress",
                "ethertype": "IPv4",
                "id": "1482a536-d1f2-4944-892e-05603a3ca079",
                "port_range_max": null,
                "port_range_min": null,
                "protocol": null,
                "remote_group_id": null,
                "remote_ip_prefix": null,
                "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
                "tenant_id": "70b------------------------5061c"
            },
            {
                "direction": "ingress",
                "ethertype": "IPv4",
                "id": "864f9fea-76b3-432b-aba4-55b98d0053f2",
                "port_range_max": 65432,
                "port_range_min": 65432,
                "protocol": "tcp",
                "remote_group_id": null,
                "remote_ip_prefix": null,
                "security_group_id": "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
                "tenant_id": "70b------------------------5061c"
            }
        ],
        "tenant_id": "70b------------------------5061c"
    }
}
iMac27:~ yasuaki$ 

これまでのセキュリティグループ、セキュリティルール設定は ConoHa ID に紐づいた共通の設定であったが、これから先の作業は個々のVPS単位での作業となる.セキュリティグループ、セキュリティルールは複数のVPSに対して同じ設定を共有させることが可能だ.


5. サーバのネットワークポートの確認

先ずは対象となるサーバのネットワークポートのIDを調べる必要がある.サーバーのネットワークポートに設定されたIPアドレスを手掛かりに、該当ポートのID(“id”)を探し出す.


ポート一覧取得 – Network API v2.0


curl -X GET \
 -H "Accept: application/json" \
 -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
 https://networking.tyo1.conoha.io/v2.0/ports \
| python -mjson.tool

コマンド実行例


iMac27:~ yasuaki$ curl -X GET \
>  -H "Accept: application/json" \
>  -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
>  https://networking.tyo1.conoha.io/v2.0/ports \
> | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1653  100  1653    0     0    904      0  0:00:01  0:00:01 --:--:--   904
{
    "ports": [
        {
            "admin_state_up": true,
            "allowed_address_pairs": [
                {
                    "ip_address": "2400:8500:1234:5678:133:100:123:210/124",
                    "mac_address": "02:01:00:88:99:ff"
                }
            ],
            "binding:vnic_type": "normal",
            "device_id": "9e7b8fb2-c60d-47c4-a3d9-7b0365fc8edc",
            "device_owner": "compute:None",
            "extra_dhcp_opts": [],
            "fixed_ips": [
                {
                    "ip_address": "133.100.123.210",
                    "subnet_id": "cbc6420c-1788-47ca-b4c3-77e893e44d2d"
                },
                {
                    "ip_address": "2400:8500:1234:5678:133:130:123:210",
                    "subnet_id": "206da8fc-abbb-45ac-a56b-c1fac6c6fd8f"
                }
            ],
            "id": "c028f256-89a3-421d-9bec-9190eab97bb5",    <=== ID 
            "mac_address": "02:00:00:88:99:ff",
            "name": "ext-i_100000-o_100000-p_0a",
            "network_id": "408bf2b4-bf7c-4a21-9a20-358905a22d7d",
            "security_groups": [
                "34daed51-a8a0-48d7-939b-895d7453b578"
            ],
            "status": "ACTIVE",
            "tenant_id": "70b------------------------5061c"
        },
        {
            "admin_state_up": true,
            "allowed_address_pairs": [
                {
                    "ip_address": "2400:8500:1234:5678:a150:100:123:211/124",
                    "mac_address": "02:01:34:50:23:87"
                }
            ],
            "binding:vnic_type": "normal",
            "device_id": "0ab8a797-be38-421c-9541-1aa9bccad116",
            "device_owner": "compute:None",
            "extra_dhcp_opts": [],
            "fixed_ips": [
                {
                    "ip_address": "150.100.123.211",
                    "subnet_id": "1b56e660-bcad-4dd6-9397-7a72359d8ed2"
                },
                {
                    "ip_address": "2400:8500:1234:5678:150:95:123:211",
                    "subnet_id": "5d1d29e6-6311-4255-b968-e302bcf15dff"
                }
            ],
            "id": "d56316b8-59a2-431d-b8f6-6cabe63da302",
            "mac_address": "02:11:89:11:23:45",
            "name": "ext-i_100000-o_100000-p_0a",
            "network_id": "76aeafcd-7f76-4852-8ac9-4c9560cb6bb8",
            "security_groups": [
                "10ce42b8-fb7c-4aad-9994-b9a65b0bd73d",
                "63ffedaf-2b6b-4d5e-9e41-71c338d9e37c",
                "8f5e3cf1-193a-407e-be77-3b0f2183324c",
                "c0881824-2561-434e-93e8-955c61d7476f"
            ],
            "status": "ACTIVE",
            "tenant_id": "70b------------------------5061c"
        }
    ]
}
iMac27:~ yasuaki$ 

ユーザIDに紐づく複数のサーバのネットワークポートの一覧が表示されるので、該当するサーバのIPアドレスを元に、該当するポートのIDを見つけ出しその値をメモする.上記の例では2台のサーバがあり、今回は上段のサーバのポート情報から ”id”: “c028f256-89a3-421d-9bec-9190eab97bb5″ が該当のポートIDだ.



6. ポートにセキュリティグループを適用する

該当するポートのIDが判明したら、次はそのポートに対して今回作成したセキュリティグループを適用する.セキュリティグループを順次追加していくことができないので、そのポートに適用したい全てのセキュリティグループを一度に全て指定しなければならない.


最初にポートに設定されているセキュリティグループの設定状況を調べておく.既に設定されているセキュリティグループをこの後で行う更新作業で指定するのを忘れてしまうと、元々設定していた通信が全て不許可になってしまうので、この作業は忘れずに実施しておく必要がある.先に実行した「ポート一覧取得」作業の結果からもポートに適用されているセキュリティグループ情報を確認することが可能だが、ポート詳細取得というAPIも用意されている.


ポート詳細取得 – Network API v2.0

ConoHa VPS では最初にVPSを作成する際に、「接続許可ポート」チェックを外した状態で作成した場合、セキュリティグループ “default” が適用される.上記のポート一覧で適用されているセキュリティグループID “34daed51-a8a0-48d7-939b-895d7453b578” はこの “default” セキュリティグループだ.


因みにVPS作成時に「接続許可ポート」の設定を何も変更しなければ、デフォルトで “全て許可” が設定されてしまう.この時のポートのセキュリティグループの設定状況は次の様になっている.



       {
            "admin_state_up": true,
            "allowed_address_pairs": [
                {
                    "ip_address": "2400:8500:1234:5678:a133:100:200:2000/124",
                    "mac_address": "02:00:89:66:70:78"
                }
            ],
            "binding:vnic_type": "normal",
            "device_id": "91ce7c68-8554-4875-b4a8-02b41da4eb0b",
            "device_owner": "compute:nova",
            "extra_dhcp_opts": [],
            "fixed_ips": [
                {
                    "ip_address": "133.130.100.200",
                    "subnet_id": "94673021-811f-4d5c-8792-189a85ed6b18"
                },
                {
                    "ip_address": "2400:8500:1234:746:133:100:200:2000",
                    "subnet_id": "38dfc9f8-72a9-4e9c-aaab-fd972da06fb8"
                }
            ],
            "id": "e7f700d1-0766-4fbb-81bb-0d836509cf2e",
            "mac_address": "02:00:89:66:70:78",
            "name": "ext-i_100000-o_100000-p_0a",
            "network_id": "f619da1e-c80d-402a-871e-8c85978fe99b",
            "security_groups": [
                "34daed51-a8a0-48d7-939b-895d7453b578",  <=== "default"
                "7745dff0-7ba5-437d-a137-768209167355",  <=== "gncs-ipv4-all"
                "d41a76a7-63f7-40d2-abd7-6e2ff98482e3"   <=== "gncs-ipv6-all"
            ],
            "status": "ACTIVE",
            "tenant_id": "70b------------------------5061c"
        }

セキュリティグループ ID “7745dff0-7ba5-437d-a137-768209167355″ は ”gncs-ipv4-all” (IPv4通信を全て許可)、”d41a76a7-63f7-40d2-abd7-6e2ff98482e3” は “gncs-ipv6-all”(IPv6通信を全て許可)だ.


この状態から、更に接続許可ポートの状態を、IPv4だけ「全て許可」⇒ OFFに、「SSH(22)」⇒ON に変更した場合のポートのセキュリティグループ設定は次の様になる.



       {
            "admin_state_up": true,
            "allowed_address_pairs": [
                {
                    "ip_address": "2400:8500:1234:5678:a133:100:200:2000/124",
                    "mac_address": "02:00:89:66:70:78"
                }
            ],
            "binding:vnic_type": "normal",
            "device_id": "91ce7c68-8554-4875-b4a8-02b41da4eb0b",
            "device_owner": "compute:nova",
            "extra_dhcp_opts": [],
            "fixed_ips": [
                {
                    "ip_address": "133.130.100.200",
                    "subnet_id": "94673021-811f-4d5c-8792-189a85ed6b18"
                },
                {
                    "ip_address": "2400:8500:1234:746:133:100:200:2000",
                    "subnet_id": "38dfc9f8-72a9-4e9c-aaab-fd972da06fb8"
                }
            ],
            "id": "e7f700d1-0766-4fbb-81bb-0d836509cf2e",
            "mac_address": "02:00:89:66:70:78",
            "name": "ext-i_100000-o_100000-p_0a",
            "network_id": "f619da1e-c80d-402a-871e-8c85978fe99b",
            "security_groups": [
                "34daed51-a8a0-48d7-939b-895d7453b578",  <=== "default"
                "be709649-a0c3-47cf-b2ba-73b8cf20e7e3",  <=== "gncs-ipv4-ssh"
                "d41a76a7-63f7-40d2-abd7-6e2ff98482e3"   <=== "gncs-ipv6-all"
            ],
            "status": "ACTIVE",
            "tenant_id": "70b------------------------5061c"
        }

上記の例からポートのセキュリティグループ設定の様子が把握できただろうか.ConaHaのAPIのドキュメントだけでは、セキュリティグループ間の関係性(適用される順番や矛盾するルールが存在する場合の振る舞いなど)が良く分からないが、とりあえず”default” セキュリティグループに穴を開けたいセキュリティグループを追加して行く方法で問題無さそうだ.この辺の挙動については詳細が分かったら別な機会に説明しようと思う.


前置きが長くなってしまったが、カスタム SSHポート許可設定のためのセキュリティグループを実際のポートに適用することにしよう.今回つけ加えるセキュリティグループは、[ “SSH-IPv4-65432″ :”a57fbf35-3472-40e6-9cee-7e9dd253c9f2” ] と [ “SSH-IPv6-65432” : “bfba80e3-eb63-46f7-b7ea-e29e83911292” ] の2つだ.


ポート更新 – Network API v2.0

コマンド例


curl -X PUT  \
 -H "Accept: application/json"  \
 -H "X-Auth-Token: 001122---- TOKEN -------eeff" \
 -d '{"port": {"security_groups": [ "34daed51-a8a0-48d7-939b-895d7453b578", "a57fbf35-3472-40e6-9cee-7e9dd253c9f2", "bfba80e3-eb63-46f7-b7ea-e29e83911292" ]}}' \
 https://networking.tyo1.conoha.io/v2.0/ports/c028f256-89a3-421d-9bec-9190eab97bb5 \
| python -mjson.tool

コマンド実行例


iMac27:~ yasuaki$ curl -X PUT   -H "Accept: application/json"   -H "X-Auth-Token: 001122---- TOKEN -------eeff"  -d '{"port": {"security_groups": [ "34daed51-a8a0-48d7-939b-895d7453b578", "a57fbf35-3472-40e6-9cee-7e9dd253c9f2", "bfba80e3-eb63-46f7-b7ea-e29e83911292" ]}}'  https://networking.tyo1.conoha.io/v2.0/ports/c028f256-89a3-421d-9bec-9190eab97bb5                     
Authentication requirediMac27:~ yasuaki$ curl -X PUT  \
>  -H "Accept: application/json"  \
>  -H "X-Auth-Token: eee6f75cb0a04268ac1af820a9216164" \
>  -d '{"port": {"security_groups": [ "34daed51-a8a0-48d7-939b-895d7453b578", "a57fbf35-3472-40e6-9cee-7e9dd253c9f2", "bfba80e3-eb63-46f7-b7ea-e29e83911292" ]}}' \
>  https://networking.tyo1.conoha.io/v2.0/ports/c028f256-89a3-421d-9bec-9190eab97bb5 \
> | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1003  100   850  100   153   1040    187 --:--:-- --:--:-- --:--:--  1040
{
    "port": {
        "admin_state_up": true,
        "allowed_address_pairs": [
            {
                "ip_address": "2400:8500:1234:5678:133:100:123:210/124",
                "mac_address": "02:01:00:88:99:ff"
            }
        ],
        "binding:vnic_type": "normal",
        "device_id": "9e7b8fb2-c60d-47c4-a3d9-7b0365fc8edc",
        "device_owner": "compute:None",
        "extra_dhcp_opts": [],
        "fixed_ips": [
            {
                "ip_address": "133.100.123.210",
                "subnet_id": "cbc6420c-1788-47ca-b4c3-77e893e44d2d"
            },
            {
                "ip_address": "2400:8500:1234:5678:133:130:123:210",
                "subnet_id": "206da8fc-abbb-45ac-a56b-c1fac6c6fd8f"
            }
        ],
        "id": "c028f256-89a3-421d-9bec-9190eab97bb5",
        "mac_address": "02:00:00:88:99:ff",
        "name": "ext-i_100000-o_100000-p_0a",
        "network_id": "408bf2b4-bf7c-4a21-9a20-358905a22d7d",
        "security_groups": [
            "34daed51-a8a0-48d7-939b-895d7453b578",
            "a57fbf35-3472-40e6-9cee-7e9dd253c9f2",
            "bfba80e3-eb63-46f7-b7ea-e29e83911292"
        ],
        "status": "ACTIVE",
        "tenant_id": "70b------------------------5061c"
    }
}
iMac27:~ yasuaki$ 

以上で、仮想基盤側でのConoHa APIを使用したSSH接続ポート番号変更作業は終了だ.この後は VPS 上のOS側のSSHポートの設定変更を行い、外部インターネット上から変更したSSHポートできちんと接続可能であることを確認しておく.


ConoHa APIを用いた仮想基盤側のFirewallをカスタマイズする作業は一筋縄では行かない面倒な作業だが、便利な機能なので使い方を覚えておいても損はないだろう.REST ベースのConoHa APIを使用する以外にも、OpenStack系のコマンドを組み合わせても操作する事が可能なので、機会があればこの辺の機能についても紹介する予定だ.参考になりそうな記事を紹介しておくので、興味の有る方はそちらを参照して欲しい.


・「個人利用から大規模開発までConoHaで始めるクラウド開発入門 : 第5回 OpenStack APIを使ったCLI操作をConoHaでやってみる」[技術評論社 Gihyo.jp]

Calendar

March 2024
S M T W T F S
 12
3456789
10111213141516
17181920212223
24252627282930
31  
  • Blogroll

  • Meta