dbbc6a50085e3345b2de9c6003e51b5169ec9419
[nonrtric.git] / test / common / istio_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2021 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 # This is a script that contains function to handle helm on localhost
21
22
23 ################ Test engine functions ################
24
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __ISTIO_imagesetup() {
29         :
30 }
31
32 # Pull image from remote repo or use locally built image
33 # arg: <pull-policy-override> <pull-policy-original>
34 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35 # <pull-policy-original> Shall be used for images that does not allow overriding
36 # Both var may contain: 'remote', 'remote-remove' or 'local'
37 __ISTIO_imagepull() {
38         :
39 }
40
41 # Build image (only for simulator or interfaces stubs owned by the test environment)
42 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
44 __ISTIO_imagebuild() {
45         :
46 }
47
48 # Generate a string for each included image using the app display name and a docker images format string
49 # If a custom image repo is used then also the source image from the local repo is listed
50 # arg: <docker-images-format-string> <file-to-append>
51 __ISTIO_image_data() {
52         :
53 }
54
55 # Scale kubernetes resources to zero
56 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
57 # This function is called for apps fully managed by the test script
58 __ISTIO_kube_scale_zero() {
59         :
60 }
61
62 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
63 # This function is called for prestarted apps not managed by the test script.
64 __ISTIO_kube_scale_zero_and_wait() {
65         :
66 }
67
68 # Delete all kube resouces for the app
69 # This function is called for apps managed by the test script.
70 __ISTIO_kube_delete_all() {
71         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ISTIO
72         __kube_delete_all_resources $KUBE_A1SIM_NAMESPACE autotest ISTIO
73         __kube_delete_all_resources $KUBE_ONAP_NAMESPACE autotest ISTIO
74         __kube_delete_all_resources $KUBE_KEYCLOAK_NAMESPACE autotest ISTIO
75         __kube_delete_all_resources $KUBE_SDNC_NAMESPACE autotest ISTIO
76         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest ISTIO
77 }
78
79 # Store docker logs
80 # This function is called for apps managed by the test script.
81 # args: <log-dir> <file-prexix>
82 __ISTIO_store_docker_logs() {
83         :
84 }
85
86 # Initial setup of protocol, host and ports
87 # This function is called for apps managed by the test script.
88 # args: -
89 __ISTIO_initial_setup() {
90         # See jwt-info.txt in simulator-group/kubeproxy for detailed info
91         KUBE_PROXY_CURL_JWT=$ISTIO_GENERIC_JWT
92         KUBE_PROXY_ISTIO_JWKS_KEYS=$ISTIO_GENERIC_JWKS_KEY
93 }
94
95 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
96 # For docker, the namespace shall be excluded
97 # This function is called for apps managed by the test script as well as for prestarted apps.
98 # args: -
99 __ISTIO_statisics_setup() {
100         :
101 }
102
103 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
104 # args: -
105 __ISTIO_test_requirements() {
106
107         kubectl $KUBECONF get requestauthentications -A &> /dev/null
108         if [ $? -ne 0 ]; then
109                 echo $RED" Istio api: kubectl get requestauthentications is not installed"
110                 exit 1
111         fi
112         kubectl $KUBECONF get authorizationpolicies -A &> /dev/null
113         if [ $? -ne 0 ]; then
114                 echo $RED" Istio api: kubectl get authorizationpolicies is not installed"
115                 exit 1
116         fi
117 }
118
119 #######################################################
120
121
122 # Enable istio on namespace
123 # arg: <namespace>
124 istio_enable_istio_namespace() {
125         __log_conf_start $@
126     if [ $# -ne 1 ]; then
127         __print_err "<namespace>" $@
128         return 1
129     fi
130         __kube_create_namespace $1
131         __kube_label_non_ns_instance ns $1 "istio-injection=enabled"
132         __log_conf_ok
133         return 0
134 }
135
136 # Request authorization by jwksuri
137 # args: <app> <namespace> <realm>
138 istio_req_auth_by_jwksuri() {
139         __log_conf_start $@
140     if [ $# -ne 3 ]; then
141         __print_err "<app> <namespace> <realm>" $@
142         return 1
143     fi
144         name="ra-jwksuri-"$3"-"$1"-"$2
145         export  ISTIO_TEMPLATE_REPLACE_RA_NAME=$(echo $name | tr '[:upper:]' '[:lower:]')
146         export  ISTIO_TEMPLATE_REPLACE_RA_NS=$2
147         export  ISTIO_TEMPLATE_REPLACE_RA_APP_NAME=$1
148         export  ISTIO_TEMPLATE_REPLACE_RA_ISSUER=$KEYCLOAK_ISSUER_PATH$KEYCLOAK_TOKEN_URL_PREFIX/$3
149         export  ISTIO_TEMPLATE_REPLACE_RA_JWKSURI=$KEYCLOAK_SERVICE_PATH$KEYCLOAK_TOKEN_URL_PREFIX/$3/protocol/openid-connect/certs
150         inputfile=$SIM_GROUP/$ISTIO_COMPOSE_DIR/ra-jwksuri-template.yaml
151         outputfile=tmp/$ISTIO_TEMPLATE_REPLACE_RA_NAME".yaml"
152         envsubst < $inputfile > $outputfile
153         if [ $? -ne 0 ]; then
154                 __log_conf_fail_general "Cannot substitute yaml: $inputfile"
155                 return 1
156         fi
157         kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
158         if [ $? -ne 0 ]; then
159                 __log_conf_fail_general "Cannot apply yaml: $outputfile"
160                 return 1
161         fi
162         __log_conf_ok
163         return 0
164 }
165
166 # Request authorization by jwks (inline keys)
167 # args: <app> <namespace> <issuer> <key>
168 istio_req_auth_by_jwks() {
169         __log_conf_start $@
170     if [ $# -ne 4 ]; then
171         __print_err "<app> <namespace> <issuer> <key>" $@
172         return 1
173     fi
174         name="ra-jwks-"$3"-"$1"-"$2
175         export  ISTIO_TEMPLATE_REPLACE_RA_NAME=$(echo $name | tr '[:upper:]' '[:lower:]')
176         export  ISTIO_TEMPLATE_REPLACE_RA_NS=$2
177         export  ISTIO_TEMPLATE_REPLACE_RA_APP_NAME=$1
178         export  ISTIO_TEMPLATE_REPLACE_RA_ISSUER=$3
179         export  ISTIO_TEMPLATE_REPLACE_RA_JWKS=$4
180         inputfile=$SIM_GROUP/$ISTIO_COMPOSE_DIR/ra-jwks-template.yaml
181         outputfile=tmp/$ISTIO_TEMPLATE_REPLACE_RA_NAME".yaml"
182         envsubst < $inputfile > $outputfile
183         if [ $? -ne 0 ]; then
184                 __log_conf_fail_general "Cannot substitute yaml: $inputfile"
185                 return 1
186         fi
187         kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
188         if [ $? -ne 0 ]; then
189                 __log_conf_fail_general "Cannot apply yaml: $outputfile"
190                 return 1
191         fi
192         __log_conf_ok
193         return 0
194 }
195
196 # Authorization policy - by realm
197 # args: <app> <namespace> <realam> [<client-id> <client-role>]
198 istio_auth_policy_by_realm() {
199         __log_conf_start $@
200     if [ $# -ne 3 ] && [ $# -ne 5 ]; then
201         __print_err "<app> <namespace> <realam> [<client-id> <client-role>]" $@
202         return 1
203     fi
204         name="ap-realm-"$3"-"$1"-"$2
205         export  ISTIO_TEMPLATE_REPLACE_AP_NAME=$(echo $name | tr '[:upper:]' '[:lower:]')
206         export  ISTIO_TEMPLATE_REPLACE_AP_NS=$2
207         export  ISTIO_TEMPLATE_REPLACE_AP_APP_NAME=$1
208         export  ISTIO_TEMPLATE_REPLACE_AP_PRINCIPAL="$KEYCLOAK_ISSUER_PATH$KEYCLOAK_TOKEN_URL_PREFIX/$3/*"
209         inputfile=$SIM_GROUP/$ISTIO_COMPOSE_DIR/ap-principal-template.yaml
210         outputfile=tmp/$ISTIO_TEMPLATE_REPLACE_AP_NAME".yaml"
211         envsubst < $inputfile > $outputfile
212         if [ $? -ne 0 ]; then
213                 __log_conf_fail_general "Cannot substitute yaml: $inputfile"
214                 return 1
215         fi
216         if [ $# -gt 3 ]; then
217                 export  ISTIO_TEMPLATE_REPLACE_AP_CLIENT=$4
218                 export  ISTIO_TEMPLATE_REPLACE_AP_ROLE=$5
219                 inputfile=$SIM_GROUP/$ISTIO_COMPOSE_DIR/ap-role-snippet.yaml
220                 envsubst < $inputfile >> $outputfile
221                 if [ $? -ne 0 ]; then
222                         __log_conf_fail_general "Cannot substitute yaml: $inputfile"
223                         return 1
224                 fi
225         fi
226
227         kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
228         if [ $? -ne 0 ]; then
229                 __log_conf_fail_general "Cannot apply yaml: $outputfile"
230                 return 1
231         fi
232         __log_conf_ok
233         return 0
234 }
235
236 # Authorization policy - by issuer
237 # args: <app> <namespace> <issuer>
238 istio_auth_policy_by_issuer() {
239         __log_conf_start $@
240     if [ $# -ne 3 ]; then
241         __print_err "<app> <namespace> <issuer>" $@
242         return 1
243     fi
244         name="ap-iss-"$3"-"$1"-"$2
245         export  ISTIO_TEMPLATE_REPLACE_AP_NAME=$(echo $name | tr '[:upper:]' '[:lower:]')
246         export  ISTIO_TEMPLATE_REPLACE_AP_NS=$2
247         export  ISTIO_TEMPLATE_REPLACE_AP_APP_NAME=$1
248         export  ISTIO_TEMPLATE_REPLACE_AP_PRINCIPAL="$3/*"
249         inputfile=$SIM_GROUP/$ISTIO_COMPOSE_DIR/ap-principal-template.yaml
250         outputfile=tmp/$ISTIO_TEMPLATE_REPLACE_AP_NAME".yaml"
251         envsubst < $inputfile > $outputfile
252         if [ $? -ne 0 ]; then
253                 __log_conf_fail_general "Cannot substitute yaml: $inputfile"
254                 return 1
255         fi
256         kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
257         if [ $? -ne 0 ]; then
258                 __log_conf_fail_general "Cannot apply yaml: $outputfile"
259                 return 1
260         fi
261         __log_conf_ok
262         return 0
263 }
264
265