Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid mapping for oauth_cb in BaseSettings #606

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
11 changes: 9 additions & 2 deletions quixstreams/kafka/configuration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Callable, Literal, Optional, Tuple, Type

import pydantic
from pydantic import AliasChoices, Field, SecretStr
from pydantic.functional_validators import BeforeValidator
from pydantic_settings import PydanticBaseSettingsSource
Expand Down Expand Up @@ -44,11 +45,17 @@ class ConnectionConfig(BaseSettings):
sasl_kerberos_min_time_before_relogin: Optional[int] = None
sasl_kerberos_service_name: Optional[str] = None
sasl_kerberos_principal: Optional[str] = None

# for oauth_cb, see https://docs.confluent.io/platform/current/clients/confluent-kafka-python/html/index.html#pythonclient-configuration
oauth_cb: Optional[Callable[[str], Tuple[str, float]]] = None
oauth_cb: Optional[Callable[[str], Tuple[str, float]]] = pydantic.Field(
# Prevent the AliasGenerator from changing the field name to "oauth.cb"
default=None,
alias_priority=2,
serialization_alias="oauth_cb",
)

sasl_oauthbearer_config: Optional[str] = None
enable_sasl_oauthbearer_unsecure_jwt: Optional[bool] = None
oauthbearer_token_refresh_cb: Optional[Callable] = None
sasl_oauthbearer_method: Annotated[
Optional[Literal["default", "oidc"]],
BeforeValidator(lambda v: v.lower() if v is not None else v),
Expand Down
7 changes: 7 additions & 0 deletions tests/test_quixstreams/test_kafka/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ def test_from_librdkafka_dict(self, mechanism_casing):
"bootstrap.servers": "url",
"sasl.mechanism": mechanism_casing,
"sasl.username": "my-username",
"oauth_cb": lambda _: _,
}
config = ConnectionConfig.from_librdkafka_dict(librdkafka_dict)

assert config.bootstrap_servers == librdkafka_dict["bootstrap.servers"]
assert config.sasl_mechanism == librdkafka_dict["sasl.mechanism"].upper()
assert config.sasl_username == librdkafka_dict["sasl.username"]
assert config.oauth_cb == librdkafka_dict["oauth_cb"]

def test_from_librdkafka_dict_extras_raise(self):
librdkafka_dict = {
Expand Down Expand Up @@ -96,6 +98,11 @@ def test_sasl_mechanism_aliases(self):
assert "sasl.mechanism" in d
assert "sasl.mechanisms" not in d

def test_oauth_cb(self):
config = ConnectionConfig(bootstrap_servers="url", oauth_cb=lambda _: _)
rd_config = config.as_librdkafka_dict()
assert config.oauth_cb == rd_config["oauth_cb"]

def test_secret_field(self):
"""
Confirm a secret field is obscured
Expand Down
Loading