Skip to content

Commit

Permalink
Run lint
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Feb 7, 2025
1 parent c1a9ebf commit 72b733e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
35 changes: 27 additions & 8 deletions backend/src/xfd_django/xfd_api/helpers/infra_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import os

# Third-Party Libraries
import pymysql
from django.conf import settings
from django.db import connections
import pymysql


def create_scan_user():
"""Create and configure the XFD scanning user if it does not already exist."""
Expand Down Expand Up @@ -56,6 +57,7 @@ def create_scan_user():
except Exception as e:
print("Error creating or configuring scan user: {}".format(e))


def create_matomo_scan_user():
"""Create and configure the Matomo scanning user if it does not already exist."""
# Only create if not in the DMZ
Expand Down Expand Up @@ -83,7 +85,7 @@ def create_matomo_scan_user():
user=db_user,
password=db_password,
database=db_name,
cursorclass=pymysql.cursors.DictCursor
cursorclass=pymysql.cursors.DictCursor,
)

with conn.cursor() as cursor:
Expand All @@ -93,19 +95,36 @@ def create_matomo_scan_user():

if not user_exists:
# Create the scan user
cursor.execute("CREATE USER %s@'%%' IDENTIFIED BY %s;", [scan_user, scan_password])
print("User '{}' created successfully in Matomo database.".format(scan_user))
cursor.execute(
"CREATE USER %s@'%%' IDENTIFIED BY %s;", [scan_user, scan_password]
)
print(
"User '{}' created successfully in Matomo database.".format(
scan_user
)
)
else:
print("User '{}' already exists. Skipping creation.".format(scan_user))

# Grant privileges (aligned with PostgreSQL version)
cursor.execute("GRANT CONNECT ON DATABASE {} TO %s@'%%';".format(db_name), [scan_user])
cursor.execute(
"GRANT CONNECT ON DATABASE {} TO %s@'%%';".format(db_name), [scan_user]
)
cursor.execute("GRANT USAGE ON SCHEMA public TO %s@'%%';", [scan_user])
cursor.execute("GRANT SELECT ON {}.* TO %s@'%%';".format(db_name), [scan_user])
cursor.execute("ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO %s@'%%';", [scan_user]) # ✅ ADDED
cursor.execute(
"GRANT SELECT ON {}.* TO %s@'%%';".format(db_name), [scan_user]
)
cursor.execute(
"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO %s@'%%';",
[scan_user],
) # ✅ ADDED
cursor.execute("FLUSH PRIVILEGES;") # MariaDB-specific, but necessary

print("User '{}' configured successfully in Matomo database.".format(scan_user))
print(
"User '{}' configured successfully in Matomo database.".format(
scan_user
)
)

conn.commit()
conn.close()
Expand Down
5 changes: 1 addition & 4 deletions backend/src/xfd_django/xfd_api/tasks/infra_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
django.setup()

# Third-Party Libraries
from xfd_api.helpers.infra_helpers import (
create_scan_user,
create_matomo_scan_user
)
from xfd_api.helpers.infra_helpers import create_matomo_scan_user, create_scan_user


def handler(event, context):
Expand Down

0 comments on commit 72b733e

Please sign in to comment.