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

refactor: only replace password by star when logging level is debug #1820

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ checkfiles = tortoise/ examples/ tests/ conftest.py
py_warn = PYTHONDEVMODE=1
pytest_opts = -n auto --cov=tortoise --cov-append --tb=native -q

TORTOISE_MYSQL_PASS ?= 123456
TORTOISE_POSTGRES_PASS ?= 123456
TORTOISE_MSSQL_PASS ?= 123456
TORTOISE_ORACLE_PASS ?= 123456

help:
@echo "Tortoise ORM development makefile"
@echo
Expand Down Expand Up @@ -76,10 +81,10 @@ docs: deps
rm -fR ./build
sphinx-build -M html docs build

style: deps _style
_style:
isort -src $(checkfiles)
black $(checkfiles)
style: _style deps

build: deps
rm -fR dist/
Expand Down
34 changes: 20 additions & 14 deletions tortoise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import importlib
import importlib.metadata as importlib_metadata
import json
import logging
import os
import warnings
from copy import deepcopy
Expand Down Expand Up @@ -497,7 +498,24 @@ async def init(

cls.table_name_generator = table_name_generator

# Mask passwords in logs output
if logger.isEnabledFor(logging.DEBUG):
waketzheng marked this conversation as resolved.
Show resolved Hide resolved
str_connection_config = cls.star_password(connections_config)
logger.debug(
"Tortoise-ORM startup\n connections: %s\n apps: %s",
str_connection_config,
str(apps_config),
)

cls._init_timezone(use_tz, timezone)
await connections._init(connections_config, _create_db)
cls._init_apps(apps_config)
cls._init_routers(routers)

cls._inited = True

@staticmethod
def star_password(connections_config) -> str:
# Mask passwords to hide sensitive information in logs output
passwords = []
for name, info in connections_config.items():
if isinstance(info, str):
Expand All @@ -512,19 +530,7 @@ async def init(
# Show one third of the password at beginning (may be better for debugging purposes)
f"{password[0:len(password) // 3]}***",
)

logger.debug(
"Tortoise-ORM startup\n connections: %s\n apps: %s",
str_connection_config,
str(apps_config),
)

cls._init_timezone(use_tz, timezone)
await connections._init(connections_config, _create_db)
cls._init_apps(apps_config)
cls._init_routers(routers)

cls._inited = True
return str_connection_config

@classmethod
def _init_routers(cls, routers: list[str | type] | None = None) -> None:
Expand Down