Skip to content

Commit ac9fefd

Browse files
committed
Add audit logging at the start of the script
1 parent d08ac4a commit ac9fefd

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/scripts/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""To generate the SHA256 checksum run shasum -a 256 script.py."""
22

3-
CREATE_DEFAULT_SUPERUSER_CHECKSUM = "051463fc44db46b9a1a791530a8ab05ee2cdd6882613fddab839ceeaa20e1c98"
3+
CREATE_DEFAULT_SUPERUSER_CHECKSUM = "f9ede74987c9a774d3d836b2088b6a7fe6d9b2a845b8a7e54db97b0eb2c6dc74"

src/scripts/create_default_superuser.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
from fastcrud.exceptions.http_exceptions import DuplicateValueException
2828

2929
from . import CREATE_DEFAULT_SUPERUSER_CHECKSUM as EXPECTED_CHECKSUM
30-
from .utils import ScriptIntegrityError, verify_script_integrity
30+
from .utils import ScriptIntegrityError, get_audit_info, verify_script_integrity
3131

32-
logger = logging.getLogger(os.path.basename(__file__))
32+
SCRIPT_PATH = os.path.abspath(__file__)
33+
SCRIPT_NAME = os.path.basename(__file__)
34+
logger = logging.getLogger(SCRIPT_NAME)
35+
audit_info = get_audit_info(SCRIPT_PATH)
36+
logger.warning(f"Script being run by: {json.dumps(audit_info, default=str, indent=2)}")
3337

3438
# Do not change these default values, read the file docstrings for context
3539
DEFAULT_NAME = "Default Superuser"
@@ -38,7 +42,7 @@
3842

3943

4044
async def async_main(password: str):
41-
logger.info(f"Running script {os.path.basename(__file__)}")
45+
logger.info(f"Running script {SCRIPT_NAME}")
4246
logger.debug("Creating hashed password")
4347
hashed_password = get_password_hash(password)
4448
logger.debug("Preparing superuser data")
@@ -99,7 +103,7 @@ def main(password: str):
99103

100104
if __name__ == "__main__":
101105
try:
102-
verify_script_integrity(os.path.abspath(__file__), EXPECTED_CHECKSUM)
106+
verify_script_integrity(SCRIPT_PATH, EXPECTED_CHECKSUM)
103107
except ScriptIntegrityError as e:
104108
logger.error(e)
105109
sys.exit(1) # Exit with failure code

src/scripts/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
import datetime
2+
import getpass
3+
import os
4+
import socket
5+
6+
7+
def get_audit_info(script_name: str) -> dict:
8+
"""Returns a dictionary with audit details about the script execution environment."""
9+
user = getpass.getuser()
10+
hostname = socket.gethostname()
11+
try:
12+
ip = socket.gethostbyname(hostname)
13+
except Exception:
14+
ip = None
15+
cwd = os.getcwd()
16+
now = datetime.datetime.now().isoformat()
17+
return {
18+
"user": user,
19+
"hostname": hostname,
20+
"ip": ip,
21+
"cwd": cwd,
22+
"script": os.path.abspath(script_name),
23+
"time": now,
24+
}
25+
26+
127
import hashlib
228

329

0 commit comments

Comments
 (0)