0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[OCI]OCI CLIとシェルスクリプトを使用して複数のCIDRに対するイングレス・ルールをまとめて追加してみた

Last updated at Posted at 2022-02-11

はじめに

OCIコンソールからセキュリティ・リストにセキュリティ・ルールを追加する際、1つのセキュリティ・ルールに複数のCIDRを指定することはできないため、アクセスを許可するCIDR毎にセキュリティ・ルールを追加する手順を行う必要があります。

そこで、多数のCIDRへのアクセス許可を効率的に行うシェルスクリプトを作成しました。

なお、こちらのスクリプトを実行するには、事前にOCI CLIの設定を済ませておく必要があります。

また、OCI CLIの仕様で、セキュリティ・リスト内の既存のイングレス・ルールは削除され、新しいイングレス・ルールで置き換えられますのでご注意ください。

1.アクセスを許可するCIDRリストの作成

アクセスを許可するCIDRリストのファイル"cidrlist.txt"を作成します。
※1つのセキュリティ・リストあたりのイングレス・ルールは最大200となっていますので、200行以下で作成します。

cidrlist.txt
10.1.0.0/16
10.2.0.0/16
10.3.0.0/16
10.4.0.0/16
10.5.0.0/16
10.6.0.0/16
10.7.0.0/16
10.8.0.0/16
10.9.0.0/16
10.10.0.0/16

2.追加するセキュリティ・ルールのJSONファイルを作成

セキュリティ・リストに追加するセキュリティ・ルール設定のベースとなるjsonファイル"base.json"を作成します。
ここでは例として、
ステートレス:オフ(ステートフル)
プロトコル:TCP
ソースタイプ:CIDRブロック
ソースポート:1522
宛先ポート:1522
といった内容のセキュリティ・ルールを設定する想定です。

シェルスクリプトの実行時に、base.json内の"_SOURCE_"をcidrlist.txtの各行のCIDRで置き換えます。

base.json
{
    "isStateless": false,
    "protocol": "6",
    "source": "_SOURCE_",
    "sourceType": "CIDR_BLOCK",
    "tcpOptions": {
      "destinationPortRange": {
        "max": 1522,
        "min": 1522
      },
      "sourcePortRange": {
        "max": 1522,
        "min": 1522
      }
    }
}

3.シェルスクリプトの作成

実行時に引数としてセキュリティ・リストのOCIDを指定すると、cidrlist.txtに記述したCIDRに対して、base.jsonに記述したセキュリティ・ルールを適用するシェルスクリプト"add_security_rule.sh"を作成しました。

add_security_rule.sh
#!/bin/bash

#セキュリティ・ルール設定用JSONを格納する変数
OUT="'[" 
#ループカウンタ用変数
i=0

#cidrlist.txtから各行を読み込む
while read line
do
#ループ回数カウンタアップ
i=$((i+1))

#base.jsonの内容をTMPに読み込む
TMP=$(<base.json)

#_SOURCE_をcidrlist.txtから読み込んだCIDRで置換
TMP=${TMP/_SOURCE_/$line}

#ループ1回目の処理以外はJSONを追加する前に","を追加
if [ $i -eq 1 ]; then
OUT="${OUT}${TMP}"
else
OUT="${OUT},${TMP}"
fi

#ループここまで
done < cidrlist.txt

OUT="${OUT}]'"
echo -e "$OUT"

# スクリプトの引数にOCIDを指定したセキュリティ・リストにoci cli でイングレス・セキュリティ・ルールを追加
# yesコマンドでセキュリティ・リスト更新時の確認に自動応答
yes | eval oci network security-list update --security-list-id $1 --ingress-security-rules ${OUT}

4.動作確認

今回は検証用に作成したセキュリティ・リスト「Test SL」にセキュリティ・リストを追加します。
スクリーンショット 2022-02-11 20.49.17.png
作成直後なので、「Test SL」にイングレス・ルールはない状態です。

では、引数にセキュリティ・リストのOCIDを指定して、add_security_rule.shを実行してみます。

[opc@work update_seclist]$ ./add_security_rule.sh ocid1.securitylist.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
WARNING: Updates to defined-tags and egress-security-rules and freeform-tags and ingress-security-rules will replace any existing values. Are you sure you want to continue? [y/N]:

セキュリティ・リストのイングレス・ルール、エグレス・ルール、タグの更新をOCI CLIから実行すると、既存の値が上書きされる旨の警告が表示されます。今回は既存のイングレス・ルールがないので、気にせずyを入力します。

WARNING: Updates to defined-tags and egress-security-rules and freeform-tags and ingress-security-rules will replace any existing values. Are you sure you want to continue? [y/N]: y
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
    "defined-tags": {},
    "display-name": "Test SL",
    "egress-security-rules": [],
    "freeform-tags": {},
    "id": "ocid1.securitylist.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "ingress-security-rules": [
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.1.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.2.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.3.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.4.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.5.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.6.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.7.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.8.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.9.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      },
      {
        "description": null,
        "icmp-options": null,
        "is-stateless": false,
        "protocol": "6",
        "source": "10.10.0.0/16",
        "source-type": "CIDR_BLOCK",
        "tcp-options": {
          "destination-port-range": {
            "max": 1522,
            "min": 1522
          },
          "source-port-range": {
            "max": 1522,
            "min": 1522
          }
        },
        "udp-options": null
      }
    ],
    "lifecycle-state": "AVAILABLE",
    "time-created": "2022-02-11T08:38:20.671000+00:00",
    "vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
  },
  "etag": "4a239462"
}

問題なく実行できました。

コンソールでセキュリティ・リストのイングレス・ルールを確認してみます。
スクリーンショット 2022-02-11 21.26.06.png

cidrlist.txtに記述したCIDRをソースとしたbase.jsonに記述したセキュリティ・ルールが、イングレス・ルールとしてセキュリティ・リストに追加されていることが確認できました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?