Update the oracle security list by json file

I want to only the cloudflare ip to access my 80 and 443 port.

1.use python to geneate the ingress rule json file

import json

output_file = "ip_ranges.json"

ip_ranges = [
    "173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", 
    "141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", 
    "197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/13", 
    "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22"
]

# Specify the list of ports
ports = [80, 443]

ingress_rules = []

for ip_range in ip_ranges:
    for port in ports:
        rule = {
            "source": ip_range,
            "protocol": "6",
            "isStateless": False,
            "Description": "CloudFlare Only",
            "tcp-options": {
                "destinationPortRange": {"min": port, "max": port}
            }
        }
        ingress_rules.append(rule)

with open(output_file, "w") as f:
    json.dump(ingress_rules, f, indent=2)

print(f"JSON file generated: {output_file}")

2. run the command below in the oracle cloud shell

oci network security-list update --security-list-id <your secrity list ocid> --ingress-security-rules file://ip_ranges.json

Please note it will overwrite all the existing ingress rules.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *