blob: b69646dfc7019b75fd5429c62615182527010491 [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#
24JAVA=/usr/bin/java
25AAF_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"
42
Instrumental9fe11532018-10-23 17:40:47 -050043JAVA_CADI="$JAVA -cp $CONFIG/bin/aaf-auth-cmd-*-full.jar org.onap.aaf.cadi.CmdLine"
44JAVA_AGENT="$JAVA -cp $CONFIG/bin/aaf-auth-cmd-*-full.jar -Dcadi_prop_files=$SSO org.onap.aaf.cadi.configure.Agent"
45JAVA_AGENT_SELF="$JAVA -cp $CONFIG/bin/aaf-auth-cmd-*-full.jar -Dcadi_prop_files=$LOCAL/${NS}.props org.onap.aaf.cadi.configure.Agent"
46JAVA_AAFCLI="$JAVA -cp $CONFIG/bin/aaf-auth-cmd-*-full.jar -Dcadi_prop_files=$LOCAL/org.osaaf.aaf.props org.onap.aaf.auth.cmd.AAFcli"
47
Instrumental49525302018-10-06 20:32:59 -050048# Check for local dir
49if [ ! -d $LOCAL ]; then
50 mkdir -p $LOCAL
51 for D in bin logs; do
52 rsync -avzh --exclude=.gitignore $CONFIG/$D/* /opt/app/osaaf/$D
53 done
54fi
55
Instrumental365638c2018-10-01 15:26:03 -050056# Setup Bash, first time only
Instrumental9fe11532018-10-23 17:40:47 -050057if [ ! -e "$HOME/.bash_aliases" ] || [ -z "$(grep agent $HOME/.bash_aliases)" ]; then
58 echo "alias cadi='$JAVA_CADI \$*'" >>$HOME/.bash_aliases
Instrumental49525302018-10-06 20:32:59 -050059 echo "alias agent='$OSAAF/bin/agent.sh EMPTY \$*'" >>$HOME/.bash_aliases
Instrumental9fe11532018-10-23 17:40:47 -050060 echo "alias aafcli='$JAVA_AAFCLI \$*'" >>$HOME/.bash_aliases
Instrumental49525302018-10-06 20:32:59 -050061 chmod a+x $OSAAF/bin/agent.sh
Instrumental365638c2018-10-01 15:26:03 -050062 . $HOME/.bash_aliases
63fi
Instrumental32cdd552018-07-19 13:29:32 -050064
65# Setup SSO info for Deploy ID
66function sso_encrypt() {
Instrumental9fe11532018-10-23 17:40:47 -050067 $JAVA_CADI digest ${1} $DOT_AAF/keyfile
Instrumental32cdd552018-07-19 13:29:32 -050068}
69
Instrumental365638c2018-10-01 15:26:03 -050070
Instrumental49525302018-10-06 20:32:59 -050071# Create Deployer Info, located at /root/.aaf
Instrumental365638c2018-10-01 15:26:03 -050072if [ ! -e "$DOT_AAF/keyfile" ]; then
73 mkdir -p $DOT_AAF
Instrumental9fe11532018-10-23 17:40:47 -050074 $JAVA_CADI keygen $DOT_AAF/keyfile
Instrumental365638c2018-10-01 15:26:03 -050075 chmod 400 $DOT_AAF/keyfile
Instrumental32cdd552018-07-19 13:29:32 -050076 echo cadi_latitude=${LATITUDE} > ${SSO}
77 echo cadi_longitude=${LONGITUDE} >> ${SSO}
78 echo aaf_id=${DEPLOY_FQI} >> ${SSO}
79 if [ ! "${DEPLOY_PASSWORD}" = "" ]; then
80 echo aaf_password=enc:$(sso_encrypt ${DEPLOY_PASSWORD}) >> ${SSO}
81 fi
82 echo aaf_locate_url=https://${AAF_FQDN}:8095 >> ${SSO}
83 echo aaf_url=https://AAF_LOCATE_URL/AAF_NS.service:${AAF_INTERFACE_VERSION} >> ${SSO}
Instrumental365638c2018-10-01 15:26:03 -050084
85 base64 -d $CONFIG/cert/truststoreONAPall.jks.b64 > $DOT_AAF/truststoreONAPall.jks
86 echo "cadi_truststore=$DOT_AAF/truststoreONAPall.jks" >> ${SSO}
Instrumental32cdd552018-07-19 13:29:32 -050087 echo cadi_truststore_password=enc:$(sso_encrypt changeit) >> ${SSO}
Instrumental284ad0a2018-10-24 07:01:09 -050088 echo "Caller Properties Initialized"
89 INITIALIZED="true"
Instrumental32cdd552018-07-19 13:29:32 -050090fi
91
92# Only initialize once, automatically...
Instrumental365638c2018-10-01 15:26:03 -050093if [ ! -e $LOCAL/${NS}.props ]; then
Instrumental9fe11532018-10-23 17:40:47 -050094 echo "#### Create Configuration files "
95 $JAVA_AGENT config $APP_FQI \
Instrumental49525302018-10-06 20:32:59 -050096 aaf_url=https://AAF_LOCATE_URL/AAF_NS.locate:${AAF_INTERFACE_VERSION} \
97 cadi_etc_dir=$LOCAL
98 cat $LOCAL/$NS.props
Instrumental32cdd552018-07-19 13:29:32 -050099
Instrumental9fe11532018-10-23 17:40:47 -0500100 echo
101 echo "#### Certificate Authorization Artifact"
102 TMP=$(mktemp)
103 $JAVA_AGENT read ${APP_FQI} ${APP_FQDN} \
Instrumental49525302018-10-06 20:32:59 -0500104 cadi_prop_files=${SSO} \
Instrumental9fe11532018-10-23 17:40:47 -0500105 cadi_etc_dir=$LOCAL > $TMP
106 cat $TMP
107 echo
108 if [ -n "$(grep 'Namespace:' $TMP)" ]; then
109 echo "#### Place Certificates (by deployer)"
110 $JAVA_AGENT place ${APP_FQI} ${APP_FQDN} \
111 cadi_prop_files=${SSO} \
112 cadi_etc_dir=$LOCAL
113
114 echo "#### Validate Configuration and Certificate with live call"
115 $JAVA_AGENT_SELF validate
Instrumental284ad0a2018-10-24 07:01:09 -0500116 echo "Obtained Certificates"
117 INITIALIZED="true"
Instrumental9fe11532018-10-23 17:40:47 -0500118 else
119 echo "#### Certificate Authorization Artifact must be valid to continue"
120 fi
121 rm $TMP
Instrumental32cdd552018-07-19 13:29:32 -0500122fi
123
124# Now run a command
125CMD=$2
Instrumental9fe11532018-10-23 17:40:47 -0500126if [ -z "$CMD" ]; then
Instrumental284ad0a2018-10-24 07:01:09 -0500127 if [ -n "$INITIALIZED" ]; then
128 echo "Initialization complete"
129 else
130 $JAVA_AGENT
131 fi
Instrumental9fe11532018-10-23 17:40:47 -0500132else
Instrumental32cdd552018-07-19 13:29:32 -0500133 shift
134 shift
135 case "$CMD" in
136 ls)
137 echo ls requested
138 find /opt/app/osaaf -depth
139 ;;
140 cat)
141 if [ "$1" = "" ]; then
142 echo "usage: cat <file... ONLY files ending in .props>"
143 else
144 if [[ $1 == *.props ]]; then
145 echo
146 echo "## CONTENTS OF $3"
147 echo
148 cat "$1"
149 else
150 echo "### ERROR ####"
151 echo " \"cat\" may only be used with files ending with \".props\""
152 fi
153 fi
154 ;;
155 update)
156 for D in bin logs; do
Instrumental365638c2018-10-01 15:26:03 -0500157 rsync -uh --exclude=.gitignore $CONFIG/$D/* /opt/app/osaaf/$D
Instrumental32cdd552018-07-19 13:29:32 -0500158 done
159 ;;
Instrumental87da9fe2018-07-19 16:44:02 -0500160 showpass)
161 echo "## Show Passwords"
Instrumental9fe11532018-10-23 17:40:47 -0500162 $JAVA_AGENT showpass ${APP_FQI} ${APP_FQDN}
Instrumental87da9fe2018-07-19 16:44:02 -0500163 ;;
164 check)
Instrumental9fe11532018-10-23 17:40:47 -0500165 $JAVA_AGENT check ${APP_FQI} ${APP_FQDN}
Instrumental87da9fe2018-07-19 16:44:02 -0500166 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500167 validate)
168 echo "## validate requested"
Instrumental9fe11532018-10-23 17:40:47 -0500169 $JAVA_AGENT_SELF validate
Instrumental32cdd552018-07-19 13:29:32 -0500170 ;;
171 bash)
Instrumental32cdd552018-07-19 13:29:32 -0500172 shift
Instrumental365638c2018-10-01 15:26:03 -0500173 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500174 /bin/bash "$@"
175 ;;
176 setProp)
Instrumental365638c2018-10-01 15:26:03 -0500177 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500178 FILES=$(grep -l "$1" ./*.props)
Instrumental9fe11532018-10-23 17:40:47 -0500179 if [ -z "$FILES" ]; then
180 if [ -z "$3" ]; then
181 FILES=${NS}.props
182 else
183 FILES="$3"
184 fi
Instrumental32cdd552018-07-19 13:29:32 -0500185 ADD=Y
186 fi
187 for F in $FILES; do
Instrumental32cdd552018-07-19 13:29:32 -0500188 if [ "$ADD" = "Y" ]; then
Instrumental9fe11532018-10-23 17:40:47 -0500189 echo "Changing $1 to $F"
190 echo "$1=$2" >> $F
Instrumental32cdd552018-07-19 13:29:32 -0500191 else
Instrumental9fe11532018-10-23 17:40:47 -0500192 echo "Changing $1 in $F"
Instrumental6095e292018-09-06 13:27:15 -0500193 sed -i.backup -e "s/\\(${1}.*=\\).*/\\1${2}/" $F
Instrumental32cdd552018-07-19 13:29:32 -0500194 fi
195 cat $F
196 done
197 ;;
198 encrypt)
Instrumental365638c2018-10-01 15:26:03 -0500199 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500200 echo $1
201 FILES=$(grep -l "$1" ./*.props)
202 if [ "$FILES" = "" ]; then
Instrumental365638c2018-10-01 15:26:03 -0500203 FILES=$LOCAL/${NS}.cred.props
Instrumental32cdd552018-07-19 13:29:32 -0500204 ADD=Y
205 fi
206 for F in $FILES; do
207 echo "Changing $1 in $F"
208 if [ "$2" = "" ]; then
209 read -r -p "Password (leave blank to cancel): " -s ORIG_PW
210 echo " "
211 if [ "$ORIG_PW" = "" ]; then
212 echo canceling...
213 break
214 fi
215 else
216 ORIG_PW="$2"
217 fi
Instrumental9fe11532018-10-23 17:40:47 -0500218 PWD=$($JAVA_CADI digest "$ORIG_PW" $LOCAL/${NS}.keyfile)
Instrumental32cdd552018-07-19 13:29:32 -0500219 if [ "$ADD" = "Y" ]; then
220 echo "$1=enc:$PWD" >> $F
221 else
222 sed -i.backup -e "s/\\($1.*enc:\\).*/\\1$PWD/" $F
223 fi
224 cat $F
225 done
226 ;;
227 taillog)
228 sh /opt/app/osaaf/logs/taillog
229 ;;
230 --help | -?)
231 case "$1" in
232 "")
233 echo "--- Agent Container Comands ---"
234 echo " ls - Lists all files in Configuration"
235 echo " cat <file.props>> - Shows the contents (Prop files only)"
236 echo " validate - Runs a test using Configuration"
237 echo " setProp <tag> [<value>] - set value on 'tag' (if no value, it will be queried from config)"
238 echo " encrypt <tag> [<pass>] - set passwords on Configuration (if no pass, it will be queried)"
239 echo " bash - run bash in Container"
240 echo " Note: the following aliases are preset"
241 echo " cadi - CADI CmdLine tool"
242 echo " agent - Agent Java tool (see above help)"
243 echo ""
244 echo " --help|-? [cadi|agent] - This help, cadi help or agent help"
245 ;;
246 cadi)
247 echo "--- cadi Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500248 $JAVA_CADI
Instrumental32cdd552018-07-19 13:29:32 -0500249 ;;
250 agent)
251 echo "--- agent Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500252 $JAVA_AGENT
Instrumental32cdd552018-07-19 13:29:32 -0500253 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500254 aafcli)
255 echo "--- aafcli Tool Comands ---"
256 $JAVA_AAFCLI
257 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500258 esac
259 echo ""
260 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500261 ### Possible Dublin
262 # sample)
263 # echo "--- run Sample Servlet App ---"
264 # $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
265 # ;;
Instrumental32cdd552018-07-19 13:29:32 -0500266 *)
Instrumental9fe11532018-10-23 17:40:47 -0500267 $JAVA_AGENT "$CMD" "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500268 ;;
269 esac
270fi