Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # This script is run when starting aaf_config Container. |
| 3 | # It needs to cover the cases where the initial data doesn't exist, and when it has already been configured (don't overwrite) |
| 4 | # |
| 5 | JAVA=/usr/bin/java |
| 6 | AAF_INTERFACE_VERSION=2.1 |
| 7 | |
| 8 | # Extract Name, Domain and NS from FQI |
| 9 | FQIA=($(echo ${APP_FQI} | tr '@' '\n')) |
| 10 | FQI_SHORT=${FQIA[0]} |
| 11 | FQI_DOMAIN=${FQIA[1]} |
| 12 | # Reverse DOMAIN for NS |
| 13 | FQIA_E=($(echo ${FQI_DOMAIN} | tr '.' '\n')) |
| 14 | for (( i=( ${#FQIA_E[@]} -1 ); i>0; i-- )); do |
| 15 | NS=${NS}${FQIA_E[i]}'.' |
| 16 | done |
| 17 | NS=${NS}${FQIA_E[0]} |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 18 | CONFIG="/opt/app/aaf_config" |
| 19 | LOCAL="/opt/app/osaaf/local" |
| 20 | DOT_AAF="$HOME/.aaf" |
| 21 | SSO="$DOT_AAF/sso.props" |
| 22 | |
| 23 | # Setup Bash, first time only |
| 24 | if [ ! -e "$HOME/.bash_aliases" ] || [ -z "$(grep aaf_config $HOME/.bash_aliases)" ]; then |
| 25 | echo "alias cadi='$CONFIG/bin/agent.sh EMPTY cadi \$*'" >>$HOME/.bash_aliases |
| 26 | echo "alias agent='$CONFIG/bin/agent.sh EMPTY \$*'" >>$HOME/.bash_aliases |
| 27 | chmod a+x $CONFIG/bin/agent.sh |
| 28 | . $HOME/.bash_aliases |
| 29 | fi |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 30 | |
| 31 | # Setup SSO info for Deploy ID |
| 32 | function sso_encrypt() { |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 33 | $JAVA -cp $CONFIG/bin/aaf-cadi-aaf-*-full.jar org.onap.aaf.cadi.CmdLine digest ${1} $DOT_AAF/keyfile |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 36 | |
| 37 | if [ ! -e "$DOT_AAF/keyfile" ]; then |
| 38 | mkdir -p $DOT_AAF |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 39 | $JAVA -cp $CONFIG/bin/aaf-cadi-aaf-*-full.jar org.onap.aaf.cadi.CmdLine keygen $DOT_AAF/keyfile |
| 40 | chmod 400 $DOT_AAF/keyfile |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 41 | echo cadi_latitude=${LATITUDE} > ${SSO} |
| 42 | echo cadi_longitude=${LONGITUDE} >> ${SSO} |
| 43 | echo aaf_id=${DEPLOY_FQI} >> ${SSO} |
| 44 | if [ ! "${DEPLOY_PASSWORD}" = "" ]; then |
| 45 | echo aaf_password=enc:$(sso_encrypt ${DEPLOY_PASSWORD}) >> ${SSO} |
| 46 | fi |
| 47 | echo aaf_locate_url=https://${AAF_FQDN}:8095 >> ${SSO} |
| 48 | echo aaf_url=https://AAF_LOCATE_URL/AAF_NS.service:${AAF_INTERFACE_VERSION} >> ${SSO} |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 49 | |
| 50 | base64 -d $CONFIG/cert/truststoreONAPall.jks.b64 > $DOT_AAF/truststoreONAPall.jks |
| 51 | echo "cadi_truststore=$DOT_AAF/truststoreONAPall.jks" >> ${SSO} |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 52 | echo cadi_truststore_password=enc:$(sso_encrypt changeit) >> ${SSO} |
| 53 | fi |
| 54 | |
| 55 | # Only initialize once, automatically... |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 56 | if [ ! -e $LOCAL/${NS}.props ]; then |
| 57 | mkdir -p $LOCAL |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 58 | for D in bin logs; do |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 59 | rsync -avzh --exclude=.gitignore $CONFIG/$D/* /opt/app/osaaf/$D |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 60 | done |
| 61 | |
| 62 | # setup Configs |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 63 | $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar config $APP_FQI \ |
| 64 | cadi_etc_dir=$LOCAL cadi_prop_files=$SSO |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 65 | |
| 66 | # Place Certificates |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 67 | $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar place ${APP_FQI} ${APP_FQDN} |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 68 | |
| 69 | # Validate |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 70 | $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar validate \ |
| 71 | cadi_prop_files=$LOCAL/${NS}.props |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 72 | fi |
| 73 | |
| 74 | # Now run a command |
| 75 | CMD=$2 |
| 76 | if [ ! "$CMD" = "" ]; then |
| 77 | shift |
| 78 | shift |
| 79 | case "$CMD" in |
| 80 | ls) |
| 81 | echo ls requested |
| 82 | find /opt/app/osaaf -depth |
| 83 | ;; |
| 84 | cat) |
| 85 | if [ "$1" = "" ]; then |
| 86 | echo "usage: cat <file... ONLY files ending in .props>" |
| 87 | else |
| 88 | if [[ $1 == *.props ]]; then |
| 89 | echo |
| 90 | echo "## CONTENTS OF $3" |
| 91 | echo |
| 92 | cat "$1" |
| 93 | else |
| 94 | echo "### ERROR ####" |
| 95 | echo " \"cat\" may only be used with files ending with \".props\"" |
| 96 | fi |
| 97 | fi |
| 98 | ;; |
| 99 | update) |
| 100 | for D in bin logs; do |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 101 | rsync -uh --exclude=.gitignore $CONFIG/$D/* /opt/app/osaaf/$D |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 102 | done |
| 103 | ;; |
Instrumental | 87da9fe | 2018-07-19 16:44:02 -0500 | [diff] [blame] | 104 | showpass) |
| 105 | echo "## Show Passwords" |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 106 | $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar showpass ${APP_FQI} ${APP_FQDN} |
Instrumental | 87da9fe | 2018-07-19 16:44:02 -0500 | [diff] [blame] | 107 | ;; |
| 108 | check) |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 109 | $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar check ${APP_FQI} ${APP_FQDN} |
Instrumental | 87da9fe | 2018-07-19 16:44:02 -0500 | [diff] [blame] | 110 | ;; |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 111 | validate) |
| 112 | echo "## validate requested" |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 113 | $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar validate $LOCAL/${NS}.props |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 114 | ;; |
| 115 | bash) |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 116 | #if [ ! -e $HOME/bash_aliases ]; then |
| 117 | # echo "alias cadi='$JAVA -cp $CONFIG/bin/aaf-cadi-aaf-*-full.jar org.onap.aaf.cadi.CmdLine \$*'" >$HOME/bash_aliases |
| 118 | # echo "alias agent='/bin/bash $CONFIG/bin/agent.sh no-op \$*'" >>$HOME/bash_aliases |
| 119 | #fi |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 120 | shift |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 121 | cd $LOCAL || exit |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 122 | /bin/bash "$@" |
| 123 | ;; |
| 124 | setProp) |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 125 | cd $LOCAL || exit |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 126 | FILES=$(grep -l "$1" ./*.props) |
| 127 | if [ "$FILES" = "" ]; then |
| 128 | FILES="$3" |
| 129 | ADD=Y |
| 130 | fi |
| 131 | for F in $FILES; do |
| 132 | echo "Changing $1 in $F" |
| 133 | if [ "$ADD" = "Y" ]; then |
| 134 | echo $2 >> $F |
| 135 | else |
Instrumental | 6095e29 | 2018-09-06 13:27:15 -0500 | [diff] [blame] | 136 | sed -i.backup -e "s/\\(${1}.*=\\).*/\\1${2}/" $F |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 137 | fi |
| 138 | cat $F |
| 139 | done |
| 140 | ;; |
| 141 | encrypt) |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 142 | cd $LOCAL || exit |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 143 | echo $1 |
| 144 | FILES=$(grep -l "$1" ./*.props) |
| 145 | if [ "$FILES" = "" ]; then |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 146 | FILES=$LOCAL/${NS}.cred.props |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 147 | ADD=Y |
| 148 | fi |
| 149 | for F in $FILES; do |
| 150 | echo "Changing $1 in $F" |
| 151 | if [ "$2" = "" ]; then |
| 152 | read -r -p "Password (leave blank to cancel): " -s ORIG_PW |
| 153 | echo " " |
| 154 | if [ "$ORIG_PW" = "" ]; then |
| 155 | echo canceling... |
| 156 | break |
| 157 | fi |
| 158 | else |
| 159 | ORIG_PW="$2" |
| 160 | fi |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 161 | PWD=$("$JAVA" -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar cadi digest "$ORIG_PW" $LOCAL/${NS}.keyfile) |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 162 | if [ "$ADD" = "Y" ]; then |
| 163 | echo "$1=enc:$PWD" >> $F |
| 164 | else |
| 165 | sed -i.backup -e "s/\\($1.*enc:\\).*/\\1$PWD/" $F |
| 166 | fi |
| 167 | cat $F |
| 168 | done |
| 169 | ;; |
| 170 | taillog) |
| 171 | sh /opt/app/osaaf/logs/taillog |
| 172 | ;; |
| 173 | --help | -?) |
| 174 | case "$1" in |
| 175 | "") |
| 176 | echo "--- Agent Container Comands ---" |
| 177 | echo " ls - Lists all files in Configuration" |
| 178 | echo " cat <file.props>> - Shows the contents (Prop files only)" |
| 179 | echo " validate - Runs a test using Configuration" |
| 180 | echo " setProp <tag> [<value>] - set value on 'tag' (if no value, it will be queried from config)" |
| 181 | echo " encrypt <tag> [<pass>] - set passwords on Configuration (if no pass, it will be queried)" |
| 182 | echo " bash - run bash in Container" |
| 183 | echo " Note: the following aliases are preset" |
| 184 | echo " cadi - CADI CmdLine tool" |
| 185 | echo " agent - Agent Java tool (see above help)" |
| 186 | echo "" |
| 187 | echo " --help|-? [cadi|agent] - This help, cadi help or agent help" |
| 188 | ;; |
| 189 | cadi) |
| 190 | echo "--- cadi Tool Comands ---" |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 191 | $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar cadi | tail -n +6 |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 192 | ;; |
| 193 | agent) |
| 194 | echo "--- agent Tool Comands ---" |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 195 | $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 196 | ;; |
| 197 | esac |
| 198 | echo "" |
| 199 | ;; |
| 200 | *) |
Instrumental | 365638c | 2018-10-01 15:26:03 -0500 | [diff] [blame] | 201 | $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar "$CMD" "$@" |
Instrumental | 32cdd55 | 2018-07-19 13:29:32 -0500 | [diff] [blame] | 202 | ;; |
| 203 | esac |
| 204 | fi |