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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=================================================
20 # This is a script that contain functions to handle istio configuration
23 ################ Test engine functions ################
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() {
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'
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() {
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() {
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() {
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 pre-started apps not managed by the test script.
64 __ISTIO_kube_scale_zero_and_wait() {
68 # Delete all kube resources 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
80 # This function is called for apps managed by the test script.
81 # args: <log-dir> <file-prefix>
82 __ISTIO_store_docker_logs() {
86 # Initial setup of protocol, host and ports
87 # This function is called for apps managed by the test script.
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
95 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes 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 pre-started apps.
99 __ISTIO_statistics_setup() {
103 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
105 __ISTIO_test_requirements() {
107 kubectl $KUBECONF get requestauthentications -A &> /dev/null
108 if [ $? -ne 0 ]; then
109 echo $RED" Istio api: kubectl get requestauthentications is not installed"
112 kubectl $KUBECONF get authorizationpolicies -A &> /dev/null
113 if [ $? -ne 0 ]; then
114 echo $RED" Istio api: kubectl get authorizationpolicies is not installed"
119 #######################################################
122 # Enable istio on namespace
124 istio_enable_istio_namespace() {
126 if [ $# -ne 1 ]; then
127 __print_err "<namespace>" $@
130 __kube_create_namespace $1
131 __kube_label_non_ns_instance ns $1 "istio-injection=enabled"
136 # Request authorization by jwksuri
137 # args: <app> <namespace> <realm>
138 istio_req_auth_by_jwksuri() {
140 if [ $# -ne 3 ]; then
141 __print_err "<app> <namespace> <realm>" $@
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"
157 kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
158 if [ $? -ne 0 ]; then
159 __log_conf_fail_general "Cannot apply yaml: $outputfile"
166 # Request authorization by jwks (inline keys)
167 # args: <app> <namespace> <issuer> <key>
168 istio_req_auth_by_jwks() {
170 if [ $# -ne 4 ]; then
171 __print_err "<app> <namespace> <issuer> <key>" $@
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"
187 kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
188 if [ $? -ne 0 ]; then
189 __log_conf_fail_general "Cannot apply yaml: $outputfile"
196 # Authorization policy - by realm
197 # args: <app> <namespace> <realm> [<client-id> <client-role>]
198 istio_auth_policy_by_realm() {
200 if [ $# -ne 3 ] && [ $# -ne 5 ]; then
201 __print_err "<app> <namespace> <realm> [<client-id> <client-role>]" $@
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"
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"
227 kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
228 if [ $? -ne 0 ]; then
229 __log_conf_fail_general "Cannot apply yaml: $outputfile"
236 # Authorization policy - by issuer
237 # args: <app> <namespace> <issuer>
238 istio_auth_policy_by_issuer() {
240 if [ $# -ne 3 ]; then
241 __print_err "<app> <namespace> <issuer>" $@
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"
256 kubectl $KUBECONF apply -f $outputfile &> tmp/kubeerr
257 if [ $? -ne 0 ]; then
258 __log_conf_fail_general "Cannot apply yaml: $outputfile"