1a71f8b4b8cac7049a8dc9f4c79d1efc345a6f50
[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 # ================================================================================
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-pg.sql"
22
23 # Remove escape backslashes if present and save output in temp file
24 sed 's/\\//g' "${POLICY_HOME}"/apps/guard/xacml-pg.properties > /tmp/temp.xacml-pg.properties
25
26 # Remove temp file
27 if [ ! -f /tmp/temp.xacml-pg.properties ]; then
28     echo "Temporary guard xacml properties file not found!"
29     exit 1
30 fi
31
32 # Extract Maria DB Credential properties from xacml.properties file
33 DB_HOSTNAME=$(awk -F[/:] '$1 == "javax.persistence.jdbc.url=jdbc" { print $3 $5 }' /tmp/temp.xacml-pg.properties)
34 DB_USERNAME=$(awk -F= '$1 == "javax.persistence.jdbc.user" { print $2 }' /tmp/temp.xacml-pg.properties)
35 DB_PASSWORD=$(awk -F= '$1 == "javax.persistence.jdbc.password" { print $2 }' /tmp/temp.xacml-pg.properties)
36
37 # Remove temp file
38 rm /tmp/temp.xacml-pg.properties
39
40 if [ -z "$DB_HOSTNAME" ]; then
41     echo "No db host provided in guard xacml-pg.properties."
42     exit 2
43 fi
44
45 if [ -z "$DB_USERNAME" ]; then
46     echo "No db username provided in guard xacml-pg.properties."
47     exit 2
48 fi
49
50 if [ -z "$DB_PASSWORD" ]; then
51     echo "No db password provided in guard xacml-pg.properties."
52     exit 2
53 fi
54
55 # Execute sql command using sql file to create table
56 psql -U postgres -h ${DB_HOSTNAME} -f ${SQL_FILE}