Skip to content

Syk/refactor the config file - #96

Draft
itsoyou wants to merge 12 commits into
mainfrom
syk/refactor-the-config-file
Draft

Syk/refactor the config file#96
itsoyou wants to merge 12 commits into
mainfrom
syk/refactor-the-config-file

Conversation

@itsoyou

@itsoyou itsoyou commented Aug 24, 2026

Copy link
Copy Markdown
Member

SYN-95


Note

Medium Risk
Config parsing at Synapse startup now depends on Pydantic validation and stricter types (e.g. OIDC allowed_client_ids); misconfiguration may fail earlier with different errors. OAuth/EPA metadata registration still treats expose_metadata_resource like a dict in token_authenticator while config now stores a Pydantic model—worth verifying those paths at runtime.

Overview
Replaces the monolithic config.py with a synapse_token_authenticator/config/ package built on Pydantic, and adds pydantic as a runtime dependency.

TokenAuthenticatorConfig now instantiates typed models (JwtConfig, OIDCConfig, OAuthConfig, EPaConfig) instead of nested manual parsing. Shared behavior lives in base.py: claim-validator coercion (parallel to the claims DSL), JWK/JWKSet resolution from dict, JSON string, PEM file, or JWKS URL, and reusable claim-mapping fields.

OAuth / EPA / JWT validation rules are largely preserved (required JWK source, encryption JWK for EPA, at least one of jwt vs introspection for OAuth, JWT secret/keyfile checks, allowed algorithms via Literal). OIDCConfig.allowed_client_ids is now list[str] (a bare string is rejected). parse_validator in claims_validator.py raises ValueError instead of generic Exception.

http_auth moves under config/http_auth.py with HttpAuthField coercion; config-class prefix was removed from auth error logs. Imports in token_authenticator and login_metadata follow the new module layout.

Coverage is expanded with tests/test_config/ for validators, JWK sources, and each config type.

Reviewed by Cursor Bugbot for commit d2351c1. Bugbot is set up for automated code reviews on this repo. Configure here.

@itsoyou
itsoyou requested a review from a team as a code owner August 24, 2026 09:41
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.05882% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.28%. Comparing base (a12c777) to head (d2351c1).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
synapse_token_authenticator/claims_validator.py 54.90% 18 Missing and 5 partials ⚠️
synapse_token_authenticator/config/epa_config.py 82.85% 3 Missing and 3 partials ⚠️
synapse_token_authenticator/config/base.py 95.60% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #96      +/-   ##
==========================================
+ Coverage   76.35%   80.28%   +3.92%     
==========================================
  Files           9       14       +5     
  Lines         791      832      +41     
  Branches      146      149       +3     
==========================================
+ Hits          604      668      +64     
+ Misses        131      115      -16     
+ Partials       56       49       -7     
Files with missing lines Coverage Δ
synapse_token_authenticator/config/__init__.py 100.00% <100.00%> (ø)
synapse_token_authenticator/config/http_auth.py 96.42% <100.00%> (ø)
synapse_token_authenticator/config/jwt_config.py 100.00% <100.00%> (ø)
synapse_token_authenticator/config/oauth_config.py 100.00% <100.00%> (ø)
synapse_token_authenticator/config/oidc_config.py 100.00% <100.00%> (ø)
...se_token_authenticator/resources/login_metadata.py 78.57% <100.00%> (ø)
synapse_token_authenticator/token_authenticator.py 71.84% <100.00%> (ø)
synapse_token_authenticator/config/base.py 95.60% <95.60%> (ø)
synapse_token_authenticator/config/epa_config.py 82.85% <82.85%> (ø)
synapse_token_authenticator/claims_validator.py 72.99% <54.90%> (-2.98%) ⬇️

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a12c777...d2351c1. Read the comment docs.

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

Comment thread synapse_token_authenticator/config_util/oidc_config.py Outdated
Comment thread synapse_token_authenticator/token_authenticator.py Outdated
@itsoyou
itsoyou force-pushed the syk/refactor-the-config-file branch from beda4cf to c65fab7 Compare August 24, 2026 12:38

@jason-famedly jason-famedly left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That was a lot 😅
Here's a first pass. Mostly, it really looks good and it helped me get it all into my headspace in a more organized way. I certainly know more now than I did before. Let's address all these and see where we sit after. I still want to make those attributes on the main TokenAuthenticatorConfig not be depending on a walrus operator so they could accidentally become unreference-able(this is not an acceptable use of getattr() in my book!), but lets get this first pass done before hand.

Comment thread synapse_token_authenticator/config_util/__init__.py Outdated
Comment on lines +28 to +29
class BaseConfigModel(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm curious what will happen if any existing configs have extra fields that were previously ignored and now will be rejected? 🤔 Just noting this as a change in behavior that may have unexpected side-effects.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

hmm I think extra fields should be prohibited and admins should keep there configs clean, but this could be breaking change, so I will switch to extra="ignore"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Normally, I would absolutely agree. But yes, breaking changes and all that. We can always tighten this up after giving fair warning(and best case scenario there would be nothing for admins to do)

Comment thread synapse_token_authenticator/config/base.py Outdated
Comment thread synapse_token_authenticator/config/base.py Outdated
Comment thread synapse_token_authenticator/config/base.py
Comment thread tests/config_util/test_base.py Outdated
Comment thread tests/config_util/test_epa_config.py Outdated
Comment thread tests/test_config/test_epa_config.py
Comment thread tests/config_util/test_http_auth.py Outdated
Comment on lines +16 to +35
class TestJwtValidationConfig:
def test_jwt_validation_config(self):
jwk = get_jwk().export(private_key=True)
config = JwtValidationConfig(jwk_set=jwk)
assert config.validator == Exist()
assert config.require_expiry is False
assert config.localpart_path is None
assert config.user_id_path is None
assert config.fq_uid_path is None
assert config.displayname_path is None
assert config.admin_path is None
assert config.email_path is None
assert config.required_scopes is None
assert config.jwk_set is not None and isinstance(config.jwk_set, JWK)
assert config.jwk_file is None
assert config.jwks_endpoint is None

def test_jwt_validation_config_missing_jwk_source(self):
with pytest.raises(ValidationError):
JwtValidationConfig()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So from here down in the review, all these Test cases are essentially testing defaults(unless something was required, of course). I would like to see a few more tests where we exercise for things that are not defaults or required. Maybe one test for each that has everything(like a full test), then selectively for odd Typed attributes(like that allowed_client_ids being a list[str]: make sure it won't accept a str since it has a lot of the same attributes of a list[str]).

@itsoyou
itsoyou force-pushed the syk/refactor-the-config-file branch from 510c446 to 8ca3565 Compare August 24, 2026 14:20
Comment thread synapse_token_authenticator/config/base.py Outdated
Comment thread synapse_token_authenticator/config/http_auth.py
Comment thread synapse_token_authenticator/config/base.py Outdated
Comment thread synapse_token_authenticator/config/base.py
Comment thread synapse_token_authenticator/config/base.py Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fc97c90. Configure here.

Comment thread synapse_token_authenticator/token_authenticator.py
@itsoyou
itsoyou requested a review from jason-famedly August 25, 2026 15:44
@itsoyou
itsoyou marked this pull request as draft August 27, 2026 13:21
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.

2 participants