Skip to content

Commit

Permalink
moving log from file write to file open (#1)
Browse files Browse the repository at this point in the history
* moving log from file write to file open

* adding read/write counters for file access, creating custom model for file access with read and write fields

* simplifying things

* making FileClose event be either a FileReadClose or a FileWriteClose depending on how the file was used

* adding missing comparison

* increased size of event field
  • Loading branch information
EJ authored and jsvrcek committed Nov 16, 2017
1 parent 6812562 commit f0b931c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
40 changes: 17 additions & 23 deletions audit_logging/file_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,58 +44,52 @@ def __init__(self, regular_file, user_details):
self.AuditEvent = AuditEvent
self.regular_file = regular_file
self.user_details = user_details
self.writes = 0
self.reads = 0

def write(self, *args, **kwargs):
res = self.regular_file.write(*args, **kwargs)
log_event(
event='FileWrite', resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
self.writes += 1
return res

def writelines(self, *args, **kwargs):
res = self.regular_file.writelines(*args, **kwargs)
log_event(
event='FileWrite', resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
self.writes += 1
return res

def truncate(self, *args, **kwargs):
res = self.regular_file.truncate(*args, **kwargs)
log_event(
event='FileWrite', resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
self.writes += 1
return res

def read(self, *args, **kwargs):
res = self.regular_file.read(*args, **kwargs)
log_event(
event='FileRead', resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
self.reads += 1
return res

def readline(self, *args, **kwargs):
res = self.regular_file.readline(*args, **kwargs)
log_event(
event='FileRead', resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
self.reads += 1
return res

def readlines(self, *args, **kwargs):
res = self.regular_file.readlines(*args, **kwargs)
log_event(event='FileRead', resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
self.reads += 1
return res

def __enter__(self, *args, **kwargs):
log_event(
event='FileOpen', resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
return self

def __exit__(self, *args, **kwargs):
event = "File{0}{1}Close".format('Read' if self.reads else '', 'Write' if self.writes else '')
log_event(
event=event, resource_type='file', resource_uuid=self.regular_file.name,
user_details=self.user_details
)
self.regular_file.close()

def __iter__(self, *args, **kwargs):
Expand Down
20 changes: 20 additions & 0 deletions audit_logging/migrations/0002_auto_20171116_2048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-11-16 20:48
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('audit_logging', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='auditevent',
name='event',
field=models.CharField(blank=True, max_length=24, null=True),
),
]
2 changes: 1 addition & 1 deletion audit_logging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AuditEvent(models.Model):
serialize=False,
auto_created=True,
primary_key=True)
event = models.CharField(max_length=16, null=True, blank=True)
event = models.CharField(max_length=24, null=True, blank=True)
username = models.CharField(max_length=255, null=True, blank=False)
ip = models.GenericIPAddressField(null=True, blank=True)
email = models.EmailField(null=True, blank=True)
Expand Down

0 comments on commit f0b931c

Please sign in to comment.