|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +NETWORK_ACL="$1" |
| 4 | +REGION="$2" |
| 5 | +RESOURCE_GROUP="$3" |
| 6 | + |
| 7 | +if [[ -z "${NETWORK_ACL}" ]] || [[ -z "${REGION}" ]] || [[ -z "${RESOURCE_GROUP}" ]]; then |
| 8 | + echo "Usage: open-acl-rules.sh NETWORK_ACL REGION RESOURCE_GROUP" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +if [[ -z "${IBMCLOUD_API_KEY}" ]]; then |
| 13 | + echo "IBMCLOUD_API_KEY environment variable must be set" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +if [[ -n "${ACL_RULES}" ]] || [[ -n "${SG_RULES}" ]]; then |
| 18 | + echo "ACL_RULES or SG_RULES provided" |
| 19 | +else |
| 20 | + echo "ACL_RULES or SG_RULES environment variable must be set" |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +SEMAPHORE="acl_rules.semaphore" |
| 25 | + |
| 26 | +while true; do |
| 27 | + echo "Checking for semaphore" |
| 28 | + if [[ ! -f "${SEMAPHORE}" ]]; then |
| 29 | + echo -n "${NETWORK_ACL}" > "${SEMAPHORE}" |
| 30 | + |
| 31 | + if [[ $(cat ${SEMAPHORE}) == "${NETWORK_ACL}" ]]; then |
| 32 | + echo "Got the semaphore. Creating acl rules" |
| 33 | + break |
| 34 | + fi |
| 35 | + fi |
| 36 | + |
| 37 | + SLEEP_TIME=$((1 + $RANDOM % 10)) |
| 38 | + echo " Waiting $SLEEP_TIME seconds for semaphore" |
| 39 | + sleep $SLEEP_TIME |
| 40 | +done |
| 41 | + |
| 42 | +function finish { |
| 43 | + rm "${SEMAPHORE}" |
| 44 | +} |
| 45 | + |
| 46 | +trap finish EXIT |
| 47 | + |
| 48 | +if ! ibmcloud account show 1> /dev/null 2> /dev/null; then |
| 49 | + ibmcloud login --apikey "${IBMCLOUD_API_KEY}" -g "${RESOURCE_GROUP}" -r "${REGION}" |
| 50 | +fi |
| 51 | + |
| 52 | +# Install jq if not available |
| 53 | +JQ=$(command -v jq || command -v ./bin/jq) |
| 54 | + |
| 55 | +if [[ -z "${JQ}" ]]; then |
| 56 | + echo "jq missing. Installing" |
| 57 | + mkdir -p ./bin && curl -Lo ./bin/jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 |
| 58 | + JQ="${PWD}/bin/jq" |
| 59 | +fi |
| 60 | + |
| 61 | +## TODO more sophisticated logic needed to 1) test for existing rules and 2) place this rule in the right order |
| 62 | + |
| 63 | +echo "Processing ACL_RULES" |
| 64 | +echo "${ACL_RULES}" | ${JQ} -c '.[]' | \ |
| 65 | + while read rule; |
| 66 | +do |
| 67 | + name=$(echo "${rule}" | ${JQ} -r '.name') |
| 68 | + action=$(echo "${rule}" | ${JQ} -r '.action') |
| 69 | + direction=$(echo "${rule}" | ${JQ} -r '.direction') |
| 70 | + source=$(echo "${rule}" | ${JQ} -r '.source') |
| 71 | + destination=$(echo "${rule}" | ${JQ} -r '.destination') |
| 72 | + |
| 73 | + tcp=$(echo "${rule}" | ${JQ} -c '.tcp // empty') |
| 74 | + udp=$(echo "${rule}" | ${JQ} -c '.udp // empty') |
| 75 | + icmp=$(echo "${rule}" | ${JQ} -c '.icmp // empty') |
| 76 | + |
| 77 | + if [[ -n "${tcp}" ]] || [[ -n "${udp}" ]]; then |
| 78 | + if [[ -n "${tcp}" ]]; then |
| 79 | + type="tcp" |
| 80 | + config="${tcp}" |
| 81 | + else |
| 82 | + type="udp" |
| 83 | + config="${udp}" |
| 84 | + fi |
| 85 | + |
| 86 | + source_port_min=$(echo "${config}" | ${JQ} -r '.source_port_min') |
| 87 | + source_port_max=$(echo "${config}" | ${JQ} -r '.source_port_max') |
| 88 | + port_min=$(echo "${config}" | ${JQ} -r '.port_min') |
| 89 | + port_max=$(echo "${config}" | ${JQ} -r '.port_max') |
| 90 | + |
| 91 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" "${type}" "${source}" "${destination}" \ |
| 92 | + --name "${name}" \ |
| 93 | + --source-port-min "${source_port_min}" \ |
| 94 | + --source-port-max "${source_port_max}" \ |
| 95 | + --destination-port-min "${port_min}" \ |
| 96 | + --destination-port-max "${port_max}" \ |
| 97 | + || exit 1 |
| 98 | + elif [[ -n "${icmp}" ]]; then |
| 99 | + icmp_type=$(echo "${icmp}" | ${JQ} -r '.type // empty') |
| 100 | + icmp_code=$(echo "${icmp}" | ${JQ} -r '.code // empty') |
| 101 | + |
| 102 | + if [[ -n "${icmp_type}" ]] && [[ -n "${icmp_code}" ]]; then |
| 103 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" icmp "${source}" "${destination}" \ |
| 104 | + --name "${name}" \ |
| 105 | + --icmp-type "${icmp_type}" \ |
| 106 | + --icmp-code "${icmp_code}" \ |
| 107 | + || exit 1 |
| 108 | + elif [[ -n "${icmp_type}" ]]; then |
| 109 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" icmp "${source}" "${destination}" \ |
| 110 | + --name "${name}" \ |
| 111 | + --icmp-type "${icmp_type}" \ |
| 112 | + || exit 1 |
| 113 | + else |
| 114 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" icmp "${source}" "${destination}" \ |
| 115 | + --name "${name}" \ |
| 116 | + || exit 1 |
| 117 | + fi |
| 118 | + else |
| 119 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" all "${source}" "${destination}" \ |
| 120 | + --name "${name}" \ |
| 121 | + || exit 1 |
| 122 | + fi |
| 123 | +done |
| 124 | + |
| 125 | +echo "Processing SG_RULES" |
| 126 | +echo "${SG_RULES}" | ${JQ} -c '.[]' | \ |
| 127 | + while read rule; |
| 128 | +do |
| 129 | + name=$(echo "${rule}" | ${JQ} -r '.name') |
| 130 | + action="allow" |
| 131 | + direction=$(echo "${rule}" | ${JQ} -r '.direction') |
| 132 | + remote=$(echo "${rule}" | ${JQ} -r '.remote') |
| 133 | + |
| 134 | + if [[ "${direction}" == "inbound" ]]; then |
| 135 | + source="${remote}" |
| 136 | + destination="0.0.0.0/0" |
| 137 | + else |
| 138 | + destination="${remote}" |
| 139 | + source="0.0.0.0/0" |
| 140 | + fi |
| 141 | + |
| 142 | + tcp=$(echo "${rule}" | ${JQ} -c '.tcp // empty') |
| 143 | + udp=$(echo "${rule}" | ${JQ} -c '.udp // empty') |
| 144 | + icmp=$(echo "${rule}" | ${JQ} -c '.icmp // empty') |
| 145 | + |
| 146 | + RC=0 |
| 147 | + |
| 148 | + if [[ -n "${tcp}" ]] || [[ -n "${udp}" ]]; then |
| 149 | + if [[ -n "${tcp}" ]]; then |
| 150 | + type="tcp" |
| 151 | + config="${tcp}" |
| 152 | + else |
| 153 | + type="udp" |
| 154 | + config="${udp}" |
| 155 | + fi |
| 156 | + |
| 157 | + port_min=$(echo "${config}" | ${JQ} -r '.port_min') |
| 158 | + port_max=$(echo "${config}" | ${JQ} -r '.port_max') |
| 159 | + |
| 160 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" "${type}" "${source}" "${destination}" \ |
| 161 | + --name "${name}" \ |
| 162 | + --source-port-min "${port_min}" \ |
| 163 | + --source-port-max "${port_max}" \ |
| 164 | + --destination-port-min "${port_min}" \ |
| 165 | + --destination-port-max "${port_max}" \ |
| 166 | + || exit 1 |
| 167 | + elif [[ -n "${icmp}" ]]; then |
| 168 | + icmp_type=$(echo "${icmp}" | ${JQ} -r '.type // empty') |
| 169 | + icmp_code=$(echo "${icmp}" | ${JQ} -r '.code // empty') |
| 170 | + |
| 171 | + if [[ -n "${icmp_type}" ]] && [[ -n "${icmp_code}" ]]; then |
| 172 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" icmp "${source}" "${destination}" \ |
| 173 | + --name "${name}" \ |
| 174 | + --icmp-type "${icmp_type}" \ |
| 175 | + --icmp-code "${icmp_code}" \ |
| 176 | + || exit 1 |
| 177 | + elif [[ -n "${icmp_type}" ]]; then |
| 178 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" icmp "${source}" "${destination}" \ |
| 179 | + --name "${name}" \ |
| 180 | + --icmp-type "${icmp_type}" \ |
| 181 | + || exit 1 |
| 182 | + else |
| 183 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" icmp "${source}" "${destination}" \ |
| 184 | + --name "${name}" \ |
| 185 | + || exit 1 |
| 186 | + fi |
| 187 | + else |
| 188 | + ibmcloud is network-acl-rule-add "${NETWORK_ACL}" "${action}" "${direction}" all "${source}" "${destination}" \ |
| 189 | + --name "${name}" \ |
| 190 | + || exit 1 |
| 191 | + fi |
| 192 | +done |
0 commit comments