Skip to content

Commit 51277a3

Browse files
authored
Merge branch 'master' into BB2-2523-cleanup-insights-files-moved-to-bfd
2 parents b195917 + 8052a7e commit 51277a3

File tree

2 files changed

+32
-76
lines changed

2 files changed

+32
-76
lines changed

apps/dot_ext/models.py

Lines changed: 24 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import hashlib
22
import itertools
33
import sys
4-
import time
54
import uuid
6-
import pytz
75

86
import apps.logging.request_logger as logging
97

@@ -14,7 +12,7 @@
1412
from django.contrib.auth import get_user_model
1513
from django.core.files.storage import default_storage
1614
from django.core.validators import RegexValidator
17-
from django.db import models, transaction
15+
from django.db import models
1816
from django.db.models import Q
1917
from django.db.models.signals import (
2018
post_delete,
@@ -33,7 +31,6 @@
3331
from waffle import get_waffle_flag_model
3432

3533
from apps.capabilities.models import ProtectedCapability
36-
from apps.authorization.models import DataAccessGrant
3734

3835
TEN_HOURS = "for 10 hours"
3936

@@ -237,71 +234,30 @@ def save(self, *args, **kwargs):
237234
):
238235
raise ValueError("Invalid data_access_type: " + self.data_access_type)
239236

240-
flag = get_waffle_flag_model().get("limit_data_access")
241-
if flag.id is not None and flag.is_active_for_user(self.user):
242-
# Check if data_access_type is changed
243-
# if so, need to void all grants associated
244-
245-
logger = logging.getLogger(logging.AUDIT_APPLICATION_TYPE_CHANGE, None)
246-
247-
app_type_changed = False
248-
249-
log_dict = {
250-
"type": "application_data_access_type_change",
251-
}
252-
with transaction.atomic():
253-
# need to put delete and save in a transaction
254-
app_from_db = None
255-
try:
256-
app_from_db = Application.objects.get(pk=self.id)
257-
if app_from_db is not None:
258-
if self.data_access_type != app_from_db.data_access_type:
259-
# log audit event: application data access type changed
260-
start_time = time.time()
261-
log_dict.update(
262-
{
263-
"application_id": self.id,
264-
"application_name": self.name,
265-
"data_access_type_old": app_from_db.data_access_type,
266-
"data_access_type_new": self.data_access_type,
267-
"grant_start": datetime.now().strftime("%m/%d/%Y, %H:%M:%S"),
268-
}
269-
)
270-
if self.has_one_time_only_data_access():
271-
dag_deleted = DataAccessGrant.objects.filter(application=self).delete()
272-
end_time = time.time()
273-
delete_stats = {
274-
"elapsed_seconds": end_time - start_time,
275-
"number_of_grant_deleted": dag_deleted[0],
276-
"grant_delete_complete": datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
277-
}
278-
log_dict.update(delete_stats)
279-
elif "THIRTEEN_MONTH" in self.data_access_type:
280-
grants = DataAccessGrant.objects.filter(application=self)
281-
for grant in grants:
282-
grant.expiration_date = datetime.now().replace(
283-
tzinfo=pytz.UTC
284-
) + relativedelta(months=+13)
285-
grant.save()
286-
end_time = time.time()
287-
update_stats = {
288-
"elapsed_seconds": end_time - start_time,
289-
"number_of_grants_updated": grants.count(),
290-
"grant_update_complete": datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
291-
}
292-
log_dict.update(update_stats)
293-
app_type_changed = True
294-
except Application.DoesNotExist:
295-
# new app
296-
pass
297-
self.copy_client_secret()
298-
super().save(*args, **kwargs)
299-
if app_type_changed:
300-
log_dict.update({"application_saved_and_grants_updated_or_deleted": "Yes"})
237+
# Check if data_access_type is changed
238+
# if so, log and leave existing access grants unchanged
239+
app_from_db = None
240+
try:
241+
app_from_db = Application.objects.get(pk=self.id)
242+
243+
if app_from_db is not None:
244+
if self.data_access_type != app_from_db.data_access_type:
245+
logger = logging.getLogger(logging.AUDIT_APPLICATION_TYPE_CHANGE, None)
246+
247+
# log audit event: application data access type changed
248+
log_dict = {
249+
"type": "application_data_access_type_change",
250+
"application_id": self.id,
251+
"application_name": self.name,
252+
"data_access_type_old": app_from_db.data_access_type,
253+
"data_access_type_new": self.data_access_type,
254+
}
301255
logger.info(log_dict)
302-
else:
303-
self.copy_client_secret()
304-
super().save(*args, **kwargs)
256+
except Application.DoesNotExist:
257+
# new app
258+
pass
259+
self.copy_client_secret()
260+
super().save(*args, **kwargs)
305261

306262
# dedicated save for high frequency used first / last active timestamp updates
307263
def save_without_validate(self, *args, **kwargs):

apps/dot_ext/tests/test_models.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ def test_application_data_access_fields(self):
7272
@override_flag('limit_data_access', active=True)
7373
def test_application_data_access_type_change(self):
7474
"""
75-
Test the application.data_access_type change, this triggers associated grants
76-
removal (become archived grants)
75+
Test the application.data_access_type change, make sure the change is logged
7776
"""
7877
assert flag_is_active('limit_data_access')
7978

@@ -103,20 +102,21 @@ def test_application_data_access_type_change(self):
103102
try:
104103
DataAccessGrant.objects.get(application__name="test_app")
105104
except DataAccessGrant.DoesNotExist:
106-
self.fail("Expecting grants for 'test_app' to carry over due to change to Research type app.")
105+
self.fail("Expecting grants for 'test_app' to carry over, no existing grants should be affected.")
107106

108107
log_content = get_log_content(self.logger_registry, logging.AUDIT_APPLICATION_TYPE_CHANGE)
109108
self.assertIsNotNone(log_content)
110109
log_entries = log_content.splitlines()
111110
self.assertEqual(len(log_entries), 1)
112111
log_entry_json = json.loads(log_entries[0])
113-
self.assertEqual(log_entry_json['application_saved_and_grants_updated_or_deleted'], "Yes")
112+
self.assertEqual(log_entry_json['type'], "application_data_access_type_change")
113+
self.assertEqual(log_entry_json['data_access_type_old'], "ONE_TIME")
114+
self.assertEqual(log_entry_json['data_access_type_new'], "RESEARCH_STUDY")
114115

115116
@override_flag('limit_data_access', active=False)
116117
def test_application_data_access_type_change_switch_off(self):
117118
"""
118-
Test the application.data_access_type change, this will NOT trigger associated grants
119-
removal due to switch off
119+
Test the application.data_access_type change, access grants will not be affected
120120
"""
121121
assert (not flag_is_active('limit_data_access'))
122122

@@ -156,8 +156,8 @@ def test_application_data_access_type_change_switch_off(self):
156156

157157
log_content = get_log_content(self.logger_registry, logging.AUDIT_APPLICATION_TYPE_CHANGE)
158158

159-
# no event logged
160-
self.assertFalse(log_content)
159+
# this will be logged
160+
self.assertTrue(log_content)
161161

162162
def test_application_count_funcs(self):
163163
"""

0 commit comments

Comments
 (0)