-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* DDLS-379 add a read only user * dockerise sql run command
- Loading branch information
1 parent
dc9c4ff
commit 1408073
Showing
23 changed files
with
200 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
api/app/scripts/readonly_user_setup/_0_readonly_user_create.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
DO | ||
$$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT * FROM pg_user WHERE usename = 'readonly_sql_user') THEN | ||
CREATE USER readonly_sql_user WITH PASSWORD 'string_to_replace_with_real_password'; | ||
END IF; | ||
END | ||
$$ | ||
; |
11 changes: 11 additions & 0 deletions
11
api/app/scripts/readonly_user_setup/_1_readonly_user_grant_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
DO $$ | ||
DECLARE | ||
schema_name_var text; | ||
BEGIN | ||
FOR schema_name_var IN | ||
SELECT schema_name FROM information_schema.schemata | ||
WHERE schema_name NOT IN ('information_schema', 'pg_catalog') -- Skip system schemas | ||
LOOP | ||
EXECUTE format('GRANT USAGE ON SCHEMA %I TO readonly_sql_user;', schema_name_var); | ||
END LOOP; | ||
END $$; |
11 changes: 11 additions & 0 deletions
11
api/app/scripts/readonly_user_setup/_2_readonly_user_grant_select.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
DO $$ | ||
DECLARE | ||
tbl record; | ||
BEGIN | ||
FOR tbl IN | ||
SELECT schemaname, tablename FROM pg_tables | ||
WHERE schemaname NOT IN ('information_schema', 'pg_catalog') -- Skip system schemas | ||
LOOP | ||
EXECUTE format('GRANT SELECT ON TABLE %I.%I TO readonly_sql_user;', tbl.schemaname, tbl.tablename); | ||
END LOOP; | ||
END $$; |
11 changes: 11 additions & 0 deletions
11
api/app/scripts/readonly_user_setup/_3_readonly_user_revoke_update_delete.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
DO $$ | ||
DECLARE | ||
tbl record; | ||
BEGIN | ||
FOR tbl IN | ||
SELECT schemaname, tablename FROM pg_tables | ||
WHERE schemaname NOT IN ('information_schema', 'pg_catalog') -- Skip system schemas | ||
LOOP | ||
EXECUTE format('REVOKE UPDATE, DELETE ON TABLE %I.%I FROM readonly_sql_user;', tbl.schemaname, tbl.tablename); | ||
END LOOP; | ||
END $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/sh | ||
|
||
# Directory where SQL scripts are stored | ||
SQL_DIR="./scripts/readonly_user_setup" | ||
|
||
# Export the database password for psql command | ||
export PGPASSWORD="$DATABASE_PASSWORD" | ||
|
||
# Check if directory exists | ||
if [ ! -d "$SQL_DIR" ]; then | ||
echo "Directory $SQL_DIR does not exist." | ||
exit 1 | ||
fi | ||
|
||
# Find all SQL files in the directory, sort them numerically, and loop through each one | ||
for sql_file in $(ls $SQL_DIR/*.sql | sort -V); do | ||
echo "Running $sql_file ..." | ||
|
||
# Create a temporary file for the modified SQL | ||
temp_file=$(mktemp) | ||
|
||
# Check if password is empty and exit if it is! | ||
if [ -z "$READONLY_SQL_DATABASE_PASSWORD" ]; then | ||
echo "READONLY_SQL_DATABASE_PASSWORD is empty. Exiting..." | ||
exit 1 | ||
fi | ||
|
||
# Replace the placeholder string with the real password | ||
sed "s/string_to_replace_with_real_password/$READONLY_SQL_DATABASE_PASSWORD/g" "$sql_file" > "$temp_file" | ||
|
||
# Run the modified SQL file | ||
psql -h "$DATABASE_HOSTNAME" -U "$DATABASE_USERNAME" -d "$DATABASE_NAME" -p "$DATABASE_PORT" -f "$temp_file" | ||
|
||
# Check for errors | ||
if [ $? -ne 0 ]; then | ||
echo "Error occurred while executing $sql_file. Exiting." | ||
rm "$temp_file" # Remove the temp file if an error occurs | ||
exit 1 | ||
fi | ||
|
||
# Remove the temporary file after successful execution | ||
rm "$temp_file" | ||
done | ||
|
||
echo "All scripts executed successfully." | ||
|
||
# Unset the password environment variable | ||
unset PGPASSWORD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
awslambdaric~=2.2.1 | ||
boto3~=1.35.20 | ||
psycopg2~=2.9.9 | ||
setuptools~=70.0.0 | ||
setuptools~=75.2.0 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Define function directory | ||
ARG FUNCTION_DIR="/function" | ||
|
||
# ===== BASE IMAGE ===== | ||
FROM python:3.12-alpine3.18 AS python-alpine | ||
RUN pip install --upgrade pip setuptools wheel | ||
RUN apk update && apk upgrade | ||
|
||
# ===== Build image ===== | ||
FROM python-alpine as build-image | ||
# Include global arg in this stage of the build | ||
ARG FUNCTION_DIR | ||
# Create function directory | ||
RUN mkdir -p ${FUNCTION_DIR} | ||
# Copy function code | ||
COPY run_custom_query.py ${FUNCTION_DIR}/run_custom_query.py | ||
COPY _verification.sql ${FUNCTION_DIR}/_verification.sql | ||
COPY _run.sql ${FUNCTION_DIR}/_run.sql | ||
|
||
COPY requirements.txt requirements.txt | ||
# Install the requirements | ||
RUN python -m pip install --upgrade pip | ||
RUN python -m pip install \ | ||
--target ${FUNCTION_DIR} \ | ||
--requirement requirements.txt | ||
|
||
# ===== FINAL IMAGE ===== | ||
FROM python-alpine | ||
# Include global arg in this stage of the build | ||
ARG FUNCTION_DIR | ||
# Set working directory to function root directory | ||
WORKDIR ${FUNCTION_DIR} | ||
# Copy in the build image dependencies | ||
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR} | ||
|
||
ENTRYPOINT [ "python3", "/function/run_custom_query.py" ] |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## How to run custom queries | ||
|
||
You will need aws-vault and operator permissions. | ||
|
||
You can then perform the commands required using a docker wrapper container wrapped by a make file. | ||
|
||
Full details about how this works here: [Custom SQL Details](../../lambdas/functions/custom_sql_query/custom_sql_query.md) | ||
|
||
Remember to edit the SQL and validation scripts in this folder. | ||
|
||
Example make commands: | ||
|
||
``` | ||
aws-vault exec identity -- make sql-custom-command-insert workspace=ddls1234000 before=1 after=0 | ||
aws-vault exec identity -- make sql-custom-command-get workspace=ddls1234000 id=1 | ||
aws-vault exec identity -- make sql-custom-command-sign-off workspace=ddls1234000 id=1 | ||
aws-vault exec identity -- make sql-custom-command-execute workspace=ddls1234000 id=1 | ||
aws-vault exec identity -- make sql-custom-command-revoke workspace=ddls1234000 id=1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
boto3~=1.35.20 | ||
requests~=2.32.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters