ConoHaのAPIはOpenStack APIと互換性があるため、OpenStackクライアントを使うことができる。curlを駆使しなくてもCLIでAPI操作ができるようになるのだが、Security Groupの変更に関してはあまり記事を見かけなかったので、書き留めておく。
VPSへのSSHを許可したら(コントロールパネルでSSH (22)
にチェックマークを入れた状態)、同時にVPN接続も許可するようにしてみる。こういう操作はコントロールパネルからではできないので、APIを使う必要がある。
OpenVPNのインストールはこちらに別に書いた。
http://qiita.com/makisyu/items/2ac579c0e169970b1609
APIユーザーの作成は本家に記事があるし、
"APIを使用するためのトークンを取得する"
https://www.conoha.jp/conoben/archives/10025
VPSの起動方法なんかもコントロールパネルからできるので、今回は省略ということで。
準備
まずはクライアントをインストール:
yum install python-novaclient python-neutronclient
環境変数を設定:
export OS_TENANT_NAME=gnct12345678 # テナント名
export OS_USERNAME=gncu12345678 # APIユーザー名
export OS_PASSWORD="P@ssw0rd" # APIパスワード
export OS_AUTH_URL='https://identity.tyo1.conoha.io/v2.0' # Identity Serviceのエンドポイント
環境変数で認証情報を設定したら、ためしにVPSのリストを出してみる:
$ nova list
+--------------------------------------+----------------+---------+------------+-------------+-----------------------------------------------------------------------------+
| ID | Name | Status | Task State | Power State | Networks |
+--------------------------------------+----------------+---------+------------+-------------+-----------------------------------------------------------------------------+
| d9884683-ee1e-4efa-b185-6dd549a01210 | <instance> | SHUTOFF | - | Shutdown | ext-cnode-<snip> |
+--------------------------------------+----------------+---------+------------+-------------+-----------------------------------------------------------------------------+
Ruleの追加
現在VPSに設定されているSecurity Groupは:
$ nova show d9884683-ee1e-4efa-b185-6dd549a01210 | grep security_groups
| security_groups | gncs-ipv4-ssh
コントロールパネルのSSH (22)
は、API上ではgncs-ipv4-ssh
という名前になっている。
このgncs-ipv4-ssh
グループにOpenVPNが使うポート(udp/1194)を許可するルールを追加する。
% neutron security-group-rule-create --ethertype=IPv4 --direction ingress --protocol udp --port-range-min 1194 --port-range-max 1194 gncs-ipv4-ssh
Created a new security_group_rule:
+-------------------+--------------------------------------+
| Field | Value |
+-------------------+--------------------------------------+
| direction | ingress |
| ethertype | IPv4 |
| id | 78b8b792-6c6d-46db-a316-8caa0dfa570b |
| port_range_max | 1194 |
| port_range_min | 1194 |
| protocol | udp |
| remote_group_id | |
| remote_ip_prefix | |
| security_group_id | eebda36e-039a-4a39-934b-1a18e0feae52 |
| tenant_id | edbf458eeb8a4b16bde4850a75bb7636 |
+-------------------+--------------------------------------+
Ruleの確認
同様にneutronコマンドで確認できる:
$ neutron security-group-show gncs-ipv4-ssh
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field | Value |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| description | gncs-ipv4-ssh |
| id | eebda36e-039a-4a39-934b-1a18e0feae52 |
| name | gncs-ipv4-ssh |
| security_group_rules | {"remote_group_id": null, "direction": "egress", "remote_ip_prefix": null, "protocol": null, "tenant_id": "edbf458eeb8a4b16bde4850a75bb7636", "port_range_max": null, "security_group_id": "eebda36e-039a-4a39-934b-1a18e0feae52", "port_range_min": null, "ethertype": "IPv6", "id": "2f4968b2-b3f9-4709-91ec-bbad236cdade"} |
| | {"remote_group_id": null, "direction": "ingress", "remote_ip_prefix": null, "protocol": "udp", "tenant_id": "edbf458eeb8a4b16bde4850a75bb7636", "port_range_max": 1194, "security_group_id": "eebda36e-039a-4a39-934b-1a18e0feae52", "port_range_min": 1194, "ethertype": "IPv4", "id": "78b8b792-6c6d-46db-a316-8caa0dfa570b"} |
| | {"remote_group_id": null, "direction": "egress", "remote_ip_prefix": null, "protocol": null, "tenant_id": "edbf458eeb8a4b16bde4850a75bb7636", "port_range_max": null, "security_group_id": "eebda36e-039a-4a39-934b-1a18e0feae52", "port_range_min": null, "ethertype": "IPv4", "id": "9795040a-5170-462e-bcb6-f3a28d5623f6"} |
| | {"remote_group_id": null, "direction": "ingress", "remote_ip_prefix": null, "protocol": "tcp", "tenant_id": "edbf458eeb8a4b16bde4850a75bb7636", "port_range_max": 22, "security_group_id": "eebda36e-039a-4a39-934b-1a18e0feae52", "port_range_min": 22, "ethertype": "IPv4", "id": "c6b3e4ba-8447-4e36-aa80-042921905ca4"} |
| tenant_id | edbf458eeb8a4b16bde4850a75bb7636 |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
ちょっと見にくいが、UDPの1194番ポートへのIngressがちゃんと追加されている。
$ neutron security-group-rule-list
+--------------------------------------+----------------+-----------+----------+------------------+--------------+
| id | security_group | direction | protocol | remote_ip_prefix | remote_group |
+--------------------------------------+----------------+-----------+----------+------------------+--------------+
| 0b9e87ee-3cd8-4b2c-91b6-e47192aa18e9 | default | ingress | | | default |
| 0f8421b4-587b-45f2-921d-cf96086c5ca2 | gncs-ipv6-ssh | egress | | | |
| 2f4968b2-b3f9-4709-91ec-bbad236cdade | gncs-ipv4-ssh | egress | | | |
| 46dea242-7557-4428-a4f9-64c9a7d3c8ef | gncs-ipv6-all | ingress | | | |
| 54dcd464-6d6a-429e-a070-c097e11b8d1c | gncs-ipv4-all | egress | | | |
| 58fb5c5b-4707-4a79-9bc4-9125d0f9bc31 | gncs-ipv6-all | egress | | | |
| 60cd7b9d-215e-4574-bf88-1bee6174ce0b | gncs-ipv4-all | ingress | | | |
| 6770c7a0-f32c-4c36-836e-86a6e2258c99 | default | egress | | | |
| 78b8b792-6c6d-46db-a316-8caa0dfa570b | gncs-ipv4-ssh | ingress | udp | | |
| 9795040a-5170-462e-bcb6-f3a28d5623f6 | gncs-ipv4-ssh | egress | | | |
| b066a748-9fd9-451d-872f-7398607eecc4 | gncs-ipv6-ssh | egress | | | |
| c6b3e4ba-8447-4e36-aa80-042921905ca4 | gncs-ipv4-ssh | ingress | tcp | | |
| c835fd1c-08d9-4d89-85b5-56288e4e04b4 | default | ingress | | | default |
| cc3c63a9-1963-4fc3-a7fd-5b10b818bfc0 | default | egress | | | |
| fdd2389a-4cd1-44d8-8e8d-978482f62a2a | gncs-ipv6-all | egress | | | |
| ff1e3aa6-8b09-476b-9c97-1a77a99dcd87 | gncs-ipv6-ssh | ingress | tcp | | |
| ff582afe-4d10-4f40-9815-d059b1e2d75d | gncs-ipv4-all | egress | | | |
+--------------------------------------+----------------+-----------+----------+------------------+--------------+
これでVPSにVPN接続できるようになる。ちなみに、ルールはオンライン(VPSの再起動等が必要無い)で設定可能で、即時反映される。
まとめ
ConoHaはぱっと見では機能が少ないように感じるが、それはコントロールパネルがそういう思想で作られているからというだけであって、APIを直接叩けば大抵のことはできる。とはいえ、ConoHaのAPIはOpenStack APIと完全互換ではないため、たまにOpenStackクライアントから使えない機能があったりする。
というか、このはモードが1種類しかないというのが!最も!機能の不足を感じる!部分です!
ので、そこはConoHaさんせめて5種類くらいで!時間帯に応じて背景が切り替わるのがいいな!
深夜作業中にはこのはちゃんがコーヒー持ってきてくれる画像になる!とか!