Merge "Sample consumer to get kafka broker from ICS"
[nonrtric.git] / service-exposure / copy_tls_secret.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2023 Nordix Foundation.
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 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20 #
21
22 function usage()
23 {
24    echo ""
25    echo "Usage: $0 -n secretName -s sourceNamespace -d destinationNamespace"
26    echo -e "\t-n Name of the secret"
27    echo -e "\t-s Namespace of the secret"
28    echo -e "\t-d Namespace to copy the secret to"
29    exit 1
30 }
31
32 while getopts "n:s:d:" opt
33 do
34    case "$opt" in
35       n ) secretName="$OPTARG" ;;
36       s ) sourceNS="$OPTARG" ;;
37       d ) destinationNS="$OPTARG" ;;
38       ? ) usage ;;
39    esac
40 done
41
42 # Check if any of the paramters are empty
43 if [ -z "$secretName" ] || [ -z "$sourceNS" ] || [ -z "$destinationNS" ]
44 then
45    echo "Some or all of the parameters are empty";
46    usage
47 fi
48
49 # Check if the secret exits
50 kubectl get secret $secretName -n $sourceNS >/dev/null 2>/dev/null
51 if [ $? -ne 0 ]
52 then
53    echo "$secretName in $sourceNS does not exist"
54    usage
55 fi
56
57 # Check if the destination namespace exists
58 kubectl get ns $destinationNS >/dev/null 2>/dev/null
59 if [ $? -ne 0 ]
60 then
61    echo "$destinationNS does not exist"
62    usage
63 fi
64
65 # Begin script in case all parameters are correct
66 echo "Copying $secretName from $sourceNS to $destinationNS"
67
68 tlsCrt=$(kubectl get secret ${secretName} -n ${sourceNS} -o json -o=jsonpath="{.data.tls\.crt}")
69 tlsKey=$(kubectl get secret ${secretName} -n ${sourceNS} -o json -o=jsonpath="{.data.tls\.key}")
70 caCrt=$(kubectl get secret ${secretName} -n ${sourceNS} -o json -o=jsonpath="{.data.ca\.crt}")
71
72 kubectl apply -f - <<EOF
73 apiVersion: v1
74 data:
75   tls.crt: ${tlsCrt}
76   tls.key: ${tlsKey}
77   ca.crt: ${caCrt}
78 kind: Secret
79 metadata:
80   name: ${secretName}
81   namespace: ${destinationNS}
82 type: kubernetes.io/tls
83 EOF