-
Notifications
You must be signed in to change notification settings - Fork 44
/
oci_csv_network_seclist.sh
executable file
·163 lines (154 loc) · 5.13 KB
/
oci_csv_network_seclist.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
#************************************************************************
#
# oci_csv_network_seclist.sh
#
# Copyright 2018 Rodrigo Jorge <http://www.dbarj.com.br/>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#************************************************************************
# Available at: https://github.com/dbarj/oci-scripts
# Created on: Jul/2018 by Rodrigo Jorge
# Version 1.02
#************************************************************************
# Define paths for oci-cli and jq or put them on $PATH. Don't use relative PATHs in the variables below.
v_oci="oci"
v_jq="jq"
echoError ()
{
(>&2 echo "$1")
}
exitError ()
{
echoError "$1"
exit 1
}
if ! $(which ${v_oci} >&- 2>&-)
then
echoError "Could not find oci-cli binary. Please adapt the path in the script if not in \$PATH."
echoError "Dowload page: https://github.com/oracle/oci-cli"
exit 1
fi
if ! $(which ${v_jq} >&- 2>&-)
then
echoError "Could not find jq binary. Please adapt the path in the script if not in \$PATH."
echoError "Download page: https://github.com/stedolan/jq/releases"
exit 1
fi
v_cmp_string="--compartment-id ${v_compartment_ocid}"
v_vcn_string="--vcn-id ${v_vcn_ocid}"
if [ -z "$v_compartment_ocid" ]
then
echoError "OCID of compartment not specified. Please export v_compartment_ocid variable with correct value."
${v_oci} iam compartment list --all | ${v_jq} -r '.data[] | [.name,.id] | @csv'
exit 1
else
v_listvcn=$(${v_oci} network vcn list --all ${v_cmp_string})
ret=$?
if [ $ret -ne 0 ]
then
echoError "OCID of compartment not specified correctly. Please export v_compartment_ocid variable with correct value."
${v_oci} iam compartment list --all | ${v_jq} -r '.data[] | [.name,.id] | @csv'
exit 1
else
if [ -z "$v_listvcn" ]
then
echoError "There is no VCN in this Compartment."
exit 1
fi
fi
fi
if [ -z "$v_vcn_ocid" ]
then
echoError "OCID of VCN not specified. Please export v_vcn_ocid variable with correct value."
echoError $(${v_oci} network vcn list --all ${v_cmp_string} | ${v_jq} -r '.data[] | [."display-name",.id] | @csv')
exit 1
else
v_listvcn=$(${v_oci} network vcn get ${v_vcn_string})
ret=$?
if [ $ret -ne 0 ]
then
echoError "OCID of VCN not specified correctly. Please export v_vcn_ocid variable with correct value."
echoError $(${v_oci} network vcn list --all ${v_cmp_string} | ${v_jq} -r '.data[] | [."display-name",.id] | @csv')
exit 1
fi
fi
function listSecListsSub ()
{
[ "$#" -ne 2 ] && return
local v_arg1="$1"
local v_arg2="$2"
local v_out1=$(
${v_oci} network security-list list ${v_vcn_string} ${v_cmp_string} --all | ${v_jq} '.data[] |
"'${v_arg1}'" as $type |
."display-name" as $display |
."lifecycle-state" as $lifecycle |
."'${v_arg1}'-security-rules"[]? |
."is-stateless" as $stateless |
."protocol" as $protocol |
"'${v_arg2}'" as $contype |
."'${v_arg2}'" as $'${v_arg2}' |
."tcp-options"."destination-port-range"."min" as $tdmin |
."tcp-options"."destination-port-range"."max" as $tdmax |
."tcp-options"."source-port-range"."min" as $tsmin |
."tcp-options"."source-port-range"."max" as $tsmax |
."udp-options"."destination-port-range"."min" as $udmin |
."udp-options"."destination-port-range"."max" as $udmax |
."udp-options"."source-port-range"."min" as $usmin |
."udp-options"."source-port-range"."max" as $usmax |
{
"display-name": $display,
"lifecycle-state": $lifecycle,
"is-stateless": $stateless,
"type" : $type,
"protocol": $protocol,
"contype": $contype,
"'${v_arg2}'": $'${v_arg2}',
"tcp-destination-port-min": $tdmin,
"tcp-destination-port-max": $tdmax,
"tcp-source-port-min": $tsmin,
"tcp-source-port-max": $tsmax,
"udp-destination-port-min": $udmin,
"udp-destination-port-max": $udmax,
"udp-source-port-min": $usmin,
"udp-source-port-max": $usmax
}' | sed 's/}/},/; 1 s/{/[{/; $s/},/}]/' | ${v_jq} -r '.[] |
[
."display-name",
."lifecycle-state",
."is-stateless",
."type",
."protocol",
."contype",
."'${v_arg2}'",
."tcp-destination-port-min",
."tcp-destination-port-max",
."tcp-source-port-min",
."tcp-source-port-max",
."udp-destination-port-min",
."udp-destination-port-max",
."udp-source-port-min",
."udp-source-port-max"] | @csv'
)
[ -n "${v_out1}" ] && echo "${v_out1}"
}
###
v_header="display-name,lifecycle-state,is-stateless,type,protocol,contype,IP,tcp-dest-port-min,tcp-dest-port-max,tcp-source-port-min,tcp-source-port-max,udp-dest-port-min,udp-dest-port-max,udp-source-port-min,udp-source-port-max"
v_out1=$(listSecListsSub "ingress" "source")
v_out2=$(listSecListsSub "egress" "destination")
[ -n "$v_out1" -o -n "$v_out2" ] && echo "$v_header"
[ -n "$v_out1" ] && echo "$v_out1"
[ -n "$v_out2" ] && echo "$v_out2"
exit 0
###