Skip to content

Commit f84cb48

Browse files
committed
refactor: remove PII from log messages in programs tasks
[APER-3723] Refactors a number of log statements from the Celery tasks in the Programs Django app. This removes username (considered PII) from the log statements and opts to use LMS User ID instead.
1 parent f2ad108 commit f84cb48

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

openedx/core/djangoapps/programs/tasks.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,14 @@ def award_program_certificates(self, username): # lint-amnesty, pylint: disable
315315
if str(programs_without_certificates[0]).lower() == "all":
316316
return
317317

318-
LOGGER.info(f"Running task award_program_certificates for user {student}")
318+
LOGGER.info(f"Running task award_program_certificates for user {student.id}")
319319
try:
320320
completed_programs = {}
321321
for site in Site.objects.all():
322322
completed_programs.update(get_completed_programs(site, student))
323323

324324
if not completed_programs:
325-
LOGGER.warning(f"Task award_program_certificates was called for user {student} with no completed programs")
325+
LOGGER.warning(f"Task award_program_certificates was called for user {student.id} with no completed programs")
326326
return
327327

328328
# determine which program certificates have been awarded to the user
@@ -331,7 +331,7 @@ def award_program_certificates(self, username): # lint-amnesty, pylint: disable
331331
# program is part of the "programs without certificates" list in our site configuration
332332
awarded_and_skipped_program_uuids = list(set(existing_program_uuids + list(programs_without_certificates)))
333333
except Exception as exc:
334-
error_msg = f"Failed to determine program certificates to be awarded for user {student}: {exc}"
334+
error_msg = f"Failed to determine program certificates to be awarded for user {student.id}: {exc}"
335335
LOGGER.exception(error_msg)
336336
raise MaxRetriesExceededError(
337337
f"Failed to award a program certificate to user {student.id}. Reason: {error_msg}"
@@ -360,52 +360,52 @@ def award_program_certificates(self, username): # lint-amnesty, pylint: disable
360360
for program_uuid in new_program_uuids:
361361
try:
362362
award_program_certificate(credentials_client, student, program_uuid)
363-
LOGGER.info(f"Awarded program certificate to user {student} in program {program_uuid}")
363+
LOGGER.info(f"Awarded program certificate to user {student.id} in program {program_uuid}")
364364
except HTTPError as exc:
365365
if exc.response.status_code == 404:
366366
LOGGER.warning(
367-
f"Unable to award a program certificate to user {student} in program {program_uuid}. A "
367+
f"Unable to award a program certificate to user {student.id} in program {program_uuid}. A "
368368
f"certificate configuration for program {program_uuid} could not be found, the program might "
369369
"not be configured correctly in Credentials"
370370
)
371371
elif exc.response.status_code == 429:
372372
# Let celery handle retry attempts and backoff
373373
error_msg = (
374-
f"Rate limited. Attempting to award certificate to user {student} in program {program_uuid}."
374+
f"Rate limited. Attempting to award certificate to user {student.id} in program {program_uuid}."
375375
)
376376
LOGGER.warning(error_msg)
377377
raise MaxRetriesExceededError(
378378
f"Failed to award a program certificate to user {student.id}. Reason: {error_msg}"
379379
) from exc
380380
else:
381381
LOGGER.warning(
382-
f"Unable to award program certificate to user {student} in program {program_uuid}. The program "
383-
"might not be configured correctly in Credentials"
382+
f"Unable to award program certificate to user {student.id} in program {program_uuid}. The "
383+
"program might not be configured correctly in Credentials"
384384
)
385385
except Exception as exc: # pylint: disable=broad-except
386386
# keep trying to award other certs, but let celery retry the whole task to fix any missing entries
387387
LOGGER.exception(
388-
f"Failed to award program certificate to user {student} in program {program_uuid}: {exc}"
388+
f"Failed to award program certificate to user {student.id} in program {program_uuid}: {exc}"
389389
)
390390
failed_program_certificate_award_attempts.append(program_uuid)
391391

392392
if failed_program_certificate_award_attempts:
393393
# N.B. This logic assumes that this task is idempotent
394-
LOGGER.info(f"Retrying failed tasks to award program certificate(s) to user {student}")
394+
LOGGER.info(f"Retrying failed tasks to award program certificate(s) to user {student.id}")
395395
# The error message may change on each reattempt but will never be raised until the max number of retries
396396
# have been exceeded. It is unlikely that this list will change by the time it reaches its maximimum number
397397
# of attempts.
398398
error_msg = (
399-
f"Failed to award program certificate(s) for user {student} in programs "
399+
f"Failed to award program certificate(s) for user {student.id} in programs "
400400
f"{failed_program_certificate_award_attempts}"
401401
)
402402
raise MaxRetriesExceededError(
403403
f"Failed to award a program certificate to user {student.id}. Reason: {error_msg}"
404404
)
405405
else:
406-
LOGGER.warning(f"User {student} is not eligible for any new program certificates")
406+
LOGGER.warning(f"User {student.id} is not eligible for any new program certificates")
407407

408-
LOGGER.info(f"Successfully completed the task award_program_certificates for user {student}")
408+
LOGGER.info(f"Successfully completed the task award_program_certificates for user {student.id}")
409409

410410

411411
# pylint: disable=W0613
@@ -504,7 +504,7 @@ def award_course_certificate(self, username, course_run_key):
504504
)
505505
return
506506

