LoginSignup
0
0

OCIサービスが利用するCIDRリストをポーリングしてセキュリティルールを自動更新する

Posted at

本記事について

Oracle Cloudの各種サービスとインターネット経由て通信する場合、
疎通するためのセキュリティルールの設定が必要となります。

Oracle Cloudのサービスが利用するCIDR範囲について以下マニュアルから取得することが可能です。
https://docs.oracle.com/ja-jp/iaas/Content/General/Concepts/addressranges.htm
しかしマニュアルの以下記載の通りCIDR範囲は定期的に変更されるため、
運用においてはポーリングして自動更新を行う実装が必要です。

公開済ファイルをポーリングして、新しいIPアドレス範囲を24時間ごとの頻度でチェックできます。公開済ファイルを少なくとも週に1回ポーリングすることをお薦めします。

以下のスクリプトは上記OCIサービスが利用するCIDR範囲をマニュアルからポーリングし、
OCI CLI(API)を使用してセキュリティリストを更新するスクリプトです。
cronなどに設定することで定期的に自動更新することが可能です。

#!/bin/bash

# URL, リージョン, キーの指定
url="https://docs.oracle.com/en-us/iaas/tools/public_ip_ranges.json"
region="ap-tokyo-1"
key="OSN"

# 更新するセキュリティリストのocid
seclist_ocid="xxxx.xxxx.xxxx"

# CIDR情報が記載されたJSONファイルをダウンロード
curl -O "${url}"

filename=$(basename "${url}")

# OSNタグがついたCIDRのリストを抽出
values=(`cat public_ip_ranges.json | jq '.regions[] | select(.region == "ap-tokyo-1")' | jq '.cidrs[] | select(.tags[] == "OSN")' | jq '.cidr'`)

# セキュリティルール更新用のJSONファイルを生成
echo "[" > sec_config.json
for value in "${values[@]}"; do
        tmp_config=`sed s#REPLACE_OSN_CIDR#${value}#g base_config.json`
        echo "$tmp_config" >> sec_config.json
done
echo "]" >> sec_config.json

# ダブルクォートが二重になっている箇所を修正
sed -i -e s/\"\"/\"/g sec_config.json

# 上記JSONファイルを使用してOCI CLIでセキュリティリストを更新
oci network security-list update --security-list-id $seclist_ocid --ingress-security-rules file://sec_config.json --force
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