blob: 9b146c5f4015b9ca749ecd6d17beb65898529d2c [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)
Instrumentalbf3d3072019-02-18 14:01:07 -0600161 echo "## Check Certificate"
Instrumental9fe11532018-10-23 17:40:47 -0500162 $JAVA_AGENT check ${APP_FQI} ${APP_FQDN}
Instrumental87da9fe2018-07-19 16:44:02 -0500163 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500164 validate)
165 echo "## validate requested"
Instrumental9fe11532018-10-23 17:40:47 -0500166 $JAVA_AGENT_SELF validate
Instrumental32cdd552018-07-19 13:29:32 -0500167 ;;
Instrumentalbf3d3072019-02-18 14:01:07 -0600168 renew)
169 echo "## Renew Certificate"
170 $JAVA_AGENT place ${APP_FQI} ${APP_FQDN}
171 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500172 bash)
Instrumental32cdd552018-07-19 13:29:32 -0500173 shift
Instrumental365638c2018-10-01 15:26:03 -0500174 cd $LOCAL || exit
Instrumental12414fe2019-01-22 10:27:32 -0600175 exec bash "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500176 ;;
177 setProp)
Instrumental365638c2018-10-01 15:26:03 -0500178 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500179 FILES=$(grep -l "$1" ./*.props)
Instrumental9fe11532018-10-23 17:40:47 -0500180 if [ -z "$FILES" ]; then
181 if [ -z "$3" ]; then
182 FILES=${NS}.props
183 else
184 FILES="$3"
185 fi
Instrumental32cdd552018-07-19 13:29:32 -0500186 ADD=Y
187 fi
188 for F in $FILES; do
Instrumental32cdd552018-07-19 13:29:32 -0500189 if [ "$ADD" = "Y" ]; then
Instrumental9fe11532018-10-23 17:40:47 -0500190 echo "Changing $1 to $F"
191 echo "$1=$2" >> $F
Instrumental32cdd552018-07-19 13:29:32 -0500192 else
Instrumental9fe11532018-10-23 17:40:47 -0500193 echo "Changing $1 in $F"
Instrumental6095e292018-09-06 13:27:15 -0500194 sed -i.backup -e "s/\\(${1}.*=\\).*/\\1${2}/" $F
Instrumental32cdd552018-07-19 13:29:32 -0500195 fi
196 cat $F
197 done
198 ;;
199 encrypt)
Instrumental365638c2018-10-01 15:26:03 -0500200 cd $LOCAL || exit
Instrumental32cdd552018-07-19 13:29:32 -0500201 echo $1
202 FILES=$(grep -l "$1" ./*.props)
203 if [ "$FILES" = "" ]; then
Instrumental365638c2018-10-01 15:26:03 -0500204 FILES=$LOCAL/${NS}.cred.props
Instrumental32cdd552018-07-19 13:29:32 -0500205 ADD=Y
206 fi
207 for F in $FILES; do
208 echo "Changing $1 in $F"
209 if [ "$2" = "" ]; then
210 read -r -p "Password (leave blank to cancel): " -s ORIG_PW
211 echo " "
212 if [ "$ORIG_PW" = "" ]; then
213 echo canceling...
214 break
215 fi
216 else
217 ORIG_PW="$2"
218 fi
Instrumental9fe11532018-10-23 17:40:47 -0500219 PWD=$($JAVA_CADI digest "$ORIG_PW" $LOCAL/${NS}.keyfile)
Instrumental32cdd552018-07-19 13:29:32 -0500220 if [ "$ADD" = "Y" ]; then
221 echo "$1=enc:$PWD" >> $F
222 else
223 sed -i.backup -e "s/\\($1.*enc:\\).*/\\1$PWD/" $F
224 fi
225 cat $F
226 done
227 ;;
228 taillog)
229 sh /opt/app/osaaf/logs/taillog
230 ;;
231 --help | -?)
232 case "$1" in
233 "")
234 echo "--- Agent Container Comands ---"
235 echo " ls - Lists all files in Configuration"
236 echo " cat <file.props>> - Shows the contents (Prop files only)"
237 echo " validate - Runs a test using Configuration"
238 echo " setProp <tag> [<value>] - set value on 'tag' (if no value, it will be queried from config)"
239 echo " encrypt <tag> [<pass>] - set passwords on Configuration (if no pass, it will be queried)"
240 echo " bash - run bash in Container"
241 echo " Note: the following aliases are preset"
242 echo " cadi - CADI CmdLine tool"
243 echo " agent - Agent Java tool (see above help)"
244 echo ""
245 echo " --help|-? [cadi|agent] - This help, cadi help or agent help"
246 ;;
247 cadi)
248 echo "--- cadi Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500249 $JAVA_CADI
Instrumental32cdd552018-07-19 13:29:32 -0500250 ;;
251 agent)
252 echo "--- agent Tool Comands ---"
Instrumental9fe11532018-10-23 17:40:47 -0500253 $JAVA_AGENT
Instrumental32cdd552018-07-19 13:29:32 -0500254 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500255 aafcli)
256 echo "--- aafcli Tool Comands ---"
257 $JAVA_AAFCLI
258 ;;
Instrumental32cdd552018-07-19 13:29:32 -0500259 esac
260 echo ""
261 ;;
Instrumental9fe11532018-10-23 17:40:47 -0500262 ### Possible Dublin
263 # sample)
264 # echo "--- run Sample Servlet App ---"
265 # $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
266 # ;;
Instrumental32cdd552018-07-19 13:29:32 -0500267 *)
Instrumental9fe11532018-10-23 17:40:47 -0500268 $JAVA_AGENT "$CMD" "$@"
Instrumental32cdd552018-07-19 13:29:32 -0500269 ;;
270 esac
271fi