blob: 6056e0371f3c0ba063f1e259cb06e8d2e22d867f [file] [log] [blame]
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +01001#!/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
Pawel Wieczorek3a474af2020-03-10 15:22:55 +010021# Check all ports exposed outside of kubernetes cluster looking for non-SSL
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +010022# endpoints.
23#
24# Dependencies:
25# nmap
26# kubectl + config
27#
Pawel Wieczorek3a474af2020-03-10 15:22:55 +010028# Return value: Number of discovered non-SSL ports
29# Output: List of pods exposing non-SSL endpoints
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +010030#
31
Pawel Wieczorek331e07b2020-03-09 16:33:53 +010032usage() {
33 cat <<EOF
Pawel Wieczorek3a474af2020-03-10 15:22:55 +010034Usage: $(basename $0) <k8s-namespace> [-l <list of non-SSL endpoints expected to fail this test>]
35 -l: list of non-SSL endpoints expected to fail this test
Pawel Wieczorek331e07b2020-03-09 16:33:53 +010036EOF
37 exit ${1:-0}
38}
39
Lucjan Bryndzac43f25d2019-05-29 16:02:46 +020040#Prerequisities commands list
Pawel Wieczorek331e07b2020-03-09 16:33:53 +010041REQ_APPS=(kubectl nmap awk column sort paste grep wc mktemp sed cat)
Lucjan Bryndzac43f25d2019-05-29 16:02:46 +020042
43# Check for prerequisites apps
44for cmd in "${REQ_APPS[@]}"; do
45 if ! [ -x "$(command -v "$cmd")" ]; then
46 echo "Error: command $cmd is not installed"
47 exit 1
48 fi
49done
50
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +010051if [ "$#" -lt 1 ]; then
Pawel Wieczorek331e07b2020-03-09 16:33:53 +010052 usage 1
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +010053fi
54
55K8S_NAMESPACE=$1
Pawel Wieczorek3a474af2020-03-10 15:22:55 +010056FILTERED_PORTS_LIST=$(mktemp nonssl_endpoints_XXXXXX)
57XF_RAW_FILE_PATH=$(mktemp raw_filtered_nonssl_endpoints_XXXXXX)
Pawel Wieczorek331e07b2020-03-09 16:33:53 +010058
59strip_white_list() {
60 if [ ! -f $XF_FILE_PATH ]; then
61 echo "File not found"
62 usage 1
63 fi
64 grep -o '^[^#]*' $XF_FILE_PATH > $XF_RAW_FILE_PATH
65}
66
67### getopts
68while :
69do
70 case $2 in
71 -h|--help|help) usage ;;
72 -l) XF_FILE_PATH=$3; strip_white_list; shift ;;
73 -*) usage 1 ;;
74 *) break ;;
75 esac
76done
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +010077
78# Get both values on single call as this may get slow
79PORTS_SVCS=`kubectl get svc --namespace=$K8S_NAMESPACE -o go-template='{{range $item := .items}}{{range $port := $item.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\t"}}{{$item.metadata.name}}{{"\n"}}{{end}}{{end}}{{end}}' | column -t | sort -n`
80
81# Split port number and service name
82PORTS=`awk '{print $1}' <<<"$PORTS_SVCS"`
83SVCS=`awk '{print $2}' <<<"$PORTS_SVCS"`
84
85# Create a list in nmap-compatible format
86PORT_LIST=`tr "\\n" "," <<<"$PORTS" | sed 's/,$//'; echo ''`
87
Pawel Wieczorek62b59082020-01-28 16:26:24 +010088# Get IP address of some cluster node (both "external-ip" and "ExternalIP" labels are matched)
89K8S_NODE=`kubectl describe nodes \`kubectl get nodes | grep -v NAME | head -n 1 | awk '{print $1}'\` | grep "external-ip\|ExternalIP" | awk '{print $2}'`
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +010090
91# perform scan
92SCAN_RESULT=`nmap $K8S_NODE -sV -p $PORT_LIST 2>/dev/null | grep \tcp`
93
94# Concatenate scan result with service name
95RESULTS=`paste <(printf %s "$SVCS") <(printf %s "$SCAN_RESULT") | column -t`
96
Pawel Wieczorek3a474af2020-03-10 15:22:55 +010097# Find all non-SSL ports
98HTTP_PORTS=`grep -v ssl <<< "$RESULTS" | tee "$FILTERED_PORTS_LIST"`
Pawel Wieczorek331e07b2020-03-09 16:33:53 +010099
100# Filter out whitelisted endpoints
101while IFS= read -r line; do
102 # for each line we test if it is in the white list with a regular expression
103 while IFS= read -r wl_line; do
104 wl_name=$(echo $wl_line | awk {'print $1'})
105 wl_port=$(echo $wl_line | awk {'print $2'})
106 if grep -e $wl_name.*$wl_port <<< "$line"; then
107 # Found in white list, exclude it
108 sed -i "/$line/d" $FILTERED_PORTS_LIST
109 fi
110 done < $XF_RAW_FILE_PATH
111done < $FILTERED_PORTS_LIST
Krzysztof Opasiakdb2fcdd2019-03-20 23:55:01 +0100112
113# Count them
Pawel Wieczorek331e07b2020-03-09 16:33:53 +0100114N_FILTERED_PORTS_LIST=$(cat $FILTERED_PORTS_LIST | wc -l)
115echo "------------------------------------"
116echo "Nb error pod(s): $N_FILTERED_PORTS_LIST"
117cat $FILTERED_PORTS_LIST
118exit $N_FILTERED_PORTS_LIST