Skip to content

Commit 6da5269

Browse files
Added check for egress rule to allow traffic on all ports in MS NSG
1 parent e0a2073 commit 6da5269

File tree

1 file changed

+101
-3
lines changed

1 file changed

+101
-3
lines changed

terraform/modules/network-validator/scripts/network_validation.sh

+101-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# Copyright (c) 2023, 2024, Oracle and/or its affiliates.
3+
# Copyright (c) 2023, 2025, Oracle and/or its affiliates.
44
# Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
55
#
66
# ############################################################################
@@ -347,6 +347,54 @@ function check_udp_port_open_in_seclist_or_nsg() {
347347
echo 1
348348
fi
349349
}
350+
###################################################
351+
# Checks if there is an egress rule to ensure that the network can establish outbound communication to any destination, utilizing all available protocols and ports.
352+
# Args:
353+
# seclist_or_nsg_ocid: OCID for the security list or nsg.
354+
# ocid_type: Valid values: "nsg" for Network Security Group OCID, "seclist" for Security List OCID (default)
355+
# Returns:
356+
# 0|1
357+
###################################################
358+
function check_egress_all_traffic_in_nsg_or_seclist() {
359+
local nsg_ocid_or_sec_list=$1
360+
local icmp_protocol="1"
361+
local port_is_open=false
362+
local egress_rules_count=0
363+
local ocid_type=$2
364+
declare -A nsg_sec_list_array
365+
366+
if [[ $ocid_type == "nsg" ]]; then
367+
egress_rules=$(oci network nsg rules list --nsg-id $nsg_ocid_or_sec_list --direction EGRESS | jq -r '.data')
368+
else
369+
egress_rules=$(oci network security-list get --security-list-id $nsg_ocid_or_sec_list | jq -r '.data["egress-security-rules"]')
370+
fi
371+
egress_rules_count=$(echo $egress_rules | jq '. | length')
372+
373+
if [[ $egress_rules_count -gt 0 ]]; then
374+
for ((j = 0; j < egress_rules_count; j++)); do
375+
egress_protocol=$(echo $egress_rules | jq -r --arg i "$j" '.[$i|tonumber].protocol')
376+
egress_destination=$(echo $egress_rules | jq -r --arg i "$j" '.[$i|tonumber].destination')
377+
egress_destination_type=$(echo $egress_rules | jq -r --arg i "$j" '.[$i|tonumber]."destination-type"')
378+
379+
if [[ $egress_destination_type != "CIDR_BLOCK" ]]; then
380+
nsg_sec_list_array[$j]="WARNING: Destinantion type is either NSG or Service. Skipping the validation check for ${egress_destination}."
381+
continue
382+
fi
383+
384+
if [[ $egress_destination == "0.0.0.0/0" && ( $egress_protocol == "all" || $egress_protocol == $icmp_protocol ) ]]; then
385+
egress_is_open=true
386+
echo 0
387+
return
388+
fi
389+
done
390+
fi
391+
392+
if [[ ${#nsg_sec_list_array[@]} != 0 ]]; then
393+
echo "${nsg_sec_list_array[@]}"
394+
else
395+
echo 1
396+
fi
397+
}
350398

351399

352400
####################################################
@@ -391,7 +439,35 @@ function validate_subnet_port_access() {
391439
done
392440
echo $port_found_open
393441
}
442+
####################################################
443+
# Validates if egress rule is present to allow all traffic on all ports in the specified subnet.
444+
#
445+
# Args:
446+
# subnet: Subnet OCID
447+
# Returns:
448+
# 0|1
449+
####################################################
450+
function validate_egress_rule() {
451+
local port_found_open=1
452+
local subnet=$1
394453

454+
sec_lists=$(oci network subnet get --subnet-id ${subnet} | jq -c '.data["security-list-ids"]')
455+
456+
declare -A seclists_array
457+
458+
while IFS="=" read -r key value
459+
do
460+
seclists_array[$key]="$value"
461+
done < <(jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' <<< "$sec_lists")
462+
# Check the ingress rules for specified destination port is open for access by source CIDR
463+
for seclist_ocid in "${seclists_array[@]}"
464+
do
465+
if [[ $port_found_open -ne 0 ]]; then
466+
port_found_open=$(check_egress_all_traffic_in_nsg_or_seclist $seclist_ocid "seclist")
467+
fi
468+
done
469+
echo $port_found_open
470+
}
395471
####################################################
396472
# Validates if the ATP_PORT is open for the WLS subnet CIDR.
397473
# This is applicable for ATP DB with private endpoint only.
@@ -779,6 +855,19 @@ fi
779855

780856
if [[ -n ${WLS_SUBNET_OCID} && -z ${ADMIN_SRV_NSG_OCID} && -z ${MANAGED_SRV_NSG_OCID} ]]
781857
then
858+
# Check egress rule to allow all traffic on all ports in WLS Subnet CIDR.
859+
res=$(validate_egress_rule ${WLS_SUBNET_OCID})
860+
861+
if [[ $res == *"WARNING"* ]]
862+
then
863+
for warning in "${res[@]}"; do
864+
echo "$warning"
865+
done
866+
elif [[ $res -ne 0 ]]
867+
then
868+
echo "ERROR: Missing egress rule to allow all traffic on all ports in WLS Subnet [$WLS_SUBNET_OCID]. ${NETWORK_VALIDATION_MSG}"
869+
validation_return_code=2
870+
fi
782871
wls_subnet_cidr_block=$(oci network subnet get --subnet-id ${WLS_SUBNET_OCID} | jq -r '.data["cidr-block"]')
783872

784873
# Check if SSH port is open for access by WLS subnet CIDR
@@ -851,9 +940,18 @@ fi
851940

852941
if [[ -n ${WLS_SUBNET_OCID} && -n ${ADMIN_SRV_NSG_OCID} && -n ${MANAGED_SRV_NSG_OCID} ]]
853942
then
854-
wls_subnet_cidr_block=$(oci network subnet get --subnet-id ${WLS_SUBNET_OCID} | jq -r '.data["cidr-block"]')
855-
943+
# Check egress rule to allow all traffic on all ports in Managed Server NSG.
944+
res=$(check_egress_all_traffic_in_nsg_or_seclist ${MANAGED_SRV_NSG_OCID} "nsg")
945+
if [[ $res == *"WARNING"* ]]; then
946+
for warning in "${res[@]}"; do
947+
echo "$warning"
948+
done
949+
elif [[ $res -ne 0 ]]; then
950+
echo "ERROR: Missing egress rule to allow traffic on all ports in Managed Server NSG [$MANAGED_SRV_NSG_OCID]. ${NETWORK_VALIDATION_MSG}"
951+
validation_return_code=2
952+
fi
856953
# Check if SSH port is open for access by WLS subnet CIDR in Admin Server NSG
954+
wls_subnet_cidr_block=$(oci network subnet get --subnet-id ${WLS_SUBNET_OCID} | jq -r '.data["cidr-block"]')
857955
res=$(check_tcp_port_open_in_seclist_or_nsg $MANAGED_SRV_NSG_OCID "${SSH_PORT}" "$wls_subnet_cidr_block" "nsg")
858956
if [[ $res == *"WARNING"* ]]
859957
then

0 commit comments

Comments
 (0)