Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # COPYRIGHT NOTICE STARTS HERE |
| 4 | # |
| 5 | # Copyright 2019 Samsung Electronics Co., Ltd. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # COPYRIGHT NOTICE ENDS HERE |
| 20 | |
| 21 | # Check all ports exposed by pods to internal network and look for |
| 22 | # open JDWP ports |
| 23 | # |
| 24 | # Dependencies: |
| 25 | # kubectl + config |
| 26 | # netcat |
| 27 | # |
| 28 | # Return value: Number of discovered JDWP ports |
| 29 | # Output: List of pods and exposing JDWP interface |
| 30 | # |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 31 | usage() { |
| 32 | cat <<EOF |
| 33 | Usage: $(basename $0) <k8s-namespace> [-l <white list file>] |
| 34 | -l: jdpw white list ports file |
| 35 | EOF |
| 36 | exit ${1:-0} |
| 37 | } |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 38 | |
| 39 | if [ "$#" -lt 1 ]; then |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 40 | usage |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 41 | exit 1 |
| 42 | fi |
| 43 | |
| 44 | K8S_NAMESPACE=$1 |
| 45 | LOCAL_PORT=12543 |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 46 | FILTERED_PORTS_LIST=$(mktemp jdpw_ports_XXXXXX) |
| 47 | WL_RAW_FILE_PATH=$(mktemp raw_filtered_ports_XXXXXX) |
| 48 | |
| 49 | manage_white_list() { |
| 50 | # init filtered port list file |
| 51 | if [ ! -f $WL_FILE_PATH ];then |
| 52 | echo "File not found" |
| 53 | usage |
| 54 | fi |
| 55 | grep -o '^[^#]*' $WL_FILE_PATH > $WL_RAW_FILE_PATH |
| 56 | } |
| 57 | |
| 58 | ### getopts |
| 59 | while : |
| 60 | do |
| 61 | case $2 in |
| 62 | -h|--help|help) usage;; |
| 63 | -l) WL_FILE_PATH=$3;manage_white_list;shift;; |
| 64 | -*) usage 1 ;; |
| 65 | *) break ;; |
| 66 | esac |
| 67 | done |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 68 | |
| 69 | list_pods() { |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 70 | kubectl get po --namespace=$K8S_NAMESPACE | grep Running | awk '{print $1}' | grep -v NAME |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | do_jdwp_handshake() { |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 74 | local ip="127.0.0.1" |
| 75 | local port=$1 |
| 76 | local jdwp_challenge="JDWP-Handshake\n" |
| 77 | local jdwp_response="JDWP-Handshake" |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 78 | |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 79 | # 10s timeout to avoid hangs when service doesn't answer at all |
| 80 | local response=`nc -w 10 $ip $port <<<$jdwp_challenge | tr '\0' '\n'` |
| 81 | local n_response_lines=`echo "$response" | wc -l` |
| 82 | if [[ "$n_response_lines" -le 1 ]] && [[ $response == *"$jdwp_response"* ]]; then |
| 83 | return 0 |
| 84 | fi |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 85 | |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 86 | return 1 |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 87 | } |
| 88 | # get open ports from procfs as netstat is not always available |
| 89 | get_open_ports_on_pod() { |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 90 | local pod=$1 |
| 91 | local open_ports_hex=`kubectl exec --namespace=$K8S_NAMESPACE $pod cat /proc/net/tcp 2>/dev/null| grep -v "local_address" | awk '{ print $2" "$4 }' | grep '0A$' | tr ":" " " | awk '{ print $2 }' | sort | uniq` |
| 92 | for hex_port in $open_ports_hex; do |
| 93 | echo $((16#$hex_port)) |
| 94 | done |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | N_PORTS=0 |
| 98 | |
| 99 | # go through all pods |
| 100 | for pod in `list_pods`; do |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 101 | open_ports=`get_open_ports_on_pod $pod` |
| 102 | # if there is no open ports just go to next pod |
| 103 | if [ -z "$open_ports" ]; then |
| 104 | continue |
| 105 | fi |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 106 | |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 107 | # let's setup a proxy and check every open port |
| 108 | for port in $open_ports; do |
| 109 | # run proxy |
| 110 | kubectl port-forward --namespace=$K8S_NAMESPACE $pod $LOCAL_PORT:$port &>/dev/null & |
| 111 | sleep 1 |
| 112 | proxy_pid=$! |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 113 | |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 114 | do_jdwp_handshake $LOCAL_PORT |
| 115 | if [ $? -eq 0 ]; then |
| 116 | echo $pod $port | tee $FILTERED_PORTS_LIST |
| 117 | ((++N_PORTS)) |
| 118 | fi |
| 119 | kill $proxy_pid 2>/dev/null |
| 120 | wait $proxy_pid 2>/dev/null |
| 121 | done |
Krzysztof Opasiak | 28c3d2e | 2019-03-21 22:49:38 +0100 | [diff] [blame] | 122 | done |
| 123 | |
mrichomme | 99b8b9e | 2020-02-11 09:46:48 +0100 | [diff] [blame] | 124 | while IFS= read -r line; do |
| 125 | # for each line we test if it is in the white list with a regular expression |
| 126 | while IFS= read -r wl_line; do |
| 127 | wl_name=$(echo $wl_line | awk {'print $1'}) |
| 128 | wl_port=$(echo $wl_line | awk {'print $2'}) |
| 129 | if grep -e $wl_name.*$wl_port <<< "$line";then |
| 130 | # Found in white list, exclude it |
| 131 | sed -i "/$line/d" $FILTERED_PORTS_LIST |
| 132 | fi |
| 133 | done < $WL_RAW_FILE_PATH |
| 134 | done < $FILTERED_PORTS_LIST |
| 135 | |
| 136 | N_FILTERED_PORTS_LIST=$(cat $FILTERED_PORTS_LIST |wc -l) |
| 137 | echo "------------------------------------" |
| 138 | echo "Nb error pod(s): $N_FILTERED_PORTS_LIST" |
| 139 | cat $FILTERED_PORTS_LIST |
| 140 | |
| 141 | exit $N_FILTERED_PORTS_LIST |