Fix guard table creation during startup issue
[policy/xacml-pdp.git] / packages / policy-xacmlpdp-tarball / src / main / resources / mysql / bin / create-guard-table.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20 #
21 SQL_FILE="${POLICY_HOME}/mysql/sql/createguardtable.sql"
22
23 # Remove escape backslashes if present and save output in temp file
24 sed 's/\\//g' "${POLICY_HOME}"/apps/guard/xacml.properties > /tmp/temp.xacml.properties
25
26 # Remove temp file
27 if [ ! -f /tmp/temp.xacml.properties ]
28   then
29     echo "Temporary guard xacml properties file not found!"
30     exit 1
31 fi
32
33 # Extract Maria DB Credential properties from xacml.properties file
34 DB_HOSTNAME=$(awk -F[/:] '$1 == "javax.persistence.jdbc.url=jdbc" { print $3 $5 }' /tmp/temp.xacml.properties)
35 DB_USERNAME=$(awk -F= '$1 == "javax.persistence.jdbc.user" { print $2 }' /tmp/temp.xacml.properties)
36 DB_PASSWORD=$(awk -F= '$1 == "javax.persistence.jdbc.password" { print $2 }' /tmp/temp.xacml.properties | base64 -d)
37
38 # Remove temp file
39 rm /tmp/temp.xacml.properties
40
41 if [ -z "$DB_HOSTNAME" ]
42   then
43     echo "No Mariadb host provided in guard xacml.properties."
44     exit 2
45 fi
46
47 if [ -z "$DB_USERNAME" ]
48   then
49     echo "No Mariadb username provided in guard xacml.properties."
50     exit 2
51 fi
52
53 if [ -z "$DB_PASSWORD" ]
54   then
55     echo "No Mariadb password provided in guard xacml.properties."
56     exit 2
57 fi
58
59 # Execute mysql command using sql file to create table
60 mysql -u${DB_USERNAME} -p${DB_PASSWORD} -h${DB_HOSTNAME} < "${SQL_FILE}"
61