ConoHaでセキュリティグループを設定する

· 3 min read
ConoHaでセキュリティグループを設定する

ConoHa では、サーバーにアクセスする手前のインフラ側で Firewall を設定できます。
さすがクラウドを謳うだけのことはあります。さくらのVPSには無いサービスですね。

しかしここで設定できるのはデフォルトで用意されたものだけです。

例えば SSH ポートは 22 番から 8022 番に変更してアタックされにくくして、
FTP は使わないので 20/21 番は閉じたい、という場合には残念ながらブラウザからは操作できません。

そこで、API です。

ConoHa には API が用意されており、自由にセキュリティグループをカスタマイズすることが出来ます。
OpenStack 準拠の API との事なので、ググればある程度の操作方法は分かりそうです。AWS でも使われてますしね。


が…

そもそも OpenStack とはなんぞね?という自分には、API を叩いて設定するなんて事が理解できず、
サクッとセキュリティグループを入れたいと思っていた自分には敷居が高すぎました。。

なので、有志の方が作ってくださったツールを使用して、サッと設定してみました。


conoha-net をつかう

conoha-net はセキュリティグループをコマンドラインから操作するツールとのこと。
ご丁寧にインストール方法や使い方は GitHub に書いていただいてます。
https://github.com/hironobu-s/conoha-net

ここからは個人的な手記として残しておきます。


インストール

今回は CentOS 6.8 の環境です。
下記コマンドを実行して、conoha-net をインストールします。

curl -sL https://github.com/hironobu-s/conoha-net/releases/download/current/conoha-net-linux.amd64.gz | zcat > conoha-net && chmod +x ./conoha-net

使い方

1. 認証

conoha-net を実行するには、API の認証情報を環境変数にセットする必要があります。
以下、export コマンドで環境変数を定義します。

export OS_USERNAME=[ユーザ名]
export OS_PASSWORD=[パスワード]
export OS_TENANT_NAME=[テナント名]
export OS_AUTH_URL=[認証用のURL]
export OS_REGION_NAME=[リージョン]

(画像: ConoHaダッシュボード – 認証情報画面)

ちなみに認証用の URL は「エンドポイント」項目にある
「Identity Service」の URL を指定すれば OK。

また、自分は東京リージョンの VPS を使用しているので、リージョンは tyo1 を指定しました。

export OS_AUTH_URL=https://identity.tyo1.conoha.io/v2.0
export OS_REGION_NAME=tyo1

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

create-group で my-group という名前のセキュリティグループを作成します。

# conoha-net create-group my-group

list-group を実行すると、作成したセキュリティグループが表示されます。
※デフォで入っているのは、IPv4/IPv6 共にアウトバウンド通信を全許可している、という意味です。

# conoha-net list-group
UUID                                     SecurityGroup     Direction     EtherType     Proto     IP Range     Port
05bb817c-5179-4156-99ec-f088ff5c5d8e     my-group          egress        IPv6          ALL                    ALL
5ecc4a23-0b92-4394-bca6-2466f08ef45e     my-group          egress        IPv4          ALL                    ALL

3. ルールを作成する

セキュリティグループにルールを追加して、フィルタリングの挙動を設定します。
これは create-rule で行います。オプションは下記。

OPTIONS:
   -d value, --direction value         (Required) The direction in which the rule applied. Must be either "ingress" or "egress" (default: "ingress")
   -e value, --ether-type value        (Required) Type of IP version. Must be either "Ipv4" or "Ipv6". (default: "IPv4")
   -p value, --port-range value        The source port or port range. For example "80", "80-8080".
   -P value, --protocol value          The IP protocol. Valid value are "tcp", "udp", "icmp" or "all". (default: "all")
   -g value, --remote-group-id value   The remote group ID to be associated with this rule.
   -i value, --remote-ip-prefix value  The IP prefix to be associated with this rule.

例えば、SSH のポートを 22 番から 8022 番に変更したい場合は以下のようにします。
-d と -e はデフォルト値があるので省略可能)

conoha-net create-rule -d ingress -e IPv4 -p 8022 -P tcp my-group

再度 list-group を実行すると、ルールが追加されていることが確認できます。

UUID                                     SecurityGroup     Direction     EtherType     Proto     IP Range     Port
05bb817c-5179-4156-99ec-f088ff5c5d8e     my-group          egress        IPv6          ALL                    ALL
5ecc4a23-0b92-4394-bca6-2466f08ef45e     my-group          egress        IPv4          ALL                    ALL
a7650cbc-b9fe-4749-aa98-e28039066ede     my-group          ingress       IPv4          tcp                    8022 - 8022

4. VPS にアタッチする

作成したセキュリティグループを一つ、もしくは複数の VPS にアタッチすることで、
その VPS に対してフィルタリングが有効になります。これには attach を使います。

conoha-net attach -n [VPS名] my-group

-n は VPS を 名前 で指定します。
他に -i(IPアドレスで指定)、--id(UUIDで指定)も利用可能です。
最後の引数は作成したセキュリティグループ名です。

list を実行すると、VPS にセキュリティグループがアタッチされたことを確認できます。

# conoha-net list
NameTag      IPv4               IPv6                                  SecurityGroups
hdserver     133.130.**.**      2400:8500:1301:739:***:***:***:***    default, default, my-group

これでセキュリティグループが完成しました。作成者様には感謝感激…!


コマンド一覧

-h オプションでヘルプが表示されます。

NAME:
  ConoHa Net - Security group management tool for ConoHa

USAGE:
  commands [global options] command [command options] [arguments...]

VERSION:
  0.1

COMMANDS:
  list          list all VPS
  attach        attach a security group to VPS
  detach        dettach a security group from VPS
  list-group    list security groups and rules
  create-group  create a security group
  delete-group  delete a security group
  create-rule   create a security group rule
  delete-rule   delete a security group rule

GLOBAL OPTIONS:
  --debug, -d                 print debug informations.
  --output value, -o value    specify output type. must be either "text" or "json". (default: "text")
  --help, -h                  show help
  --version, -v               print the version