Skip to content

Commit 95a0f50

Browse files
authored
Add key authentication (#34)
Update examples to use key and TLS
2 parents 50fa7af + 6723c18 commit 95a0f50

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
# Dispatch Highlevel Interface Release Notes
22

3-
## Summary
4-
5-
<!-- Here goes a general summary of what this release is about -->
6-
73
## Upgrading
84

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
10-
11-
## New Features
12-
13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
14-
15-
## Bug Fixes
16-
17-
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
5+
* An API key for authorization must now be passed to the client.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dependencies = [
4242
"frequenz-sdk == 1.0.0rc601",
4343
"frequenz-channels >= 1.0.1, < 2.0.0",
4444
"frequenz-api-dispatch >= 0.13.0, < 0.14",
45-
"frequenz-client-dispatch >= 0.2.0, < 0.3.0",
45+
"frequenz-client-dispatch >= 0.4.0, < 0.5.0",
4646
"frequenz-client-base >= 0.3.0, < 0.5.0",
4747
"frequenz-client-common >= 0.1.0, < 0.3.0",
4848
]

src/frequenz/dispatch/_dispatcher.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ class Dispatcher:
6262
async def run():
6363
host = os.getenv("DISPATCH_API_HOST", "localhost")
6464
port = os.getenv("DISPATCH_API_PORT", "50051")
65+
key = os.getenv("DISPATCH_API_KEY", "some-key")
6566
6667
service_address = f"{host}:{port}"
67-
grpc_channel = grpc.aio.insecure_channel(service_address)
68-
microgrid_id = 1
69-
dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
68+
grpc_channel = grpc.aio.secure_channel(
69+
service_address,
70+
credentials=grpc.ssl_channel_credentials()
71+
)
72+
dispatcher = Dispatcher(
73+
microgrid_id=1,
74+
grpc_channel=grpc_channel,
75+
svc_addr=service_address,
76+
key=key
77+
)
7078
await dispatcher.start()
7179
7280
actor = MagicMock() # replace with your actor
@@ -110,12 +118,20 @@ async def run():
110118
async def run():
111119
host = os.getenv("DISPATCH_API_HOST", "localhost")
112120
port = os.getenv("DISPATCH_API_PORT", "50051")
121+
key = os.getenv("DISPATCH_API_KEY", "some-key")
113122
114123
service_address = f"{host}:{port}"
115-
grpc_channel = grpc.aio.insecure_channel(service_address)
116-
microgrid_id = 1
117-
dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
118-
dispatcher.start() # this will start the actor
124+
grpc_channel = grpc.aio.secure_channel(
125+
service_address,
126+
credentials=grpc.ssl_channel_credentials()
127+
)
128+
dispatcher = Dispatcher(
129+
microgrid_id=1,
130+
grpc_channel=grpc_channel,
131+
svc_addr=service_address,
132+
key=key
133+
)
134+
await dispatcher.start() # this will start the actor
119135
120136
events_receiver = dispatcher.lifecycle_events.new_receiver()
121137
@@ -146,11 +162,21 @@ async def run():
146162
async def run():
147163
host = os.getenv("DISPATCH_API_HOST", "localhost")
148164
port = os.getenv("DISPATCH_API_PORT", "50051")
165+
key = os.getenv("DISPATCH_API_KEY", "some-key")
149166
150-
service_address = f"{host}:{port}"
151-
grpc_channel = grpc.aio.insecure_channel(service_address)
152167
microgrid_id = 1
153-
dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
168+
169+
service_address = f"{host}:{port}"
170+
grpc_channel = grpc.aio.secure_channel(
171+
service_address,
172+
credentials=grpc.ssl_channel_credentials()
173+
)
174+
dispatcher = Dispatcher(
175+
microgrid_id=microgrid_id,
176+
grpc_channel=grpc_channel,
177+
svc_addr=service_address,
178+
key=key
179+
)
154180
await dispatcher.start() # this will start the actor
155181
156182
# Create a new dispatch
@@ -175,20 +201,26 @@ async def run():
175201
"""
176202

177203
def __init__(
178-
self, microgrid_id: int, grpc_channel: grpc.aio.Channel, svc_addr: str
204+
self,
205+
*,
206+
microgrid_id: int,
207+
grpc_channel: grpc.aio.Channel,
208+
svc_addr: str,
209+
key: str,
179210
):
180211
"""Initialize the dispatcher.
181212
182213
Args:
183214
microgrid_id: The microgrid id.
184215
grpc_channel: The gRPC channel.
185216
svc_addr: The service address.
217+
key: The key to access the service.
186218
"""
187219
self._running_state_channel = Broadcast[Dispatch](name="running_state_change")
188220
self._lifecycle_events_channel = Broadcast[DispatchEvent](
189221
name="lifecycle_events"
190222
)
191-
self._client = Client(grpc_channel, svc_addr)
223+
self._client = Client(grpc_channel=grpc_channel, svc_addr=svc_addr, key=key)
192224
self._actor = DispatchingActor(
193225
microgrid_id,
194226
self._client,

0 commit comments

Comments
 (0)