[AAI-2174] Run container process as non-root
[aai/babel.git] / src / main / bin / start.sh
1 #!/bin/sh
2
3 # ============LICENSE_START=======================================================
4 # org.onap.aai
5 # ================================================================================
6 # Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
7 # Copyright (c) 2017-2019 European Software Marketing Ltd.
8 # ================================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END=========================================================
21
22 # jre-alpine image has $JAVA_HOME set and added to $PATH
23 # ubuntu image requires to set $JAVA_HOME and add java to $PATH manually
24 if ( uname -v | grep -i "ubuntu" ); then
25     export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-`dpkg --print-architecture | awk -F- '{ print $NF }'`
26     export PATH=${JAVA_HOME}:$PATH
27 fi
28
29 APP_HOME="${APP_HOME:-/opt/app/babel}"
30
31 if [ -z "${CONFIG_HOME}" ]; then
32     echo "CONFIG_HOME must be set in order to start the process"
33     exit 1
34 fi
35
36 if [ -z "${KEY_STORE_PASSWORD}" ]; then
37     echo "KEY_STORE_PASSWORD must be set in order to start the process"
38     exit 1
39 fi
40
41 # Changes related to:AAI-2174
42 # Change aai babel  container processes to run as non-root on the host
43 USER_ID=${LOCAL_USER_ID:-9001}
44 GROUP_ID=${LOCAL_GROUP_ID:-9001}
45
46 if [ $(cat /etc/passwd | grep aaiadmin | wc -l) -eq 0 ]; then
47
48         groupadd aaiadmin -g ${GROUP_ID} || {
49                 echo "Unable to create the group id for ${GROUP_ID}";
50                 exit 1;
51         }
52         useradd --shell=/bin/bash -u ${USER_ID} -g ${GROUP_ID} -o -c "" -m aaiadmin || {
53                 echo "Unable to create the user id for ${USER_ID}";
54                 exit 1;
55         }
56 fi;
57
58 chown -R aaiadmin:aaiadmin ${APP_HOME}
59 find ${APP_HOME}  -name "*.sh" -exec chmod +x {} +
60
61 gosu aaiadmin ln -s /logs $MICRO_HOME/logs
62
63 JAVA_CMD="exec gosu aaiadmin java";
64 ###
65 PROPS="-DAPP_HOME=${APP_HOME}"
66 PROPS="${PROPS} -DCONFIG_HOME=${CONFIG_HOME}"
67 PROPS="${PROPS} -Dtosca.mappings.config=${CONFIG_HOME}/tosca-mappings.json"
68 PROPS="${PROPS} -DKEY_STORE_PASSWORD=${KEY_STORE_PASSWORD}"
69 if [ ! -z "$REQUIRE_CLIENT_AUTH" ]; then
70     PROPS="$PROPS -Dserver.ssl.client-auth=${REQUIRE_CLIENT_AUTH}"
71 fi
72
73 JVM_MAX_HEAP=${MAX_HEAP:-1024}
74
75 JARFILE=$(ls ./babel*.jar);
76
77 ${JAVA_CMD} -Xmx${JVM_MAX_HEAP}m ${PROPS} -jar ${APP_HOME}/${JARFILE}