Skip to content

Commit

Permalink
Strictier type checking for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
michelhe committed Aug 27, 2023
1 parent ccf6db8 commit a4a3d0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
32 changes: 22 additions & 10 deletions glogger/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class SendBatch(NamedTuple):
lost_logs_count: int


class AuthToken(str):
pass


class BasicAuthCredentials(NamedTuple):
username: str
password: str


class Sender:
# If Tuple[float, float], then the first value is connection-timeout and the second read-timeout.
# See https://requests.readthedocs.io/en/latest/user/advanced/#timeouts
Expand All @@ -38,7 +47,7 @@ def __init__(
server_address: str,
*,
auth_token: Optional[str] = None,
basic_auth: Optional[Tuple[str, str]] = None,
auth: Union[AuthToken, BasicAuthCredentials] = None,
scheme: str = "https",
send_interval: float = 30.0,
send_threshold: float = 0.8,
Expand All @@ -50,8 +59,8 @@ def __init__(
Create a new Sender and start flushing log messages in a background thread.
:param application_name: Unique identifier requests coming from this handler.
:param auth_token: Token for authenticating requests to the server.
:param basic_auth: Basic auth credentials for authenticating requests to the server.
:param auth: The auth to use for this handler. One of AuthToken or BasicAuthCredentials.
:param auth_token: Deprecated - please use the auth param instead
:param server_address: Address of server where to send messages.
:param scheme: The scheme to use as string ('http' or 'https')
:param send_interval: Seconds between sending batches.
Expand All @@ -76,13 +85,16 @@ def __init__(
self.session = Session()

# Set up auth
# basic-auth is preferred over X-Token
if basic_auth is not None:
self.session.auth = HTTPBasicAuth(*basic_auth)
elif auth_token is not None:
self.session.headers["X-Token"] = auth_token
else:
raise ValueError("Either auth_token or basic_auth must be provided")
# TODO: Remove auth_token in next version
if auth_token is not None:
import warnings

warnings.warn("auth_token is deprecated, please use the auth param", DeprecationWarning)

if isinstance(auth, BasicAuthCredentials):
self.session.auth = HTTPBasicAuth(*auth)
elif isinstance(auth, AuthToken):
self.session.headers["X-Token"] = str(auth)

self.session.verify = verify
self.messages_buffer: Optional[MessagesBuffer] = None
Expand Down
6 changes: 3 additions & 3 deletions tests/glogger/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from glogger.extra_adapter import ExtraAdapter
from glogger.handler import BatchRequestsHandler
from glogger.sender import SERVER_SEND_ERROR_MESSAGE, Sender
from glogger.sender import SERVER_SEND_ERROR_MESSAGE, AuthToken, Sender


class MockBatchRequestsHandler(BatchRequestsHandler):
Expand All @@ -33,7 +33,7 @@ def _send_once_to_server(self, data: bytes) -> None:
def __init__(self, *args, max_total_length=100000, max_message_size=10000, overflow_drop_factor=0.25, **kwargs):
super().__init__(
self.MockSender(
"app", *args, auth_token="token", scheme="http", send_min_interval=0.2, max_send_tries=1, **kwargs
"app", *args, auth=AuthToken("token"), scheme="http", send_min_interval=0.2, max_send_tries=1, **kwargs
),
max_total_length=max_total_length,
max_message_size=max_message_size,
Expand All @@ -53,7 +53,7 @@ def __init__(self, *args, send_interval=0.2, **kwargs):
self.HttpSender(
"app",
*args,
auth_token="token",
auth=AuthToken("token"),
scheme="http",
send_interval=send_interval,
send_min_interval=0.2,
Expand Down

0 comments on commit a4a3d0b

Please sign in to comment.