|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# OpenShift egress DNS proxy setup script |
| 4 | + |
| 5 | +set -o errexit |
| 6 | +set -o nounset |
| 7 | +set -o pipefail |
| 8 | + |
| 9 | +# Default DNS nameserver port |
| 10 | +NS_PORT=53 |
| 11 | +CONF=/etc/haproxy/haproxy.cfg |
| 12 | + |
| 13 | +BLANK_LINE_OR_COMMENT_REGEX="([[:space:]]*$|#.*)" |
| 14 | +IPADDR_REGEX="[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" |
| 15 | +PORT_REGEX="[[:digit:]]+" |
| 16 | +DOMAINNAME_REGEX="[[:alnum:]][[:alnum:]-]+?\.[[:alnum:].-]+" |
| 17 | + |
| 18 | +function die() { |
| 19 | + echo "$*" 1>&2 |
| 20 | + exit 1 |
| 21 | +} |
| 22 | + |
| 23 | +function check_prereqs() { |
| 24 | + if [[ -z "${EGRESS_SOURCE}" ]]; then |
| 25 | + die "Must specify EGRESS_SOURCE" |
| 26 | + fi |
| 27 | + if ! [[ "${EGRESS_SOURCE}" =~ ^${IPADDR_REGEX} ]]; then |
| 28 | + die "EGRESS_SOURCE must be IPv4 address" |
| 29 | + fi |
| 30 | + |
| 31 | + if [[ -z "${EGRESS_DESTINATION}" ]]; then |
| 32 | + die "Must specify EGRESS_DESTINATION" |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +function validate_port() { |
| 37 | + local port=$1 |
| 38 | + if [[ "${port}" -lt "1" || "${port}" -gt "65535" ]]; then |
| 39 | + die "Invalid port: ${port}, must be in the range 1 to 65535" |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +function generate_haproxy_defaults() { |
| 44 | + echo " |
| 45 | +global |
| 46 | + log 127.0.0.1 local2 |
| 47 | +
|
| 48 | + chroot /var/lib/haproxy |
| 49 | + pidfile /var/run/haproxy.pid |
| 50 | + maxconn 4000 |
| 51 | + user haproxy |
| 52 | + group haproxy |
| 53 | +
|
| 54 | +defaults |
| 55 | + log global |
| 56 | + mode tcp |
| 57 | + option dontlognull |
| 58 | + option tcplog |
| 59 | + option redispatch |
| 60 | + retries 3 |
| 61 | + timeout http-request 100s |
| 62 | + timeout queue 1m |
| 63 | + timeout connect 10s |
| 64 | + timeout client 1m |
| 65 | + timeout server 1m |
| 66 | + timeout http-keep-alive 100s |
| 67 | + timeout check 10s |
| 68 | +" |
| 69 | +} |
| 70 | + |
| 71 | +function generate_dns_resolvers() { |
| 72 | + echo "resolvers dns-resolver" |
| 73 | + # Fetch nameservers from /etc/resolv.conf |
| 74 | + declare -a nameservers=$(cat /etc/resolv.conf |grep nameserver|awk -F" " '{print $2}') |
| 75 | + n=0 |
| 76 | + for ns in ${nameservers[@]}; do |
| 77 | + n=$(($n + 1)) |
| 78 | + echo " nameserver ns$n ${ns}:${NS_PORT}" |
| 79 | + done |
| 80 | + |
| 81 | + # Add google DNS servers as fallback |
| 82 | + echo " nameserver nsfallback1 8.8.8.8:${NS_PORT}" |
| 83 | + echo " nameserver nsfallback2 8.8.4.4:${NS_PORT}" |
| 84 | + |
| 85 | + # Set default optional params |
| 86 | + echo " resolve_retries 3" |
| 87 | + echo " timeout retry 1s" |
| 88 | + echo " hold valid 10s" |
| 89 | + echo "" |
| 90 | +} |
| 91 | + |
| 92 | +function generate_haproxy_frontends_backends() { |
| 93 | + local n=0 |
| 94 | + declare -A used_ports=() |
| 95 | + |
| 96 | + while read dest; do |
| 97 | + local port target targetport resolvers |
| 98 | + |
| 99 | + if [[ "${dest}" =~ ^${BLANK_LINE_OR_COMMENT_REGEX}$ ]]; then |
| 100 | + continue |
| 101 | + fi |
| 102 | + n=$(($n + 1)) |
| 103 | + resolvers="" |
| 104 | + |
| 105 | + if [[ "${dest}" =~ ^${PORT_REGEX}\ +${IPADDR_REGEX}$ ]]; then |
| 106 | + read port target <<< "${dest}" |
| 107 | + targetport="${port}" |
| 108 | + elif [[ "${dest}" =~ ^${PORT_REGEX}\ +${IPADDR_REGEX}\ +${PORT_REGEX}$ ]]; then |
| 109 | + read port target targetport <<< "${dest}" |
| 110 | + elif [[ "${dest}" =~ ^${PORT_REGEX}\ +${DOMAINNAME_REGEX}$ ]]; then |
| 111 | + read port target <<< "${dest}" |
| 112 | + targetport="${port}" |
| 113 | + resolvers="resolvers dns-resolver" |
| 114 | + elif [[ "${dest}" =~ ^${PORT_REGEX}\ +${DOMAINNAME_REGEX}\ +${PORT_REGEX}$ ]]; then |
| 115 | + read port target targetport <<< "${dest}" |
| 116 | + resolvers="resolvers dns-resolver" |
| 117 | + else |
| 118 | + die "Bad destination '${dest}'" |
| 119 | + fi |
| 120 | + |
| 121 | + validate_port ${port} |
| 122 | + validate_port ${targetport} |
| 123 | + |
| 124 | + if [[ "${used_ports[${port}]:-x}" == "x" ]]; then |
| 125 | + used_ports[${port}]=1 |
| 126 | + else |
| 127 | + die "Proxy port $port already used, must be unique for each destination" |
| 128 | + fi |
| 129 | + |
| 130 | + echo " |
| 131 | +frontend fe$n |
| 132 | + bind ${EGRESS_SOURCE}:${port} |
| 133 | + default_backend be$n |
| 134 | +
|
| 135 | +backend be$n |
| 136 | + server dest$n ${target}:${targetport} check $resolvers |
| 137 | +" |
| 138 | + done <<< "${EGRESS_DESTINATION}" |
| 139 | +} |
| 140 | + |
| 141 | +function setup_haproxy_config() { |
| 142 | + generate_haproxy_defaults |
| 143 | + generate_dns_resolvers |
| 144 | + generate_haproxy_frontends_backends |
| 145 | +} |
| 146 | + |
| 147 | +function setup_haproxy_syslog() { |
| 148 | + cat >> /etc/rsyslog.conf <<EOF |
| 149 | +module(load="imudp") |
| 150 | +input(type="imudp" port="514") |
| 151 | +EOF |
| 152 | + |
| 153 | + echo "local2.* /var/log/haproxy.log" >> /etc/rsyslog.d/haproxy.conf |
| 154 | + |
| 155 | + /usr/sbin/rsyslogd |
| 156 | + touch /var/log/haproxy.log |
| 157 | + tail -f /var/log/haproxy.log & |
| 158 | +} |
| 159 | + |
| 160 | +function run() { |
| 161 | + |
| 162 | + check_prereqs |
| 163 | + |
| 164 | + rm -f ${CONF} |
| 165 | + setup_haproxy_config > ${CONF} |
| 166 | + |
| 167 | + setup_haproxy_syslog |
| 168 | + |
| 169 | + echo "Running haproxy with config:" |
| 170 | + sed -e 's/^/ /' ${CONF} |
| 171 | + echo "" |
| 172 | + echo "" |
| 173 | + |
| 174 | + exec haproxy -f ${CONF} |
| 175 | + } |
| 176 | + |
| 177 | +if [[ "${EGRESS_DNS_PROXY_MODE:-}" == "unit-test" ]]; then |
| 178 | + check_prereqs |
| 179 | + setup_haproxy_config |
| 180 | + exit 0 |
| 181 | +fi |
| 182 | + |
| 183 | +run |
0 commit comments