Skip to content

feat: Add OpaClient to wrap auth checks#1541

Open
tpoliaw wants to merge 5 commits into
depends-userfrom
opa-client
Open

feat: Add OpaClient to wrap auth checks#1541
tpoliaw wants to merge 5 commits into
depends-userfrom
opa-client

Conversation

@tpoliaw
Copy link
Copy Markdown
Contributor

@tpoliaw tpoliaw commented May 15, 2026

Proof of concept opa client with dependency injection and example check

@tpoliaw tpoliaw changed the title feat: Add OpaClient to wrap auth checks" feat: Add OpaClient to wrap auth checks May 15, 2026
Comment thread src/blueapi/service/main.py Fixed
@tpoliaw tpoliaw force-pushed the opa-client branch 2 times, most recently from e8ddc77 to db3325b Compare May 15, 2026 15:31
@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.77%. Comparing base (e6ca161) to head (a093b5a).

Additional details and impacted files
@@               Coverage Diff                @@
##           depends-user    #1541      +/-   ##
================================================
+ Coverage         95.63%   95.77%   +0.14%     
================================================
  Files                43       44       +1     
  Lines              3228     3264      +36     
================================================
+ Hits               3087     3126      +39     
+ Misses              141      138       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tpoliaw tpoliaw changed the base branch from main to depends-user May 29, 2026 10:50
@tpoliaw tpoliaw marked this pull request as ready for review June 2, 2026 11:13
@tpoliaw tpoliaw requested a review from a team as a code owner June 2, 2026 11:13
Copy link
Copy Markdown

@fajinyuan fajinyuan left a comment

Choose a reason for hiding this comment

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

please see my questions.

Comment thread src/blueapi/config.py


class OpaConfig(BlueapiBaseModel):
root: HttpUrl = HttpUrl("http://localhost:8181")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

why hard coded this URL in ? will this always the case on beamline?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The hardcoded value is a default (the opa server used in system tests) similar to the ones for numtracker and OIDC. On beamlines, this will be overwritten by the config (in the beamline's values.yaml)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the explaination as from the codes I did see how this URL being replaced by other on beamline, I must have missed some codes that does the overwrite.

}
},
"additionalProperties": false
},
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am new to cloud programming, but would like to understand why there are the same json section here as in config_schema.json above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When deploying blueapi via helm, the config is included in the values.yaml file. When the schema of the config changes, the embedded config in the values file changes in the same way.

@tpoliaw tpoliaw requested a review from ZohebShaikh June 2, 2026 16:04
Copy link
Copy Markdown
Contributor

@ZohebShaikh ZohebShaikh left a comment

Choose a reason for hiding this comment

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

looks good

def __init__(self, instrument: str, config: OpaConfig):
LOGGER.info("Creating OpaClient for %s with config %s", instrument, config)
self._instrument = instrument
self._conf = config
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.

nit: it is a private variable so you can leave it as it is

Suggested change
self._conf = config
self._config = config

json={
"input": {
"beamline": self._instrument,
"audience": "account",
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 have been going back and forth on this, but I think this should be configurable in the config rather than hardcoded. we can default it to "account"

Suggested change
"audience": "account",
"audience": self._audience,

LOGGER.info("Closing OPA session")
await self._session.close()

async def _call_opa(self, endpoint, data: Mapping[str, Any]) -> bool:
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.

Suggested change
async def _call_opa(self, endpoint, data: Mapping[str, Any]) -> bool:
async def _call_opa(self, endpoint: str, data: Mapping[str, Any]) -> bool:

}
},
)
return (await resp.json())["result"]
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.

nit: Will be good to TypeAdapter the result to bool because of python

Something like this

        from pydantic import TypeAdapter
        return TypeAdapter(bool).validate_python((await resp.json())["result"])

I think we should as put this in

try:
  	TypeAdapter(bool).validate_python((await resp.json())["result"])
except KeyError : # on result unlikely
	...
except Timeout to OPA as e:
	raise e as Timeouterror

or just Exception

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What does the TypeAdapter protect against? OPA returning "False" instead of False?

For the exception handling, does wrapping the exception here add much beyond letting the original exception be raised?

Comment thread src/blueapi/config.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants