Skip to content

Commit c0e884f

Browse files
committed
Revised logging integration changes based on code review
1 parent 72097f0 commit c0e884f

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

application/Profile/DBProfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(self, app):
8383
self.activation_email_body_template = f.read()
8484
except OSError as e:
8585
logger.exception('Failed to open "%s"', ACTIVATION_EMAIL_TEMPLATE)
86-
raise e
86+
raise
8787

8888
class User(db.Model):
8989
__table_args__ = {"schema": "ictrl"}
@@ -217,7 +217,7 @@ def query(self):
217217
"username": session.username
218218
}
219219

220-
logger.info("Query user sessions successful, all user sessions:\n%s", json.dumps(_profile))
220+
logger.info("User sessions queried successfully.")
221221

222222
return _profile
223223

@@ -436,6 +436,6 @@ def send_activation_email(self, username):
436436
expire_min=int(ACTIVATION_TTL_SECOND / 60))
437437
send_email(user.email, 'Activate Your iCtrl Account', body)
438438

439-
logger.info("Successfully sent out activation email to email=%s", user.email)
439+
logger.info("Successfully sent out activation email to user ID=%s", user.id)
440440

441441
return True

application/Profile/LocalProfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def add_session(self, host, username, conn=None) -> tuple[bool, str]:
104104

105105
def delete_session(self, session_id) -> tuple[bool, str]:
106106
if session_id not in self._profile['sessions']:
107-
logger.error('Cannot delete session %s, session does not exist', session_id)
107+
logger.warning('Cannot delete session %s, session does not exist', session_id)
108108
return False, f'failed: session {session_id} does not exist'
109109

110110
try:
111111
os.remove(os.path.join(PRIVATE_KEY_PATH, session_id))
112112
except FileNotFoundError:
113-
logger.exception('No valid SSH key found for deletion')
113+
logger.warning('No valid SSH key found for deletion')
114114

115115
self._profile['sessions'].pop(session_id)
116116
self.save_profile()
@@ -121,7 +121,7 @@ def delete_session(self, session_id) -> tuple[bool, str]:
121121

122122
def change_host(self, session_id, new_host) -> tuple[bool, str]:
123123
if session_id not in self._profile['sessions']:
124-
logger.error("Cannot change host, session %s does not exist", session_id)
124+
logger.warning("Cannot change host, session %s does not exist", session_id)
125125
return False, f'failed: session {session_id} does not exist'
126126

127127
self._profile["sessions"][session_id]['host'] = new_host
@@ -206,7 +206,7 @@ def get_session_vnc_credentials(self, session_id) -> tuple[bool, object]:
206206
logger.error("Cannot retrieve session %s, session does not exist", session_id)
207207
return False, f'failed: session {session_id} does not exist'
208208

209-
logger.info("Retrieving vnc credentials for session %s", session_id)
209+
logger.debug("Retrieving VNC credentials for session %s", session_id)
210210
if 'vnc_credentials' in self._profile['sessions'][session_id]:
211211
json_str = base64.b64decode(self._profile['sessions'][session_id]['vnc_credentials'])
212212
return True, json.loads(json_str.decode('ascii'))

application/paths.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
def makedir_if_not_exists(path):
3030
if not os.path.exists(path):
3131
os.mkdir(path)
32-
logger.info('Created directory, path = %s', path)
32+
logger.info('Created directory at path = %s', path)
3333

3434

3535
# setup profile path
@@ -38,7 +38,8 @@ def makedir_if_not_exists(path):
3838
elif platform.system() == "Darwin" or 'Linux':
3939
PROFILE_PATH = os.path.join(os.path.expanduser("~"), ".ictrl")
4040
else:
41-
logger.error("Operating System: %s not supported", platform.system())
41+
logger.error("Unsupported operating system detected - OS: %s, Version: %s, Machine: %s",
42+
platform.system(), platform.version(), platform.machine())
4243
raise SystemError(f"Operating System: {platform.system()} not supported")
4344

4445
makedir_if_not_exists(PROFILE_PATH)

application/utils.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def send_email(to_email, subject, body):
6464

6565
server_ssl.sendmail(sender_email, to_email, msg.as_string())
6666

67-
logger.info('Successfully sent email from %s to %s', sender_email, to_email)
67+
logger.info('Successfully sent email from %s to %s', mask_email(sender_email), mask_email(to_email))
6868

6969
server_ssl.close()
7070

@@ -125,3 +125,35 @@ def local_auth(headers, abort_func):
125125
abort_func(403, "You are not authorized to access this API.")
126126

127127
return auth_passed
128+
129+
130+
def mask_email(email: str) -> str:
131+
"""
132+
Masks an email address for secure logging by keeping first character and domain.
133+
134+
Example:
135+
mask_email("user@example.com")
136+
'u***@example.com'
137+
mask_email("a.b.c@sub.example.com")
138+
'a***@sub.example.com'
139+
mask_email("invalid.email")
140+
'***@***'
141+
142+
Args:
143+
email: The email address to mask
144+
145+
Returns:
146+
Masked email address with only first character and domain visible
147+
"""
148+
try:
149+
if '@' not in email:
150+
return '***@***'
151+
152+
local, domain = email.split('@', 1)
153+
if not local:
154+
return '***@' + domain
155+
156+
masked = local[0] + '*' * (len(local) - 1)
157+
return f'{masked}@{domain}'
158+
except Exception:
159+
return '***@***' # Fallback for any parsing errors

0 commit comments

Comments
 (0)