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

Add key authentication #34

Merged
merged 4 commits into from
Jun 25, 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
14 changes: 1 addition & 13 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
# Dispatch Highlevel Interface Release Notes

## Summary

<!-- Here goes a general summary of what this release is about -->

## Upgrading

<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->

## New Features

<!-- Here goes the main new features and examples or instructions on how to use them -->

## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
* An API key for authorization must now be passed to the client.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies = [
"frequenz-sdk == 1.0.0rc601",
"frequenz-channels >= 1.0.1, < 2.0.0",
"frequenz-api-dispatch >= 0.13.0, < 0.14",
"frequenz-client-dispatch >= 0.2.0, < 0.3.0",
"frequenz-client-dispatch >= 0.4.0, < 0.5.0",
"frequenz-client-base >= 0.3.0, < 0.5.0",
"frequenz-client-common >= 0.1.0, < 0.3.0",
]
Expand Down
56 changes: 44 additions & 12 deletions src/frequenz/dispatch/_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ class Dispatcher:
async def run():
host = os.getenv("DISPATCH_API_HOST", "localhost")
port = os.getenv("DISPATCH_API_PORT", "50051")
key = os.getenv("DISPATCH_API_KEY", "some-key")

service_address = f"{host}:{port}"
grpc_channel = grpc.aio.insecure_channel(service_address)
microgrid_id = 1
dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
grpc_channel = grpc.aio.secure_channel(
service_address,
credentials=grpc.ssl_channel_credentials()
)
dispatcher = Dispatcher(
microgrid_id=1,
grpc_channel=grpc_channel,
svc_addr=service_address,
key=key
)
await dispatcher.start()

actor = MagicMock() # replace with your actor
Expand Down Expand Up @@ -110,12 +118,20 @@ async def run():
async def run():
host = os.getenv("DISPATCH_API_HOST", "localhost")
port = os.getenv("DISPATCH_API_PORT", "50051")
key = os.getenv("DISPATCH_API_KEY", "some-key")

service_address = f"{host}:{port}"
grpc_channel = grpc.aio.insecure_channel(service_address)
microgrid_id = 1
dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
dispatcher.start() # this will start the actor
grpc_channel = grpc.aio.secure_channel(
service_address,
credentials=grpc.ssl_channel_credentials()
)
dispatcher = Dispatcher(
microgrid_id=1,
grpc_channel=grpc_channel,
svc_addr=service_address,
key=key
)
await dispatcher.start() # this will start the actor

events_receiver = dispatcher.lifecycle_events.new_receiver()

Expand Down Expand Up @@ -146,11 +162,21 @@ async def run():
async def run():
host = os.getenv("DISPATCH_API_HOST", "localhost")
port = os.getenv("DISPATCH_API_PORT", "50051")
key = os.getenv("DISPATCH_API_KEY", "some-key")

service_address = f"{host}:{port}"
grpc_channel = grpc.aio.insecure_channel(service_address)
microgrid_id = 1
dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)

service_address = f"{host}:{port}"
grpc_channel = grpc.aio.secure_channel(
service_address,
credentials=grpc.ssl_channel_credentials()
)
dispatcher = Dispatcher(
microgrid_id=microgrid_id,
grpc_channel=grpc_channel,
svc_addr=service_address,
key=key
)
await dispatcher.start() # this will start the actor

# Create a new dispatch
Expand All @@ -175,20 +201,26 @@ async def run():
"""

def __init__(
self, microgrid_id: int, grpc_channel: grpc.aio.Channel, svc_addr: str
self,
*,
microgrid_id: int,
grpc_channel: grpc.aio.Channel,
svc_addr: str,
key: str,
):
"""Initialize the dispatcher.

Args:
microgrid_id: The microgrid id.
grpc_channel: The gRPC channel.
svc_addr: The service address.
key: The key to access the service.
"""
self._running_state_channel = Broadcast[Dispatch](name="running_state_change")
self._lifecycle_events_channel = Broadcast[DispatchEvent](
name="lifecycle_events"
)
self._client = Client(grpc_channel, svc_addr)
self._client = Client(grpc_channel=grpc_channel, svc_addr=svc_addr, key=key)
Comment on lines 203 to +223

Choose a reason for hiding this comment

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

Looks good to me.
Just side note that 3 out of 4 arguments to Dispatcher constructor are really arguments to Client constructor.
And they are just passed to Client. And every description for these arguments is repeated in Client.
Maybe we could have single struct like ClientArgs in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That could work, though we want to change to the base-client where it will do this job for us

self._actor = DispatchingActor(
microgrid_id,
self._client,
Expand Down
Loading