Skip to content

Commit 716359b

Browse files
Ensure sb_metadata_db is given
1 parent 183aa2d commit 716359b

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ async def policy(
113113
required_prefix: str | None = None,
114114
):
115115
assert action, "action is a mandatory parameter"
116+
assert sandbox_metadata_db, "sandbox_metadata_db is a mandatory parameter"
116117

117118
if action == ActionType.CREATE:
118119

@@ -132,25 +133,20 @@ async def policy(
132133
raise NotImplementedError(
133134
"required_prefix is None. This shouldn't happen"
134135
)
135-
assert sandbox_metadata_db, "sandbox_metadata_db is a mandatory parameter"
136136
for pfn in pfns:
137137
if not pfn.startswith(required_prefix):
138138
raise HTTPException(
139139
status_code=status.HTTP_403_FORBIDDEN,
140140
detail=f"Invalid PFN. PFN must start with {required_prefix}",
141141
)
142-
if action in [ActionType.READ, ActionType.QUERY]:
143-
# We are reading or querying
144-
# Checking if the user owns the sandbox
145-
owner_id = await sandbox_metadata_db.get_owner_id(user_info)
146-
sandbox_owner_id = await sandbox_metadata_db.get_sandbox_owner_id(
147-
pfn
142+
# Checking if the user owns the sandbox
143+
owner_id = await sandbox_metadata_db.get_owner_id(user_info)
144+
sandbox_owner_id = await sandbox_metadata_db.get_sandbox_owner_id(pfn)
145+
if not owner_id or owner_id != sandbox_owner_id:
146+
raise HTTPException(
147+
status_code=status.HTTP_403_FORBIDDEN,
148+
detail=f"{user_info.preferred_username} is not the owner of the sandbox",
148149
)
149-
if not owner_id or owner_id != sandbox_owner_id:
150-
raise HTTPException(
151-
status_code=status.HTTP_403_FORBIDDEN,
152-
detail=f"{user_info.preferred_username} is not the owner of the sandbox",
153-
)
154150

155151

156152
CheckSandboxPolicyCallable = Annotated[Callable, Depends(SandboxAccessPolicy.check)]

diracx-routers/tests/jobs/test_wms_access_policy.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,23 @@
2323
}
2424

2525

26-
class FakeDB:
26+
class FakeJobDB:
2727
async def summary(self, *args): ...
2828

2929

30+
class FakeSBMetadataDB:
31+
async def get_owner_id(self, *args): ...
32+
async def get_sandbox_owner_id(self, *args): ...
33+
34+
3035
@pytest.fixture
3136
def job_db():
32-
yield FakeDB()
37+
yield FakeJobDB()
38+
39+
40+
@pytest.fixture
41+
def sandbox_metadata_db():
42+
yield FakeSBMetadataDB()
3343

3444

3545
WMS_POLICY_NAME = "WMSAccessPolicy_AlthoughItDoesNotMatter"
@@ -220,7 +230,7 @@ async def summary_other_vo(*args):
220230
)
221231

222232

223-
async def test_sandbox_access_policy_create():
233+
async def test_sandbox_access_policy_create(sandbox_metadata_db):
224234

225235
admin_user = AuthorizedUserInfo(properties=[JOB_ADMINISTRATOR], **base_payload)
226236
normal_user = AuthorizedUserInfo(properties=[NORMAL_USER], **base_payload)
@@ -230,6 +240,7 @@ async def test_sandbox_access_policy_create():
230240
await SandboxAccessPolicy.policy(
231241
SANDBOX_POLICY_NAME,
232242
normal_user,
243+
sandbox_metadata_db=sandbox_metadata_db,
233244
)
234245

235246
# An admin cannot create any resource
@@ -238,6 +249,7 @@ async def test_sandbox_access_policy_create():
238249
SANDBOX_POLICY_NAME,
239250
admin_user,
240251
action=ActionType.CREATE,
252+
sandbox_metadata_db=sandbox_metadata_db,
241253
pfns=[USER_SANDBOX_PFN],
242254
)
243255

@@ -246,13 +258,14 @@ async def test_sandbox_access_policy_create():
246258
SANDBOX_POLICY_NAME,
247259
normal_user,
248260
action=ActionType.CREATE,
261+
sandbox_metadata_db=sandbox_metadata_db,
249262
pfns=[USER_SANDBOX_PFN],
250263
)
251264

252265
##############
253266

254267

255-
async def test_sandbox_access_policy_read():
268+
async def test_sandbox_access_policy_read(sandbox_metadata_db):
256269

257270
admin_user = AuthorizedUserInfo(properties=[JOB_ADMINISTRATOR], **base_payload)
258271
normal_user = AuthorizedUserInfo(properties=[NORMAL_USER], **base_payload)
@@ -261,6 +274,7 @@ async def test_sandbox_access_policy_read():
261274
SANDBOX_POLICY_NAME,
262275
admin_user,
263276
action=ActionType.READ,
277+
sandbox_metadata_db=sandbox_metadata_db,
264278
pfns=[USER_SANDBOX_PFN],
265279
required_prefix=SANDBOX_PREFIX,
266280
)
@@ -269,6 +283,7 @@ async def test_sandbox_access_policy_read():
269283
SANDBOX_POLICY_NAME,
270284
admin_user,
271285
action=ActionType.READ,
286+
sandbox_metadata_db=sandbox_metadata_db,
272287
pfns=[OTHER_USER_SANDBOX_PFN],
273288
required_prefix=SANDBOX_PREFIX,
274289
)
@@ -279,6 +294,7 @@ async def test_sandbox_access_policy_read():
279294
SANDBOX_POLICY_NAME,
280295
normal_user,
281296
action=ActionType.READ,
297+
sandbox_metadata_db=sandbox_metadata_db,
282298
pfns=[USER_SANDBOX_PFN],
283299
)
284300

@@ -287,6 +303,7 @@ async def test_sandbox_access_policy_read():
287303
SANDBOX_POLICY_NAME,
288304
normal_user,
289305
action=ActionType.READ,
306+
sandbox_metadata_db=sandbox_metadata_db,
290307
pfns=[USER_SANDBOX_PFN],
291308
required_prefix=SANDBOX_PREFIX,
292309
)
@@ -297,6 +314,7 @@ async def test_sandbox_access_policy_read():
297314
SANDBOX_POLICY_NAME,
298315
normal_user,
299316
action=ActionType.READ,
317+
sandbox_metadata_db=sandbox_metadata_db,
300318
pfns=[OTHER_USER_SANDBOX_PFN],
301319
required_prefix=SANDBOX_PREFIX,
302320
)

0 commit comments

Comments
 (0)