Skip to content

Commit 31c6303

Browse files
fix: Changing from a secret with an id to a ref
1 parent ab05c1f commit 31c6303

File tree

6 files changed

+56
-28
lines changed

6 files changed

+56
-28
lines changed

diracx-client/src/diracx/client/generated/aio/operations/_operations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -827,14 +827,14 @@ async def complete_authorization_flow(
827827

828828
@distributed_trace_async
829829
async def pilot_login(
830-
self, *, pilot_id: int, pilot_secret: str, **kwargs: Any
830+
self, *, pilot_job_reference: str, pilot_secret: str, **kwargs: Any
831831
) -> Any:
832832
"""Pilot Login.
833833
834834
Endpoint without policy, the pilot uses only its secret.
835835
836-
:keyword pilot_id: Required.
837-
:paramtype pilot_id: int
836+
:keyword pilot_job_reference: Required.
837+
:paramtype pilot_job_reference: str
838838
:keyword pilot_secret: Required.
839839
:paramtype pilot_secret: str
840840
:return: any
@@ -855,7 +855,7 @@ async def pilot_login(
855855
cls: ClsType[Any] = kwargs.pop("cls", None)
856856

857857
_request = build_auth_pilot_login_request(
858-
pilot_id=pilot_id,
858+
pilot_job_reference=pilot_job_reference,
859859
pilot_secret=pilot_secret,
860860
headers=_headers,
861861
params=_params,

diracx-client/src/diracx/client/generated/operations/_operations.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def build_auth_complete_authorization_flow_request( # pylint: disable=name-too-
264264

265265

266266
def build_auth_pilot_login_request(
267-
*, pilot_id: int, pilot_secret: str, **kwargs: Any
267+
*, pilot_job_reference: str, pilot_secret: str, **kwargs: Any
268268
) -> HttpRequest:
269269
_headers = case_insensitive_dict(kwargs.pop("headers", {}) or {})
270270
_params = case_insensitive_dict(kwargs.pop("params", {}) or {})
@@ -275,7 +275,9 @@ def build_auth_pilot_login_request(
275275
_url = "/api/auth/pilot-login"
276276

277277
# Construct parameters
278-
_params["pilot_id"] = _SERIALIZER.query("pilot_id", pilot_id, "int")
278+
_params["pilot_job_reference"] = _SERIALIZER.query(
279+
"pilot_job_reference", pilot_job_reference, "str"
280+
)
279281
_params["pilot_secret"] = _SERIALIZER.query("pilot_secret", pilot_secret, "str")
280282

281283
# Construct headers
@@ -1417,13 +1419,15 @@ def complete_authorization_flow(
14171419
return deserialized # type: ignore
14181420

14191421
@distributed_trace
1420-
def pilot_login(self, *, pilot_id: int, pilot_secret: str, **kwargs: Any) -> Any:
1422+
def pilot_login(
1423+
self, *, pilot_job_reference: str, pilot_secret: str, **kwargs: Any
1424+
) -> Any:
14211425
"""Pilot Login.
14221426
14231427
Endpoint without policy, the pilot uses only its secret.
14241428
1425-
:keyword pilot_id: Required.
1426-
:paramtype pilot_id: int
1429+
:keyword pilot_job_reference: Required.
1430+
:paramtype pilot_job_reference: str
14271431
:keyword pilot_secret: Required.
14281432
:paramtype pilot_secret: str
14291433
:return: any
@@ -1444,7 +1448,7 @@ def pilot_login(self, *, pilot_id: int, pilot_secret: str, **kwargs: Any) -> Any
14441448
cls: ClsType[Any] = kwargs.pop("cls", None)
14451449

14461450
_request = build_auth_pilot_login_request(
1447-
pilot_id=pilot_id,
1451+
pilot_job_reference=pilot_job_reference,
14481452
pilot_secret=pilot_secret,
14491453
headers=_headers,
14501454
params=_params,

diracx-db/src/diracx/db/sql/pilot_agents/db.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ async def increment_pilot_secret_use(
7474
if res.rowcount == 0:
7575
raise PilotNotFoundError(pilot_id=pilot_id)
7676

77-
async def verify_pilot_secret(self, pilot_id: int, pilot_secret: str) -> None:
77+
async def verify_pilot_secret(
78+
self, pilot_job_reference: str, pilot_secret: str
79+
) -> None:
7880
hashed_secret = hash(pilot_secret)
7981

82+
pilot = await self.get_pilot_by_reference(pilot_job_reference)
83+
84+
pilot_id = pilot["PilotID"]
85+
8086
stmt = (
8187
select(PilotRegistrations)
8288
.where(PilotRegistrations.pilot_hashed_secret == hashed_secret)
@@ -97,13 +103,15 @@ async def verify_pilot_secret(self, pilot_id: int, pilot_secret: str) -> None:
97103
async def register_new_pilot(
98104
self,
99105
vo: str,
106+
pilot_job_reference: str,
100107
submission_time: DateTime | None = None, # ?
101108
last_update_time: DateTime | None = None, # = now?
102109
) -> int | None:
103110
stmt = insert(PilotAgents).values(
104111
vo=vo,
105112
submission_time=submission_time,
106113
last_update_time=last_update_time,
114+
pilot_job_reference=pilot_job_reference,
107115
)
108116

109117
# Execute the request
@@ -147,11 +155,12 @@ async def fetch_all_pilots(self):
147155

148156
return pilots
149157

150-
async def get_pilot_by_id(self, pilot_id: int):
158+
async def get_pilot_by_reference(self, pilot_ref: str):
151159
stmt = (
152160
select(PilotAgents)
153161
.with_for_update()
154-
.where(PilotAgents.pilot_id == pilot_id)
162+
.where(PilotAgents.pilot_job_reference == pilot_ref)
155163
)
156164

165+
# We assume it is unique...
157166
return dict((await self.conn.execute(stmt)).one()._mapping)

diracx-db/tests/pilot_agents/test_pilot_agents_db.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ async def test_insert_and_select(pilot_agents_db: PilotAgentsDB):
3636
async def test_insert_and_select_single(pilot_agents_db: PilotAgentsDB):
3737

3838
async with pilot_agents_db as pilot_agents_db:
39-
new_pilot_id = await pilot_agents_db.register_new_pilot(vo="pilot-vo")
39+
pilot_reference = "pilot-reference-test"
40+
await pilot_agents_db.register_new_pilot(
41+
vo="pilot-vo", pilot_job_reference=pilot_reference
42+
)
4043

41-
res = await pilot_agents_db.get_pilot_by_id(new_pilot_id)
44+
res = await pilot_agents_db.get_pilot_by_reference(pilot_ref=pilot_reference)
4245

4346
with pytest.raises(NoResultFound):
44-
await pilot_agents_db.get_pilot_by_id(10)
47+
await pilot_agents_db.get_pilot_by_reference("I am a fake ref")
4548

4649
# Set values
47-
assert res["PilotID"] == new_pilot_id
4850
assert res["VO"] == "pilot-vo"
51+
assert res["PilotJobReference"] == pilot_reference
4952

5053
# Default values
5154
assert res["PilotStamp"] == ""
@@ -56,22 +59,26 @@ async def test_insert_and_select_single(pilot_agents_db: PilotAgentsDB):
5659
async def test_create_pilot_and_verify_secret(pilot_agents_db: PilotAgentsDB):
5760

5861
async with pilot_agents_db as pilot_agents_db:
59-
new_pilot_id = await pilot_agents_db.register_new_pilot(vo="pilot-vo")
62+
pilot_reference = "pilot-reference-test"
63+
pilot_id = await pilot_agents_db.register_new_pilot(
64+
vo="pilot-vo", pilot_job_reference=pilot_reference
65+
)
6066

6167
# Add creds
62-
secret = await pilot_agents_db.add_pilot_credentials(new_pilot_id)
68+
secret = await pilot_agents_db.add_pilot_credentials(pilot_id=pilot_id)
6369

6470
assert secret is not None
6571

6672
await pilot_agents_db.verify_pilot_secret(
67-
pilot_id=new_pilot_id, pilot_secret=secret
73+
pilot_job_reference=pilot_reference, pilot_secret=secret
6874
)
6975

7076
with pytest.raises(AuthorizationError):
7177
await pilot_agents_db.verify_pilot_secret(
72-
pilot_id=new_pilot_id, pilot_secret="I love stawberries :)"
78+
pilot_job_reference=pilot_reference,
79+
pilot_secret="I love stawberries :)",
7380
)
7481

7582
await pilot_agents_db.verify_pilot_secret(
76-
pilot_id=63000, pilot_secret=secret
83+
pilot_job_reference="I am a spider", pilot_secret=secret
7784
)

diracx-routers/src/diracx/routers/auth/pilot.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,23 @@
2121
async def pilot_login(
2222
pilot_db: PilotAgentsDB,
2323
auth_db: AuthDB,
24-
pilot_id: int,
24+
pilot_job_reference: str,
2525
pilot_secret: str,
2626
config: Config,
2727
settings: AuthSettings,
2828
available_properties: AvailableSecurityProperties,
2929
):
3030
"""Endpoint without policy, the pilot uses only its secret."""
3131
try:
32-
await pilot_db.verify_pilot_secret(pilot_id=pilot_id, pilot_secret=pilot_secret)
32+
await pilot_db.verify_pilot_secret(
33+
pilot_job_reference=pilot_job_reference, pilot_secret=pilot_secret
34+
)
3335
except AuthorizationError as e:
3436
raise HTTPException(
3537
status_code=status.HTTP_401_UNAUTHORIZED, detail=e.detail
3638
) from e
3739

38-
pilot = await pilot_db.get_pilot_by_id(pilot_id=pilot_id)
40+
pilot = await pilot_db.get_pilot_by_reference(pilot_ref=pilot_job_reference)
3941

4042
pilot_info = {
4143
"pilot_reference": pilot["PilotJobReference"],

diracx-routers/tests/auth/test_pilot_auth.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@ async def test_create_pilot_and_verify_secret(test_client):
3535

3636
# Add a pilot vo
3737
pilot_vo = "lhcb"
38+
pilot_reference = "pilot-test-ref"
3839

3940
async with db as pilot_agents_db:
4041
# Register a pilot
41-
pilot_id = await pilot_agents_db.register_new_pilot(vo=pilot_vo)
42+
pilot_id = await pilot_agents_db.register_new_pilot(
43+
vo=pilot_vo, pilot_job_reference=pilot_reference
44+
)
4245

4346
# Add credentials to this pilot
4447
secret = await pilot_agents_db.add_pilot_credentials(pilot_id=pilot_id)
4548

4649
assert secret is not None
4750

48-
request_data = {"pilot_id": pilot_id, "pilot_secret": secret}
51+
request_data = {"pilot_reference": pilot_reference, "pilot_secret": secret}
4952

5053
r = test_client.post(
5154
"/api/auth/pilot-login",
@@ -84,7 +87,10 @@ async def test_create_pilot_and_verify_secret(test_client):
8487
assert r.json()["detail"] == "Invalid JWT"
8588

8689
# ----------------- Wrong password -----------------
87-
request_data = {"pilot_id": pilot_id, "pilot_secret": "My 1ncr3d1bl3 t0k3n"}
90+
request_data = {
91+
"pilot_reference": pilot_reference,
92+
"pilot_secret": "My 1ncr3d1bl3 t0k3n",
93+
}
8894

8995
r = test_client.post(
9096
"/api/auth/pilot-login",

0 commit comments

Comments
 (0)