Implement public private locator
[aaf/authz.git] / auth / sample / bin / service.sh
1 #!/bin/bash
2 #########
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 #
21 # This script is run when starting aaf_config Container.
22 #  It needs to cover the cases where the initial data doesn't exist, and when it has already been configured (don't overwrite)
23 #
24 JAVA=/usr/bin/java
25
26 OSAAF=/opt/app/osaaf
27 LOCAL=$OSAAF/local
28 DATA=$OSAAF/data
29 PUBLIC=$OSAAF/public
30 CONFIG=/opt/app/aaf_config
31
32 JAVA_CADI="$JAVA -cp $CONFIG/bin/aaf-auth-cmd-*-full.jar org.onap.aaf.cadi.CmdLine" 
33 JAVA_AGENT="$JAVA -cp $CONFIG/bin/aaf-auth-cmd-*-full.jar -Dcadi_prop_files=$LOCAL/org.osaaf.aaf.props org.onap.aaf.cadi.configure.Agent" 
34 JAVA_AAFCLI="$JAVA -cp $CONFIG/bin/aaf-auth-cmd-*-full.jar -Dcadi_prop_files=$LOCAL/org.osaaf.aaf.props org.onap.aaf.auth.cmd.AAFcli" 
35
36 # If doesn't exist... still create
37 mkdir -p $OSAAF
38
39 # If not created by separate PV, create
40 mkdir -p /opt/app/aaf/status
41
42 # Temp use for clarity of code
43 FILE=
44
45 # Setup Bash, first time only
46 if [ ! -e "$HOME/.bash_aliases" ] || [ -z "$(grep cadi $HOME/.bash_aliases)" ]; then
47   echo "alias cadi='$JAVA_CADI \$*'" >>$HOME/.bash_aliases
48   echo "alias agent='$CONFIG/bin/agent.sh \$*'" >>$HOME/.bash_aliases
49   echo "alias aafcli='$JAVA_AAFCLI \$*'" >>$HOME/.bash_aliases
50   chmod a+x $CONFIG/bin/agent.sh
51   . $HOME/.bash_aliases
52 fi
53
54 # Only load Identities once
55 # echo "Check Identities"
56 FILE="$DATA/identities.dat"
57 if [ ! -e $FILE ]; then
58     mkdir -p $DATA
59     cp $CONFIG/data/sample.identities.dat $FILE
60     echo "Set Identities"
61     INITIALIZED="true"
62 fi
63
64 # Load up Cert/X509 Artifacts
65 # echo "Check Signer Keyfile"
66 FILE="$LOCAL/org.osaaf.aaf.signer.p12"
67 if [ ! -e $FILE ]; then
68     mkdir -p $LOCAL
69     mkdir -p $PUBLIC
70     if [ -e $CONFIG/cert/org.osaaf.aaf.signer.p12 ]; then
71         cp $CONFIG/cert/org.osaaf.aaf.signer.p12 $FILE
72         echo "Installed Signer P12"
73         INITIALIZED="true"
74     else
75         echo "Decode"
76         base64 -d $CONFIG/cert/demoONAPsigner.p12.b64 > $FILE
77         base64 -d $CONFIG/cert/truststoreONAP.p12.b64 > $PUBLIC/truststoreONAP.p12 
78         base64 -d $CONFIG/cert/truststoreONAPall.jks.b64 > $PUBLIC/truststoreONAPall.jks
79         ln -s $PUBLIC/truststoreONAPall.jks $LOCAL
80         cp $CONFIG/cert/AAF_RootCA.cer $PUBLIC
81         CM_TRUST_CAS="$PUBLIC/AAF_RootCA.cer"
82         echo "cadi_keystore_password=something easy" >> $CONFIG/local/aaf.props        
83         echo "Setup ONAP Test CAs and Signers"
84         INITIALIZED="true"
85     fi
86 fi
87
88 # echo "Check keyfile"
89 FILE="$LOCAL/org.osaaf.aaf.p12"
90 if [ ! -e $FILE ]; then
91     if [ -e $CONFIG/cert/org.osaaf.aaf.p12 ]; then
92         cp $CONFIG/cert/org.osaaf.aaf.p12 $FILE
93         echo "Installed AAF P12"
94         INITIALIZED="true"
95     else
96         echo "Bootstrap Creation of Keystore from Signer"
97         cd $CONFIG/CA
98         
99         # Remove this after Casablanca
100         CADI_X509_ISSUERS="CN=intermediateCA_1, OU=OSAAF, O=ONAP, C=US:CN=intermediateCA_7, OU=OSAAF, O=ONAP, C=US"
101         bash bootstrap.sh $LOCAL/org.osaaf.aaf.signer.p12 'something easy'
102         cp aaf.bootstrap.p12 $FILE
103         if [ -n "$CADI_X509_ISSUERS" ]; then
104             CADI_X509_ISSUERS="$CADI_X509_ISSUERS:"
105         fi
106         BOOT_ISSUER="$(cat aaf.bootstrap.issuer)"
107         CADI_X509_ISSUERS="$CADI_X509_ISSUERS$BOOT_ISSUER"
108
109         I=${BOOT_ISSUER##CN=};I=${I%%,*}
110         CM_CA_PASS="something easy"
111         CM_CA_LOCAL="org.onap.aaf.auth.cm.ca.LocalCA,$LOCAL/org.osaaf.aaf.signer.p12;aaf_intermediate_9;enc:"
112         CM_TRUST_CAS="$PUBLIC/AAF_RootCA.cer"
113         echo "Generated ONAP Test AAF certs"
114         INITIALIZED="true"
115     fi
116 fi
117
118 # Only initialize once, automatically...
119 if [ ! -e $LOCAL/org.osaaf.aaf.props ]; then
120     cp  $CONFIG/local/org.osaaf.aaf* $LOCAL
121     for D in public etc logs; do
122         mkdir -p $OSAAF/$D
123         cp $CONFIG/$D/* $OSAAF/$D
124     done
125
126     TMP=$(mktemp)
127     echo aaf_env=${AAF_ENV} >> ${TMP}
128     echo aaf_release=${VERSION} >> ${TMP}
129     echo cadi_latitude=${LATITUDE} >> ${TMP}
130     echo cadi_longitude=${LONGITUDE} >> ${TMP}
131     echo cadi_x509_issuers=${CADI_X509_ISSUERS} >> ${TMP}
132     AAF_LOCATE_URL=${aaf_locate_url:="https://${HOSTNAME}:8095"}
133     echo aaf_locate_url=${AAF_LOCATE_URL} >> ${TMP}
134     for P in `env`; do
135       if [[ "$P" == aaf_locator* ]]; then
136         echo "$P" >> ${TMP}
137         if [[ "$P" == aaf_locator_container=* ]]; then
138             echo aaf_locator_container.hostname=${HOSTNAME} >> ${TMP}
139         fi
140       fi
141     done
142
143     cat $TMP
144
145     $JAVA_AGENT config \
146         aaf@aaf.osaaf.org \
147         cadi_etc_dir=$LOCAL \
148         cadi_prop_files=$CONFIG/local/initialConfig.props:$CONFIG/local/aaf.props:${TMP}
149     rm ${TMP}
150
151     # Cassandra Config stuff
152     # Default is expect a Cassandra on same Node
153     CASS_HOST=${CASS_HOST:="aaf-cass"}
154     CASS_PASS=$($JAVA_CADI digest "${CASSANDRA_PASSWORD:-cassandra}" $LOCAL/org.osaaf.aaf.keyfile)
155     CASS_NAME=${CASS_HOST/:*/}
156     sed -i.backup -e "s/\\(cassandra.clusters=\\).*/\\1${CASSANDRA_CLUSTERS:=$CASS_HOST}/" \
157                   -e "s/\\(cassandra.clusters.user=\\).*/\\1${CASSANDRA_USER:=cassandra}/" \
158                   -e "s/\\(cassandra.clusters.password=enc:\\).*/\\1$CASS_PASS/" \
159                   -e "s/\\(cassandra.clusters.port=\\).*/\\1${CASSANDRA_PORT:=9042}/" \
160                   $LOCAL/org.osaaf.aaf.cassandra.props
161
162     if [ -n "$CM_CA_LOCAL" ]; then
163       if [ -n "$CM_CA_PASS" ]; then
164           CM_CA_LOCAL=$CM_CA_LOCAL$($JAVA_CADI digest "$CM_CA_PASS" $LOCAL/org.osaaf.aaf.keyfile)       
165       fi
166       # Move and copy method, rather than sed, because of slashes in CM_CA_LOCAL makes too complex
167       FILE=$LOCAL/org.osaaf.aaf.cm.ca.props
168       mv $FILE $FILE.backup
169       grep -v "cm_ca.local=" $FILE.backup > $FILE
170       echo "cm_ca.local=$CM_CA_LOCAL" >> $FILE
171       echo "cm_trust_cas=$CM_TRUST_CAS" >> $FILE
172     fi
173     echo "Created AAF Initial Configurations"
174     INITIALIZED="true"
175 fi
176
177
178 # Now run a command
179 CMD=$1
180 if [ -z "$CMD"  ]; then
181     if [ -n "$INITIALIZED" ]; then
182         echo "Initialization Complete"
183     else
184         echo "No Additional Initialization required"
185     fi
186 else
187     shift
188     case "$CMD" in
189     ls)
190         echo ls requested
191         find $OSAAF -depth
192         ;;
193     cat)
194         if [ "$1" = "" ]; then
195             echo "usage: cat <file... ONLY files ending in .props>"
196         else
197             if [[ $1 == *.props ]]; then
198                 echo
199                 echo "## CONTENTS OF $3"
200                 echo
201                 cat "$1"
202             else
203                 echo "### ERROR ####"
204                 echo "   \"cat\" may only be used with files ending with \".props\""
205             fi
206         fi
207         ;;
208     validate)
209         echo "## validate requested"
210         $JAVA_AAFCLI perm list user aaf@aaf.osaaf.org
211         ;;
212     onap)
213         #echo Initializing ONAP configurations.
214         ;;
215     bash)
216         shift
217         cd $LOCAL || exit
218         exec /bin/bash -c "$@"
219         ;;
220     setProp)
221         cd $LOCAL || exit
222         FILES=$(grep -l "$1" ./*.props)
223         if [ -z "$FILES" ]; then 
224             if [ -z "$3" ]; then
225                FILES=org.osaaf.aaf.props
226             else 
227                FILES="$3"
228             fi
229             ADD=Y
230         fi
231         for F in $FILES; do
232             if [ "$ADD" = "Y" ]; then
233                 echo "Changing $1 for $F"
234                 echo "$1=$2" >> $F
235             else 
236                echo "Changing $1 in $F"
237                sed -i.backup -e "s/\\(${1}=\\).*/\\1${2}/" $F
238             fi
239             cat $F
240         done
241         ;;
242     encrypt)
243         cd $LOCAL || exit
244         echo $1
245         FILES=$(grep -l "$1" ./*.props)
246         if [ "$FILES" = "" ]; then
247              FILES=$LOCAL/org.osaaf.aaf.cred.props
248              ADD=Y
249         fi
250         for F in $FILES; do
251             echo "Changing $1 in $F"
252             if [ "$2" = "" ]; then
253                 read -r -p "Password (leave blank to cancel): " -s ORIG_PW
254                 echo " "
255                 if [ "$ORIG_PW" = "" ]; then
256                     echo canceling...
257                     break
258                 fi
259             else
260                 ORIG_PW="$2"
261             fi
262             PWD=$($JAVA_CADI digest "$ORIG_PW" $LOCAL/org.osaaf.aaf.keyfile)
263             if [ "$ADD" = "Y" ]; then
264                   echo "$1=enc:$PWD" >> $F
265             else 
266                 sed -i.backup -e "s/\\($1.*enc:\\).*/\\1$PWD/" $F
267            fi
268             cat $F
269         done
270         ;;
271     taillog) 
272         sh $OSAAF/logs/taillog
273         ;;
274     wait)
275         bash $CONFIG/bin/pod_wait.sh wait $1
276         ;;
277     --help | -?)
278         case "$1" in
279         "")
280             echo "--- Agent Container Comands ---"
281             echo "  ls                      - Lists all files in Configuration"
282             echo "  cat <file.props>>       - Shows the contents (Prop files only)"
283             echo "  validate                - Runs a test using Configuration"
284             echo "  setProp <tag> [<value>] - set value on 'tag' (if no value, it will be queried from config)"
285             echo "  encrypt <tag> [<pass>]  - set passwords on Configuration (if no pass, it will be queried)"
286             echo "  bash                    - run bash in Container"
287             echo "     Note: the following aliases are preset"
288             echo "       cadi               - CADI CmdLine tool"
289             echo "       agent              - Agent Java tool (see above help)"
290             echo ""
291             echo " --help|-? [cadi|agent]   - This help, cadi help or agent help"
292             ;;
293         cadi)
294             echo "--- cadi Tool Comands ---"
295             $JAVA_CADI 
296             ;;
297         agent)
298             echo "--- agent Tool Comands ---"
299             $JAVA_AGENT 
300             ;;
301         aafcli)
302             echo "--- aafcli Tool Comands ---"
303             $JAVA_AAFCLI 
304             ;;
305         esac
306         echo ""
307         ;;
308     *)
309         $JAVA_AGENT "$CMD" "$@" cadi_prop_files=$LOCAL/org.osaaf.aaf.props 
310         ;;
311     esac
312 fi