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

Categorize audit logs by log type #5063

Merged
merged 8 commits into from
Aug 21, 2024
Merged

Categorize audit logs by log type #5063

merged 8 commits into from
Aug 21, 2024

Conversation

rgraber
Copy link
Contributor

@rgraber rgraber commented Aug 15, 2024

Checklist

  1. If you've added code that should be tested, add tests
  2. If you've changed APIs, update (or create!) the documentation
  3. Ensure the tests pass
  4. Make sure that your code lints and that you've followed our coding style
  5. Write a title and, if necessary, a description of your work suitable for publishing in our release notes
  6. Mention any related issues in this repository (as #ISSUE) and in other repositories (as kobotoolbox/other#ISSUE)
  7. Open an issue in the docs if there are UI/UX changes

Description

Distinguish audit logs pertaining to different categories (access, user management, submission management, project history, data editing, and asset management).

Notes

Adds a new log_type field to the AuditLog model (and a sql query to backfill any existing audit logs). This will allow us to better filter the endpoints to get only access logs, only project history logs, etc. No change is made to the endpoint, that will be done in other PRs, but the model change will make that easier down the road.
The categories to backfill the existing audit logs for everything but access logs may end up being changed in the future but are good enough approximations for now. Since we are currently only logging deletes/put-back for assets, submissions, and users, the categories in the runsql should cover all existing audit logs and allow us to make the field not-nullable.

Copy link

USER_MANAGEMENT = 'user-management'
ASSET_MANAGEMENT = 'asset-management'
SUBMISSION_MANAGEMENT = 'submission-management'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP-8: 2 blank lines between classes ;-)

@@ -48,6 +57,7 @@ class AuditLog(models.Model):
db_index=True
)
user_uid = models.CharField(db_index=True, max_length=UUID_LENGTH + 1) # 1 is prefix length
log_type = models.CharField(choices=AuditType.choices)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a criteria we are going to (often) use to narrow down query? I think I would use db_index=True?
What do you think?

@@ -31,7 +31,7 @@ def test_delete_user(self):

# Create dummy logs for someuser
audit_log = AuditLog.objects.create(
app_label='foo', model_name='bar', object_id=1, user=someuser
app_label='foo', model_name='bar', object_id=1, user=someuser, log_type=AuditType.ACCESS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be over the 80 characters per line limit?

@@ -138,6 +139,7 @@ def move_to_trash(
**{fk_field_name: obj_dict['pk']},
)
)
log_type = AuditType.USER_MANAGEMENT if related_model._meta.model_name == 'user' else AuditType.ASSET_MANAGEMENT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat to keep 80-ish characters per line ;-)

@@ -217,6 +220,7 @@ def put_back(

if del_pto_count != len(obj_ids):
raise TrashTaskInProgressError
log_type = AuditType.USER_MANAGEMENT if related_model._meta.model_name == 'user' else AuditType.ASSET_MANAGEMENT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat to keep 80-ish characters per line ;-)

@@ -789,7 +789,7 @@ def test_audit_log_on_delete(self):
model_name,
) = self.asset.deployment.submission_model.get_app_label_and_model_name()
audit_log_count = AuditLog.objects.filter(
user=self.someuser, app_label=app_label, model_name=model_name
user=self.someuser, app_label=app_label, model_name=model_name, log_type=AuditType.SUBMISSION_MANAGEMENT,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat to keep 80-ish characters per line ;-)

@@ -799,7 +799,8 @@ def test_audit_log_on_delete(self):
# All submissions have been deleted and should be logged
deleted_submission_ids = AuditLog.objects.values_list(
'pk', flat=True
).filter(user=self.someuser, app_label=app_label, model_name=model_name)
).filter(user=self.someuser, app_label=app_label, model_name=model_name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not seem to be black-formatted?

),
preserve_default=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

@noliveleger noliveleger merged commit e03eb3a into beta Aug 21, 2024
4 checks passed
@noliveleger noliveleger deleted the TASK-955-add-log-type branch August 21, 2024 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants