Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / packages / policy-xacmlpdp-tarball / src / main / resources / mysql / bin / create-guard-table.sh
1 #!/usr/bin/env sh
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
5 #  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6 #  Modifications Copyright (C) 2022-2023 Nordix Foundation. 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 #
20 # SPDX-License-Identifier: Apache-2.0
21 # ============LICENSE_END=========================================================
22 #
23
24 set -x
25
26 SQL_FILE="${POLICY_HOME}/mysql/sql/createguardtable.sql"
27 SQL_ADDON_FILE="${POLICY_HOME}/mysql/sql/db.sql"
28
29 # Remove escape backslashes if present and save output in temp file
30 sed 's/\\//g' "${POLICY_HOME}"/apps/guard/xacml.properties > /tmp/temp.xacml.properties
31
32 # Remove temp file
33 if [ ! -f /tmp/temp.xacml.properties ]; then
34     echo "Temporary guard xacml properties file not found!"
35     exit 1
36 fi
37
38 # Extract Maria DB Credential properties from xacml.properties file
39 DB_HOSTNAME=$(awk -F[/:] '$1 == "jakarta.persistence.jdbc.url=jdbc" { print $3 $5 }' /tmp/temp.xacml.properties)
40 DB_USERNAME=$(awk -F= '$1 == "jakarta.persistence.jdbc.user" { print $2 }' /tmp/temp.xacml.properties)
41 DB_PASSWORD=$(awk -F= '$1 == "jakarta.persistence.jdbc.password" { st = index($0,"="); print substr($0,st+1) }' /tmp/temp.properties)
42
43 # Remove temp file
44 rm /tmp/temp.xacml.properties
45
46 if [ -z "$DB_HOSTNAME" ]; then
47     echo "No db host provided in guard xacml.properties."
48     exit 2
49 fi
50
51 if [ -z "$DB_USERNAME" ]; then
52     echo "No db username provided in guard xacml.properties."
53     exit 2
54 fi
55
56 if [ -z "$DB_PASSWORD" ]; then
57     echo "No db password provided in guard xacml.properties."
58     exit 2
59 fi
60
61 if [ -z "$MYSQL_CMD" ]; then
62     MYSQL_CMD="mysql"
63 fi
64
65 # Execute sql command using sql file to create table
66 ${MYSQL_CMD} -u${DB_USERNAME} -p${DB_PASSWORD} -h${DB_HOSTNAME} < "${SQL_FILE}"
67
68 # Execute additional SQL configuration if provided
69 if [ -f "${POLICY_HOME}/mysql/sql/db.sql" ]; then
70     echo "additional SQL to be loaded found"
71     ${MYSQL_CMD} -u${DB_USERNAME} -p${DB_PASSWORD} -h${DB_HOSTNAME} < "${SQL_ADDON_FILE}"
72 fi