Skip to content

Commit 8d7226e

Browse files
committed
Extend GET /results to filter by subject and scope
Add optional `subject` and `scopeuuid` query parameters to GET /results. When subject is provided, accounts can access their own results without needing the `read_any` role. Update docs accordingly.
1 parent 0c7f31b commit 8d7226e

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

compliance-monitor/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ The return value is a _list of objects_ like the following:
154154

155155
Supports query parameters:
156156

157+
- `subject=SUBJECT`: restrict results to this subject; if given, accounts without role `read_any`
158+
may still access their own subject's results; if omitted, role `read_any` is required;
159+
- `scopeuuid=SCOPEUUID`: restrict results to this scope;
157160
- `approved=APPROVED`: return only results with approval status `APPROVED` (either 0 or 1);
158161
default: no such restriction is applied;
159162
- `limit=N`: return at most N items (default: 10);

compliance-monitor/monitor.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,16 @@ async def get_results(
799799
account: Annotated[tuple[str, str], Depends(auth)],
800800
conn: Annotated[connection, Depends(get_conn)],
801801
approved: Optional[bool] = None, limit: int = 10, skip: int = 0,
802+
subject: Optional[str] = None, scopeuuid: Optional[str] = None,
802803
):
803-
"""get recent results, potentially filtered by approval status"""
804-
check_role(account, roles=ROLES['read_any'])
804+
"""get recent results, optionally filtered by subject, scope, and approval status"""
805+
if subject is None:
806+
check_role(account, roles=ROLES['read_any'])
807+
else:
808+
check_role(account, subject, ROLES['read_any'])
805809
with conn.cursor() as cur:
806-
return db_get_recent_results2(cur, approved, limit, skip, max_age_days=GRACE_PERIOD_DAYS)
810+
return db_get_recent_results2(cur, approved, limit, skip, max_age_days=GRACE_PERIOD_DAYS,
811+
subject=subject, scopeuuid=scopeuuid)
807812

808813

809814
@app.post("/results")

compliance-monitor/sql.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def db_get_relevant_results2(
398398
return cur.fetchall()
399399

400400

401-
def db_get_recent_results2(cur: cursor, approved, limit, skip, max_age_days=None):
401+
def db_get_recent_results2(cur: cursor, approved, limit, skip, max_age_days=None, subject=None, scopeuuid=None):
402402
"""list recent test results without grouping by scope/version/check"""
403403
columns = ('reportuuid', 'subject', 'checked_at', 'scopeuuid', 'version', 'check', 'result', 'approval')
404404
cur.execute(sql.SQL('''
@@ -414,8 +414,10 @@ def db_get_recent_results2(cur: cursor, approved, limit, skip, max_age_days=None
414414
f"checked_at > NOW() - interval '{max_age_days:d} days'"
415415
),
416416
None if approved is None else sql.SQL('approval = %(approved)s'),
417+
None if subject is None else sql.SQL('result2.subject = %(subject)s'),
418+
None if scopeuuid is None else sql.SQL('result2.scopeuuid = %(scopeuuid)s'),
417419
),
418-
), {"limit": limit, "skip": skip, "approved": approved})
420+
), {"limit": limit, "skip": skip, "approved": approved, "subject": subject, "scopeuuid": scopeuuid})
419421
return [{col: val for col, val in zip(columns, row)} for row in cur.fetchall()]
420422

421423

0 commit comments

Comments
 (0)