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

Implement key-based authentication #56

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Make test keys constants
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz committed Jun 19, 2024
commit fb203147873b65b07f4a87a19d4eaa30be1e5e7b
10 changes: 8 additions & 2 deletions src/frequenz/client/dispatch/test/_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
from .._internal_types import DispatchCreateRequest
from ..types import Dispatch

ALL_KEY = "all"
"""Key that has access to all resources in the FakeService."""

NONE_KEY = "none"
"""Key that has no access to any resources in the FakeService."""


@dataclass
class FakeService:
Expand Down Expand Up @@ -75,13 +81,13 @@ def _check_access(self, metadata: grpc.aio.Metadata) -> None:
"No access key provided",
)

if key == "none":
if key == NONE_KEY:
raise grpc.RpcError(
grpc.StatusCode.PERMISSION_DENIED,
"Permission denied",
)

if key != "all":
if key != ALL_KEY:
raise grpc.RpcError(
grpc.StatusCode.UNAUTHENTICATED,
"Invalid access key",
Expand Down
6 changes: 4 additions & 2 deletions src/frequenz/client/dispatch/test/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

from .. import Client
from ..types import Dispatch
from ._service import FakeService
from ._service import ALL_KEY, NONE_KEY, FakeService

__all__ = ["FakeClient", "to_create_params", "ALL_KEY", "NONE_KEY"]


class FakeClient(Client):
Expand All @@ -26,7 +28,7 @@ def __init__(
Args:
shuffle_after_create: Whether to shuffle the dispatches after creating them.
"""
super().__init__(grpc_channel=MagicMock(), svc_addr="mock", key="all")
super().__init__(grpc_channel=MagicMock(), svc_addr="mock", key=ALL_KEY)
self._stub = FakeService() # type: ignore
self._service._shuffle_after_create = shuffle_after_create

Expand Down
4 changes: 2 additions & 2 deletions tests/test_dispatch_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from frequenz.client.common.microgrid.components import ComponentCategory
from frequenz.client.dispatch.__main__ import cli
from frequenz.client.dispatch.test.client import FakeClient
from frequenz.client.dispatch.test.client import ALL_KEY, FakeClient
from frequenz.client.dispatch.types import (
Dispatch,
EndCriteria,
Expand All @@ -26,7 +26,7 @@
TEST_NOW = datetime(2023, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
"""Arbitrary time used as NOW for testing."""

ENVIRONMENT_VARIABLES = {"DISPATCH_API_KEY": "all"}
ENVIRONMENT_VARIABLES = {"DISPATCH_API_KEY": ALL_KEY}


@pytest.fixture
Expand Down