blob: 4132e6cadbf7067ca03f681326785c16cb8ad914 [file] [log] [blame]
Instrumental32cdd552018-07-19 13:29:32 -05001#!/bin/bash
Instrumental7a1817b2018-11-05 11:11:15 -06002#########
3# ============LICENSE_START====================================================
4# org.onap.aaf
5# ===========================================================================
6# Copyright (c) 2017 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#
Instrumental9fe11532018-10-23 17:40:47 -050021# This script is run when starting client Container.
Instrumental32cdd552018-07-19 13:29:32 -050022# It needs to cover the cases where the initial data doesn't exist, and when it has already been configured (don't overwrite)
23#
Instrumental4d0a0452019-11-14 12:14:16 -060024JAVA=${JAVA_HOME}/bin/java
Instrumental32cdd552018-07-19 13:29:32 -050025AAF_INTERFACE_VERSION=2.1
26
27# Extract Name, Domain and NS from FQI
28FQIA=($(echo ${APP_FQI} | tr '@' '\n'))
29FQI_SHORT=${FQIA[0]}
30FQI_DOMAIN=${FQIA[1]}
31# Reverse DOMAIN for NS
32FQIA_E=($(echo ${FQI_DOMAIN} | tr '.' '\n'))
33for (( i=( ${#FQIA_E[@]} -1 ); i>0; i-- )); do
34 NS=${NS}${FQIA_E[i]}'.'
35done
36NS=${NS}${FQIA_E[0]}
Instrumental365638c2018-10-01 15:26:03 -050037CONFIG="/opt/app/aaf_config"
Instrumental49525302018-10-06 20:32:59 -050038OSAAF="/opt/app/osaaf"
39LOCAL="$OSAAF/local"
Instrumental365638c2018-10-01 15:26:03 -050040DOT_AAF="$HOME/.aaf"
41SSO="$DOT_AAF/sso.props"
Instrumentalbe7e0d12019-04-04 21:42:19 -050042
43if [ -e "$CONFIG" ]; then
44 CONFIG_BIN="$CONFIG/bin"
45else
46 CONFIG_BIN="."
47fi
48
Instrumentalb3a68142019-07-24 14:42:22 -050049AGENT_JAR="$CONFIG_BIN/aaf-cadi-aaf-*-full.jar"
Instrumentalbe7e0d12019-04-04 21:42:19 -050050
Instrumentalb3a68142019-07-24 14:42:22 -050051JAVA_AGENT="$JAVA -Dcadi_loglevel=DEBUG -Dcadi_etc_dir=${LOCAL} -Dcadi_log_dir=${LOCAL} -jar $AGENT_JAR "
Instrumental32cdd552018-07-19 13:29:32 -050052
53# Setup SSO info for Deploy ID
54function sso_encrypt() {
Instrumentalb3a68142019-07-24 14:42:22 -050055 $JAVA_AGENT cadi digest ${1} $DOT_AAF/keyfile
Instrumental32cdd552018-07-19 13:29:32 -050056}
57
Instrumentalb3a68142019-07-24 14:42:22 -050058# Setup Bash, first time only
59if [ ! -e "$HOME/.bashrc" ] || [ -z "$(grep agent $HOME/.bashrc)" ]; then
60 echo "alias agent='$CONFIG_BIN/agent.sh agent \$*'" >>$HOME/.bashrc
61 chmod a+x $CONFIG_BIN/agent.sh
62 . $HOME/.bashrc
63fi
Instrumental4ac37bf2019-04-10 13:01:30 -050064if [ ! -e "$DOT_AAF/truststoreONAPall.jks" ]; then
65 mkdir -p $DOT_AAF
66 base64 -d $CONFIG/cert/truststoreONAPall.jks.b64 > $DOT_AAF/truststoreONAPall.jks
67fi
Instrumental365638c2018-10-01 15:26:03 -050068
Instrumental49525302018-10-06 20:32:59 -050069# Create Deployer Info, located at /root/.aaf
Instrumental365638c2018-10-01 15:26:03 -050070if [ ! -e "$DOT_AAF/keyfile" ]; then
Instrumentalb3a68142019-07-24 14:42:22 -050071 $JAVA_AGENT cadi keygen $DOT_AAF/keyfile
Instrumental365638c2018-10-01 15:26:03 -050072 chmod 400 $DOT_AAF/keyfile
Instrumental69549ce2019-08-05 14:28:17 -050073 echo "cadi_keyfile=$DOT_AAF/keyfile" > ${SSO}
Instrumental4ac37bf2019-04-10 13:01:30 -050074
75 # Add Deployer Creds to Root's SSO
76 DEPLOY_FQI="${DEPLOY_FQI:=$app_id}"
Instrumental69549ce2019-08-05 14:28:17 -050077 echo "aaf_id=${DEPLOY_FQI}" >> ${SSO}
Instrumental32cdd552018-07-19 13:29:32 -050078 if [ ! "${DEPLOY_PASSWORD}" = "" ]; then
79 echo aaf_password=enc:$(sso_encrypt ${DEPLOY_PASSWORD}) >> ${SSO}
80 fi
Instrumentalbe7e0d12019-04-04 21:42:19 -050081
Instrumental4ac37bf2019-04-10 13:01:30 -050082 # Cover case where using app.props
Instrumentalb3a68142019-07-24 14:42:22 -050083 aaf_locator_container_ns=${aaf_locator_container_ns:=$CONTAINER_NS}
84 if [ "$aaf_locator_container" = "docker" ]; then
85 echo "aaf_locate_url=https://aaf-locate:8095" >> ${SSO}
86 echo "aaf_url_cm=https://aaf-cm:8150" >> ${SSO}
87 echo "aaf_url=https://aaf-service:8100" >> ${SSO}
88 else
89 echo "aaf_locate_url=https://$aaf-locator.${CONTAINER_NS}:8095" >> ${SSO}
90 echo "aaf_url_cm=https://AAF_LOCATE_URL/%CNS.%NS.cm:2.1" >> ${SSO}
91 echo "aaf_url=https://AAF_LOCATE_URL/%CNS.%NS.service:2.1" >> ${SSO}
92 fi
Instrumental365638c2018-10-01 15:26:03 -050093
Instrumentalb3a68142019-07-24 14:42:22 -050094 echo "cadi_truststore=$DOT_AAF/truststoreONAPall.jks" >> ${SSO}
95 echo "cadi_truststore_password=changeit" >> ${SSO}
96 echo "cadi_latitude=${LATITUDE}" >> ${SSO}
97 echo "cadi_longitude=${LONGITUDE}" >> ${SSO}
98 echo "hostname=${aaf_locator_fqdn}" >> ${SSO}
99
100 # Push in all AAF and CADI properties to SSO
Instrumental4ac37bf2019-04-10 13:01:30 -0500101 for E in $(env); do
102 if [ "${E:0:4}" = "aaf_" ] || [ "${E:0:5}" = "cadi_" ]; then
103 # Use Deployer ID in ${SSO}
104 if [ "app_id" != "${E%=*}" ]; then
105 S="${E/_helm/.helm}"
106 S="${S/_oom/.oom}"
107 echo "$S" >> ${SSO}
108 fi
109 fi
110 done
111
Instrumentalb3a68142019-07-24 14:42:22 -0500112 . ${SSO}
Instrumental284ad0a2018-10-24 07:01:09 -0500113 echo "Caller Properties Initialized"
114 INITIALIZED="true"
Instrumentalb3a68142019-07-24 14:42:22 -0500115 echo "cat SSO"
116 cat ${SSO}
Instrumental32cdd552018-07-19 13:29:32 -0500117fi
Instrumentalb3a68142019-07-24 14:42:22 -0500118
119# Check for local dir
120if [ -d $LOCAL ]; then
121 echo "$LOCAL exists"
122else
123 mkdir -p $LOCAL
124 echo "Created $LOCAL"
125fi
126
127cd $LOCAL
128echo "Existing files in $LOCAL"
129ls -l
Instrumentalf64f4822019-04-30 10:19:21 -0500130
131# Should we clean up?
Instrumentalb3a68142019-07-24 14:42:22 -0500132if [ "${VERSION}" != "$(cat ${LOCAL}/VERSION 2> /dev/null)" ]; then
Instrumentalf64f4822019-04-30 10:19:21 -0500133 echo "Clean up directory ${LOCAL}"
134 rm -Rf ${LOCAL}/*
135fi
136echo "${VERSION}" > $LOCAL/VERSION
Instrumental32cdd552018-07-19 13:29:32 -0500137
Instrumentalb3a68142019-07-24 14:42:22 -0500138echo "Namespace is ${NS}"
Instrumental32cdd552018-07-19 13:29:32 -0500139# Only initialize once, automatically...
Instrumental365638c2018-10-01 15:26:03 -0500140if [ ! -e $LOCAL/${NS}.props ]; then
Instrumental9fe11532018-10-23 17:40:47 -0500141 echo "#### Create Configuration files "
Instrumentalb3a68142019-07-24 14:42:22 -0500142 $JAVA_AGENT config $APP_FQI $APP_FQDN
Instrumental49525302018-10-06 20:32:59 -0500143 cat $LOCAL/$NS.props
Instrumental32cdd552018-07-19 13:29:32 -0500144
Instrumental9fe11532018-10-23 17:40:47 -0500145 echo
146 echo "#### Certificate Authorization Artifact"
Instrumental4ac37bf2019-04-10 13:01:30 -0500147 # TMP=$(mktemp)
148 TMP=$LOCAL/agent.log
Instrumentalb3a68142019-07-24 14:42:22 -0500149 $JAVA_AGENT read ${APP_FQI} ${APP_FQDN} | tee $TMP
Instrumental4ac37bf2019-04-10 13:01:30 -0500150
Instrumental9fe11532018-10-23 17:40:47 -0500151 if [ -n "$(grep 'Namespace:' $TMP)" ]; then
152 echo "#### Place Certificates (by deployer)"
Instrumentalb3a68142019-07-24 14:42:22 -0500153 $JAVA_AGENT place $APP_FQI $APP_FQDN
Instrumental9fe11532018-10-23 17:40:47 -0500154
Instrumentalb3a68142019-07-24 14:42:22 -0500155 if [ -z "$(grep cadi_alias $NS.cred.props)" ]; then
156 echo "FAILED to get Certificate"
157 INITIALIZED="false"
158 else
159 echo "Obtained Certificates"
160 echo "#### Validate Configuration and Certificate with live call"
161 $JAVA_AGENT validate cadi_prop_files=${NS}.props
162 INITIALIZED="true"
163 fi
Instrumental9fe11532018-10-23 17:40:47 -0500164 else
165 echo "#### Certificate Authorization Artifact must be valid to continue"
166 fi
167 rm $TMP
Instrumentalb3a68142019-07-24 14:42:22 -0500168else
169 INITIALIZED="true"
Instrumental32cdd552018-07-19 13:29:32 -0500170fi
171
172# Now run a command
173CMD=$2
Instrumental9fe11532018-10-23 17:40:47 -0500174if [ -z "$CMD" ]; then
Instrumental284ad0a2018-10-24 07:01:09 -0500175 if [ -n "$INITIALIZED" ]; then
176 echo "Initialization complete"
Instrumental284ad0a2018-10-24 07:01:09 -0500177 fi
Instrumental9fe11532018-10-23 17:40:47 -0500178else
Instrumental32cdd552018-07-19 13:29:32 -0500179 shift
180 shift
181 case "$CMD" in
182 ls)
183 echo ls requested
184 find /opt/app/osaaf -depth
185 ;;
186 cat)
187 if [ "$1" = "" ]; then
188 echo "usage: cat <file... ONLY files ending in .props>"
189 else
190 if [[ $1 == *.props ]]; then
191 echo
192 echo "## CONTENTS OF $3"
193 echo
194 cat "$1"
195 else
196 echo "### ERROR ####"
197 echo " \"cat\" may only be used with files ending with \".props\""
198 fi
199 fi
200 ;;
Instrumental69549ce2019-08-05 14:28:17 -0500201 read)
202 echo "## Read Artifacts"
203 $JAVA_AGENT read $APP_FQI $APP_FQDN cadi_prop_files=${SSO} cadi_loglevel=INFO
204 ;;
Instrumental87da9fe2018-07-19 16:44:02 -0500205 showpass)
206 echo "## Show Passwords"
Instrumental69549ce2019-08-05 14:28:17 -0500207 $JAVA_AGENT showpass $APP_FQI $APP_FQDN cadi_prop_files=${SSO} cadi_loglevel=ERROR
Instrumental87da9fe2018-07-19 16:44:02 -0500208 ;;
209 check)
Instrumentalbf3d3072019-02-18 14:01:07 -0600210 echo "## Check Certificate"
Instrumentalb3a68142019-07-24 14:42:22 -0500211 echo "$JAVA_AGENT check $APP_FQI $APP_FQDN cadi_prop_files=${LOCAL}/${NS}.props"
212 $JAVA_AGENT check $APP_FQI $APP_FQDN cadi_prop_files=${LOCAL}/${NS}.props
Instrumental87da9fe2018-07-19 16:44:02 -0500213 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500214 validate)
215 echo "## validate requested"
Instrumentalb3a68142019-07-24 14:42:22 -0500216 $JAVA_AGENT validate $APP_FQI $APP_FQDN
217 ;;
218 place)
219 echo "## Renew Certificate"
220 $JAVA_AGENT place $APP_FQI $APP_FQDN cadi_prop_files=${SSO}
Instrumental32cdd552018-07-19 13:29:32 -0500221 ;;
Instrumentalbf3d3072019-02-18 14:01:07 -0600222 renew)
223 echo "## Renew Certificate"
Instrumentalb3a68142019-07-24 14:42:22 -0500224 $JAVA_AGENT place $APP_FQI $APP_FQDN
Instrumentalbf3d3072019-02-18 14:01:07 -0600225 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500226 bash)
Instrumental32cdd552018-07-19 13:29:32 -0500227 shift
Instrumental365638c2018-10-01 15:26:03 -0500228 cd $LOCAL || exit
Instrumental12414fe2019-01-22 10:27:32 -0600229 exec bash "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500230 ;;
231 setProp)
Instrumental365638c2018-10-01 15:26:03 -0500232 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500233 FILES=$(grep -l "$1" ./*.props)
Instrumental9fe11532018-10-23 17:40:47 -0500234 if [ -z "$FILES" ]; then
235 if [ -z "$3" ]; then
236 FILES=${NS}.props
237 else
238 FILES="$3"
239 fi
Instrumental32cdd552018-07-19 13:29:32 -0500240 ADD=Y
241 fi
242 for F in $FILES; do
Instrumental32cdd552018-07-19 13:29:32 -0500243 if [ "$ADD" = "Y" ]; then
Instrumental9fe11532018-10-23 17:40:47 -0500244 echo "Changing $1 to $F"
245 echo "$1=$2" >> $F
Instrumental32cdd552018-07-19 13:29:32 -0500246 else
Instrumental9fe11532018-10-23 17:40:47 -0500247 echo "Changing $1 in $F"
Instrumental6095e292018-09-06 13:27:15 -0500248 sed -i.backup -e "s/\\(${1}.*=\\).*/\\1${2}/" $F
Instrumental32cdd552018-07-19 13:29:32 -0500249 fi
250 cat $F
251 done
252 ;;
253 encrypt)
Instrumental365638c2018-10-01 15:26:03 -0500254 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500255 echo $1
256 FILES=$(grep -l "$1" ./*.props)
257 if [ "$FILES" = "" ]; then
Instrumental365638c2018-10-01 15:26:03 -0500258 FILES=$LOCAL/${NS}.cred.props
Instrumental32cdd552018-07-19 13:29:32 -0500259 ADD=Y
260 fi
261 for F in $FILES; do
262 echo "Changing $1 in $F"
263 if [ "$2" = "" ]; then
264 read -r -p "Password (leave blank to cancel): " -s ORIG_PW
265 echo " "
266 if [ "$ORIG_PW" = "" ]; then
267 echo canceling...
268 break
269 fi
270 else
271 ORIG_PW="$2"
272 fi
Instrumental9fe11532018-10-23 17:40:47 -0500273 PWD=$($JAVA_CADI digest "$ORIG_PW" $LOCAL/${NS}.keyfile)
Instrumental32cdd552018-07-19 13:29:32 -0500274 if [ "$ADD" = "Y" ]; then
275 echo "$1=enc:$PWD" >> $F
276 else
277 sed -i.backup -e "s/\\($1.*enc:\\).*/\\1$PWD/" $F
278 fi
279 cat $F
280 done
281 ;;
282 taillog)
283 sh /opt/app/osaaf/logs/taillog
284 ;;
Instrumentalbd7def72019-04-03 08:25:28 -0500285 testConnectivity|testconnectivity)
286 echo "--- Test Connectivity ---"
Instrumentalbe7e0d12019-04-04 21:42:19 -0500287 $JAVA -cp $CONFIG_BIN/aaf-auth-cmd-*-full.jar org.onap.aaf.cadi.aaf.TestConnectivity $LOCAL/org.osaaf.aaf.props
Instrumentalbd7def72019-04-03 08:25:28 -0500288 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500289 --help | -?)
290 case "$1" in
291 "")
292 echo "--- Agent Container Comands ---"
293 echo " ls - Lists all files in Configuration"
294 echo " cat <file.props>> - Shows the contents (Prop files only)"
295 echo " validate - Runs a test using Configuration"
296 echo " setProp <tag> [<value>] - set value on 'tag' (if no value, it will be queried from config)"
297 echo " encrypt <tag> [<pass>] - set passwords on Configuration (if no pass, it will be queried)"
298 echo " bash - run bash in Container"
299 echo " Note: the following aliases are preset"
300 echo " cadi - CADI CmdLine tool"
301 echo " agent - Agent Java tool (see above help)"
302 echo ""
303 echo " --help|-? [cadi|agent] - This help, cadi help or agent help"
304 ;;
305 cadi)
306 echo "--- cadi Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500307 $JAVA_CADI
Instrumental32cdd552018-07-19 13:29:32 -0500308 ;;
309 agent)
310 echo "--- agent Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500311 $JAVA_AGENT
Instrumental32cdd552018-07-19 13:29:32 -0500312 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500313 aafcli)
314 echo "--- aafcli Tool Comands ---"
315 $JAVA_AAFCLI
316 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500317 esac
318 echo ""
319 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500320 ### Possible Dublin
321 # sample)
322 # echo "--- run Sample Servlet App ---"
Instrumentalbe7e0d12019-04-04 21:42:19 -0500323 # $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -cp $CONFIG_BIN/aaf-auth-cmd-*-full.jar:$CONFIG_BIN/aaf-cadi-servlet-sample-*-sample.jar org.onap.aaf.sample.cadi.jetty.JettyStandalone ${NS}.props
Instrumental9fe11532018-10-23 17:40:47 -0500324 # ;;
Instrumental32cdd552018-07-19 13:29:32 -0500325 *)
Instrumental9fe11532018-10-23 17:40:47 -0500326 $JAVA_AGENT "$CMD" "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500327 ;;
328 esac
329fi