I manage OCI security lists with a CSV and a small Python helper so that repeatable firewall changes stay reviewable. This post walks through append_security_list_rules_csv.py from oci-csv-rule-tools-demo.zip, using the demo template security_list_rules.csv.

This is Part 1 of 3. Next: Batch-upload routes to an OCI VCN route table.

Screenshots below are from the original run on my Mac. IPs, descriptions, OCIDs and AP labels are redacted for publication. Desktop captures are cropped to Termius or Excel (Mac dock and background apps removed).

Prerequisites

  • OCI CLI configured with a DEMO profile that can read and update the target security list
  • Python 3 with the OCI SDK (oci)
  • The demo ZIP unpacked locally
  • The OCID of the security list you intend to change, and a note of which subnets currently use that list
Desktop capture of the CSV rule tools folder with IPs, descriptions, OCIDs and AP labels redacted
Original run: tooling folder with security list CSV templates ready. IPs, descriptions, OCIDs and AP labels redacted.

Which subnets use the security list

In the Console, open the security list and check Associated Resources, or inspect each subnet and confirm the security lists attached to it. Every subnet that references the list inherits the ingress and egress rules you append. Confirm you are editing the list those subnets actually use before you run --apply.

How the CSV fields behave

The demo header is:

environment,security_list_ocid,direction,cidr,protocol,is_stateless,tcp_dest_port_min,tcp_dest_port_max,udp_dest_port_min,udp_dest_port_max,icmp_type,icmp_code,description
  • direction is ingress or egress. For ingress, cidr is the source. For egress, cidr is the destination.
  • protocol accepts values the script maps into OCI rules. Use 6 for TCP, 17 for UDP, 1 for ICMP, or all when you intentionally allow every protocol.
  • is_stateless is true or false. Stateful rules (the usual default) track connection state so return traffic is implied. Stateless rules do not, so you must allow the return path explicitly when you need it.
  • TCP destination ports use tcp_dest_port_min and tcp_dest_port_max. Leave them blank for non-TCP rows.
  • UDP destination ports use the optional udp_* fields. ICMP uses optional icmp_type and icmp_code.
  • environment is informational only. It helps you organise rows in a workbook. The script does not select a tenancy from that column.
  • Leave description blank in the published examples.

1. Prepare a narrow TCP example

Copy the demo CSV and replace placeholders only. Keep production names and OCIDs out of shared copies.

environment,security_list_ocid,direction,cidr,protocol,is_stateless,tcp_dest_port_min,tcp_dest_port_max,udp_dest_port_min,udp_dest_port_max,icmp_type,icmp_code,description
DEMO,<SECURITY_LIST_OCID>,ingress,<SOURCE_CIDR>,6,false,22,22,,,,,
DEMO,<SECURITY_LIST_OCID>,egress,<DESTINATION_CIDR>,6,false,443,443,,,,,

That pair opens stateful TCP/22 from a named source CIDR on ingress, and stateful TCP/443 to a named destination CIDR on egress. Add UDP or ICMP rows only when you need those protocols. Empty optional fields stay empty.

2. Preview without changing OCI

From the unpacked demo directory:

python3 append_security_list_rules_csv.py --profile DEMO --csv security_list_rules.csv

Expected shape of the output:

OCI CSV Security List Rule Appender
Mode: DRY RUN
Rows: 2
Security List: <SECURITY_LIST_OCID>
Backup written: backups/security_list_<suffix>.json
ADD ingress: <SOURCE_CIDR> protocol 6
ADD egress: <DESTINATION_CIDR> protocol 6
Ingress payload: /tmp/oci-ingress-<id>.json
Egress payload: /tmp/oci-egress-<id>.json
DRY RUN - no changes made.

A dry run still contacts OCI to read the security list. It can write a local backup and the ingress and egress payloads. Review those files against the backup before you apply. Only add --apply after that review.

Desktop capture starting the security list CSV appender with IPs, descriptions, OCIDs and AP labels redacted
Original run: starting the security list CSV appender. IPs, descriptions, OCIDs and AP labels redacted.

If a rule already exists, the script reports a skip instead of adding it again:

Redacted terminal output showing SKIP duplicate ingress lines from the original run

Redacted crop from the original run: duplicate ingress rows are skipped during preview.

Desktop capture of SKIP duplicate ingress output with IPs, descriptions, OCIDs and AP labels redacted
Original run desktop: dry-run skip and add lines scrolling in the terminal. Sublime other-file tabs cropped out. IPs, descriptions, OCIDs and AP labels redacted.

3. Apply the same command

python3 append_security_list_rules_csv.py --profile DEMO --csv security_list_rules.csv --apply

Expected confirmation includes Mode: APPLY and an UPDATED line for the security list that changed. Updates send the full ingress and egress rule arrays for that resource. Avoid concurrent Console or API edits on the same list, and check which resources were updated before retrying a partial batch.

Desktop capture of security list apply completing with IPs, descriptions, OCIDs and AP labels redacted
Original run: apply mode completing against the target security list. IPs, descriptions, OCIDs and AP labels redacted.

4. Verify in OCI

  • Re-read the security list in the Console or with the CLI and confirm the new rules.
  • From a host in an associated subnet, test only the intended TCP path.
  • Confirm duplicates remain skipped if you re-run the preview.

This script changes security lists. Network security groups (NSGs) are a separate OCI resource and are not updated here.

Summary

I prepared a narrow TCP CSV against one security list, previewed the combined ingress and egress payloads against a local backup, applied with --apply, and verified the list in OCI. Duplicate rows stay skipped. Download the demo from oci-csv-rule-tools-demo.zip.

Next: Batch-upload routes to an OCI VCN route table.

Keep filled CSVs, backups and payloads private. Use only the demo templates and redacted images in published material.