507-
LOGGER.info(f"Running task award_course_certificate for user {user}")
507+
LOGGER.info(f"Running task award_course_certificate for user {user.id}")
508508
try:
509509
course_key = CourseKey.from_string(course_run_key)
510510
except InvalidKeyError as exc:
@@ -574,7 +574,7 @@ def award_course_certificate(self, username, course_run_key):
574574
org=course_key.org,
575575
)
576576
except Exception as exc:
577-
error_msg = f"Failed to post course certificate to be awarded for user {user}."
577+
error_msg = f"Failed to post course certificate to be awarded for user {user.id}."
578578
raise MaxRetriesExceededError(
579579
f"Failed to award course certificate for user {user.id} for course {course_run_key}. Reason: {error_msg}"
580580
) from exc
@@ -628,14 +628,14 @@ def revoke_program_certificates(self, username, course_key): # lint-amnesty, py
628628
)
629629
return
630630

631-
LOGGER.info(f"Running task revoke_program_certificates for user {student}")
631+
LOGGER.info(f"Running task revoke_program_certificates for user {student.id}")
632632
try:
633633
inverted_programs = get_inverted_programs(student)
634634
course_specific_programs = inverted_programs.get(course_key)
635635
if not course_specific_programs:
636636
LOGGER.warning(
637-
f"Task revoke_program_certificates was called for user {student} and course run {course_key} with no "
638-
"engaged programs"
637+
f"Task revoke_program_certificates was called for user {student.id} and course run {course_key} with "
638+
"no engaged programs"
639639
)
640640
return
641641

@@ -644,7 +644,7 @@ def revoke_program_certificates(self, username, course_key): # lint-amnesty, py
644644
except Exception as exc:
645645
error_msg = (
646646
f"Failed to determine if any program certificates associated with course run {course_key} should be "
647-
f"revoked from user {student}"
647+
f"revoked from user {student.id}"
648648
)
649649
LOGGER.exception(error_msg)
650650
raise MaxRetriesExceededError(
@@ -668,17 +668,17 @@ def revoke_program_certificates(self, username, course_key): # lint-amnesty, py
668668
for program_uuid in program_uuids_to_revoke:
669669
try:
670670
revoke_program_certificate(credentials_client, username, program_uuid)
671-
LOGGER.info(f"Revoked program certificate from user {student} in program {program_uuid}")
671+
LOGGER.info(f"Revoked program certificate from user {student.id} in program {program_uuid}")
672672
except HTTPError as exc:
673673
if exc.response.status_code == 404:
674674
LOGGER.warning(
675-
f"Unable to revoke program certificate from user {student} in program {program_uuid}, a "
675+
f"Unable to revoke program certificate from user {student.id} in program {program_uuid}, a "
676676
"program certificate could not be found"
677677
)
678678
elif exc.response.status_code == 429:
679679
# Let celery handle retry attempts and backoff
680680
error_msg = (
681-
f"Rate limited. Attempting to revoke a program certificate from user {student} in program "
681+
f"Rate limited. Attempting to revoke a program certificate from user {student.id} in program "
682682
f"{program_uuid}."
683683
)
684684
LOGGER.warning(error_msg)
@@ -687,33 +687,33 @@ def revoke_program_certificates(self, username, course_key): # lint-amnesty, py
687687
) from exc
688688
else:
689689
LOGGER.warning(
690-
f"Unable to revoke program certificate from user {student} in program {program_uuid}"
690+
f"Unable to revoke program certificate from user {student.id} in program {program_uuid}"
691691
)
692692
except Exception as exc: # pylint: disable=broad-except
693693
# keep trying to revoke other certs, but let celery retry the whole task to fix any missing entries
694694
LOGGER.exception(
695-
f"Failed to revoke program certificate from user {student} in program {program_uuid}: {exc}"
695+
f"Failed to revoke program certificate from user {student.id} in program {program_uuid}: {exc}"
696696
)
697697
failed_program_certificate_revoke_attempts.append(program_uuid)
698698

