Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some database scripts and .gitignore entry #16

Merged
merged 8 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions scripts/database/create-auth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Database credentials
DATABASE="aerius_authorization"
USER="aerius"
PASSWORD="aerius"

# Create a new user
echo "Creating user if not exists"
psql -q -U postgres postgres <<OMG
DO \$\$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_catalog.pg_roles
WHERE rolname = '$USER')
THEN
CREATE ROLE $USER WITH LOGIN PASSWORD '$PASSWORD';
ALTER ROLE $USER SUPERUSER;
ALTER USER $USER CREATEDB;
END IF;
END
\$\$;
OMG

# Check if the database exists
DB_EXISTS=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE'")

# If it doesn't exist, create it
if [ "$DB_EXISTS" = "1" ]
then
echo "Database $DATABASE already exists. Skipping creation."
else
echo "Database $DATABASE does not exist. Creating..."
psql -U postgres -c "CREATE DATABASE $DATABASE WITH OWNER $USER"
fi

echo -e "===\nDatabase creation complete."
8 changes: 8 additions & 0 deletions scripts/database/drop-auth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# Database credentials
DATABASE="aerius_authorization"

dropdb -U postgres $DATABASE

echo -e "===\nDatabase uncreation complete."
10 changes: 10 additions & 0 deletions scripts/database/generate-auth-database-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e

# Change current directory to source directory so it can be called from everywhere
SCRIPT_PATH=$(readlink -f "${0}")
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")

cd "${SCRIPT_DIR}/../../source"

mvn clean generate-sources -Dgenerate-jooq.url=jdbc:postgresql://localhost:5432/auth
12 changes: 12 additions & 0 deletions scripts/misc/add-host-mapping.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
HOSTS_FILE="/etc/hosts"
HOST_LINE="127.0.0.1 aerius-auth-local"

# Check if the line already exists in the file
if ! grep -q "$HOST_LINE" $HOSTS_FILE; then
# If it doesn't exist, append it to the file
echo "$HOST_LINE" | sudo tee -a $HOSTS_FILE > /dev/null
echo "Added $HOST_LINE to your $HOSTS_FILE"
else
echo "$HOST_LINE already exists in your $HOSTS_FILE"
fi
42 changes: 42 additions & 0 deletions scripts/misc/create-user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# First, you need to ensure that you have received all the necessary arguments
if [ "$#" -ne 3 ]; then
echo "You must enter exactly 3 arguments: username, plaintext password, and role name"
exit 1
fi

# Assigning command line arguments to variables
DBNAME="aerius_authorization"
DBUSER="aerius"
DBPASS="aerius"
DBHOST="localhost"

USERNAME=$1
PLAIN_PASSWORD=$2
ROLE=$3

# Create a temporary file for htpasswd output
TEMP_FILE=$(mktemp)

# Generate bcrypt hash with htpasswd
# Here, -B means bcrypt, -bn means don't add a newline and username:password is the input
htpasswd -Bbn "$USERNAME" "$PLAIN_PASSWORD" > "$TEMP_FILE"

# Extract just the hash from the output
# Cut -d: -f2 removes the username and colon at the start
BCRYPT_HASH=$(cut -d: -f2 "$TEMP_FILE")

# Clean up the temporary file
rm "$TEMP_FILE"

export PGPASSWORD=$DBPASS

# SQL Command
SQL_COMMAND="SELECT auth.ae_create_local_user('$USERNAME', '$BCRYPT_HASH', '$ROLE');"

# Execute SQL Command
psql -h $DBHOST -U $DBUSER -d $DBNAME -c "$SQL_COMMAND"

# Unset the PGPASSWORD environment variable
unset PGPASSWORD
18 changes: 18 additions & 0 deletions scripts/misc/view-roles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Database details
DBNAME="aerius_authorization"
DBUSER="aerius"
DBPASS="aerius"
DBHOST="localhost" # Assuming your database is on the same machine. Change if needed.

export PGPASSWORD=$DBPASS

# SQL Command to list roles from the auth.roles table
SQL_COMMAND="SELECT * FROM auth.roles;"

# Execute SQL Command
psql -h $DBHOST -U $DBUSER -d $DBNAME -c "$SQL_COMMAND"

# Unset the PGPASSWORD environment variable
unset PGPASSWORD
18 changes: 18 additions & 0 deletions scripts/misc/view-users.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Database details
DBNAME="aerius_authorization"
DBUSER="aerius"
DBPASS="aerius"
DBHOST="localhost" # Assuming your database is on the same machine. Change if needed.

export PGPASSWORD=$DBPASS

# SQL Command to list roles from the auth.roles table
SQL_COMMAND="SELECT * FROM auth.users;"

# Execute SQL Command
psql -h $DBHOST -U $DBUSER -d $DBNAME -c "$SQL_COMMAND"

# Unset the PGPASSWORD environment variable
unset PGPASSWORD
1 change: 1 addition & 0 deletions source/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application.properties
JornC marked this conversation as resolved.
Show resolved Hide resolved