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

signals.py: Add notifiers to audit logs #473

Merged
merged 1 commit into from
Jan 26, 2024
Merged

Conversation

vincent-olivert-riera
Copy link
Contributor

We had a query for listing notifiers in views.py::AuditList, however, we never registered any logs from signals.py, therefore the result of that query was always empty.

This commit adds the necessary instructions for registering logs when creating, updating or deleting notifiers, so they will appear in the audit logs.

@vincent-olivert-riera vincent-olivert-riera requested a review from a team as a code owner January 24, 2024 07:18
@kfdm
Copy link
Collaborator

kfdm commented Jan 24, 2024

I think we can't just do this as-is, because Sender can sometimes include sensitive information.

promgen/promgen/models.py

Lines 564 to 579 in f6a00ef

@classmethod
def log(cls, body, instance=None, old=None, **kwargs):
from promgen.middleware import get_current_user
kwargs["body"] = body
kwargs["created"] = timezone.now()
kwargs["user"] = get_current_user()
if instance:
kwargs["content_type"] = ContentType.objects.get_for_model(instance)
kwargs["object_id"] = instance.id
kwargs["data"] = json.dumps(model_to_dict(instance), sort_keys=True)
if old:
kwargs["old"] = json.dumps(model_to_dict(old), sort_keys=True)
return cls.objects.create(**kwargs)

Audit.log() uses model_to_dict to dump all the information, and we need to ensure the value field can not accidentally be logged.

@vincent-olivert-riera
Copy link
Contributor Author

I think we can't just do this as-is, because Sender can sometimes include sensitive information.

promgen/promgen/models.py

Lines 564 to 579 in f6a00ef

@classmethod
def log(cls, body, instance=None, old=None, **kwargs):
from promgen.middleware import get_current_user
kwargs["body"] = body
kwargs["created"] = timezone.now()
kwargs["user"] = get_current_user()
if instance:
kwargs["content_type"] = ContentType.objects.get_for_model(instance)
kwargs["object_id"] = instance.id
kwargs["data"] = json.dumps(model_to_dict(instance), sort_keys=True)
if old:
kwargs["old"] = json.dumps(model_to_dict(old), sort_keys=True)
return cls.objects.create(**kwargs)

Audit.log() uses model_to_dict to dump all the information, and we need to ensure the value field can not accidentally be logged.

According to my tests, when the "alias" field has been populated, that is what is displayed in the logs. Otherwise the value is displayed.

Is that not good enough? If users do not set an alias, I guess we can assume they don't care if the value is displayed.

@kfdm
Copy link
Collaborator

kfdm commented Jan 24, 2024

Try clicking the 'show' button to see all the details that are stored
スクリーンショット 2024-01-24 16 28 59

@vincent-olivert-riera
Copy link
Contributor Author

Try clicking the 'show' button to see all the details that are stored スクリーンショット 2024-01-24 16 28 59

Oh, I didn't check that "Show" button. Thanks!

I fixed that issue in 5df5890.

@kfdm
Copy link
Collaborator

kfdm commented Jan 26, 2024

LGTM. Rebase and I'll do a final check.

We had a query for listing notifiers in views.py::AuditList, however, we never
registered any logs from signals.py, therefore the result of that query was
always empty.

This commit adds the necessary instructions for registering logs when creating,
updating or deleting notifiers, so they will appear in the audit logs.

Since the data is dumped using model_to_dict(), we also needed to modify
models.py to ensure that sensible information is not displayed. That is, when
the users have set the "alias" field because they want to hide the value.
@vincent-olivert-riera
Copy link
Contributor Author

LGTM. Rebase and I'll do a final check.

Done.

@kfdm kfdm merged commit c8580f9 into master Jan 26, 2024
5 checks passed
@kfdm kfdm deleted the add-notifiers-to-audit-logs branch January 26, 2024 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants