Merge "JUnits for Mapper_2_0"
[aaf/authz.git] / auth / sample / bin / client.sh
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]}
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
30
31 # Setup SSO info for Deploy ID
32 function sso_encrypt() {
33  $JAVA -cp $CONFIG/bin/aaf-cadi-aaf-*-full.jar org.onap.aaf.cadi.CmdLine digest ${1} $DOT_AAF/keyfile
34 }
35
36
37 if [ ! -e "$DOT_AAF/keyfile" ]; then
38     mkdir -p $DOT_AAF
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
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}
49
50     base64 -d $CONFIG/cert/truststoreONAPall.jks.b64 > $DOT_AAF/truststoreONAPall.jks
51     echo "cadi_truststore=$DOT_AAF/truststoreONAPall.jks" >> ${SSO}
52     echo cadi_truststore_password=enc:$(sso_encrypt changeit) >> ${SSO}
53 fi
54
55 # Only initialize once, automatically...
56 if [ ! -e $LOCAL/${NS}.props ]; then
57     mkdir -p $LOCAL
58     for D in bin logs; do
59         rsync -avzh --exclude=.gitignore $CONFIG/$D/* /opt/app/osaaf/$D
60     done
61
62     # setup Configs
63     $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar config $APP_FQI \
64         cadi_etc_dir=$LOCAL cadi_prop_files=$SSO
65
66     # Place Certificates
67     $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar place ${APP_FQI} ${APP_FQDN}
68
69     # Validate
70     $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar validate \
71         cadi_prop_files=$LOCAL/${NS}.props
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
101             rsync -uh --exclude=.gitignore $CONFIG/$D/* /opt/app/osaaf/$D
102         done
103         ;;
104     showpass)
105         echo "## Show Passwords"
106         $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar showpass ${APP_FQI} ${APP_FQDN}
107         ;;
108     check)
109         $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar check ${APP_FQI} ${APP_FQDN}
110         ;;
111     validate)
112         echo "## validate requested"
113         $JAVA -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar validate $LOCAL/${NS}.props
114         ;;
115     bash)
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
120         shift
121         cd $LOCAL || exit
122         /bin/bash "$@"
123         ;;
124     setProp)
125         cd $LOCAL || exit
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 
136                sed -i.backup -e "s/\\(${1}.*=\\).*/\\1${2}/" $F
137             fi
138             cat $F
139         done
140         ;;
141     encrypt)
142         cd $LOCAL || exit
143         echo $1
144         FILES=$(grep -l "$1" ./*.props)
145         if [ "$FILES" = "" ]; then
146              FILES=$LOCAL/${NS}.cred.props
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
161             PWD=$("$JAVA" -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar cadi digest "$ORIG_PW" $LOCAL/${NS}.keyfile)
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 ---"
191             $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar cadi | tail -n +6
192             ;;
193         agent)
194             echo "--- agent Tool Comands ---"
195             $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar
196             ;;
197         esac
198         echo ""
199         ;;
200     *)
201         $JAVA -Dcadi_prop_files=$LOCAL/${NS}.props -jar $CONFIG/bin/aaf-cadi-aaf-*-full.jar "$CMD" "$@"
202         ;;
203     esac
204 fi