Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/blueapi/service/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,12 @@ def get_access_token(self):
def sync_auth_flow(self, request):
request.headers["Authorization"] = f"Bearer {self.get_access_token()}"
yield request


class OPAClient: # placeholder until https://jira.diamond.ac.uk/browse/ACQP-550 is done
def do_some_checks(self, task_request) -> bool:
return True


def get_opa_client() -> OPAClient: # placeholder
return OPAClient()
12 changes: 12 additions & 0 deletions src/blueapi/service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from blueapi import __version__
from blueapi.config import ApplicationConfig, OIDCConfig, Tag
from blueapi.service import interface
from blueapi.service.authentication import OPAClient, get_opa_client
from blueapi.worker import TrackableTask, WorkerState
from blueapi.worker.event import TaskStatusEnum

Expand Down Expand Up @@ -278,6 +279,16 @@ def get_device_by_name(
)


def submission_check(
opa: Annotated[OPAClient, Depends(get_opa_client)],
Comment on lines +282 to +283
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there will be multiple of these depends

  1. submit_task
  2. get tasks
  3. delete_tasks
    ....
    Basically most of the endpoint in the tasks group

and as we will have multiple of these we can move them closed to where OPAClient will be

task_request: TaskRequest,
):
allowed = opa.do_some_checks(task_request)

if not allowed:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)


@secure_router_v1.post("/tasks", status_code=status.HTTP_201_CREATED, tags=[Tag.TASK])
@secure_router.post("/tasks", status_code=status.HTTP_201_CREATED, tags=[Tag.TASK])
@start_as_current_span(
Expand All @@ -291,6 +302,7 @@ def submit_task(
request: Request,
response: Response,
task_request: Annotated[TaskRequest, Body(..., examples=[example_task_request])],
authz_check: Annotated[None, Depends(submission_check)],
runner: Annotated[WorkerDispatcher, Depends(_runner)],
) -> TaskResponse:
"""Submit a task to the worker."""
Expand Down
Loading