561dea246f62b3968aee1558e91023e87ce7920d
[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     Login With Public Key  ${USERNAME}  ${SSH_KEYFILE}
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     Should Be True    ${STATUS}
93
94 Run Scan Within Pod
95     ${result}=        Run Process  kubectl  apply  -f  job.yaml
96     Should Be Equal As Integers  ${result.rc}  0
97     ${result}=        Run Process  kubectl  wait  --for\=condition\=complete
98     ...                            --timeout\=15m  job/kube-hunter
99     Should Be Equal As Integers  ${result.rc}  0
100     ${result}=        Run Process  kubectl  get  pods  --selector\=job-name\=kube-hunter
101     ...                            -o  jsonpath\={.items[*].metadata.name}
102     Should Be Equal As Integers  ${result.rc}  0
103     ${result}=        Run Process  kubectl  logs  ${result.stdout}
104     ...                 stdout=pod.log
105     Copy File         pod.log  ${REPORTDIR}/
106     Should Be Equal As Integers  ${result.rc}  0
107     Set Scan Status  ${result.stdout}
108
109 Run Node Scan
110     ${addrs}=         Get Remote Addresses
111     ${result}=        Run Process  kube-hunter  --remote  @{addrs}
112     ...                 stdout=node.log
113     Copy File         node.log  ${REPORTDIR}/
114     Should Be Equal As Integers  ${result.rc}  0
115     Set Scan Status  ${result.stdout}
116
117 Run Cluster Scan
118     ${addr}=          Get Cluster Address
119     ${result}=        Run Process  kube-hunter  --remote  ${addr}
120     ...                 stdout=cluster.log
121     Copy File         cluster.log  ${REPORTDIR}/
122     Should Be Equal As Integers  ${result.rc}  0
123     Set Scan Status  ${result.stdout}