Add client cert PEM files for authenticated requests
[dmaap/buscontroller.git] / dbc-client / misc / dbc-client
1 #!/bin/sh
2 #
3 # ============LICENSE_START==========================================
4 # org.onap.dmaap
5 # ===================================================================
6 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
7 # ===================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #        http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END============================================
20 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 #
22 #
23
24 umask 0022
25 TZ=GMT0
26 COMPONENT=dbc-client
27 APP_ROOT=${APP_ROOT:-/opt/app/$COMPONENT}
28 USER=root
29 export TZ
30 PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
31 export PATH
32 CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config}
33 CONTAINER_CONFIG=$CONFIGMAP_ROOT/conf/dbc-client.env
34 REQUESTID=${REQUESTID:-dbc-client}
35 DBC=${DBC:-dmaap-bc}
36 PROTO=${PROTO:-https}
37 PORT=${PORT:-8443}
38 WAIT_TO_EXIT=${WAIT_TO_EXIT:-N}
39 PEMDIR=${PEMDIR:-/opt/app/osaaf/local}
40 CAPEM=${CAPEM:-ca.pem}
41 CLIENTPEM=${CLIENTPEM:-client.pem}
42 KEYPEM=${KEYPEM:-key.pem}
43 CERTPWD=${CERTPWD:-"2U[iOZzMHI:.#tdCwlBqc;}S"}
44
45
46
47
48 config() {
49         echo "ENTER config"
50         set -x
51         if [ ! -d $APP_ROOT ]
52         then
53                 echo "Expected app root directory $APP_ROOT does not exist"
54                 exit 1
55         fi
56         cd $PEMDIR
57         pwd
58         ls -l
59         if [ -f $CAPEM  -a -f $CLIENTPEM -a -f $KEYPEM ]
60         then
61                 echo "--key $PEMDIR/$KEYPEM --cacert $PEMDIR/$CAPEM --cert $PEMDIR/${CLIENTPEM}:${CERTPWD}" > $PEMDIR/curl.cred
62                 CURLCRED="-K $PEMDIR/curl.cred"
63         else
64                 echo "Warning: PEM files for authorization not found!"
65                 CURLCRED=""
66         fi
67         echo "CURLCRED=$CURLCRED"
68         cd $APP_ROOT
69         find . -type f -exec ls -l {} \;
70         find $CONFIGMAP_ROOT -type f -exec ls -l {} \;
71         if [ !  -f $CONTAINER_CONFIG ]
72         then
73                 echo "WARNING: Expected env file $CONTAINER_CONFIG not found. Default behaviors in effect"
74         else
75                 source $CONTAINER_CONFIG
76         fi
77
78         if [ "$WAIT_TO_EXIT" != "Y" ]
79         then
80                 echo "Creating $APP_ROOT/ok_to_exit so no waiting..."
81                 > $APP_ROOT/ok_to_exit
82         else
83                 echo "Not creating $APP_ROOT/ok_to_exit"
84         fi      
85         
86         set +x
87 }
88
89
90 init() {
91         set -x
92         echo "ENTER init"
93         if [ ! -d $CONFIGMAP_ROOT ]
94         then
95                 echo $CONFIGMAP_ROOT does not exist
96                 return
97         fi
98
99         #loop on get /dmaap until we get a good response to indicate other provisioning can continue
100         rc=${RESP:-999}
101         while [ $rc != "200" ]
102         do
103                 sleep 10
104                 rc=`curl $CURLCRED -s -o /dev/null -I -w "%{http_code}" -X GET -H "X-ECOMP-RequestID: $REQUESTID" -H "Content-Type: application/json" ${PROTO}://${DBC}:${PORT}/webapi/dmaap`
105                 echo "get dmaap response=${rc}"
106         done
107         set +x
108 }
109 dopost() {
110         set -x
111         RETRY_TIME=60
112
113         loop=true
114
115         while [ $loop = true ]
116         do
117                 rc=`curl $CURLCRED -s -X POST -w "%{http_code}" -H "X-ECOMP-RequestID: $REQUESTID" -H "Content-Type: application/json" -d @${1} ${PROTO}://${DBC}:${PORT}/webapi/${2} -o /dev/null`
118                 case $rc in
119                 200 | 201 | 409 )
120                         echo "response=$rc"
121                         loop=false
122                         ;;
123                 * )
124                         echo "`date`: http response=$rc.  Will retry after $RETRY_TIME seconds"
125                         sleep $RETRY_TIME
126                         ;;
127                 esac
128         done
129         set +x
130
131 }
132 doprov() {
133
134         set -x
135         cd $CONFIGMAP_ROOT
136         pwd
137         # order is important in this next list
138         for uri in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs
139         do
140                 if [ -d ${uri} ]
141                 then
142                         for j in `ls ${uri}/*.json`
143                         do
144                                 echo "POST $j to $uri"
145                                 dopost $j $uri
146                         done
147                 fi
148         done
149         set +x
150 }
151 delay() {
152         echo "DELAY=$DELAY"
153         if [ ! -z "$DELAY" ]
154         then
155                 sleep $DELAY
156         fi
157 }
158
159 delay
160 config
161 init
162 doprov
163
164 echo "Check $APP_ROOT/ok_to_exit"
165 while [ ! -f $APP_ROOT/ok_to_exit ]
166 do
167         echo "$APP_ROOT/ok_to_exit does not exist.  Sticking around for debugging..."
168         sleep 10
169 done
170 exit 0