3
3
from http import HTTPStatus
4
4
from typing import Annotated
5
5
6
+ from diracx .core .properties import GENERIC_PILOT
6
7
from fastapi import Body , Depends , HTTPException , Query , status
7
8
8
9
from diracx .core .exceptions import (
@@ -65,7 +66,19 @@ async def add_pilot_stamps(
65
66
If a pilot stamp already exists, it will block the insertion.
66
67
"""
67
68
# 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
+ )
69
82
70
83
try :
71
84
await register_new_pilots (
@@ -183,6 +196,7 @@ async def update_pilot_fields(
183
196
],
184
197
pilot_db : PilotAgentsDB ,
185
198
check_permissions : CheckPilotManagementPolicyCallable ,
199
+ user_info : Annotated [AuthorizedUserInfo , Depends (verify_dirac_access_token )],
186
200
):
187
201
"""Modify a field of a pilot.
188
202
@@ -191,9 +205,23 @@ async def update_pilot_fields(
191
205
# Ensures stamps validity
192
206
pilot_stamps = [mapping .PilotStamp for mapping in pilot_stamps_to_fields_mapping ]
193
207
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
195
212
)
196
213
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
+
197
225
await update_pilots_fields (
198
226
pilot_db = pilot_db ,
199
227
pilot_stamps_to_fields_mapping = pilot_stamps_to_fields_mapping ,
0 commit comments