Support password-based ssh auth
[validation.git] / tests / k8s / kube-hunter / kube-hunter.resource
1 ##############################################################################
2 # Copyright (c) 2020 AT&T Intellectual Property.                             #
3 # Copyright (c) 2020 Nokia.                                                  #
4 #                                                                            #
5 # Licensed under the Apache License, Version 2.0 (the "License");            #
6 # you maynot use this file except in compliance with the License.            #
7 #                                                                            #
8 # You may obtain a copy of the License at                                    #
9 #       http://www.apache.org/licenses/LICENSE-2.0                           #
10 #                                                                            #
11 # Unless required by applicable law or agreed to in writing, software        #
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  #
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.           #
14 # See the License for the specific language governing permissions and        #
15 # limitations under the License.                                             #
16 ##############################################################################
17
18
19 *** Settings ***
20 Library             Collections
21 Library             OperatingSystem
22 Library             Process
23 Library             SSHLibrary
24 Library             String
25
26
27 *** Variables ***
28 ${REPORTDIR}        ${LOG_PATH}/${SUITE_NAME.replace(' ','_')}
29 &{KUBE_HUNTER}      path=akraino
30 ...                 name=validation:kube-hunter-latest
31
32
33 *** Keywords ***
34 Open Connection And Log In
35     Open Connection  ${HOST}
36     Run Keyword IF  '${SSH_KEYFILE}' != 'None'  Login With Public Key  ${USERNAME}  ${SSH_KEYFILE}  ELSE IF  '${PASSWORD}' != 'None'  Login  ${USERNAME}  ${PASSWORD}  ELSE  FAIL
37
38 Get Cluster Address
39     ${result}=        Run Process  kubectl  config  view  --minify
40     ...                            -o  jsonpath\={.clusters[0].cluster.server}
41     Should Be Equal As Integers  ${result.rc}  0
42     ${addr}=          Fetch From Right  ${result.stdout}  ://
43     ${addr}=          Fetch From Left  ${addr}  :
44     Should Not Be Empty  ${addr}
45     [Return]          ${addr}
46
47 Get Remote Addresses
48     ${result}=        Run Process  kubectl  get  nodes
49     ...                   -o  jsonpath\={.items[*].status.addresses[?(@.type\=\="ExternalIP")].address}
50     Should Be Equal As Integers  ${result.rc}  0
51     Pass Execution If  '${result.stdout}' == '${EMPTY}'  No external node IPs exposed
52     @{addrs}=         Split String  ${result.stdout}
53     [Return]          ${addrs}
54
55 Upload To Internal Registry
56     [Arguments]       ${path}  ${name}
57     ${rc}=            Execute Command
58     ...               docker pull ${path}/${name}
59     ...                 return_stdout=False  return_rc=True
60     Should Be Equal As Integers  ${rc}  0
61     ${rc}=            Execute Command
62     ...               docker tag ${path}/${name} ${INT_REG}/bluval/${name}
63     ...                 return_stdout=False  return_rc=True
64     Should Be Equal As Integers  ${rc}  0
65     ${rc}=            Execute Command
66     ...               docker push ${INT_REG}/bluval/${name}
67     ...                 return_stdout=False  return_rc=True
68     Should Be Equal As Integers  ${rc}  0
69
70 Onboard Image
71     ${INT_REG}=       Get Variable Value  ${INTERNAL_REGISTRY}  ${EMPTY}
72     Set Test Variable  ${INT_REG}
73     Return From Keyword If  $INT_REG == '${EMPTY}'
74     Open Connection And Log In
75     Upload To Internal Registry  ${KUBE_HUNTER['path']}  ${KUBE_HUNTER['name']}
76     Set To Dictionary  ${KUBE_HUNTER}  path=${INT_REG}/bluval
77
78 Prepare Job Manifest
79     Run Process       sed  -i  s|{{ image }}|${KUBE_HUNTER['path']}/${KUBE_HUNTER['name']}|g
80     ...               ${CURDIR}/job.yaml
81
82 Set Scan Status
83     [Arguments]       ${log}
84     ${STATUS}=        Evaluate  "No vulnerabilities were found" in """${log}"""
85     Set Test Variable  ${STATUS}
86
87 Delete Scan Job
88     ${result}=        Run Process  kubectl  delete  job  kube-hunter
89     Should Be Equal As Integers  ${result.rc}  0
90
91 Should Discover No Vulnerabilities
92     Return From Keyword If  ${STATUS}
93     Fail                    Vulnerabilities discovered
94     ...                     non-critical
95
96 Run Scan Within Pod
97     ${result}=        Run Process  kubectl  apply  -f  ${CURDIR}/job.yaml
98     Should Be Equal As Integers  ${result.rc}  0
99     ${result}=        Run Process  kubectl  wait  --for\=condition\=complete
100     ...                            --timeout\=15m  job/kube-hunter
101     Should Be Equal As Integers  ${result.rc}  0
102     ${result}=        Run Process  kubectl  get  pods  --selector\=job-name\=kube-hunter
103     ...                            -o  jsonpath\={.items[*].metadata.name}
104     Should Be Equal As Integers  ${result.rc}  0
105     ${result}=        Run Process  kubectl  logs  ${result.stdout}
106     ...                 stdout=pod.log
107     Copy File         pod.log  ${REPORTDIR}/
108     Should Be Equal As Integers  ${result.rc}  0
109     Set Scan Status  ${result.stdout}
110
111 Run Node Scan
112     ${addrs}=         Get Remote Addresses
113     ${result}=        Run Process  kube-hunter  --remote  @{addrs}
114     ...                 stdout=node.log
115     Copy File         node.log  ${REPORTDIR}/
116     Should Be Equal As Integers  ${result.rc}  0
117     Set Scan Status  ${result.stdout}
118
119 Run Cluster Scan
120     ${addr}=          Get Cluster Address
121     ${result}=        Run Process  kube-hunter  --remote  ${addr}
122     ...                 stdout=cluster.log
123     Copy File         cluster.log  ${REPORTDIR}/
124     Should Be Equal As Integers  ${result.rc}  0
125     Set Scan Status  ${result.stdout}