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