Skip to content

Commit ebcfe02

Browse files
NRL-2099 Remove pointer type permissions
1 parent 584af68 commit ebcfe02

File tree

1 file changed

+101
-5
lines changed

1 file changed

+101
-5
lines changed

scripts/manage_permissions.py

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ def add_pointer_type_perms(
273273
supplier_type: SupplierType, app_id: str, org_ods=None, *pointer_types_to_add: str
274274
) -> None:
275275
"""
276+
Add permissions for a given list of pointer types to an app or org.
277+
278+
Specify pointer_types = all to add a list of all (current) pointer types.
279+
276280
TODO:
277-
confirm before proceeding mode
278-
formatting help for pointer types ?
279-
validate not adding a duplicate type
280-
add list of all pointer types vs adding access control
281281
highlight new additions in proposed pointer types list e.g. [NEW]
282282
don't create at app level if ODS level present & backwards too? - hmm maybe too fancy
283283
"""
@@ -324,7 +324,9 @@ def add_pointer_type_perms(
324324
if new_pointer_type in current_pointer_types
325325
)
326326
if len(already_added_types):
327-
print(f"Error: These pointer types are already assigned to {lookup_path}:")
327+
print(
328+
f"Error: Unable to add pointer types. These pointer types are already assigned to {lookup_path}:"
329+
)
328330
_print_perm_with_lookup("", already_added_types, TYPE_ATTRIBUTES)
329331
print()
330332
return
@@ -363,6 +365,99 @@ def add_pointer_type_perms(
363365
show_perms(supplier_type, app_id, org_ods)
364366

365367

368+
def remove_pointer_type_perms(
369+
supplier_type: SupplierType,
370+
app_id: str,
371+
org_ods=None,
372+
*pointer_types_to_remove: str,
373+
) -> None:
374+
"""
375+
Remove a list of pointer type permissions for a given app or org.
376+
"""
377+
if supplier_type.lower() not in SupplierType.list() or not app_id:
378+
print("Usage: remove pointer type permissions for a given organisation or app")
379+
print(" remove_pointer_type_perms consumer <app_id> <org_ods> <pointer_types>")
380+
print(" remove_pointer_type_perms producer <app_id> <org_ods> <pointer_types>")
381+
print(" remove_pointer_type_perms consumer <app_id> <pointer_types>")
382+
print(" remove_pointer_type_perms producer <app_id> <pointer_types>")
383+
return
384+
385+
if not pointer_types_to_remove:
386+
print(
387+
"No pointer types provided. Please specify at least one pointer type or use clear_perms command."
388+
)
389+
return
390+
391+
if org_ods:
392+
lookup_path = f"{supplier_type}/{app_id}/{org_ods}.json"
393+
else:
394+
lookup_path = f"{supplier_type}/{app_id}.json"
395+
396+
unknown_types = [pt for pt in pointer_types_to_remove if pt not in TYPE_ATTRIBUTES]
397+
if unknown_types:
398+
print(f"Error: Unknown pointer types provided: {', '.join(unknown_types)}")
399+
print()
400+
return
401+
402+
perms_ugly = _get_perms_from_s3(lookup_path)
403+
if not perms_ugly:
404+
return
405+
406+
current_perms = json.loads(perms_ugly)
407+
current_pointer_types: list = current_perms.get("types", [])
408+
409+
# Cannot remove pointer types not already assigned
410+
types_not_assigned = list(
411+
type_to_remove
412+
for type_to_remove in pointer_types_to_remove
413+
if type_to_remove not in current_pointer_types
414+
)
415+
if len(types_not_assigned):
416+
print(
417+
f"Error: Unable to remove pointer types. These pointer types aren't assigned to {lookup_path}:"
418+
)
419+
_print_perm_with_lookup("", types_not_assigned, TYPE_ATTRIBUTES)
420+
print()
421+
return
422+
423+
proposed_pointer_types = [
424+
current_pointer_type
425+
for current_pointer_type in current_pointer_types
426+
if current_pointer_type not in pointer_types_to_remove
427+
]
428+
print()
429+
_print_perm_with_lookup(
430+
"proposed pointer types", proposed_pointer_types, TYPE_ATTRIBUTES
431+
)
432+
433+
if COMPARE_AND_CONFIRM:
434+
print()
435+
confirm = (
436+
input("Do you want to proceed with these changes? (yes/NO): ")
437+
.strip()
438+
.lower()
439+
)
440+
if confirm != "yes":
441+
print("Operation cancelled at user request.")
442+
return
443+
444+
current_perms["types"] = proposed_pointer_types
445+
446+
s3 = _get_s3_client()
447+
s3.put_object(
448+
Bucket=nrl_auth_bucket_name,
449+
Key=lookup_path,
450+
Body=json.dumps(current_perms, indent=4),
451+
ContentType="application/json",
452+
)
453+
454+
print()
455+
print(f"Set permissions for {lookup_path}")
456+
457+
print()
458+
show_perms(supplier_type, app_id, org_ods)
459+
460+
366461
def clear_perms(supplier_type: SupplierType, app_id: str, org_ods=None) -> None:
367462
"""
368463
Clear permissions for an application or organization.
@@ -425,6 +520,7 @@ def clear_perms(supplier_type: SupplierType, app_id: str, org_ods=None) -> None:
425520
"list_available_access_controls": list_available_access_controls,
426521
"show_perms": show_perms,
427522
"add_pointer_type_to_perms": add_pointer_type_perms,
523+
"remove_pointer_type_perms": remove_pointer_type_perms,
428524
"clear_perms": clear_perms,
429525
# "help": help,
430526
}

0 commit comments

Comments
 (0)