forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase_push_notification_config_store.py
More file actions
417 lines (369 loc) · 15.4 KB
/
database_push_notification_config_store.py
File metadata and controls
417 lines (369 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# ruff: noqa: PLC0415
import logging
from typing import TYPE_CHECKING
from google.protobuf.json_format import MessageToJson, Parse
try:
from sqlalchemy import ColumnElement, Table, and_, delete, select
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
)
from sqlalchemy.orm import class_mapper
except ImportError as e:
raise ImportError(
'DatabasePushNotificationConfigStore requires SQLAlchemy and a database driver. '
'Install with one of: '
"'pip install a2a-sdk[postgresql]', "
"'pip install a2a-sdk[mysql]', "
"'pip install a2a-sdk[sqlite]', "
"or 'pip install a2a-sdk[sql]'"
) from e
from collections.abc import Callable
from a2a.compat.v0_3.model_conversions import (
compat_push_notification_config_model_to_core,
)
from a2a.server.context import ServerCallContext
from a2a.server.models import (
Base,
PushNotificationConfigModel,
create_push_notification_config_model,
)
from a2a.server.owner_resolver import OwnerResolver, resolve_user_scope
from a2a.server.tasks.push_notification_config_store import (
PushNotificationConfigStore,
)
from a2a.types.a2a_pb2 import TaskPushNotificationConfig
if TYPE_CHECKING:
from cryptography.fernet import Fernet
logger = logging.getLogger(__name__)
class DatabasePushNotificationConfigStore(PushNotificationConfigStore):
"""SQLAlchemy-based implementation of PushNotificationConfigStore.
Stores push notification configurations in a database supported by SQLAlchemy.
"""
engine: AsyncEngine
async_session_maker: async_sessionmaker[AsyncSession]
create_table: bool
_initialized: bool
config_model: type[PushNotificationConfigModel]
_fernet: 'Fernet | None'
owner_resolver: OwnerResolver
core_to_model_conversion: (
Callable[
[str, TaskPushNotificationConfig, str, 'Fernet | None'],
PushNotificationConfigModel,
]
| None
)
model_to_core_conversion: (
Callable[[PushNotificationConfigModel], TaskPushNotificationConfig]
| None
)
def __init__( # noqa: PLR0913
self,
engine: AsyncEngine,
create_table: bool = True,
table_name: str = 'push_notification_configs',
encryption_key: str | bytes | None = None,
owner_resolver: OwnerResolver = resolve_user_scope,
core_to_model_conversion: Callable[
[str, TaskPushNotificationConfig, str, 'Fernet | None'],
PushNotificationConfigModel,
]
| None = None,
model_to_core_conversion: Callable[
[PushNotificationConfigModel], TaskPushNotificationConfig
]
| None = None,
) -> None:
"""Initializes the DatabasePushNotificationConfigStore.
Args:
engine: An existing SQLAlchemy AsyncEngine to be used by the store.
create_table: If true, create the table on initialization.
table_name: Name of the database table. Defaults to 'push_notification_configs'.
encryption_key: A key for encrypting sensitive configuration data.
If provided, `config_data` will be encrypted in the database.
The key must be a URL-safe base64-encoded 32-byte key.
owner_resolver: Function to resolve the owner from the context.
core_to_model_conversion: Optional function to convert a TaskPushNotificationConfig to a TaskPushNotificationConfigModel.
model_to_core_conversion: Optional function to convert a TaskPushNotificationConfigModel to a TaskPushNotificationConfig.
"""
logger.debug(
'Initializing DatabasePushNotificationConfigStore with existing engine, table: %s',
table_name,
)
self.engine = engine
self.async_session_maker = async_sessionmaker(
self.engine, expire_on_commit=False
)
self.create_table = create_table
self._initialized = False
self.owner_resolver = owner_resolver
self.config_model = (
PushNotificationConfigModel
if table_name == 'push_notification_configs'
else create_push_notification_config_model(table_name)
)
self._fernet = None
self.core_to_model_conversion = core_to_model_conversion
self.model_to_core_conversion = model_to_core_conversion
if encryption_key:
try:
from cryptography.fernet import (
Fernet,
)
except ImportError as e:
raise ImportError(
"DatabasePushNotificationConfigStore with encryption requires the 'cryptography' "
'library. Install with: '
"'pip install a2a-sdk[encryption]'"
) from e
if isinstance(encryption_key, str):
encryption_key = encryption_key.encode('utf-8')
self._fernet = Fernet(encryption_key)
logger.debug(
'Encryption enabled for push notification config store.'
)
async def initialize(self) -> None:
"""Initialize the database and create the table if needed."""
if self._initialized:
return
logger.debug(
'Initializing database schema for push notification configs...'
)
if self.create_table:
async with self.engine.begin() as conn:
mapper = class_mapper(self.config_model)
tables_to_create = [
table for table in mapper.tables if isinstance(table, Table)
]
await conn.run_sync(
Base.metadata.create_all, tables=tables_to_create
)
self._initialized = True
logger.debug(
'Database schema for push notification configs initialized.'
)
async def _ensure_initialized(self) -> None:
"""Ensure the database connection is initialized."""
if not self._initialized:
await self.initialize()
def _to_orm(
self, task_id: str, config: TaskPushNotificationConfig, owner: str
) -> PushNotificationConfigModel:
"""Maps a TaskPushNotificationConfig proto to a SQLAlchemy model instance.
The config data is serialized to JSON bytes, and encrypted if a key is configured.
"""
if self.core_to_model_conversion:
return self.core_to_model_conversion(
task_id, config, owner, self._fernet
)
json_payload = MessageToJson(config).encode('utf-8')
if self._fernet:
data_to_store = self._fernet.encrypt(json_payload)
else:
data_to_store = json_payload
return self.config_model(
task_id=task_id,
config_id=config.id,
owner=owner,
config_data=data_to_store,
protocol_version='1.0',
)
def _from_orm(
self, model_instance: PushNotificationConfigModel
) -> TaskPushNotificationConfig:
"""Maps a SQLAlchemy model instance to a TaskPushNotificationConfig proto.
Handles decryption if a key is configured, with a fallback to plain JSON.
"""
if self.model_to_core_conversion:
return self.model_to_core_conversion(model_instance)
payload = model_instance.config_data
if self._fernet:
from cryptography.fernet import (
InvalidToken,
)
try:
decrypted_payload = self._fernet.decrypt(payload)
return self._parse_config(
decrypted_payload.decode('utf-8'),
model_instance.task_id,
model_instance.protocol_version,
)
except Exception as e:
if isinstance(e, InvalidToken):
# Decryption failed. This could be because the data is not encrypted.
# We'll log a warning and try to parse it as plain JSON as a fallback.
logger.warning(
'Failed to decrypt push notification config for task %s, config %s. '
'Attempting to parse as unencrypted JSON. '
'This may indicate an incorrect encryption key or unencrypted data in the database.',
model_instance.task_id,
model_instance.config_id,
)
# Fall through to the unencrypted parsing logic below.
else:
logger.exception(
'Failed to parse decrypted push notification config for task %s, config %s. '
'Data is corrupted or not valid JSON after decryption.',
model_instance.task_id,
model_instance.config_id,
)
raise ValueError( # noqa: TRY004
'Failed to parse decrypted push notification config data'
) from e
# Try to parse as plain JSON.
try:
payload_str = (
payload.decode('utf-8')
if isinstance(payload, bytes)
else payload
)
return self._parse_config(
payload_str,
model_instance.task_id,
model_instance.protocol_version,
)
except Exception as e:
if self._fernet:
logger.exception(
'Failed to parse push notification config for task %s, config %s. '
'Decryption failed and the data is not valid JSON. '
'This likely indicates the data is corrupted or encrypted with a different key.',
model_instance.task_id,
model_instance.config_id,
)
else:
# if no key is configured and the payload is not valid JSON.
logger.exception(
'Failed to parse push notification config for task %s, config %s. '
'Data is not valid JSON and no encryption key is configured.',
model_instance.task_id,
model_instance.config_id,
)
raise ValueError(
'Failed to parse push notification config data. '
'Data is not valid JSON, or it is encrypted with the wrong key.'
) from e
async def set_info(
self,
task_id: str,
notification_config: TaskPushNotificationConfig,
context: ServerCallContext,
) -> None:
"""Sets or updates the push notification configuration for a task."""
await self._ensure_initialized()
owner = self.owner_resolver(context)
# Create a copy of the config using proto CopyFrom
config_to_save = TaskPushNotificationConfig()
config_to_save.CopyFrom(notification_config)
if not config_to_save.id:
config_to_save.id = task_id
db_config = self._to_orm(task_id, config_to_save, owner)
async with self.async_session_maker.begin() as session:
await session.merge(db_config)
logger.debug(
'Push notification config for task %s with config id %s for owner %s saved/updated.',
task_id,
config_to_save.id,
owner,
)
async def _select_configs(
self,
*predicates: 'ColumnElement[bool]',
) -> list[TaskPushNotificationConfig]:
"""Loads configs matching the given predicates and decodes them."""
await self._ensure_initialized()
async with self.async_session_maker() as session:
stmt = select(self.config_model).where(and_(*predicates))
result = await session.execute(stmt)
models = result.scalars().all()
configs = []
for model in models:
try:
configs.append(self._from_orm(model))
except ValueError: # noqa: PERF203
logger.exception(
'Could not deserialize push notification config for task %s, config %s, owner %s',
model.task_id,
model.config_id,
model.owner,
)
return configs
async def get_info(
self,
task_id: str,
context: ServerCallContext,
) -> list[TaskPushNotificationConfig]:
"""Retrieves all push notification configurations for a task, for the given owner.
Used by the user-callable read endpoints.
"""
owner = self.owner_resolver(context)
return await self._select_configs(
self.config_model.task_id == task_id,
self.config_model.owner == owner,
)
async def get_info_for_dispatch(
self,
task_id: str,
) -> list[TaskPushNotificationConfig]:
"""Retrieves all push notification configurations for a task, across all owners.
Used by the push-notification dispatch path.
"""
return await self._select_configs(
self.config_model.task_id == task_id,
)
async def delete_info(
self,
task_id: str,
context: ServerCallContext,
config_id: str | None = None,
) -> None:
"""Deletes push notification configurations for a task.
If config_id is provided, only that specific configuration is deleted.
If config_id is None, all configurations for the task for the owner are deleted.
"""
await self._ensure_initialized()
owner = self.owner_resolver(context)
async with self.async_session_maker.begin() as session:
stmt = delete(self.config_model).where(
and_(
self.config_model.task_id == task_id,
self.config_model.owner == owner,
)
)
if config_id is not None:
stmt = stmt.where(self.config_model.config_id == config_id)
result = await session.execute(stmt)
if result.rowcount > 0: # type: ignore[attr-defined]
logger.info(
'Deleted %s push notification config(s) for task %s, owner %s.',
result.rowcount, # type: ignore[attr-defined]
task_id,
owner,
)
else:
logger.warning(
'Attempted to delete push notification config for task %s, owner %s with config_id: %s that does not exist.',
task_id,
owner,
config_id,
)
def _parse_config(
self,
json_payload: str,
task_id: str | None = None,
protocol_version: str | None = None,
) -> TaskPushNotificationConfig:
"""Parses a JSON payload into a TaskPushNotificationConfig proto.
Args:
json_payload: The JSON payload to parse.
task_id: The unique identifier of the task. Only required for legacy
(0.3) protocol versions.
protocol_version: The protocol version used for serialization.
"""
if protocol_version == '1.0':
return Parse(json_payload, TaskPushNotificationConfig())
return compat_push_notification_config_model_to_core(
json_payload, task_id or ''
)