Skip to content

Commit 0170c85

Browse files
committed
access reconcile
1 parent 6fc0870 commit 0170c85

File tree

4 files changed

+55
-34
lines changed

4 files changed

+55
-34
lines changed

docker/test-values.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ externalService:
3434
servicePort: 5432
3535
repo: prt
3636
instance: test
37+
namespace:
38+
enabled: true
3739
targetAverageUtilization: 95
3840
resources:
3941
limits:

endorsement/management/commands/reconcile_access.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ def handle(self, *args, **options):
2525
try:
2626
reconcile_access(commit_changes=commit_changes)
2727
except Exception as ex:
28-
logger.error("reconcile_access: Exception: {}".format(ex))
28+
logger.exception(
29+
"reconcile_access: Exception: {}".format(ex), stack_info=True)

endorsement/reconcile_access.py

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ def reconcile_access(commit_changes=False):
4343
netid = strip_domain(row[0])
4444
accessee = get_accessee_model(netid)
4545

46-
for delegate, rights in get_delegations(row[1]).items():
46+
for delegate, right in mailbox_delegations(row[1]):
4747
try:
48-
record = reconcile_delegation(accessee, delegate, rights)
48+
record = reconcile_delegation(accessee, delegate, right)
4949
clear_record_id(record_ids, record.id)
5050
except NullDelegateException:
5151
logger.info(
5252
f"NULL DELEGATE: mailbox {netid} delegate null "
53-
f"with rights: {rights}")
53+
f"with right: {right}")
5454
except NoAccessRecordException:
5555
logger.info(f"NO ACCESS RECORD FOR: mailbox {netid} "
56-
f"delegate {delegate} rights: {rights}")
56+
f"delegate {delegate} right: {right}")
5757
if commit_changes:
5858
new_access_record(accessee, delegate, right_record)
5959
except DeletedAccessRecordException as ex:
@@ -71,9 +71,23 @@ def reconcile_access(commit_changes=False):
7171
f"{record.datetime_expired}")
7272

7373
if commit_changes:
74-
right = next(iter(rights))
75-
right_record = get_access_right(right)
76-
assign_access_right(record, right_record)
74+
# right still match? update
75+
if record.access_right.name != right:
76+
logger.info(
77+
"UPDATE DELETED ACCESS RECORD: "
78+
f"mailbox {netid} "
79+
f"delegate {delegate} "
80+
f"({record.access_right.name}) to {right}")
81+
right_record = get_access_right(right)
82+
assign_access_right(record, right_record)
83+
84+
logger.info(
85+
f"UNDELETED ACCESS RECORD: mailbox {netid} "
86+
f"delegate {delegate} "
87+
f"({record.access_right.name})")
88+
undelete_access_record(record)
89+
90+
clear_record_id(record_ids, record.id)
7791
except EmptyDelegateRightsException as ex:
7892
record = ex.record
7993
logger.info(f"NO RIGHTS FOR DELEGATION: "
@@ -83,17 +97,15 @@ def reconcile_access(commit_changes=False):
8397
except TooManyRightsException as ex:
8498
logger.info(
8599
f"CONFLICT: mailbox {netid} delegate {delegate} "
86-
f"rights: {rights}")
100+
f"right: {right}")
87101
record = ex.record
88102
if commit_changes:
89103
revoke_record(record)
90-
save_conflict_record(accessee, record, delegate, rights)
104+
save_conflict_record(accessee, record, delegate, right)
91105

92106
clear_record_id(record_ids, record.id)
93107
except DelegateRightMismatchException as ex:
94108
record = ex.record
95-
right = next(iter(rights))
96-
97109
logger.info(
98110
f"DELEGATION CHANGE: mailbox {netid} delegate {delegate}"
99111
f" ({record.access_right.name}) to {right}")
@@ -103,10 +115,6 @@ def reconcile_access(commit_changes=False):
103115
assign_access_right(record, right_record)
104116

105117
clear_record_id(record_ids, record.id)
106-
except Exception as ex:
107-
logger.error(
108-
f"UNEXPECTED ERROR: mailbox {netid} delegate {delegate} "
109-
f"rights: {rights} error: {ex}")
110118

111119
# access records for which no delegation was reported
112120
for record in AccessRecord.objects.filter(id__in=record_ids):
@@ -127,7 +135,7 @@ def clear_record_id(record_ids, record_id):
127135
pass
128136

129137

130-
def reconcile_delegation(accessee, delegate, rights):
138+
def reconcile_delegation(accessee, delegate, right):
131139
if not delegate or delegate.lower() == 'null':
132140
raise NullDelegateException()
133141

@@ -137,16 +145,20 @@ def reconcile_delegation(accessee, delegate, rights):
137145
except AccessRecord.DoesNotExist:
138146
raise NoAccessRecordException()
139147

140-
if len(rights) > 1:
141-
raise TooManyRightsException(record=record)
142-
143148
if record.is_deleted:
144149
raise DeletedAccessRecordException(record=record)
145150

146-
if len(rights) < 1:
147-
raise EmptyDelegateRightsException(record=record)
151+
if isinstance(right, str):
152+
if not right:
153+
raise EmptyDelegateRightsException(record=record)
154+
elif isinstance(right, list):
155+
if len(right) == 0:
156+
raise EmptyDelegateRightsException(record=record)
157+
elif len(rights) > 1:
158+
raise TooManyRightsException(record=record)
159+
160+
right = right[0]
148161

149-
right = next(iter(rights))
150162
if record.access_right.name != right:
151163
raise DelegateRightMismatchException(record=record)
152164

@@ -206,6 +218,14 @@ def revoke_record(record):
206218
record.revoke()
207219

208220

221+
def undelete_access_record(record):
222+
logger.info("FAILSAFE HIT")
223+
return
224+
225+
record.is_deleted = False
226+
record.save()
227+
228+
209229
def assign_access_right(record, right):
210230
logger.info(f"UPDATE CHANGE: mailbox {record.accessee.netid} "
211231
f"delegate {record.accessor.name} "
@@ -236,18 +256,16 @@ def save_conflict_record(accessee, record, delegate, rights):
236256
conflict.save()
237257

238258

239-
def get_delegations(raw):
240-
delegates = {}
241-
cooked = json.loads(raw)
242-
for right in [cooked] if isinstance(cooked, dict) else cooked:
259+
def mailbox_delegations(column):
260+
rights = json.loads(column)
261+
for right in [rights] if isinstance(rights, dict) else rights:
243262
user = right["User"]
244263
if user and user.lower() != 'null':
245-
try:
246-
delegates[user].append(right['AccessRights'])
247-
except KeyError:
248-
delegates[user] = [right['AccessRights']]
249-
250-
return delegates
264+
yield user, right['AccessRights']
265+
else:
266+
logger.debug(
267+
f"NULL RIGHT: mailbox {netid} delegate {delegate}"
268+
f" right: {right}")
251269

252270

253271
def access_user(a):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
'UW-RestClients-UWNetID~=1.1.2',
4646
'UW-RestClients-Django-Utils~=2.1.5',
4747
'UW-RestClients-ITBill~=0.1',
48-
'UW-RestClients-MSCA~=0.1.3',
48+
'UW-RestClients-MSCA~=0.1',
4949
'Django-Safe-EmailBackend~=1.2',
5050
'UW-Django-SAML2>=1.3.8,<2.0',
5151
'django-pyscss',

0 commit comments

Comments
 (0)