blob: 0dff80370d518ac9276d72bf38101990d4146c54 [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"
Instrumentalbe7e0d12019-04-04 21:42:19 -050042
43if [ -e "$CONFIG" ]; then
44 CONFIG_BIN="$CONFIG/bin"
45else
46 CONFIG_BIN="."
47fi
48
49CLPATH="$CONFIG_BIN/aaf-auth-cmd-*-full.jar"
50
51JAVA_CADI="$JAVA -cp $CLPATH org.onap.aaf.cadi.CmdLine"
52JAVA_AGENT="$JAVA -cp $CLPATH -Dcadi_prop_files=$SSO org.onap.aaf.cadi.configure.Agent"
53JAVA_AGENT_SELF="$JAVA -cp $CLPATH -Dcadi_prop_files=$LOCAL/${NS}.props org.onap.aaf.cadi.configure.Agent"
54JAVA_AAFCLI="$JAVA -cp $CLPATH -Dcadi_prop_files=$LOCAL/org.osaaf.aaf.props org.onap.aaf.auth.cmd.AAFcli"
Instrumental9fe11532018-10-23 17:40:47 -050055
Instrumental49525302018-10-06 20:32:59 -050056# Check for local dir
57if [ ! -d $LOCAL ]; then
58 mkdir -p $LOCAL
59 for D in bin logs; do
Instrumental12414fe2019-01-22 10:27:32 -060060 mkdir -p $OSAAF/$D
Instrumental7f1e2c02019-02-18 10:06:21 -060061 cp $CONFIG/$D/* $OSAAF/$D
Instrumental49525302018-10-06 20:32:59 -050062 done
63fi
64
Instrumental365638c2018-10-01 15:26:03 -050065# Setup Bash, first time only
Instrumental628b7102019-02-15 19:40:04 -060066if [ ! -e "$HOME/.bashrc" ] || [ -z "$(grep cadi $HOME/.bashrc)" ]; then
67 echo "alias cadi='$JAVA_CADI \$*'" >>$HOME/.bashrc
Instrumentalbe7e0d12019-04-04 21:42:19 -050068 echo "alias agent='$CONFIG_BIN/agent.sh agent \$*'" >>$HOME/.bashrc
Instrumental628b7102019-02-15 19:40:04 -060069 echo "alias aafcli='$JAVA_AAFCLI \$*'" >>$HOME/.bashrc
Instrumentalbe7e0d12019-04-04 21:42:19 -050070 chmod a+x $CONFIG_BIN/agent.sh
Instrumental628b7102019-02-15 19:40:04 -060071 . $HOME/.bashrc
Instrumental365638c2018-10-01 15:26:03 -050072fi
Instrumental32cdd552018-07-19 13:29:32 -050073
74# Setup SSO info for Deploy ID
75function sso_encrypt() {
Instrumental9fe11532018-10-23 17:40:47 -050076 $JAVA_CADI digest ${1} $DOT_AAF/keyfile
Instrumental32cdd552018-07-19 13:29:32 -050077}
78
Instrumental4ac37bf2019-04-10 13:01:30 -050079if [ ! -e "$DOT_AAF/truststoreONAPall.jks" ]; then
80 mkdir -p $DOT_AAF
81 base64 -d $CONFIG/cert/truststoreONAPall.jks.b64 > $DOT_AAF/truststoreONAPall.jks
82fi
Instrumental365638c2018-10-01 15:26:03 -050083
Instrumental49525302018-10-06 20:32:59 -050084# Create Deployer Info, located at /root/.aaf
Instrumental365638c2018-10-01 15:26:03 -050085if [ ! -e "$DOT_AAF/keyfile" ]; then
Instrumental9fe11532018-10-23 17:40:47 -050086 $JAVA_CADI keygen $DOT_AAF/keyfile
Instrumental365638c2018-10-01 15:26:03 -050087 chmod 400 $DOT_AAF/keyfile
Instrumental4ac37bf2019-04-10 13:01:30 -050088
89 # Add Deployer Creds to Root's SSO
90 DEPLOY_FQI="${DEPLOY_FQI:=$app_id}"
91 echo "aaf_id=${DEPLOY_FQI}" > ${SSO}
Instrumental32cdd552018-07-19 13:29:32 -050092 if [ ! "${DEPLOY_PASSWORD}" = "" ]; then
93 echo aaf_password=enc:$(sso_encrypt ${DEPLOY_PASSWORD}) >> ${SSO}
94 fi
Instrumentalbe7e0d12019-04-04 21:42:19 -050095
Instrumental4ac37bf2019-04-10 13:01:30 -050096 # Cover case where using app.props
97 aaf_locater_container_ns=${aaf_locator_container_ns:=$CONTAINER_NS}
Instrumental365638c2018-10-01 15:26:03 -050098
Instrumental4ac37bf2019-04-10 13:01:30 -050099 for E in $(env); do
100 if [ "${E:0:4}" = "aaf_" ] || [ "${E:0:5}" = "cadi_" ]; then
101 # Use Deployer ID in ${SSO}
102 if [ "app_id" != "${E%=*}" ]; then
103 S="${E/_helm/.helm}"
104 S="${S/_oom/.oom}"
105 echo "$S" >> ${SSO}
106 fi
107 fi
108 done
109
Instrumental365638c2018-10-01 15:26:03 -0500110 echo "cadi_truststore=$DOT_AAF/truststoreONAPall.jks" >> ${SSO}
Instrumental32cdd552018-07-19 13:29:32 -0500111 echo cadi_truststore_password=enc:$(sso_encrypt changeit) >> ${SSO}
Instrumental284ad0a2018-10-24 07:01:09 -0500112 echo "Caller Properties Initialized"
113 INITIALIZED="true"
Instrumental32cdd552018-07-19 13:29:32 -0500114fi
Instrumental4ac37bf2019-04-10 13:01:30 -0500115echo "cat SSO"
116cat ${SSO}
117echo "dog"
Instrumental32cdd552018-07-19 13:29:32 -0500118
119# Only initialize once, automatically...
Instrumental365638c2018-10-01 15:26:03 -0500120if [ ! -e $LOCAL/${NS}.props ]; then
Instrumentalbe7e0d12019-04-04 21:42:19 -0500121 if [ -e '/opt/app/aaf_config/bin' ]; then
122 cp /opt/app/aaf_config/bin/*.jar $LOCAL
123 echo "#!/bin/bash" > agent
124 echo 'case "$1" in' >> agent
125 echo ' ""|-?|--help)CMD="";FQI="";FQDN="";;' >> agent
126 echo ' validate)CMD="$1";FQI="";FQDN="${2:-'"$NS.props"'}";;' >> agent
127 echo ' *)CMD="$1";FQI="${2:-'"$APP_FQI"'}";FQDN="${3:-'"$APP_FQDN"'}";;' >> agent
128 echo 'esac' >> agent
129 echo 'java -cp '$(ls aaf-auth-cmd-*-full.jar)' -Dcadi_prop_files='"$NS"'.props org.onap.aaf.cadi.configure.Agent $CMD $FQI $FQDN' >> agent
130
131 echo "#!/bin/bash" > cadi
132 echo "java -cp $(ls aaf-auth-cmd-*-full.jar) -Dcadi_prop_files=$NS.props org.onap.aaf.cadi.CmdLine " '$*' >> cadi
133 # echo "#!/bin/bash" > aafcli
134 # echo "java -cp $(ls aaf-auth-cmd-*-full.jar) -Dcadi_prop_files=$NS.props org.onap.aaf.auth.cmd.AAFcli " '$*' >> aafcli
135
136 echo "#!/bin/bash" > testConnectivity
137 echo "java -cp $(ls aaf-auth-cmd-*-full.jar) org.onap.aaf.cadi.aaf.TestConnectivity $NS.props" >> testConnectivity
138 chmod ug+x agent cadi testConnectivity
139 fi
Instrumental4ac37bf2019-04-10 13:01:30 -0500140
Instrumental9fe11532018-10-23 17:40:47 -0500141 echo "#### Create Configuration files "
142 $JAVA_AGENT config $APP_FQI \
Instrumental4ac37bf2019-04-10 13:01:30 -0500143 cadi_etc_dir=$LOCAL \
144 cadi_prop_files=$SSO
145 #aaf_url=https://AAF_LOCATE_URL/AAF_NS.locate:${AAF_INTERFACE_VERSION}
Instrumental49525302018-10-06 20:32:59 -0500146 cat $LOCAL/$NS.props
Instrumental32cdd552018-07-19 13:29:32 -0500147
Instrumental9fe11532018-10-23 17:40:47 -0500148 echo
149 echo "#### Certificate Authorization Artifact"
Instrumental4ac37bf2019-04-10 13:01:30 -0500150 # TMP=$(mktemp)
151 TMP=$LOCAL/agent.log
Instrumental9fe11532018-10-23 17:40:47 -0500152 $JAVA_AGENT read ${APP_FQI} ${APP_FQDN} \
Instrumental49525302018-10-06 20:32:59 -0500153 cadi_prop_files=${SSO} \
Instrumental4ac37bf2019-04-10 13:01:30 -0500154 cadi_etc_dir=$LOCAL | tee $TMP
155
Instrumental9fe11532018-10-23 17:40:47 -0500156 if [ -n "$(grep 'Namespace:' $TMP)" ]; then
157 echo "#### Place Certificates (by deployer)"
158 $JAVA_AGENT place ${APP_FQI} ${APP_FQDN} \
159 cadi_prop_files=${SSO} \
160 cadi_etc_dir=$LOCAL
161
162 echo "#### Validate Configuration and Certificate with live call"
Instrumental284ad0a2018-10-24 07:01:09 -0500163 echo "Obtained Certificates"
164 INITIALIZED="true"
Instrumental9fe11532018-10-23 17:40:47 -0500165 else
166 echo "#### Certificate Authorization Artifact must be valid to continue"
167 fi
168 rm $TMP
Instrumental32cdd552018-07-19 13:29:32 -0500169fi
170
171# Now run a command
172CMD=$2
Instrumental9fe11532018-10-23 17:40:47 -0500173if [ -z "$CMD" ]; then
Instrumental284ad0a2018-10-24 07:01:09 -0500174 if [ -n "$INITIALIZED" ]; then
175 echo "Initialization complete"
176 else
177 $JAVA_AGENT
178 fi
Instrumental9fe11532018-10-23 17:40:47 -0500179else
Instrumental32cdd552018-07-19 13:29:32 -0500180 shift
181 shift
182 case "$CMD" in
183 ls)
184 echo ls requested
185 find /opt/app/osaaf -depth
186 ;;
187 cat)
188 if [ "$1" = "" ]; then
189 echo "usage: cat <file... ONLY files ending in .props>"
190 else
191 if [[ $1 == *.props ]]; then
192 echo
193 echo "## CONTENTS OF $3"
194 echo
195 cat "$1"
196 else
197 echo "### ERROR ####"
198 echo " \"cat\" may only be used with files ending with \".props\""
199 fi
200 fi
201 ;;
Instrumental87da9fe2018-07-19 16:44:02 -0500202 showpass)
203 echo "## Show Passwords"
Instrumental9fe11532018-10-23 17:40:47 -0500204 $JAVA_AGENT showpass ${APP_FQI} ${APP_FQDN}
Instrumental87da9fe2018-07-19 16:44:02 -0500205 ;;
206 check)
Instrumentalbf3d3072019-02-18 14:01:07 -0600207 echo "## Check Certificate"
Instrumental9fe11532018-10-23 17:40:47 -0500208 $JAVA_AGENT check ${APP_FQI} ${APP_FQDN}
Instrumental87da9fe2018-07-19 16:44:02 -0500209 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500210 validate)
211 echo "## validate requested"
Instrumental9fe11532018-10-23 17:40:47 -0500212 $JAVA_AGENT_SELF validate
Instrumental32cdd552018-07-19 13:29:32 -0500213 ;;
Instrumentalbf3d3072019-02-18 14:01:07 -0600214 renew)
215 echo "## Renew Certificate"
216 $JAVA_AGENT place ${APP_FQI} ${APP_FQDN}
217 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500218 bash)
Instrumental32cdd552018-07-19 13:29:32 -0500219 shift
Instrumental365638c2018-10-01 15:26:03 -0500220 cd $LOCAL || exit
Instrumental12414fe2019-01-22 10:27:32 -0600221 exec bash "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500222 ;;
223 setProp)
Instrumental365638c2018-10-01 15:26:03 -0500224 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500225 FILES=$(grep -l "$1" ./*.props)
Instrumental9fe11532018-10-23 17:40:47 -0500226 if [ -z "$FILES" ]; then
227 if [ -z "$3" ]; then
228 FILES=${NS}.props
229 else
230 FILES="$3"
231 fi
Instrumental32cdd552018-07-19 13:29:32 -0500232 ADD=Y
233 fi
234 for F in $FILES; do
Instrumental32cdd552018-07-19 13:29:32 -0500235 if [ "$ADD" = "Y" ]; then
Instrumental9fe11532018-10-23 17:40:47 -0500236 echo "Changing $1 to $F"
237 echo "$1=$2" >> $F
Instrumental32cdd552018-07-19 13:29:32 -0500238 else
Instrumental9fe11532018-10-23 17:40:47 -0500239 echo "Changing $1 in $F"
Instrumental6095e292018-09-06 13:27:15 -0500240 sed -i.backup -e "s/\\(${1}.*=\\).*/\\1${2}/" $F
Instrumental32cdd552018-07-19 13:29:32 -0500241 fi
242 cat $F
243 done
244 ;;
245 encrypt)
Instrumental365638c2018-10-01 15:26:03 -0500246 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500247 echo $1
248 FILES=$(grep -l "$1" ./*.props)
249 if [ "$FILES" = "" ]; then
Instrumental365638c2018-10-01 15:26:03 -0500250 FILES=$LOCAL/${NS}.cred.props
Instrumental32cdd552018-07-19 13:29:32 -0500251 ADD=Y
252 fi
253 for F in $FILES; do
254 echo "Changing $1 in $F"
255 if [ "$2" = "" ]; then
256 read -r -p "Password (leave blank to cancel): " -s ORIG_PW
257 echo " "
258 if [ "$ORIG_PW" = "" ]; then
259 echo canceling...
260 break
261 fi
262 else
263 ORIG_PW="$2"
264 fi
Instrumental9fe11532018-10-23 17:40:47 -0500265 PWD=$($JAVA_CADI digest "$ORIG_PW" $LOCAL/${NS}.keyfile)
Instrumental32cdd552018-07-19 13:29:32 -0500266 if [ "$ADD" = "Y" ]; then
267 echo "$1=enc:$PWD" >> $F
268 else
269 sed -i.backup -e "s/\\($1.*enc:\\).*/\\1$PWD/" $F
270 fi
271 cat $F
272 done
273 ;;
274 taillog)
275 sh /opt/app/osaaf/logs/taillog
276 ;;
Instrumentalbd7def72019-04-03 08:25:28 -0500277 testConnectivity|testconnectivity)
278 echo "--- Test Connectivity ---"
Instrumentalbe7e0d12019-04-04 21:42:19 -0500279 $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 -0500280 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500281 --help | -?)
282 case "$1" in
283 "")
284 echo "--- Agent Container Comands ---"
285 echo " ls - Lists all files in Configuration"
286 echo " cat <file.props>> - Shows the contents (Prop files only)"
287 echo " validate - Runs a test using Configuration"
288 echo " setProp <tag> [<value>] - set value on 'tag' (if no value, it will be queried from config)"
289 echo " encrypt <tag> [<pass>] - set passwords on Configuration (if no pass, it will be queried)"
290 echo " bash - run bash in Container"
291 echo " Note: the following aliases are preset"
292 echo " cadi - CADI CmdLine tool"
293 echo " agent - Agent Java tool (see above help)"
294 echo ""
295 echo " --help|-? [cadi|agent] - This help, cadi help or agent help"
296 ;;
297 cadi)
298 echo "--- cadi Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500299 $JAVA_CADI
Instrumental32cdd552018-07-19 13:29:32 -0500300 ;;
301 agent)
302 echo "--- agent Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500303 $JAVA_AGENT
Instrumental32cdd552018-07-19 13:29:32 -0500304 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500305 aafcli)
306 echo "--- aafcli Tool Comands ---"
307 $JAVA_AAFCLI
308 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500309 esac
310 echo ""
311 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500312 ### Possible Dublin
313 # sample)
314 # echo "--- run Sample Servlet App ---"
Instrumentalbe7e0d12019-04-04 21:42:19 -0500315 # $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 -0500316 # ;;
Instrumental32cdd552018-07-19 13:29:32 -0500317 *)
Instrumental9fe11532018-10-23 17:40:47 -0500318 $JAVA_AGENT "$CMD" "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500319 ;;
320 esac
321fi