From 89ac2a76bf6458717db3e233e22d0c25690365d7 Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Thu, 1 Feb 2024 17:08:36 -0500 Subject: [PATCH] Add auth to librarian client --- hera_librarian/client.py | 8 ++++++- hera_librarian/settings.py | 2 ++ tests/integration_test/conftest.py | 21 ++++++++++++++++++- .../integration_test/test_secondary_server.py | 1 + 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/hera_librarian/client.py b/hera_librarian/client.py index 5cd2699..1ccf44e 100644 --- a/hera_librarian/client.py +++ b/hera_librarian/client.py @@ -42,8 +42,9 @@ class LibrarianClient: host: str port: int user: str + password: str - def __init__(self, host: str, port: int, user: str): + def __init__(self, host: str, port: int, user: str, password: str): """ Create a new LibrarianClient. @@ -55,6 +56,8 @@ def __init__(self, host: str, port: int, user: str): The port of the Librarian server. user : str The name of the user. + password : str + The password of the user. """ if host[-1] == "/": @@ -64,6 +67,7 @@ def __init__(self, host: str, port: int, user: str): self.port = port self.user = user + self.password = password def __repr__(self): return f"Librarian Client ({self.user}) for {self.host}:{self.port}" @@ -88,6 +92,7 @@ def from_info(cls, client_info: ClientInfo): host=client_info.host, port=client_info.port, user=client_info.user, + password=client_info.password, ) @property @@ -154,6 +159,7 @@ def post( self.resolve(endpoint), data=data, headers={"Content-Type": "application/json"}, + auth=(self.user, self.password), ) if str(r.status_code)[0] != "2": diff --git a/hera_librarian/settings.py b/hera_librarian/settings.py index 4828fed..e532064 100644 --- a/hera_librarian/settings.py +++ b/hera_librarian/settings.py @@ -24,6 +24,8 @@ class ClientInfo(BaseModel): "The port of this librarian server" host: str "The hostname of this librarian server" + password: str + "Your password on this librarian" class ClientSettings(BaseSettings): diff --git a/tests/integration_test/conftest.py b/tests/integration_test/conftest.py index 4b3f657..1099f57 100644 --- a/tests/integration_test/conftest.py +++ b/tests/integration_test/conftest.py @@ -69,7 +69,25 @@ def librarian_client(server) -> LibrarianClient: Returns a LibrarianClient connected to the server. """ - client = LibrarianClient(host="http://localhost", port=server.id, user="test-A") + connections = json.dumps( + { + "test-A": { + "user": "test-A", + "port": server.id, + "host": "http://localhost", + "password": "test_A_password", + } + } + ) + + os.environ["LIBRARIAN_CLIENT_CONNECTIONS"] = connections + + client = LibrarianClient( + host="http://localhost", + port=server.id, + user="test-A", + password="test_A_password", + ) yield client @@ -88,6 +106,7 @@ def librarian_client_command_line(server): "user": "test-B", "port": server.id, "host": "http://localhost", + "password": "test_B_password", } } ) diff --git a/tests/integration_test/test_secondary_server.py b/tests/integration_test/test_secondary_server.py index d458003..c0d18f6 100644 --- a/tests/integration_test/test_secondary_server.py +++ b/tests/integration_test/test_secondary_server.py @@ -20,6 +20,7 @@ def test_secondary_server_simple( response = test_client.post( "/api/v2/ping", content=PingRequest().model_dump_json(), + auth=("test-A", "test_A_password"), ) assert response.status_code == 200