Skip to content

Commit 4ba2c9b

Browse files
author
Robin VAN DE MERGHEL
committed
feat: Add legacy pilot support in management (dirac-admin-add-pilot)
1 parent 4430e06 commit 4ba2c9b

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

diracx-routers/src/diracx/routers/pilots/access_policies.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from fastapi import Depends, HTTPException, status
88

99
from diracx.core.models import VectorSearchOperator, VectorSearchSpec
10-
from diracx.core.properties import SERVICE_ADMINISTRATOR
10+
from diracx.core.properties import GENERIC_PILOT, SERVICE_ADMINISTRATOR
1111
from diracx.db.sql.job.db import JobDB
1212
from diracx.db.sql.pilots.db import PilotAgentsDB
1313
from diracx.logic.pilots.query import get_pilots_by_stamp
@@ -39,20 +39,32 @@ async def policy(
3939
pilot_stamps: list[str] | None = None,
4040
job_db: JobDB | None = None,
4141
job_ids: list[int] | None = None,
42+
allow_legacy_pilots: bool = False
4243
):
4344
assert action, "action is a mandatory parameter"
4445

4546
# Users can query
4647
# NOTE: Add into queries a VO constraint
4748
# To manage pilots, user have to be an admin
48-
if (
49-
action == ActionType.MANAGE_PILOTS
50-
and SERVICE_ADMINISTRATOR not in user_info.properties
51-
):
52-
raise HTTPException(
53-
status_code=status.HTTP_403_FORBIDDEN,
54-
detail="You don't have the permission to manage pilots.",
55-
)
49+
# In some special cases (described with allow_legacy_pilots), we can allow pilots
50+
if action == ActionType.MANAGE_PILOTS:
51+
52+
# To make it clear, we separate
53+
is_an_admin = SERVICE_ADMINISTRATOR in user_info.properties
54+
is_a_pilot_if_allowed = allow_legacy_pilots and GENERIC_PILOT in user_info.properties
55+
56+
if not is_an_admin and not is_a_pilot_if_allowed:
57+
raise HTTPException(
58+
status_code=status.HTTP_403_FORBIDDEN,
59+
detail="You don't have the permission to manage pilots.",
60+
)
61+
62+
if action == ActionType.READ_PILOT_FIELDS:
63+
if GENERIC_PILOT in user_info.properties:
64+
raise HTTPException(
65+
status_code=status.HTTP_403_FORBIDDEN,
66+
detail="Pilots can't read other pilots info."
67+
)
5668

5769
#
5870
# Additional checks if job_ids or pilot_stamps are provided

diracx-routers/src/diracx/routers/pilots/management.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from http import HTTPStatus
44
from typing import Annotated
55

6+
from diracx.core.properties import GENERIC_PILOT
67
from fastapi import Body, Depends, HTTPException, Query, status
78

89
from diracx.core.exceptions import (
@@ -65,7 +66,19 @@ async def add_pilot_stamps(
6566
If a pilot stamp already exists, it will block the insertion.
6667
"""
6768
# TODO: Verify that grid types, sites, destination sites, etc. are valids
68-
await check_permissions(action=ActionType.MANAGE_PILOTS)
69+
await check_permissions(
70+
action=ActionType.MANAGE_PILOTS,
71+
allow_legacy_pilots=True # dirac-admin-add-pilot
72+
)
73+
74+
# Prevent someone who stole a pilot X509 to create thousands of pilots at a time
75+
# (It would be still able to create thousands of pilots, but slower)
76+
if GENERIC_PILOT in user_info.properties:
77+
if len(pilot_stamps) != 1:
78+
raise HTTPException(
79+
status_code=status.HTTP_401_UNAUTHORIZED,
80+
detail="As a pilot, you can only create yourself."
81+
)
6982

7083
try:
7184
await register_new_pilots(
@@ -183,6 +196,7 @@ async def update_pilot_fields(
183196
],
184197
pilot_db: PilotAgentsDB,
185198
check_permissions: CheckPilotManagementPolicyCallable,
199+
user_info: Annotated[AuthorizedUserInfo, Depends(verify_dirac_access_token)],
186200
):
187201
"""Modify a field of a pilot.
188202
@@ -191,9 +205,23 @@ async def update_pilot_fields(
191205
# Ensures stamps validity
192206
pilot_stamps = [mapping.PilotStamp for mapping in pilot_stamps_to_fields_mapping]
193207
await check_permissions(
194-
action=ActionType.MANAGE_PILOTS, pilot_db=pilot_db, pilot_stamps=pilot_stamps
208+
action=ActionType.MANAGE_PILOTS,
209+
pilot_db=pilot_db,
210+
pilot_stamps=pilot_stamps,
211+
allow_legacy_pilots=True # dirac-admin-add-pilot
195212
)
196213

214+
# Prevent someone who stole a pilot X509 to modify thousands of pilots at a time
215+
# (It would be still able to modify thousands of pilots, but slower)
216+
# We are not able to affirm that this pilots modifies itself
217+
if GENERIC_PILOT in user_info.properties:
218+
if len(pilot_stamps) != 1:
219+
raise HTTPException(
220+
status_code=status.HTTP_401_UNAUTHORIZED,
221+
detail="As a pilot, you can only modify yourself."
222+
)
223+
224+
197225
await update_pilots_fields(
198226
pilot_db=pilot_db,
199227
pilot_stamps_to_fields_mapping=pilot_stamps_to_fields_mapping,

0 commit comments

Comments
 (0)