699699
if failed_program_certificate_revoke_attempts:
700700
# N.B. This logic assumes that this task is idempotent
701-
LOGGER.info(f"Failed task to revoke program certificate(s) from user {student}")
701+
LOGGER.info(f"Failed task to revoke program certificate(s) from user {student .id}")
702702
# The error message may change on each reattempt but will never be raised until the max number of retries
703703
# have been exceeded. It is unlikely that this list will change by the time it reaches its maximimum number
704704
# of attempts.
705705
error_msg = (
706-
f"Failed to revoke program certificate(s) from user {student} for programs "
706+
f"Failed to revoke program certificate(s) from user {student.id} for programs "
707707
f"{failed_program_certificate_revoke_attempts}"
708708
)
709709
raise MaxRetriesExceededError(
710710
f"Failed to revoke program certificate for user {student.id} for course {course_key}. "
711711
f"Reason: {error_msg}"
712712
)
713713
else:
714-
LOGGER.info(f"No program certificates to revoke from user {student}")
714+
LOGGER.info(f"No program certificates to revoke from user {student.id}")
715715

716-
LOGGER.info(f"Successfully completed the task revoke_program_certificates for user {student}")
716+
LOGGER.info(f"Successfully completed the task revoke_program_certificates for user {student.id}")
717717

718718

719719
@shared_task(

openedx/core/djangoapps/programs/tests/test_tasks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ def test_continue_awarding_certs_if_error(
365365
tasks.award_program_certificates.delay(self.student.username).get()
366366
assert mock_award_program_certificate.call_count == 3
367367
mock_warning.assert_called_once_with(
368-
f"Failed to award program certificate to user {self.student} in program 1: boom"
368+
f"Failed to award program certificate to user {self.student.id} in program 1: boom"
369369
)
370-
mock_info.assert_any_call(f"Awarded program certificate to user {self.student} in program 1")
371-
mock_info.assert_any_call(f"Awarded program certificate to user {self.student} in program 2")
370+
mock_info.assert_any_call(f"Awarded program certificate to user {self.student.id} in program 1")
371+
mock_info.assert_any_call(f"Awarded program certificate to user {self.student.id} in program 2")
372372

373373
def test_retry_on_programs_api_errors(self, mock_get_completed_programs, *_mock_helpers):
374374
"""
@@ -835,10 +835,10 @@ def test_continue_revoking_certs_if_error(
835835

836836
assert mock_revoke_program_certificate.call_count == 3
837837
mock_warning.assert_called_once_with(
838-
f"Failed to revoke program certificate from user {self.student} in program 1: boom"
838+
f"Failed to revoke program certificate from user {self.student.id} in program 1: boom"
839839
)
840-
mock_info.assert_any_call(f"Revoked program certificate from user {self.student} in program 1")
841-
mock_info.assert_any_call(f"Revoked program certificate from user {self.student} in program 2")
840+
mock_info.assert_any_call(f"Revoked program certificate from user {self.student.id} in program 1")
841+
mock_info.assert_any_call(f"Revoked program certificate from user {self.student.id} in program 2")
842842

843843
def test_retry_on_credentials_api_errors(
844844
self,

0 commit comments

Comments
 (0)