blob: 7af8a974c8c1bf1fb03ca35a032d6c81b301a334 [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
Instrumental12414fe2019-01-22 10:27:32 -060052 mkdir -p $OSAAF/$D
Instrumental7f1e2c02019-02-18 10:06:21 -060053 cp $CONFIG/$D/* $OSAAF/$D
Instrumental49525302018-10-06 20:32:59 -050054 done
55fi
56
Instrumental365638c2018-10-01 15:26:03 -050057# Setup Bash, first time only
Instrumental628b7102019-02-15 19:40:04 -060058if [ ! -e "$HOME/.bashrc" ] || [ -z "$(grep cadi $HOME/.bashrc)" ]; then
59 echo "alias cadi='$JAVA_CADI \$*'" >>$HOME/.bashrc
60 echo "alias agent='$CONFIG/bin/agent.sh agent \$*'" >>$HOME/.bashrc
61 echo "alias aafcli='$JAVA_AAFCLI \$*'" >>$HOME/.bashrc
62 chmod a+x $CONFIG/bin/agent.sh
63 . $HOME/.bashrc
Instrumental365638c2018-10-01 15:26:03 -050064fi
Instrumental32cdd552018-07-19 13:29:32 -050065
66# Setup SSO info for Deploy ID
67function sso_encrypt() {
Instrumental9fe11532018-10-23 17:40:47 -050068 $JAVA_CADI digest ${1} $DOT_AAF/keyfile
Instrumental32cdd552018-07-19 13:29:32 -050069}
70
Instrumental365638c2018-10-01 15:26:03 -050071
Instrumental49525302018-10-06 20:32:59 -050072# Create Deployer Info, located at /root/.aaf
Instrumental365638c2018-10-01 15:26:03 -050073if [ ! -e "$DOT_AAF/keyfile" ]; then
74 mkdir -p $DOT_AAF
Instrumental9fe11532018-10-23 17:40:47 -050075 $JAVA_CADI keygen $DOT_AAF/keyfile
Instrumental365638c2018-10-01 15:26:03 -050076 chmod 400 $DOT_AAF/keyfile
Instrumental32cdd552018-07-19 13:29:32 -050077 echo cadi_latitude=${LATITUDE} > ${SSO}
78 echo cadi_longitude=${LONGITUDE} >> ${SSO}
79 echo aaf_id=${DEPLOY_FQI} >> ${SSO}
80 if [ ! "${DEPLOY_PASSWORD}" = "" ]; then
81 echo aaf_password=enc:$(sso_encrypt ${DEPLOY_PASSWORD}) >> ${SSO}
82 fi
83 echo aaf_locate_url=https://${AAF_FQDN}:8095 >> ${SSO}
84 echo aaf_url=https://AAF_LOCATE_URL/AAF_NS.service:${AAF_INTERFACE_VERSION} >> ${SSO}
Instrumental365638c2018-10-01 15:26:03 -050085
86 base64 -d $CONFIG/cert/truststoreONAPall.jks.b64 > $DOT_AAF/truststoreONAPall.jks
87 echo "cadi_truststore=$DOT_AAF/truststoreONAPall.jks" >> ${SSO}
Instrumental32cdd552018-07-19 13:29:32 -050088 echo cadi_truststore_password=enc:$(sso_encrypt changeit) >> ${SSO}
Instrumental284ad0a2018-10-24 07:01:09 -050089 echo "Caller Properties Initialized"
90 INITIALIZED="true"
Instrumental32cdd552018-07-19 13:29:32 -050091fi
92
93# Only initialize once, automatically...
Instrumental365638c2018-10-01 15:26:03 -050094if [ ! -e $LOCAL/${NS}.props ]; then
Instrumental9fe11532018-10-23 17:40:47 -050095 echo "#### Create Configuration files "
96 $JAVA_AGENT config $APP_FQI \
Instrumental49525302018-10-06 20:32:59 -050097 aaf_url=https://AAF_LOCATE_URL/AAF_NS.locate:${AAF_INTERFACE_VERSION} \
98 cadi_etc_dir=$LOCAL
99 cat $LOCAL/$NS.props
Instrumental32cdd552018-07-19 13:29:32 -0500100
Instrumental9fe11532018-10-23 17:40:47 -0500101 echo
102 echo "#### Certificate Authorization Artifact"
103 TMP=$(mktemp)
104 $JAVA_AGENT read ${APP_FQI} ${APP_FQDN} \
Instrumental49525302018-10-06 20:32:59 -0500105 cadi_prop_files=${SSO} \
Instrumental9fe11532018-10-23 17:40:47 -0500106 cadi_etc_dir=$LOCAL > $TMP
107 cat $TMP
108 echo
109 if [ -n "$(grep 'Namespace:' $TMP)" ]; then
110 echo "#### Place Certificates (by deployer)"
111 $JAVA_AGENT place ${APP_FQI} ${APP_FQDN} \
112 cadi_prop_files=${SSO} \
113 cadi_etc_dir=$LOCAL
114
115 echo "#### Validate Configuration and Certificate with live call"
116 $JAVA_AGENT_SELF validate
Instrumental284ad0a2018-10-24 07:01:09 -0500117 echo "Obtained Certificates"
118 INITIALIZED="true"
Instrumental9fe11532018-10-23 17:40:47 -0500119 else
120 echo "#### Certificate Authorization Artifact must be valid to continue"
121 fi
122 rm $TMP
Instrumental32cdd552018-07-19 13:29:32 -0500123fi
124
125# Now run a command
126CMD=$2
Instrumental9fe11532018-10-23 17:40:47 -0500127if [ -z "$CMD" ]; then
Instrumental284ad0a2018-10-24 07:01:09 -0500128 if [ -n "$INITIALIZED" ]; then
129 echo "Initialization complete"
130 else
131 $JAVA_AGENT
132 fi
Instrumental9fe11532018-10-23 17:40:47 -0500133else
Instrumental32cdd552018-07-19 13:29:32 -0500134 shift
135 shift
136 case "$CMD" in
137 ls)
138 echo ls requested
139 find /opt/app/osaaf -depth
140 ;;
141 cat)
142 if [ "$1" = "" ]; then
143 echo "usage: cat <file... ONLY files ending in .props>"
144 else
145 if [[ $1 == *.props ]]; then
146 echo
147 echo "## CONTENTS OF $3"
148 echo
149 cat "$1"
150 else
151 echo "### ERROR ####"
152 echo " \"cat\" may only be used with files ending with \".props\""
153 fi
154 fi
155 ;;
Instrumental87da9fe2018-07-19 16:44:02 -0500156 showpass)
157 echo "## Show Passwords"
Instrumental9fe11532018-10-23 17:40:47 -0500158 $JAVA_AGENT showpass ${APP_FQI} ${APP_FQDN}
Instrumental87da9fe2018-07-19 16:44:02 -0500159 ;;
160 check)
Instrumental9fe11532018-10-23 17:40:47 -0500161 $JAVA_AGENT check ${APP_FQI} ${APP_FQDN}
Instrumental87da9fe2018-07-19 16:44:02 -0500162 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500163 validate)
164 echo "## validate requested"
Instrumental9fe11532018-10-23 17:40:47 -0500165 $JAVA_AGENT_SELF validate
Instrumental32cdd552018-07-19 13:29:32 -0500166 ;;
167 bash)
Instrumental32cdd552018-07-19 13:29:32 -0500168 shift
Instrumental365638c2018-10-01 15:26:03 -0500169 cd $LOCAL || exit
Instrumental12414fe2019-01-22 10:27:32 -0600170 exec bash "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500171 ;;
172 setProp)
Instrumental365638c2018-10-01 15:26:03 -0500173 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500174 FILES=$(grep -l "$1" ./*.props)
Instrumental9fe11532018-10-23 17:40:47 -0500175 if [ -z "$FILES" ]; then
176 if [ -z "$3" ]; then
177 FILES=${NS}.props
178 else
179 FILES="$3"
180 fi
Instrumental32cdd552018-07-19 13:29:32 -0500181 ADD=Y
182 fi
183 for F in $FILES; do
Instrumental32cdd552018-07-19 13:29:32 -0500184 if [ "$ADD" = "Y" ]; then
Instrumental9fe11532018-10-23 17:40:47 -0500185 echo "Changing $1 to $F"
186 echo "$1=$2" >> $F
Instrumental32cdd552018-07-19 13:29:32 -0500187 else
Instrumental9fe11532018-10-23 17:40:47 -0500188 echo "Changing $1 in $F"
Instrumental6095e292018-09-06 13:27:15 -0500189 sed -i.backup -e "s/\\(${1}.*=\\).*/\\1${2}/" $F
Instrumental32cdd552018-07-19 13:29:32 -0500190 fi
191 cat $F
192 done
193 ;;
194 encrypt)
Instrumental365638c2018-10-01 15:26:03 -0500195 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500196 echo $1
197 FILES=$(grep -l "$1" ./*.props)
198 if [ "$FILES" = "" ]; then
Instrumental365638c2018-10-01 15:26:03 -0500199 FILES=$LOCAL/${NS}.cred.props
Instrumental32cdd552018-07-19 13:29:32 -0500200 ADD=Y
201 fi
202 for F in $FILES; do
203 echo "Changing $1 in $F"
204 if [ "$2" = "" ]; then
205 read -r -p "Password (leave blank to cancel): " -s ORIG_PW
206 echo " "
207 if [ "$ORIG_PW" = "" ]; then
208 echo canceling...
209 break
210 fi
211 else
212 ORIG_PW="$2"
213 fi
Instrumental9fe11532018-10-23 17:40:47 -0500214 PWD=$($JAVA_CADI digest "$ORIG_PW" $LOCAL/${NS}.keyfile)
Instrumental32cdd552018-07-19 13:29:32 -0500215 if [ "$ADD" = "Y" ]; then
216 echo "$1=enc:$PWD" >> $F
217 else
218 sed -i.backup -e "s/\\($1.*enc:\\).*/\\1$PWD/" $F
219 fi
220 cat $F
221 done
222 ;;
223 taillog)
224 sh /opt/app/osaaf/logs/taillog
225 ;;
226 --help | -?)
227 case "$1" in
228 "")
229 echo "--- Agent Container Comands ---"
230 echo " ls - Lists all files in Configuration"
231 echo " cat <file.props>> - Shows the contents (Prop files only)"
232 echo " validate - Runs a test using Configuration"
233 echo " setProp <tag> [<value>] - set value on 'tag' (if no value, it will be queried from config)"
234 echo " encrypt <tag> [<pass>] - set passwords on Configuration (if no pass, it will be queried)"
235 echo " bash - run bash in Container"
236 echo " Note: the following aliases are preset"
237 echo " cadi - CADI CmdLine tool"
238 echo " agent - Agent Java tool (see above help)"
239 echo ""
240 echo " --help|-? [cadi|agent] - This help, cadi help or agent help"
241 ;;
242 cadi)
243 echo "--- cadi Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500244 $JAVA_CADI
Instrumental32cdd552018-07-19 13:29:32 -0500245 ;;
246 agent)
247 echo "--- agent Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500248 $JAVA_AGENT
Instrumental32cdd552018-07-19 13:29:32 -0500249 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500250 aafcli)
251 echo "--- aafcli Tool Comands ---"
252 $JAVA_AAFCLI
253 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500254 esac
255 echo ""
256 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500257 ### Possible Dublin
258 # sample)
259 # echo "--- run Sample Servlet App ---"
260 # $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
261 # ;;
Instrumental32cdd552018-07-19 13:29:32 -0500262 *)
Instrumental9fe11532018-10-23 17:40:47 -0500263 $JAVA_AGENT "$CMD" "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500264 ;;
265 esac
266fi