33from http import HTTPStatus
44from typing import Annotated
55
6+ from diracx .core .properties import GENERIC_PILOT
67from fastapi import Body , Depends , HTTPException , Query , status
78
89from 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