| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import json |
| 19 |
import time |
| 20 |
from datetime import datetime, timedelta |
| 21 |
|
| 22 |
import pytest |
| 23 |
from confluent_kafka.schema_registry.rule_registry import RuleRegistry, \ |
| 24 |
RuleOverride |
| 25 |
from fastavro._logical_readers import UUID |
| 26 |
|
| 27 |
from confluent_kafka.schema_registry import AsyncSchemaRegistryClient, \ |
| 28 |
Schema, Metadata, MetadataProperties, header_schema_id_serializer |
| 29 |
from confluent_kafka.schema_registry.avro import AsyncAvroSerializer, \ |
| 30 |
AsyncAvroDeserializer |
| 31 |
from confluent_kafka.schema_registry.rules.cel.cel_executor import CelExecutor |
| 32 |
from confluent_kafka.schema_registry.rules.cel.cel_field_executor import \ |
| 33 |
CelFieldExecutor |
| 34 |
from confluent_kafka.schema_registry.rules.encryption.awskms.aws_driver import \ |
| 35 |
AwsKmsDriver |
| 36 |
from confluent_kafka.schema_registry.rules.encryption.azurekms.azure_driver import \ |
| 37 |
AzureKmsDriver |
| 38 |
from confluent_kafka.schema_registry.rules.encryption.dek_registry.dek_registry_client import \ |
| 39 |
DekRegistryClient, DekAlgorithm |
| 40 |
from confluent_kafka.schema_registry.rules.encryption.encrypt_executor import \ |
| 41 |
FieldEncryptionExecutor, Clock, EncryptionExecutor |
| 42 |
from confluent_kafka.schema_registry.rules.encryption.gcpkms.gcp_driver import \ |
| 43 |
GcpKmsDriver |
| 44 |
from confluent_kafka.schema_registry.rules.encryption.hcvault.hcvault_driver import \ |
| 45 |
HcVaultKmsDriver |
| 46 |
from confluent_kafka.schema_registry.rules.encryption.localkms.local_driver import \ |
| 47 |
LocalKmsDriver |
| 48 |
from confluent_kafka.schema_registry.rules.jsonata.jsonata_executor import \ |
| 49 |
JsonataExecutor |
| 50 |
from confluent_kafka.schema_registry.schema_registry_client import RuleSet, \ |
| 51 |
Rule, RuleKind, RuleMode, SchemaReference, RuleParams, ServerConfig |
| 52 |
from confluent_kafka.schema_registry.serde import RuleConditionError |
| 53 |
from confluent_kafka.serialization import SerializationContext, MessageField, SerializationError |
| 54 |
|
| 55 |
|
| 56 |
class FakeClock(Clock): |
| 57 |
|
| 58 |
def __init__(self): |
| 59 |
self.fixed_now = int(round(time.time() * 1000)) |
| 60 |
|
| 61 |
def now(self) -> int: |
| 62 |
return self.fixed_now |
| 63 |
|
| 64 |
|
| 65 |
_BASE_URL = "mock://" |
| 66 |
|
| 67 |
_TOPIC = "topic1" |
| 68 |
_SUBJECT = _TOPIC + "-value" |
| 69 |
|
| 70 |
|
| 71 |
@pytest.fixture(autouse=True) |
| 72 |
async def run_before_and_after_tests(tmpdir): |
| 73 |
"""Fixture to execute asserts before and after a test is run""" |
| 74 |
|
| 75 |
|
| 76 |
CelExecutor.register() |
| 77 |
CelFieldExecutor.register() |
| 78 |
AwsKmsDriver.register() |
| 79 |
AzureKmsDriver.register() |
| 80 |
GcpKmsDriver.register() |
| 81 |
HcVaultKmsDriver.register() |
| 82 |
JsonataExecutor.register() |
| 83 |
LocalKmsDriver.register() |
| 84 |
|
| 85 |
yield |
| 86 |
|
| 87 |
|
| 88 |
conf = {'url': _BASE_URL} |
| 89 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 90 |
try: |
| 91 |
await client.delete_subject(_SUBJECT, True) |
| 92 |
except Exception: |
| 93 |
pass |
| 94 |
subjects = await client.get_subjects() |
| 95 |
for subject in subjects: |
| 96 |
try: |
| 97 |
await client.delete_subject(subject, True) |
| 98 |
except Exception: |
| 99 |
pass |
| 100 |
|
| 101 |
|
| 102 |
async def test_avro_basic_serialization(): |
| 103 |
conf = {'url': _BASE_URL} |
| 104 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 105 |
ser_conf = {'auto.register.schemas': True} |
| 106 |
obj = { |
| 107 |
'intField': 123, |
| 108 |
'doubleField': 45.67, |
| 109 |
'stringField': 'hi', |
| 110 |
'booleanField': True, |
| 111 |
'bytesField': b'foobar', |
| 112 |
} |
| 113 |
schema = { |
| 114 |
'type': 'record', |
| 115 |
'name': 'test', |
| 116 |
'fields': [ |
| 117 |
{'name': 'intField', 'type': 'int'}, |
| 118 |
{'name': 'doubleField', 'type': 'double'}, |
| 119 |
{'name': 'stringField', 'type': 'string'}, |
| 120 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 121 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 122 |
] |
| 123 |
} |
| 124 |
ser = await AsyncAvroSerializer(client, schema_str=json.dumps(schema), conf=ser_conf) |
| 125 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 126 |
obj_bytes = await ser(obj, ser_ctx) |
| 127 |
|
| 128 |
deser = await AsyncAvroDeserializer(client) |
| 129 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 130 |
assert obj == obj2 |
| 131 |
|
| 132 |
|
| 133 |
async def test_avro_guid_in_header(): |
| 134 |
conf = {'url': _BASE_URL} |
| 135 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 136 |
ser_conf = { |
| 137 |
'auto.register.schemas': True, |
| 138 |
'schema.id.serializer': header_schema_id_serializer |
| 139 |
} |
| 140 |
obj = { |
| 141 |
'intField': 123, |
| 142 |
'doubleField': 45.67, |
| 143 |
'stringField': 'hi', |
| 144 |
'booleanField': True, |
| 145 |
'bytesField': b'foobar', |
| 146 |
} |
| 147 |
schema = { |
| 148 |
'type': 'record', |
| 149 |
'name': 'test', |
| 150 |
'fields': [ |
| 151 |
{'name': 'intField', 'type': 'int'}, |
| 152 |
{'name': 'doubleField', 'type': 'double'}, |
| 153 |
{'name': 'stringField', 'type': 'string'}, |
| 154 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 155 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 156 |
] |
| 157 |
} |
| 158 |
ser = await AsyncAvroSerializer(client, schema_str=json.dumps(schema), conf=ser_conf) |
| 159 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE, {}) |
| 160 |
obj_bytes = await ser(obj, ser_ctx) |
| 161 |
|
| 162 |
deser = await AsyncAvroDeserializer(client) |
| 163 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 164 |
assert obj == obj2 |
| 165 |
|
| 166 |
|
| 167 |
async def test_avro_serialize_use_schema_id(): |
| 168 |
conf = {'url': _BASE_URL} |
| 169 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 170 |
ser_conf = {'auto.register.schemas': False, 'use.schema.id': 1} |
| 171 |
|
| 172 |
obj = { |
| 173 |
'intField': 123, |
| 174 |
'doubleField': 45.67, |
| 175 |
'stringField': 'hi', |
| 176 |
'booleanField': True, |
| 177 |
'bytesField': b'foobar', |
| 178 |
} |
| 179 |
schema = { |
| 180 |
'type': 'record', |
| 181 |
'name': 'ref', |
| 182 |
'fields': [ |
| 183 |
{'name': 'intField', 'type': 'int'}, |
| 184 |
{'name': 'doubleField', 'type': 'double'}, |
| 185 |
{'name': 'stringField', 'type': 'string'}, |
| 186 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 187 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 188 |
] |
| 189 |
} |
| 190 |
await client.register_schema(_SUBJECT, Schema(json.dumps(schema), 'AVRO')) |
| 191 |
|
| 192 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 193 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 194 |
obj_bytes = await ser(obj, ser_ctx) |
| 195 |
|
| 196 |
deser = await AsyncAvroDeserializer(client) |
| 197 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 198 |
assert obj == obj2 |
| 199 |
|
| 200 |
|
| 201 |
async def test_avro_serialize_nested(): |
| 202 |
conf = {'url': _BASE_URL} |
| 203 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 204 |
ser_conf = {'auto.register.schemas': True} |
| 205 |
nested = { |
| 206 |
'intField': 123, |
| 207 |
'doubleField': 45.67, |
| 208 |
'stringField': 'hi', |
| 209 |
'booleanField': True, |
| 210 |
'bytesField': b'foobar', |
| 211 |
} |
| 212 |
obj = { |
| 213 |
'nested': nested |
| 214 |
} |
| 215 |
schema = { |
| 216 |
'type': 'record', |
| 217 |
'name': 'test', |
| 218 |
'fields': [ |
| 219 |
{'name': 'nested', 'type': { |
| 220 |
'type': 'record', |
| 221 |
'name': 'nested', |
| 222 |
'fields': [ |
| 223 |
{'name': 'intField', 'type': 'int'}, |
| 224 |
{'name': 'doubleField', 'type': 'double'}, |
| 225 |
{'name': 'stringField', 'type': 'string'}, |
| 226 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 227 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 228 |
] |
| 229 |
}}, |
| 230 |
] |
| 231 |
} |
| 232 |
ser = await AsyncAvroSerializer(client, schema_str=json.dumps(schema), conf=ser_conf) |
| 233 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 234 |
obj_bytes = await ser(obj, ser_ctx) |
| 235 |
|
| 236 |
deser = await AsyncAvroDeserializer(client) |
| 237 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 238 |
assert obj == obj2 |
| 239 |
|
| 240 |
|
| 241 |
async def test_avro_serialize_references(): |
| 242 |
conf = {'url': _BASE_URL} |
| 243 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 244 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 245 |
|
| 246 |
referenced = { |
| 247 |
'intField': 123, |
| 248 |
'doubleField': 45.67, |
| 249 |
'stringField': 'hi', |
| 250 |
'booleanField': True, |
| 251 |
'bytesField': b'foobar', |
| 252 |
} |
| 253 |
obj = { |
| 254 |
'refField': referenced |
| 255 |
} |
| 256 |
ref_schema = { |
| 257 |
'type': 'record', |
| 258 |
'name': 'ref', |
| 259 |
'fields': [ |
| 260 |
{'name': 'intField', 'type': 'int'}, |
| 261 |
{'name': 'doubleField', 'type': 'double'}, |
| 262 |
{'name': 'stringField', 'type': 'string'}, |
| 263 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 264 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 265 |
] |
| 266 |
} |
| 267 |
await client.register_schema('ref', Schema(json.dumps(ref_schema))) |
| 268 |
schema = { |
| 269 |
'type': 'record', |
| 270 |
'name': 'test', |
| 271 |
'fields': [ |
| 272 |
{'name': 'refField', 'type': 'ref'}, |
| 273 |
] |
| 274 |
} |
| 275 |
refs = [SchemaReference('ref', 'ref', 1)] |
| 276 |
await client.register_schema(_SUBJECT, Schema(json.dumps(schema), 'AVRO', refs)) |
| 277 |
|
| 278 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 279 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 280 |
obj_bytes = await ser(obj, ser_ctx) |
| 281 |
|
| 282 |
deser = await AsyncAvroDeserializer(client) |
| 283 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 284 |
assert obj == obj2 |
| 285 |
|
| 286 |
|
| 287 |
async def test_avro_serialize_union(): |
| 288 |
conf = {'url': _BASE_URL} |
| 289 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 290 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 291 |
|
| 292 |
obj = { |
| 293 |
'First': {'stringField': 'hi'}, |
| 294 |
'Second': {'stringField': 'hi'}, |
| 295 |
} |
| 296 |
schema = ['null', { |
| 297 |
'type': 'record', |
| 298 |
'name': 'A', |
| 299 |
'namespace': 'test', |
| 300 |
'fields': [ |
| 301 |
{'name': 'First', 'type': {'type': 'record', 'name': 'B', 'fields': [ |
| 302 |
{'name': 'stringField', 'type': 'string'}, |
| 303 |
]}}, |
| 304 |
{'name': 'Second', 'type': 'B'} |
| 305 |
] |
| 306 |
}] |
| 307 |
await client.register_schema(_SUBJECT, Schema(json.dumps(schema), 'AVRO')) |
| 308 |
|
| 309 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 310 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 311 |
obj_bytes = await ser(obj, ser_ctx) |
| 312 |
|
| 313 |
deser = await AsyncAvroDeserializer(client) |
| 314 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 315 |
assert obj == obj2 |
| 316 |
|
| 317 |
|
| 318 |
async def test_avro_serialize_union_with_record_references(): |
| 319 |
conf = {'url': _BASE_URL} |
| 320 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 321 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 322 |
|
| 323 |
obj = { |
| 324 |
'First': {'stringField': 'hi'}, |
| 325 |
'Second': {'stringField': 'hi'}, |
| 326 |
} |
| 327 |
ref_schema = { |
| 328 |
'type': 'record', |
| 329 |
'namespace': 'test', |
| 330 |
'name': 'B', |
| 331 |
'fields': [ |
| 332 |
{'name': 'stringField', 'type': 'string'}, |
| 333 |
] |
| 334 |
} |
| 335 |
await client.register_schema('ref', Schema(json.dumps(ref_schema))) |
| 336 |
schema = ['null', { |
| 337 |
'type': 'record', |
| 338 |
'name': 'A', |
| 339 |
'namespace': 'test', |
| 340 |
'fields': [ |
| 341 |
{'name': 'First', 'type': 'B'}, |
| 342 |
{'name': 'Second', 'type': 'B'} |
| 343 |
] |
| 344 |
}] |
| 345 |
refs = [SchemaReference('test.B', 'ref', 1)] |
| 346 |
await client.register_schema(_SUBJECT, Schema(json.dumps(schema), 'AVRO', refs)) |
| 347 |
|
| 348 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 349 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 350 |
obj_bytes = await ser(obj, ser_ctx) |
| 351 |
|
| 352 |
deser = await AsyncAvroDeserializer(client) |
| 353 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 354 |
assert obj == obj2 |
| 355 |
|
| 356 |
|
| 357 |
async def test_avro_serialize_union_with_references(): |
| 358 |
conf = {'url': _BASE_URL} |
| 359 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 360 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 361 |
|
| 362 |
obj = { |
| 363 |
'intField': 123, |
| 364 |
'doubleField': 45.67, |
| 365 |
'stringField': 'hi', |
| 366 |
'booleanField': True, |
| 367 |
'bytesField': b'foobar', |
| 368 |
} |
| 369 |
ref_schema = { |
| 370 |
'type': 'record', |
| 371 |
'name': 'ref', |
| 372 |
'fields': [ |
| 373 |
{'name': 'intField', 'type': 'int'}, |
| 374 |
{'name': 'doubleField', 'type': 'double'}, |
| 375 |
{'name': 'stringField', 'type': 'string'}, |
| 376 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 377 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 378 |
] |
| 379 |
} |
| 380 |
await client.register_schema('ref', Schema(json.dumps(ref_schema))) |
| 381 |
ref2_schema = { |
| 382 |
'type': 'record', |
| 383 |
'name': 'ref2', |
| 384 |
'fields': [ |
| 385 |
{'name': 'otherField', 'type': 'string'} |
| 386 |
] |
| 387 |
} |
| 388 |
await client.register_schema('ref2', Schema(json.dumps(ref2_schema))) |
| 389 |
schema = ['ref', 'ref2'] |
| 390 |
refs = [SchemaReference('ref', 'ref', 1), SchemaReference('ref2', 'ref2', 1)] |
| 391 |
await client.register_schema(_SUBJECT, Schema(json.dumps(schema), 'AVRO', refs)) |
| 392 |
|
| 393 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 394 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 395 |
obj_bytes = await ser(obj, ser_ctx) |
| 396 |
|
| 397 |
deser = await AsyncAvroDeserializer(client) |
| 398 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 399 |
assert obj == obj2 |
| 400 |
|
| 401 |
|
| 402 |
async def test_avro_schema_evolution(): |
| 403 |
conf = {'url': _BASE_URL} |
| 404 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 405 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 406 |
|
| 407 |
evolution1 = { |
| 408 |
"name": "SchemaEvolution", |
| 409 |
"type": "record", |
| 410 |
"fields": [ |
| 411 |
{ |
| 412 |
"name": "fieldToDelete", |
| 413 |
"type": "string" |
| 414 |
} |
| 415 |
] |
| 416 |
} |
| 417 |
evolution2 = { |
| 418 |
"name": "SchemaEvolution", |
| 419 |
"type": "record", |
| 420 |
"fields": [ |
| 421 |
{ |
| 422 |
"name": "newOptionalField", |
| 423 |
"type": ["string", "null"], |
| 424 |
"default": "optional" |
| 425 |
} |
| 426 |
] |
| 427 |
} |
| 428 |
obj = { |
| 429 |
'fieldToDelete': 'bye', |
| 430 |
} |
| 431 |
|
| 432 |
await client.register_schema(_SUBJECT, Schema(json.dumps(evolution1))) |
| 433 |
|
| 434 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 435 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 436 |
obj_bytes = await ser(obj, ser_ctx) |
| 437 |
|
| 438 |
await client.register_schema(_SUBJECT, Schema(json.dumps(evolution2))) |
| 439 |
|
| 440 |
client.clear_latest_caches() |
| 441 |
deser = await AsyncAvroDeserializer(client, conf={'use.latest.version': True}) |
| 442 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 443 |
assert obj2.get('fieldToDelete') is None |
| 444 |
assert obj2.get('newOptionalField') == 'optional' |
| 445 |
|
| 446 |
|
| 447 |
async def test_avro_cel_condition(): |
| 448 |
conf = {'url': _BASE_URL} |
| 449 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 450 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 451 |
schema = { |
| 452 |
'type': 'record', |
| 453 |
'name': 'test', |
| 454 |
'fields': [ |
| 455 |
{'name': 'intField', 'type': 'int'}, |
| 456 |
{'name': 'doubleField', 'type': 'double'}, |
| 457 |
{'name': 'stringField', 'type': 'string'}, |
| 458 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 459 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 460 |
] |
| 461 |
} |
| 462 |
|
| 463 |
rule = Rule( |
| 464 |
"test-cel", |
| 465 |
"", |
| 466 |
RuleKind.CONDITION, |
| 467 |
RuleMode.WRITE, |
| 468 |
"CEL", |
| 469 |
None, |
| 470 |
None, |
| 471 |
"message.stringField == 'hi'", |
| 472 |
None, |
| 473 |
None, |
| 474 |
False |
| 475 |
) |
| 476 |
await client.register_schema(_SUBJECT, Schema( |
| 477 |
json.dumps(schema), |
| 478 |
"AVRO", |
| 479 |
[], |
| 480 |
None, |
| 481 |
RuleSet(None, [rule]) |
| 482 |
)) |
| 483 |
|
| 484 |
obj = { |
| 485 |
'intField': 123, |
| 486 |
'doubleField': 45.67, |
| 487 |
'stringField': 'hi', |
| 488 |
'booleanField': True, |
| 489 |
'bytesField': b'foobar', |
| 490 |
} |
| 491 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 492 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 493 |
obj_bytes = await ser(obj, ser_ctx) |
| 494 |
|
| 495 |
deser = await AsyncAvroDeserializer(client) |
| 496 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 497 |
assert obj == obj2 |
| 498 |
|
| 499 |
|
| 500 |
async def test_avro_cel_condition_logical_type(): |
| 501 |
conf = {'url': _BASE_URL} |
| 502 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 503 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 504 |
schema = { |
| 505 |
'type': 'record', |
| 506 |
'name': 'test', |
| 507 |
'fields': [ |
| 508 |
{'name': 'intField', 'type': 'int'}, |
| 509 |
{'name': 'doubleField', 'type': 'double'}, |
| 510 |
{'name': 'stringField', |
| 511 |
'type': { |
| 512 |
'type': 'string', |
| 513 |
'logicalType': 'uuid' |
| 514 |
} |
| 515 |
}, |
| 516 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 517 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 518 |
] |
| 519 |
} |
| 520 |
|
| 521 |
uuid = "550e8400-e29b-41d4-a716-446655440000" |
| 522 |
|
| 523 |
rule = Rule( |
| 524 |
"test-cel", |
| 525 |
"", |
| 526 |
RuleKind.CONDITION, |
| 527 |
RuleMode.WRITE, |
| 528 |
"CEL", |
| 529 |
None, |
| 530 |
None, |
| 531 |
"message.stringField == '" + uuid + "'", |
| 532 |
None, |
| 533 |
None, |
| 534 |
False |
| 535 |
) |
| 536 |
await client.register_schema(_SUBJECT, Schema( |
| 537 |
json.dumps(schema), |
| 538 |
"AVRO", |
| 539 |
[], |
| 540 |
None, |
| 541 |
RuleSet(None, [rule]) |
| 542 |
)) |
| 543 |
|
| 544 |
obj = { |
| 545 |
'intField': 123, |
| 546 |
'doubleField': 45.67, |
| 547 |
'stringField': UUID(uuid), |
| 548 |
'booleanField': True, |
| 549 |
'bytesField': b'foobar', |
| 550 |
} |
| 551 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 552 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 553 |
obj_bytes = await ser(obj, ser_ctx) |
| 554 |
|
| 555 |
deser = await AsyncAvroDeserializer(client) |
| 556 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 557 |
assert obj == obj2 |
| 558 |
|
| 559 |
|
| 560 |
async def test_avro_cel_condition_fail(): |
| 561 |
conf = {'url': _BASE_URL} |
| 562 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 563 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 564 |
schema = { |
| 565 |
'type': 'record', |
| 566 |
'name': 'test', |
| 567 |
'fields': [ |
| 568 |
{'name': 'intField', 'type': 'int'}, |
| 569 |
{'name': 'doubleField', 'type': 'double'}, |
| 570 |
{'name': 'stringField', 'type': 'string'}, |
| 571 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 572 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 573 |
] |
| 574 |
} |
| 575 |
|
| 576 |
rule = Rule( |
| 577 |
"test-cel", |
| 578 |
"", |
| 579 |
RuleKind.CONDITION, |
| 580 |
RuleMode.WRITE, |
| 581 |
"CEL", |
| 582 |
None, |
| 583 |
None, |
| 584 |
"message.stringField != 'hi'", |
| 585 |
None, |
| 586 |
None, |
| 587 |
False |
| 588 |
) |
| 589 |
await client.register_schema(_SUBJECT, Schema( |
| 590 |
json.dumps(schema), |
| 591 |
"AVRO", |
| 592 |
[], |
| 593 |
None, |
| 594 |
RuleSet(None, [rule]) |
| 595 |
)) |
| 596 |
|
| 597 |
obj = { |
| 598 |
'intField': 123, |
| 599 |
'doubleField': 45.67, |
| 600 |
'stringField': 'hi', |
| 601 |
'booleanField': True, |
| 602 |
'bytesField': b'foobar', |
| 603 |
} |
| 604 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 605 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 606 |
with pytest.raises(SerializationError) as e: |
| 607 |
await ser(obj, ser_ctx) |
| 608 |
assert isinstance(e.value.__cause__, RuleConditionError) |
| 609 |
|
| 610 |
|
| 611 |
async def test_avro_cel_condition_ignore_fail(): |
| 612 |
conf = {'url': _BASE_URL} |
| 613 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 614 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 615 |
schema = { |
| 616 |
'type': 'record', |
| 617 |
'name': 'test', |
| 618 |
'fields': [ |
| 619 |
{'name': 'intField', 'type': 'int'}, |
| 620 |
{'name': 'doubleField', 'type': 'double'}, |
| 621 |
{'name': 'stringField', 'type': 'string'}, |
| 622 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 623 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 624 |
] |
| 625 |
} |
| 626 |
|
| 627 |
rule = Rule( |
| 628 |
"test-cel", |
| 629 |
"", |
| 630 |
RuleKind.CONDITION, |
| 631 |
RuleMode.WRITE, |
| 632 |
"CEL", |
| 633 |
None, |
| 634 |
None, |
| 635 |
"message.stringField != 'hi'", |
| 636 |
None, |
| 637 |
"NONE", |
| 638 |
False |
| 639 |
) |
| 640 |
await client.register_schema(_SUBJECT, Schema( |
| 641 |
json.dumps(schema), |
| 642 |
"AVRO", |
| 643 |
[], |
| 644 |
None, |
| 645 |
RuleSet(None, [rule]) |
| 646 |
)) |
| 647 |
|
| 648 |
obj = { |
| 649 |
'intField': 123, |
| 650 |
'doubleField': 45.67, |
| 651 |
'stringField': 'hi', |
| 652 |
'booleanField': True, |
| 653 |
'bytesField': b'foobar', |
| 654 |
} |
| 655 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 656 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 657 |
obj_bytes = await ser(obj, ser_ctx) |
| 658 |
|
| 659 |
deser = await AsyncAvroDeserializer(client) |
| 660 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 661 |
assert obj == obj2 |
| 662 |
|
| 663 |
|
| 664 |
async def test_avro_cel_field_transform(): |
| 665 |
conf = {'url': _BASE_URL} |
| 666 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 667 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 668 |
schema = { |
| 669 |
'type': 'record', |
| 670 |
'name': 'test', |
| 671 |
'fields': [ |
| 672 |
{'name': 'intField', 'type': 'int'}, |
| 673 |
{'name': 'doubleField', 'type': 'double'}, |
| 674 |
{'name': 'stringField', 'type': 'string'}, |
| 675 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 676 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 677 |
] |
| 678 |
} |
| 679 |
|
| 680 |
rule = Rule( |
| 681 |
"test-cel", |
| 682 |
"", |
| 683 |
RuleKind.TRANSFORM, |
| 684 |
RuleMode.WRITE, |
| 685 |
"CEL_FIELD", |
| 686 |
None, |
| 687 |
None, |
| 688 |
"name == 'stringField' ; value + '-suffix'", |
| 689 |
None, |
| 690 |
None, |
| 691 |
False |
| 692 |
) |
| 693 |
await client.register_schema(_SUBJECT, Schema( |
| 694 |
json.dumps(schema), |
| 695 |
"AVRO", |
| 696 |
[], |
| 697 |
None, |
| 698 |
RuleSet(None, [rule]) |
| 699 |
)) |
| 700 |
|
| 701 |
obj = { |
| 702 |
'intField': 123, |
| 703 |
'doubleField': 45.67, |
| 704 |
'stringField': 'hi', |
| 705 |
'booleanField': True, |
| 706 |
'bytesField': b'foobar', |
| 707 |
} |
| 708 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 709 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 710 |
obj_bytes = await ser(obj, ser_ctx) |
| 711 |
|
| 712 |
obj2 = { |
| 713 |
'intField': 123, |
| 714 |
'doubleField': 45.67, |
| 715 |
'stringField': 'hi-suffix', |
| 716 |
'booleanField': True, |
| 717 |
'bytesField': b'foobar', |
| 718 |
} |
| 719 |
deser = await AsyncAvroDeserializer(client) |
| 720 |
newobj = await deser(obj_bytes, ser_ctx) |
| 721 |
assert obj2 == newobj |
| 722 |
|
| 723 |
|
| 724 |
async def test_avro_cel_field_transform_missing_prop(): |
| 725 |
conf = {'url': _BASE_URL} |
| 726 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 727 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 728 |
schema = { |
| 729 |
'type': 'record', |
| 730 |
'name': 'test', |
| 731 |
'fields': [ |
| 732 |
{'name': 'intField', 'type': 'int'}, |
| 733 |
{'name': 'doubleField', 'type': 'double'}, |
| 734 |
{'name': 'stringField', 'type': 'string'}, |
| 735 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 736 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 737 |
{'name': 'missing', 'type': ['null', 'string'], 'default': None}, |
| 738 |
] |
| 739 |
} |
| 740 |
|
| 741 |
rule = Rule( |
| 742 |
"test-cel", |
| 743 |
"", |
| 744 |
RuleKind.TRANSFORM, |
| 745 |
RuleMode.WRITEREAD, |
| 746 |
"CEL_FIELD", |
| 747 |
None, |
| 748 |
None, |
| 749 |
"name == 'stringField' ; value + '-suffix'", |
| 750 |
None, |
| 751 |
None, |
| 752 |
False |
| 753 |
) |
| 754 |
await client.register_schema(_SUBJECT, Schema( |
| 755 |
json.dumps(schema), |
| 756 |
"AVRO", |
| 757 |
[], |
| 758 |
None, |
| 759 |
RuleSet(None, [rule]) |
| 760 |
)) |
| 761 |
|
| 762 |
obj = { |
| 763 |
'intField': 123, |
| 764 |
'doubleField': 45.67, |
| 765 |
'stringField': 'hi', |
| 766 |
'booleanField': True, |
| 767 |
'bytesField': b'foobar', |
| 768 |
} |
| 769 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 770 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 771 |
obj_bytes = await ser(obj, ser_ctx) |
| 772 |
|
| 773 |
obj2 = { |
| 774 |
'intField': 123, |
| 775 |
'doubleField': 45.67, |
| 776 |
'stringField': 'hi-suffix-suffix', |
| 777 |
'booleanField': True, |
| 778 |
'bytesField': b'foobar', |
| 779 |
'missing': None, |
| 780 |
} |
| 781 |
deser = await AsyncAvroDeserializer(client) |
| 782 |
newobj = await deser(obj_bytes, ser_ctx) |
| 783 |
assert obj2 == newobj |
| 784 |
|
| 785 |
|
| 786 |
async def test_avro_cel_field_transform_disable(): |
| 787 |
conf = {'url': _BASE_URL} |
| 788 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 789 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 790 |
schema = { |
| 791 |
'type': 'record', |
| 792 |
'name': 'test', |
| 793 |
'fields': [ |
| 794 |
{'name': 'intField', 'type': 'int'}, |
| 795 |
{'name': 'doubleField', 'type': 'double'}, |
| 796 |
{'name': 'stringField', 'type': 'string'}, |
| 797 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 798 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 799 |
] |
| 800 |
} |
| 801 |
|
| 802 |
rule = Rule( |
| 803 |
"test-cel", |
| 804 |
"", |
| 805 |
RuleKind.TRANSFORM, |
| 806 |
RuleMode.WRITE, |
| 807 |
"CEL_FIELD", |
| 808 |
None, |
| 809 |
None, |
| 810 |
"name == 'stringField' ; value + '-suffix'", |
| 811 |
None, |
| 812 |
None, |
| 813 |
False |
| 814 |
) |
| 815 |
await client.register_schema(_SUBJECT, Schema( |
| 816 |
json.dumps(schema), |
| 817 |
"AVRO", |
| 818 |
[], |
| 819 |
None, |
| 820 |
RuleSet(None, [rule]) |
| 821 |
)) |
| 822 |
|
| 823 |
obj = { |
| 824 |
'intField': 123, |
| 825 |
'doubleField': 45.67, |
| 826 |
'stringField': 'hi', |
| 827 |
'booleanField': True, |
| 828 |
'bytesField': b'foobar', |
| 829 |
} |
| 830 |
|
| 831 |
registry = RuleRegistry() |
| 832 |
registry.register_rule_executor(CelFieldExecutor()) |
| 833 |
registry.register_override(RuleOverride("CEL_FIELD", None, None, True)) |
| 834 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_registry=registry) |
| 835 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 836 |
obj_bytes = await ser(obj, ser_ctx) |
| 837 |
|
| 838 |
deser = await AsyncAvroDeserializer(client) |
| 839 |
newobj = await deser(obj_bytes, ser_ctx) |
| 840 |
assert "hi" == newobj['stringField'] |
| 841 |
|
| 842 |
|
| 843 |
async def test_avro_cel_field_transform_complex(): |
| 844 |
conf = {'url': _BASE_URL} |
| 845 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 846 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 847 |
schema = { |
| 848 |
'type': 'record', |
| 849 |
'name': 'test', |
| 850 |
'fields': [ |
| 851 |
{'name': 'arrayField', 'type': |
| 852 |
{'type': 'array', 'items': 'string'} |
| 853 |
}, |
| 854 |
{'name': 'mapField', 'type': |
| 855 |
{'type': 'map', 'values': 'string'} |
| 856 |
}, |
| 857 |
{'name': 'unionField', 'type': ['null', 'string'], 'confluent:tags': ['PII']} |
| 858 |
] |
| 859 |
} |
| 860 |
|
| 861 |
rule = Rule( |
| 862 |
"test-cel", |
| 863 |
"", |
| 864 |
RuleKind.TRANSFORM, |
| 865 |
RuleMode.WRITE, |
| 866 |
"CEL_FIELD", |
| 867 |
None, |
| 868 |
None, |
| 869 |
"typeName == 'STRING' ; value + '-suffix'", |
| 870 |
None, |
| 871 |
None, |
| 872 |
False |
| 873 |
) |
| 874 |
await client.register_schema(_SUBJECT, Schema( |
| 875 |
json.dumps(schema), |
| 876 |
"AVRO", |
| 877 |
[], |
| 878 |
None, |
| 879 |
RuleSet(None, [rule]) |
| 880 |
)) |
| 881 |
|
| 882 |
obj = { |
| 883 |
'arrayField': ['hello'], |
| 884 |
'mapField': {'key': 'world'}, |
| 885 |
'unionField': 'bye', |
| 886 |
} |
| 887 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 888 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 889 |
obj_bytes = await ser(obj, ser_ctx) |
| 890 |
|
| 891 |
obj2 = { |
| 892 |
'arrayField': ['hello-suffix'], |
| 893 |
'mapField': {'key': 'world-suffix'}, |
| 894 |
'unionField': 'bye-suffix', |
| 895 |
} |
| 896 |
deser = await AsyncAvroDeserializer(client) |
| 897 |
newobj = await deser(obj_bytes, ser_ctx) |
| 898 |
assert obj2 == newobj |
| 899 |
|
| 900 |
|
| 901 |
async def test_avro_cel_field_transform_complex_with_none(): |
| 902 |
conf = {'url': _BASE_URL} |
| 903 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 904 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 905 |
schema = { |
| 906 |
'type': 'record', |
| 907 |
'name': 'test', |
| 908 |
'fields': [ |
| 909 |
{'name': 'arrayField', 'type': |
| 910 |
{'type': 'array', 'items': 'string'} |
| 911 |
}, |
| 912 |
{'name': 'mapField', 'type': |
| 913 |
{'type': 'map', 'values': 'string'} |
| 914 |
}, |
| 915 |
{'name': 'unionField', 'type': ['null', 'string'], 'confluent:tags': ['PII']} |
| 916 |
] |
| 917 |
} |
| 918 |
|
| 919 |
rule = Rule( |
| 920 |
"test-cel", |
| 921 |
"", |
| 922 |
RuleKind.TRANSFORM, |
| 923 |
RuleMode.WRITE, |
| 924 |
"CEL_FIELD", |
| 925 |
None, |
| 926 |
None, |
| 927 |
"typeName == 'STRING' ; value + '-suffix'", |
| 928 |
None, |
| 929 |
None, |
| 930 |
False |
| 931 |
) |
| 932 |
await client.register_schema(_SUBJECT, Schema( |
| 933 |
json.dumps(schema), |
| 934 |
"AVRO", |
| 935 |
[], |
| 936 |
None, |
| 937 |
RuleSet(None, [rule]) |
| 938 |
)) |
| 939 |
|
| 940 |
obj = { |
| 941 |
'arrayField': ['hello'], |
| 942 |
'mapField': {'key': 'world'}, |
| 943 |
'unionField': None, |
| 944 |
} |
| 945 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 946 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 947 |
obj_bytes = await ser(obj, ser_ctx) |
| 948 |
|
| 949 |
obj2 = { |
| 950 |
'arrayField': ['hello-suffix'], |
| 951 |
'mapField': {'key': 'world-suffix'}, |
| 952 |
'unionField': None, |
| 953 |
} |
| 954 |
deser = await AsyncAvroDeserializer(client) |
| 955 |
newobj = await deser(obj_bytes, ser_ctx) |
| 956 |
assert obj2 == newobj |
| 957 |
|
| 958 |
|
| 959 |
async def test_avro_cel_field_transform_complex_nested(): |
| 960 |
conf = {'url': _BASE_URL} |
| 961 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 962 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 963 |
schema = { |
| 964 |
'type': 'record', |
| 965 |
'name': 'UnionTest', |
| 966 |
'namespace': 'test', |
| 967 |
'fields': [ |
| 968 |
{ |
| 969 |
'name': 'emails', |
| 970 |
'type': [ |
| 971 |
'null', |
| 972 |
{ |
| 973 |
'type': 'array', |
| 974 |
'items': { |
| 975 |
'type': 'record', |
| 976 |
'name': 'Email', |
| 977 |
'fields': [ |
| 978 |
{ |
| 979 |
'name': 'email', |
| 980 |
'type': [ |
| 981 |
'null', |
| 982 |
'string' |
| 983 |
], |
| 984 |
'doc': 'Email address', |
| 985 |
'confluent:tags': [ |
| 986 |
'PII' |
| 987 |
] |
| 988 |
} |
| 989 |
] |
| 990 |
} |
| 991 |
} |
| 992 |
], |
| 993 |
'doc': 'Communication Email', |
| 994 |
} |
| 995 |
] |
| 996 |
} |
| 997 |
|
| 998 |
rule = Rule( |
| 999 |
"test-cel", |
| 1000 |
"", |
| 1001 |
RuleKind.TRANSFORM, |
| 1002 |
RuleMode.WRITE, |
| 1003 |
"CEL_FIELD", |
| 1004 |
None, |
| 1005 |
None, |
| 1006 |
"typeName == 'STRING' ; value + '-suffix'", |
| 1007 |
None, |
| 1008 |
None, |
| 1009 |
False |
| 1010 |
) |
| 1011 |
await client.register_schema(_SUBJECT, Schema( |
| 1012 |
json.dumps(schema), |
| 1013 |
"AVRO", |
| 1014 |
[], |
| 1015 |
None, |
| 1016 |
RuleSet(None, [rule]) |
| 1017 |
)) |
| 1018 |
|
| 1019 |
obj = { |
| 1020 |
'emails': [{'email': '[email protected]'}] |
| 1021 |
} |
| 1022 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 1023 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1024 |
obj_bytes = await ser(obj, ser_ctx) |
| 1025 |
|
| 1026 |
obj2 = { |
| 1027 |
'emails': [{'email': '[email protected]'}] |
| 1028 |
} |
| 1029 |
deser = await AsyncAvroDeserializer(client) |
| 1030 |
newobj = await deser(obj_bytes, ser_ctx) |
| 1031 |
assert obj2 == newobj |
| 1032 |
|
| 1033 |
|
| 1034 |
async def test_avro_cel_field_condition(): |
| 1035 |
conf = {'url': _BASE_URL} |
| 1036 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1037 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1038 |
schema = { |
| 1039 |
'type': 'record', |
| 1040 |
'name': 'test', |
| 1041 |
'fields': [ |
| 1042 |
{'name': 'intField', 'type': 'int'}, |
| 1043 |
{'name': 'doubleField', 'type': 'double'}, |
| 1044 |
{'name': 'stringField', 'type': 'string'}, |
| 1045 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1046 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 1047 |
] |
| 1048 |
} |
| 1049 |
|
| 1050 |
rule = Rule( |
| 1051 |
"test-cel", |
| 1052 |
"", |
| 1053 |
RuleKind.CONDITION, |
| 1054 |
RuleMode.WRITE, |
| 1055 |
"CEL_FIELD", |
| 1056 |
None, |
| 1057 |
None, |
| 1058 |
"name == 'stringField' ; value == 'hi'", |
| 1059 |
None, |
| 1060 |
None, |
| 1061 |
False |
| 1062 |
) |
| 1063 |
await client.register_schema(_SUBJECT, Schema( |
| 1064 |
json.dumps(schema), |
| 1065 |
"AVRO", |
| 1066 |
[], |
| 1067 |
None, |
| 1068 |
RuleSet(None, [rule]) |
| 1069 |
)) |
| 1070 |
|
| 1071 |
obj = { |
| 1072 |
'intField': 123, |
| 1073 |
'doubleField': 45.67, |
| 1074 |
'stringField': 'hi', |
| 1075 |
'booleanField': True, |
| 1076 |
'bytesField': b'foobar', |
| 1077 |
} |
| 1078 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 1079 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1080 |
obj_bytes = await ser(obj, ser_ctx) |
| 1081 |
|
| 1082 |
deser = await AsyncAvroDeserializer(client) |
| 1083 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1084 |
assert obj == obj2 |
| 1085 |
|
| 1086 |
|
| 1087 |
async def test_avro_cel_field_condition_fail(): |
| 1088 |
conf = {'url': _BASE_URL} |
| 1089 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1090 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1091 |
schema = { |
| 1092 |
'type': 'record', |
| 1093 |
'name': 'test', |
| 1094 |
'fields': [ |
| 1095 |
{'name': 'intField', 'type': 'int'}, |
| 1096 |
{'name': 'doubleField', 'type': 'double'}, |
| 1097 |
{'name': 'stringField', 'type': 'string'}, |
| 1098 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1099 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 1100 |
] |
| 1101 |
} |
| 1102 |
|
| 1103 |
rule = Rule( |
| 1104 |
"test-cel", |
| 1105 |
"", |
| 1106 |
RuleKind.CONDITION, |
| 1107 |
RuleMode.WRITE, |
| 1108 |
"CEL_FIELD", |
| 1109 |
None, |
| 1110 |
None, |
| 1111 |
"name == 'stringField' ; value == 'bye'", |
| 1112 |
None, |
| 1113 |
None, |
| 1114 |
False |
| 1115 |
) |
| 1116 |
await client.register_schema(_SUBJECT, Schema( |
| 1117 |
json.dumps(schema), |
| 1118 |
"AVRO", |
| 1119 |
[], |
| 1120 |
None, |
| 1121 |
RuleSet(None, [rule]) |
| 1122 |
)) |
| 1123 |
|
| 1124 |
obj = { |
| 1125 |
'intField': 123, |
| 1126 |
'doubleField': 45.67, |
| 1127 |
'stringField': 'hi', |
| 1128 |
'booleanField': True, |
| 1129 |
'bytesField': b'foobar', |
| 1130 |
} |
| 1131 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 1132 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1133 |
with pytest.raises(SerializationError) as e: |
| 1134 |
await ser(obj, ser_ctx) |
| 1135 |
assert isinstance(e.value.__cause__, RuleConditionError) |
| 1136 |
|
| 1137 |
|
| 1138 |
async def test_avro_encryption(): |
| 1139 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1140 |
|
| 1141 |
conf = {'url': _BASE_URL} |
| 1142 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1143 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1144 |
rule_conf = {'secret': 'mysecret'} |
| 1145 |
schema = { |
| 1146 |
'type': 'record', |
| 1147 |
'name': 'test', |
| 1148 |
'fields': [ |
| 1149 |
{'name': 'intField', 'type': 'int'}, |
| 1150 |
{'name': 'doubleField', 'type': 'double'}, |
| 1151 |
{'name': 'stringField', 'type': 'string', 'confluent:tags': ['PII']}, |
| 1152 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1153 |
{'name': 'bytesField', 'type': 'bytes', 'confluent:tags': ['PII']}, |
| 1154 |
] |
| 1155 |
} |
| 1156 |
|
| 1157 |
rule = Rule( |
| 1158 |
"test-encrypt", |
| 1159 |
"", |
| 1160 |
RuleKind.TRANSFORM, |
| 1161 |
RuleMode.WRITEREAD, |
| 1162 |
"ENCRYPT", |
| 1163 |
["PII"], |
| 1164 |
RuleParams({ |
| 1165 |
"encrypt.kek.name": "kek1", |
| 1166 |
"encrypt.kms.type": "local-kms", |
| 1167 |
"encrypt.kms.key.id": "mykey" |
| 1168 |
}), |
| 1169 |
None, |
| 1170 |
None, |
| 1171 |
"ERROR,NONE", |
| 1172 |
False |
| 1173 |
) |
| 1174 |
await client.register_schema(_SUBJECT, Schema( |
| 1175 |
json.dumps(schema), |
| 1176 |
"AVRO", |
| 1177 |
[], |
| 1178 |
None, |
| 1179 |
RuleSet(None, [rule]) |
| 1180 |
)) |
| 1181 |
|
| 1182 |
obj = { |
| 1183 |
'intField': 123, |
| 1184 |
'doubleField': 45.67, |
| 1185 |
'stringField': 'hi', |
| 1186 |
'booleanField': True, |
| 1187 |
'bytesField': b'foobar', |
| 1188 |
} |
| 1189 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1190 |
dek_client = executor.executor.client |
| 1191 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1192 |
obj_bytes = await ser(obj, ser_ctx) |
| 1193 |
|
| 1194 |
|
| 1195 |
assert obj['stringField'] != 'hi' |
| 1196 |
obj['stringField'] = 'hi' |
| 1197 |
obj['bytesField'] = b'foobar' |
| 1198 |
|
| 1199 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1200 |
executor.executor.client = dek_client |
| 1201 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1202 |
assert obj == obj2 |
| 1203 |
|
| 1204 |
|
| 1205 |
async def test_avro_payload_encryption(): |
| 1206 |
executor = EncryptionExecutor.register_with_clock(FakeClock()) |
| 1207 |
|
| 1208 |
conf = {'url': _BASE_URL} |
| 1209 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1210 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1211 |
rule_conf = {'secret': 'mysecret'} |
| 1212 |
schema = { |
| 1213 |
'type': 'record', |
| 1214 |
'name': 'test', |
| 1215 |
'fields': [ |
| 1216 |
{'name': 'intField', 'type': 'int'}, |
| 1217 |
{'name': 'doubleField', 'type': 'double'}, |
| 1218 |
{'name': 'stringField', 'type': 'string', 'confluent:tags': ['PII']}, |
| 1219 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1220 |
{'name': 'bytesField', 'type': 'bytes', 'confluent:tags': ['PII']}, |
| 1221 |
] |
| 1222 |
} |
| 1223 |
|
| 1224 |
rule = Rule( |
| 1225 |
"test-encrypt", |
| 1226 |
"", |
| 1227 |
RuleKind.TRANSFORM, |
| 1228 |
RuleMode.WRITEREAD, |
| 1229 |
"ENCRYPT_PAYLOAD", |
| 1230 |
None, |
| 1231 |
RuleParams({ |
| 1232 |
"encrypt.kek.name": "kek1", |
| 1233 |
"encrypt.kms.type": "local-kms", |
| 1234 |
"encrypt.kms.key.id": "mykey" |
| 1235 |
}), |
| 1236 |
None, |
| 1237 |
None, |
| 1238 |
"ERROR,NONE", |
| 1239 |
False |
| 1240 |
) |
| 1241 |
await client.register_schema(_SUBJECT, Schema( |
| 1242 |
json.dumps(schema), |
| 1243 |
"AVRO", |
| 1244 |
[], |
| 1245 |
None, |
| 1246 |
RuleSet(None, None, [rule]) |
| 1247 |
)) |
| 1248 |
|
| 1249 |
obj = { |
| 1250 |
'intField': 123, |
| 1251 |
'doubleField': 45.67, |
| 1252 |
'stringField': 'hi', |
| 1253 |
'booleanField': True, |
| 1254 |
'bytesField': b'foobar', |
| 1255 |
} |
| 1256 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1257 |
dek_client = executor.client |
| 1258 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1259 |
obj_bytes = await ser(obj, ser_ctx) |
| 1260 |
|
| 1261 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1262 |
executor.client = dek_client |
| 1263 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1264 |
assert obj == obj2 |
| 1265 |
|
| 1266 |
|
| 1267 |
async def test_avro_encryption_alternate_keks(): |
| 1268 |
executor = EncryptionExecutor.register_with_clock(FakeClock()) |
| 1269 |
|
| 1270 |
conf = {'url': _BASE_URL} |
| 1271 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1272 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1273 |
rule_conf = {'secret': 'mysecret', 'encrypt.alternate.kms.key.ids': 'mykey2,mykey3'} |
| 1274 |
schema = { |
| 1275 |
'type': 'record', |
| 1276 |
'name': 'test', |
| 1277 |
'fields': [ |
| 1278 |
{'name': 'intField', 'type': 'int'}, |
| 1279 |
{'name': 'doubleField', 'type': 'double'}, |
| 1280 |
{'name': 'stringField', 'type': 'string', 'confluent:tags': ['PII']}, |
| 1281 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1282 |
{'name': 'bytesField', 'type': 'bytes', 'confluent:tags': ['PII']}, |
| 1283 |
] |
| 1284 |
} |
| 1285 |
|
| 1286 |
rule = Rule( |
| 1287 |
"test-encrypt", |
| 1288 |
"", |
| 1289 |
RuleKind.TRANSFORM, |
| 1290 |
RuleMode.WRITEREAD, |
| 1291 |
"ENCRYPT_PAYLOAD", |
| 1292 |
None, |
| 1293 |
RuleParams({ |
| 1294 |
"encrypt.kek.name": "kek1", |
| 1295 |
"encrypt.kms.type": "local-kms", |
| 1296 |
"encrypt.kms.key.id": "mykey" |
| 1297 |
}), |
| 1298 |
None, |
| 1299 |
None, |
| 1300 |
"ERROR,NONE", |
| 1301 |
False |
| 1302 |
) |
| 1303 |
await client.register_schema(_SUBJECT, Schema( |
| 1304 |
json.dumps(schema), |
| 1305 |
"AVRO", |
| 1306 |
[], |
| 1307 |
None, |
| 1308 |
RuleSet(None, None, [rule]) |
| 1309 |
)) |
| 1310 |
|
| 1311 |
obj = { |
| 1312 |
'intField': 123, |
| 1313 |
'doubleField': 45.67, |
| 1314 |
'stringField': 'hi', |
| 1315 |
'booleanField': True, |
| 1316 |
'bytesField': b'foobar', |
| 1317 |
} |
| 1318 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1319 |
dek_client = executor.client |
| 1320 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1321 |
obj_bytes = await ser(obj, ser_ctx) |
| 1322 |
|
| 1323 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1324 |
executor.client = dek_client |
| 1325 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1326 |
assert obj == obj2 |
| 1327 |
|
| 1328 |
|
| 1329 |
async def test_avro_encryption_deterministic(): |
| 1330 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1331 |
|
| 1332 |
conf = {'url': _BASE_URL} |
| 1333 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1334 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1335 |
rule_conf = {'secret': 'mysecret'} |
| 1336 |
schema = { |
| 1337 |
'type': 'record', |
| 1338 |
'name': 'test', |
| 1339 |
'fields': [ |
| 1340 |
{'name': 'intField', 'type': 'int'}, |
| 1341 |
{'name': 'doubleField', 'type': 'double'}, |
| 1342 |
{'name': 'stringField', 'type': 'string', 'confluent:tags': ['PII']}, |
| 1343 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1344 |
{'name': 'bytesField', 'type': 'bytes', 'confluent:tags': ['PII']}, |
| 1345 |
] |
| 1346 |
} |
| 1347 |
|
| 1348 |
rule = Rule( |
| 1349 |
"test-encrypt", |
| 1350 |
"", |
| 1351 |
RuleKind.TRANSFORM, |
| 1352 |
RuleMode.WRITEREAD, |
| 1353 |
"ENCRYPT", |
| 1354 |
["PII"], |
| 1355 |
RuleParams({ |
| 1356 |
"encrypt.kek.name": "kek1", |
| 1357 |
"encrypt.kms.type": "local-kms", |
| 1358 |
"encrypt.kms.key.id": "mykey", |
| 1359 |
"encrypt.dek.algorithm": "AES256_SIV" |
| 1360 |
}), |
| 1361 |
None, |
| 1362 |
None, |
| 1363 |
"ERROR,NONE", |
| 1364 |
False |
| 1365 |
) |
| 1366 |
await client.register_schema(_SUBJECT, Schema( |
| 1367 |
json.dumps(schema), |
| 1368 |
"AVRO", |
| 1369 |
[], |
| 1370 |
None, |
| 1371 |
RuleSet(None, [rule]) |
| 1372 |
)) |
| 1373 |
|
| 1374 |
obj = { |
| 1375 |
'intField': 123, |
| 1376 |
'doubleField': 45.67, |
| 1377 |
'stringField': 'hi', |
| 1378 |
'booleanField': True, |
| 1379 |
'bytesField': b'foobar', |
| 1380 |
} |
| 1381 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1382 |
dek_client = executor.executor.client |
| 1383 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1384 |
obj_bytes = await ser(obj, ser_ctx) |
| 1385 |
|
| 1386 |
|
| 1387 |
assert obj['stringField'] != 'hi' |
| 1388 |
obj['stringField'] = 'hi' |
| 1389 |
obj['bytesField'] = b'foobar' |
| 1390 |
|
| 1391 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1392 |
executor.executor.client = dek_client |
| 1393 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1394 |
assert obj == obj2 |
| 1395 |
|
| 1396 |
|
| 1397 |
async def test_avro_encryption_cel(): |
| 1398 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1399 |
|
| 1400 |
conf = {'url': _BASE_URL} |
| 1401 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1402 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1403 |
rule_conf = {'secret': 'mysecret'} |
| 1404 |
schema = { |
| 1405 |
'type': 'record', |
| 1406 |
'name': 'test', |
| 1407 |
'fields': [ |
| 1408 |
{'name': 'intField', 'type': 'int'}, |
| 1409 |
{'name': 'doubleField', 'type': 'double'}, |
| 1410 |
{'name': 'stringField', 'type': 'string', 'confluent:tags': ['PII']}, |
| 1411 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1412 |
{'name': 'bytesField', 'type': 'bytes', 'confluent:tags': ['PII']}, |
| 1413 |
] |
| 1414 |
} |
| 1415 |
|
| 1416 |
rule1 = Rule( |
| 1417 |
"test-cel", |
| 1418 |
"", |
| 1419 |
RuleKind.TRANSFORM, |
| 1420 |
RuleMode.WRITE, |
| 1421 |
"CEL_FIELD", |
| 1422 |
None, |
| 1423 |
None, |
| 1424 |
"name == 'stringField' ; value + '-suffix'", |
| 1425 |
None, |
| 1426 |
None, |
| 1427 |
False |
| 1428 |
) |
| 1429 |
rule2 = Rule( |
| 1430 |
"test-encrypt", |
| 1431 |
"", |
| 1432 |
RuleKind.TRANSFORM, |
| 1433 |
RuleMode.WRITEREAD, |
| 1434 |
"ENCRYPT", |
| 1435 |
["PII"], |
| 1436 |
RuleParams({ |
| 1437 |
"encrypt.kek.name": "kek1", |
| 1438 |
"encrypt.kms.type": "local-kms", |
| 1439 |
"encrypt.kms.key.id": "mykey" |
| 1440 |
}), |
| 1441 |
None, |
| 1442 |
None, |
| 1443 |
"ERROR,NONE", |
| 1444 |
False |
| 1445 |
) |
| 1446 |
await client.register_schema(_SUBJECT, Schema( |
| 1447 |
json.dumps(schema), |
| 1448 |
"AVRO", |
| 1449 |
[], |
| 1450 |
None, |
| 1451 |
RuleSet(None, [rule1, rule2]) |
| 1452 |
)) |
| 1453 |
|
| 1454 |
obj = { |
| 1455 |
'intField': 123, |
| 1456 |
'doubleField': 45.67, |
| 1457 |
'stringField': 'hi', |
| 1458 |
'booleanField': True, |
| 1459 |
'bytesField': b'foobar', |
| 1460 |
} |
| 1461 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1462 |
dek_client = executor.executor.client |
| 1463 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1464 |
obj_bytes = await ser(obj, ser_ctx) |
| 1465 |
|
| 1466 |
|
| 1467 |
assert obj['stringField'] != 'hi-suffix' |
| 1468 |
obj['stringField'] = 'hi-suffix' |
| 1469 |
obj['bytesField'] = b'foobar' |
| 1470 |
|
| 1471 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1472 |
executor.executor.client = dek_client |
| 1473 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1474 |
assert obj == obj2 |
| 1475 |
|
| 1476 |
|
| 1477 |
async def test_avro_encryption_dek_rotation(): |
| 1478 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1479 |
|
| 1480 |
conf = {'url': _BASE_URL} |
| 1481 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1482 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1483 |
rule_conf = {'secret': 'mysecret'} |
| 1484 |
schema = { |
| 1485 |
'type': 'record', |
| 1486 |
'name': 'test', |
| 1487 |
'fields': [ |
| 1488 |
{'name': 'intField', 'type': 'int'}, |
| 1489 |
{'name': 'doubleField', 'type': 'double'}, |
| 1490 |
{'name': 'stringField', 'type': 'string', 'confluent:tags': ['PII']}, |
| 1491 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1492 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 1493 |
] |
| 1494 |
} |
| 1495 |
|
| 1496 |
rule = Rule( |
| 1497 |
"test-encrypt", |
| 1498 |
"", |
| 1499 |
RuleKind.TRANSFORM, |
| 1500 |
RuleMode.WRITEREAD, |
| 1501 |
"ENCRYPT", |
| 1502 |
["PII"], |
| 1503 |
RuleParams({ |
| 1504 |
"encrypt.kek.name": "kek1-rot", |
| 1505 |
"encrypt.kms.type": "local-kms", |
| 1506 |
"encrypt.kms.key.id": "mykey", |
| 1507 |
"encrypt.dek.expiry.days": "1" |
| 1508 |
}), |
| 1509 |
None, |
| 1510 |
None, |
| 1511 |
"ERROR,NONE", |
| 1512 |
False |
| 1513 |
) |
| 1514 |
await client.register_schema(_SUBJECT, Schema( |
| 1515 |
json.dumps(schema), |
| 1516 |
"AVRO", |
| 1517 |
[], |
| 1518 |
None, |
| 1519 |
RuleSet(None, [rule]) |
| 1520 |
)) |
| 1521 |
|
| 1522 |
obj = { |
| 1523 |
'intField': 123, |
| 1524 |
'doubleField': 45.67, |
| 1525 |
'stringField': 'hi', |
| 1526 |
'booleanField': True, |
| 1527 |
'bytesField': b'foobar', |
| 1528 |
} |
| 1529 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1530 |
dek_client: DekRegistryClient = executor.executor.client |
| 1531 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1532 |
obj_bytes = await ser(obj, ser_ctx) |
| 1533 |
|
| 1534 |
|
| 1535 |
assert obj['stringField'] != 'hi' |
| 1536 |
obj['stringField'] = 'hi' |
| 1537 |
|
| 1538 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1539 |
executor.executor.client = dek_client |
| 1540 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1541 |
assert obj == obj2 |
| 1542 |
|
| 1543 |
dek_client = executor.executor.client |
| 1544 |
dek = dek_client.get_dek("kek1-rot", _SUBJECT, version=-1) |
| 1545 |
assert dek.version == 1 |
| 1546 |
|
| 1547 |
|
| 1548 |
now = datetime.now() + timedelta(days=2) |
| 1549 |
executor.executor.clock.fixed_now = int(round(now.timestamp() * 1000)) |
| 1550 |
|
| 1551 |
obj_bytes = await ser(obj, ser_ctx) |
| 1552 |
|
| 1553 |
|
| 1554 |
assert obj['stringField'] != 'hi' |
| 1555 |
obj['stringField'] = 'hi' |
| 1556 |
|
| 1557 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1558 |
assert obj == obj2 |
| 1559 |
|
| 1560 |
dek = dek_client.get_dek("kek1-rot", _SUBJECT, version=-1) |
| 1561 |
assert dek.version == 2 |
| 1562 |
|
| 1563 |
|
| 1564 |
now = datetime.now() + timedelta(days=2) |
| 1565 |
executor.executor.clock.fixed_now = int(round(now.timestamp() * 1000)) |
| 1566 |
|
| 1567 |
obj_bytes = await ser(obj, ser_ctx) |
| 1568 |
|
| 1569 |
|
| 1570 |
assert obj['stringField'] != 'hi' |
| 1571 |
obj['stringField'] = 'hi' |
| 1572 |
|
| 1573 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1574 |
assert obj == obj2 |
| 1575 |
|
| 1576 |
dek = dek_client.get_dek("kek1-rot", _SUBJECT, version=-1) |
| 1577 |
assert dek.version == 3 |
| 1578 |
|
| 1579 |
|
| 1580 |
async def test_avro_encryption_f1_preserialized(): |
| 1581 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1582 |
|
| 1583 |
conf = {'url': _BASE_URL} |
| 1584 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1585 |
rule_conf = {'secret': 'mysecret'} |
| 1586 |
schema = { |
| 1587 |
'type': 'record', |
| 1588 |
'name': 'f1Schema', |
| 1589 |
'fields': [ |
| 1590 |
{'name': 'f1', 'type': 'string', 'confluent:tags': ['PII']} |
| 1591 |
] |
| 1592 |
} |
| 1593 |
|
| 1594 |
rule = Rule( |
| 1595 |
"test-encrypt", |
| 1596 |
"", |
| 1597 |
RuleKind.TRANSFORM, |
| 1598 |
RuleMode.WRITEREAD, |
| 1599 |
"ENCRYPT", |
| 1600 |
["PII"], |
| 1601 |
RuleParams({ |
| 1602 |
"encrypt.kek.name": "kek1-f1", |
| 1603 |
"encrypt.kms.type": "local-kms", |
| 1604 |
"encrypt.kms.key.id": "mykey" |
| 1605 |
}), |
| 1606 |
None, |
| 1607 |
None, |
| 1608 |
"ERROR,ERROR", |
| 1609 |
False |
| 1610 |
) |
| 1611 |
await client.register_schema(_SUBJECT, Schema( |
| 1612 |
json.dumps(schema), |
| 1613 |
"AVRO", |
| 1614 |
[], |
| 1615 |
None, |
| 1616 |
RuleSet(None, [rule]) |
| 1617 |
)) |
| 1618 |
|
| 1619 |
obj = { |
| 1620 |
'f1': 'hello world' |
| 1621 |
} |
| 1622 |
|
| 1623 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1624 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1625 |
|
| 1626 |
dek_client: DekRegistryClient = executor.executor.client |
| 1627 |
dek_client.register_kek("kek1-f1", "local-kms", "mykey") |
| 1628 |
|
| 1629 |
encrypted_dek = "07V2ndh02DA73p+dTybwZFm7DKQSZN1tEwQh+FoX1DZLk4Yj2LLu4omYjp/84tAg3BYlkfGSz+zZacJHIE4=" |
| 1630 |
dek_client.register_dek("kek1-f1", _SUBJECT, encrypted_dek) |
| 1631 |
|
| 1632 |
obj_bytes = bytes([0, 0, 0, 0, 1, 104, 122, 103, 121, 47, 106, 70, 78, 77, |
| 1633 |
86, 47, 101, 70, 105, 108, 97, 72, 114, 77, 121, 101, 66, |
| 1634 |
103, 100, 97, 86, 122, 114, 82, 48, 117, 100, 71, 101, |
| 1635 |
111, 116, 87, 56, 99, 65, 47, 74, 97, 108, 55, 117, 107, |
| 1636 |
114, 43, 77, 47, 121, 122]) |
| 1637 |
|
| 1638 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1639 |
assert obj == obj2 |
| 1640 |
|
| 1641 |
|
| 1642 |
async def test_avro_encryption_deterministic_f1_preserialized(): |
| 1643 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1644 |
|
| 1645 |
conf = {'url': _BASE_URL} |
| 1646 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1647 |
rule_conf = {'secret': 'mysecret'} |
| 1648 |
schema = { |
| 1649 |
'type': 'record', |
| 1650 |
'name': 'f1Schema', |
| 1651 |
'fields': [ |
| 1652 |
{'name': 'f1', 'type': 'string', 'confluent:tags': ['PII']} |
| 1653 |
] |
| 1654 |
} |
| 1655 |
|
| 1656 |
rule = Rule( |
| 1657 |
"test-encrypt", |
| 1658 |
"", |
| 1659 |
RuleKind.TRANSFORM, |
| 1660 |
RuleMode.WRITEREAD, |
| 1661 |
"ENCRYPT", |
| 1662 |
["PII"], |
| 1663 |
RuleParams({ |
| 1664 |
"encrypt.kek.name": "kek1-det-f1", |
| 1665 |
"encrypt.kms.type": "local-kms", |
| 1666 |
"encrypt.kms.key.id": "mykey", |
| 1667 |
"encrypt.dek.algorithm": "AES256_SIV", |
| 1668 |
}), |
| 1669 |
None, |
| 1670 |
None, |
| 1671 |
"ERROR,ERROR", |
| 1672 |
False |
| 1673 |
) |
| 1674 |
await client.register_schema(_SUBJECT, Schema( |
| 1675 |
json.dumps(schema), |
| 1676 |
"AVRO", |
| 1677 |
[], |
| 1678 |
None, |
| 1679 |
RuleSet(None, [rule]) |
| 1680 |
)) |
| 1681 |
|
| 1682 |
obj = { |
| 1683 |
'f1': 'hello world' |
| 1684 |
} |
| 1685 |
|
| 1686 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1687 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1688 |
|
| 1689 |
dek_client: DekRegistryClient = executor.executor.client |
| 1690 |
dek_client.register_kek("kek1-det-f1", "local-kms", "mykey") |
| 1691 |
|
| 1692 |
encrypted_dek = ("YSx3DTlAHrmpoDChquJMifmPntBzxgRVdMzgYL82rgWBKn7aUSnG+WIu9oz" |
| 1693 |
"BNS3y2vXd++mBtK07w4/W/G6w0da39X9hfOVZsGnkSvry/QRht84V8yz3dqKxGMOK5A==") |
| 1694 |
dek_client.register_dek("kek1-det-f1", _SUBJECT, encrypted_dek, algorithm=DekAlgorithm.AES256_SIV) |
| 1695 |
|
| 1696 |
obj_bytes = bytes([0, 0, 0, 0, 1, 72, 68, 54, 89, 116, 120, 114, 108, 66, |
| 1697 |
110, 107, 84, 87, 87, 57, 78, 54, 86, 98, 107, 51, 73, |
| 1698 |
73, 110, 106, 87, 72, 56, 49, 120, 109, 89, 104, 51, 107, 52, 100]) |
| 1699 |
|
| 1700 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1701 |
assert obj == obj2 |
| 1702 |
|
| 1703 |
|
| 1704 |
async def test_avro_encryption_dek_rotation_f1_preserialized(): |
| 1705 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1706 |
|
| 1707 |
conf = {'url': _BASE_URL} |
| 1708 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1709 |
rule_conf = {'secret': 'mysecret'} |
| 1710 |
schema = { |
| 1711 |
'type': 'record', |
| 1712 |
'name': 'f1Schema', |
| 1713 |
'fields': [ |
| 1714 |
{'name': 'f1', 'type': 'string', 'confluent:tags': ['PII']} |
| 1715 |
] |
| 1716 |
} |
| 1717 |
|
| 1718 |
rule = Rule( |
| 1719 |
"test-encrypt", |
| 1720 |
"", |
| 1721 |
RuleKind.TRANSFORM, |
| 1722 |
RuleMode.WRITEREAD, |
| 1723 |
"ENCRYPT", |
| 1724 |
["PII"], |
| 1725 |
RuleParams({ |
| 1726 |
"encrypt.kek.name": "kek1-rot-f1", |
| 1727 |
"encrypt.kms.type": "local-kms", |
| 1728 |
"encrypt.kms.key.id": "mykey", |
| 1729 |
"encrypt.dek.expiry.days": "1", |
| 1730 |
}), |
| 1731 |
None, |
| 1732 |
None, |
| 1733 |
"ERROR,ERROR", |
| 1734 |
False |
| 1735 |
) |
| 1736 |
await client.register_schema(_SUBJECT, Schema( |
| 1737 |
json.dumps(schema), |
| 1738 |
"AVRO", |
| 1739 |
[], |
| 1740 |
None, |
| 1741 |
RuleSet(None, [rule]) |
| 1742 |
)) |
| 1743 |
|
| 1744 |
obj = { |
| 1745 |
'f1': 'hello world' |
| 1746 |
} |
| 1747 |
|
| 1748 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1749 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1750 |
|
| 1751 |
dek_client: DekRegistryClient = executor.executor.client |
| 1752 |
dek_client.register_kek("kek1-rot-f1", "local-kms", "mykey") |
| 1753 |
|
| 1754 |
encrypted_dek = "W/v6hOQYq1idVAcs1pPWz9UUONMVZW4IrglTnG88TsWjeCjxmtRQ4VaNe/I5dCfm2zyY9Cu0nqdvqImtUk4=" |
| 1755 |
dek_client.register_dek("kek1-rot-f1", _SUBJECT, encrypted_dek, algorithm=DekAlgorithm.AES256_GCM) |
| 1756 |
|
| 1757 |
obj_bytes = bytes([0, 0, 0, 0, 1, 120, 65, 65, 65, 65, 65, 65, 71, 52, 72, |
| 1758 |
73, 54, 98, 49, 110, 88, 80, 88, 113, 76, 121, 71, 56, |
| 1759 |
99, 73, 73, 51, 53, 78, 72, 81, 115, 101, 113, 113, 85, |
| 1760 |
67, 100, 43, 73, 101, 76, 101, 70, 86, 65, 101, 78, 112, |
| 1761 |
83, 83, 51, 102, 120, 80, 110, 74, 51, 50, 65, 61]) |
| 1762 |
|
| 1763 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1764 |
assert obj == obj2 |
| 1765 |
|
| 1766 |
|
| 1767 |
async def test_avro_encryption_references(): |
| 1768 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1769 |
|
| 1770 |
conf = {'url': _BASE_URL} |
| 1771 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1772 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1773 |
rule_conf = {'secret': 'mysecret'} |
| 1774 |
|
| 1775 |
referenced = { |
| 1776 |
'intField': 123, |
| 1777 |
'doubleField': 45.67, |
| 1778 |
'stringField': 'hi', |
| 1779 |
'booleanField': True, |
| 1780 |
'bytesField': b'foobar', |
| 1781 |
} |
| 1782 |
obj = { |
| 1783 |
'refField': referenced |
| 1784 |
} |
| 1785 |
ref_schema = { |
| 1786 |
'type': 'record', |
| 1787 |
'name': 'ref', |
| 1788 |
'fields': [ |
| 1789 |
{'name': 'intField', 'type': 'int'}, |
| 1790 |
{'name': 'doubleField', 'type': 'double'}, |
| 1791 |
{'name': 'stringField', 'type': 'string', 'confluent:tags': ['PII']}, |
| 1792 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1793 |
{'name': 'bytesField', 'type': 'bytes'}, |
| 1794 |
] |
| 1795 |
} |
| 1796 |
await client.register_schema('ref', Schema(json.dumps(ref_schema))) |
| 1797 |
schema = { |
| 1798 |
'type': 'record', |
| 1799 |
'name': 'test', |
| 1800 |
'fields': [ |
| 1801 |
{'name': 'refField', 'type': 'ref'}, |
| 1802 |
] |
| 1803 |
} |
| 1804 |
refs = [SchemaReference('ref', 'ref', 1)] |
| 1805 |
rule = Rule( |
| 1806 |
"test-encrypt", |
| 1807 |
"", |
| 1808 |
RuleKind.TRANSFORM, |
| 1809 |
RuleMode.WRITEREAD, |
| 1810 |
"ENCRYPT", |
| 1811 |
["PII"], |
| 1812 |
RuleParams({ |
| 1813 |
"encrypt.kek.name": "kek1-ref", |
| 1814 |
"encrypt.kms.type": "local-kms", |
| 1815 |
"encrypt.kms.key.id": "mykey" |
| 1816 |
}), |
| 1817 |
None, |
| 1818 |
None, |
| 1819 |
"ERROR,NONE", |
| 1820 |
False |
| 1821 |
) |
| 1822 |
await client.register_schema(_SUBJECT, Schema( |
| 1823 |
json.dumps(schema), |
| 1824 |
"AVRO", |
| 1825 |
refs, |
| 1826 |
None, |
| 1827 |
RuleSet(None, [rule]) |
| 1828 |
)) |
| 1829 |
|
| 1830 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1831 |
dek_client = executor.executor.client |
| 1832 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1833 |
obj_bytes = await ser(obj, ser_ctx) |
| 1834 |
|
| 1835 |
|
| 1836 |
assert obj['refField']['stringField'] != 'hi' |
| 1837 |
obj['refField']['stringField'] = 'hi' |
| 1838 |
obj['refField']['bytesField'] = b'foobar' |
| 1839 |
|
| 1840 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1841 |
executor.executor.client = dek_client |
| 1842 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1843 |
assert obj == obj2 |
| 1844 |
|
| 1845 |
|
| 1846 |
async def test_avro_encryption_with_union(): |
| 1847 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 1848 |
|
| 1849 |
conf = {'url': _BASE_URL} |
| 1850 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1851 |
ser_conf = {'auto.register.schemas': False, 'use.latest.version': True} |
| 1852 |
rule_conf = {'secret': 'mysecret'} |
| 1853 |
schema = { |
| 1854 |
'type': 'record', |
| 1855 |
'name': 'test', |
| 1856 |
'fields': [ |
| 1857 |
{'name': 'intField', 'type': 'int'}, |
| 1858 |
{'name': 'doubleField', 'type': 'double'}, |
| 1859 |
{'name': 'stringField', 'type': ['null', 'string'], 'confluent:tags': ['PII']}, |
| 1860 |
{'name': 'booleanField', 'type': 'boolean'}, |
| 1861 |
{'name': 'bytesField', 'type': ['null', 'bytes'], 'confluent:tags': ['PII']}, |
| 1862 |
] |
| 1863 |
} |
| 1864 |
|
| 1865 |
rule = Rule( |
| 1866 |
"test-encrypt", |
| 1867 |
"", |
| 1868 |
RuleKind.TRANSFORM, |
| 1869 |
RuleMode.WRITEREAD, |
| 1870 |
"ENCRYPT", |
| 1871 |
["PII"], |
| 1872 |
RuleParams({ |
| 1873 |
"encrypt.kek.name": "kek1-union", |
| 1874 |
"encrypt.kms.type": "local-kms", |
| 1875 |
"encrypt.kms.key.id": "mykey" |
| 1876 |
}), |
| 1877 |
None, |
| 1878 |
None, |
| 1879 |
"ERROR,NONE", |
| 1880 |
False |
| 1881 |
) |
| 1882 |
await client.register_schema(_SUBJECT, Schema( |
| 1883 |
json.dumps(schema), |
| 1884 |
"AVRO", |
| 1885 |
[], |
| 1886 |
None, |
| 1887 |
RuleSet(None, [rule]) |
| 1888 |
)) |
| 1889 |
|
| 1890 |
obj = { |
| 1891 |
'intField': 123, |
| 1892 |
'doubleField': 45.67, |
| 1893 |
'stringField': 'hi', |
| 1894 |
'booleanField': True, |
| 1895 |
'bytesField': b'foobar', |
| 1896 |
} |
| 1897 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf, rule_conf=rule_conf) |
| 1898 |
dek_client = executor.executor.client |
| 1899 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 1900 |
obj_bytes = await ser(obj, ser_ctx) |
| 1901 |
|
| 1902 |
|
| 1903 |
assert obj['stringField'] != 'hi' |
| 1904 |
obj['stringField'] = 'hi' |
| 1905 |
obj['bytesField'] = b'foobar' |
| 1906 |
|
| 1907 |
deser = await AsyncAvroDeserializer(client, rule_conf=rule_conf) |
| 1908 |
executor.executor.client = dek_client |
| 1909 |
obj2 = await deser(obj_bytes, ser_ctx) |
| 1910 |
assert obj == obj2 |
| 1911 |
|
| 1912 |
|
| 1913 |
async def test_avro_jsonata_with_cel(): |
| 1914 |
rule1_to_2 = "$merge([$sift($, function($v, $k) {$k != 'size'}), {'height': $.'size'}])" |
| 1915 |
|
| 1916 |
conf = {'url': _BASE_URL} |
| 1917 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 1918 |
|
| 1919 |
await client.set_config(_SUBJECT, ServerConfig( |
| 1920 |
compatibility_group='application.version' |
| 1921 |
)) |
| 1922 |
|
| 1923 |
schema = { |
| 1924 |
'type': 'record', |
| 1925 |
'name': 'old', |
| 1926 |
'fields': [ |
| 1927 |
{'name': 'name', 'type': 'string'}, |
| 1928 |
{'name': 'size', 'type': 'int'}, |
| 1929 |
{'name': 'version', 'type': 'int'}, |
| 1930 |
] |
| 1931 |
} |
| 1932 |
await client.register_schema(_SUBJECT, Schema( |
| 1933 |
json.dumps(schema), |
| 1934 |
"AVRO", |
| 1935 |
[], |
| 1936 |
Metadata( |
| 1937 |
None, |
| 1938 |
MetadataProperties({"application.version": "v1"}), |
| 1939 |
None |
| 1940 |
), |
| 1941 |
None |
| 1942 |
)) |
| 1943 |
|
| 1944 |
schema = { |
| 1945 |
'type': 'record', |
| 1946 |
'name': 'new', |
| 1947 |
'fields': [ |
| 1948 |
{'name': 'name', 'type': 'string'}, |
| 1949 |
{'name': 'height', 'type': 'int'}, |
| 1950 |
{'name': 'version', 'type': 'int'}, |
| 1951 |
] |
| 1952 |
} |
| 1953 |
|
| 1954 |
rule1 = Rule( |
| 1955 |
"test-jsonata", |
| 1956 |
"", |
| 1957 |
RuleKind.TRANSFORM, |
| 1958 |
RuleMode.UPGRADE, |
| 1959 |
"JSONATA", |
| 1960 |
None, |
| 1961 |
None, |
| 1962 |
rule1_to_2, |
| 1963 |
None, |
| 1964 |
None, |
| 1965 |
False |
| 1966 |
) |
| 1967 |
rule2 = Rule( |
| 1968 |
"test-cel", |
| 1969 |
"", |
| 1970 |
RuleKind.TRANSFORM, |
| 1971 |
RuleMode.READ, |
| 1972 |
"CEL_FIELD", |
| 1973 |
None, |
| 1974 |
None, |
| 1975 |
"name == 'name' ; value + '-suffix'", |
| 1976 |
None, |
| 1977 |
None, |
| 1978 |
False |
| 1979 |
) |
| 1980 |
await client.register_schema(_SUBJECT, Schema( |
| 1981 |
json.dumps(schema), |
| 1982 |
"AVRO", |
| 1983 |
[], |
| 1984 |
Metadata( |
| 1985 |
None, |
| 1986 |
MetadataProperties({"application.version": "v2"}), |
| 1987 |
None |
| 1988 |
), |
| 1989 |
RuleSet([rule1], [rule2]) |
| 1990 |
)) |
| 1991 |
|
| 1992 |
obj = { |
| 1993 |
'name': 'alice', |
| 1994 |
'size': 123, |
| 1995 |
'version': 1, |
| 1996 |
} |
| 1997 |
ser_conf = { |
| 1998 |
'auto.register.schemas': False, |
| 1999 |
'use.latest.version': False, |
| 2000 |
'use.latest.with.metadata': { |
| 2001 |
'application.version': 'v1' |
| 2002 |
} |
| 2003 |
} |
| 2004 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 2005 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 2006 |
obj_bytes = await ser(obj, ser_ctx) |
| 2007 |
|
| 2008 |
obj2 = { |
| 2009 |
'name': 'alice-suffix', |
| 2010 |
'height': 123, |
| 2011 |
'version': 1, |
| 2012 |
} |
| 2013 |
deser_conf = { |
| 2014 |
'use.latest.with.metadata': { |
| 2015 |
'application.version': 'v2' |
| 2016 |
} |
| 2017 |
} |
| 2018 |
deser = await AsyncAvroDeserializer(client, conf=deser_conf) |
| 2019 |
newobj = await deser(obj_bytes, ser_ctx) |
| 2020 |
assert obj2 == newobj |
| 2021 |
|
| 2022 |
|
| 2023 |
async def test_avro_jsonata_fully_compatible(): |
| 2024 |
rule1_to_2 = "$merge([$sift($, function($v, $k) {$k != 'size'}), {'height': $.'size'}])" |
| 2025 |
rule2_to_1 = "$merge([$sift($, function($v, $k) {$k != 'height'}), {'size': $.'height'}])" |
| 2026 |
rule2_to_3 = "$merge([$sift($, function($v, $k) {$k != 'height'}), {'length': $.'height'}])" |
| 2027 |
rule3_to_2 = "$merge([$sift($, function($v, $k) {$k != 'length'}), {'height': $.'length'}])" |
| 2028 |
|
| 2029 |
conf = {'url': _BASE_URL} |
| 2030 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 2031 |
|
| 2032 |
await client.set_config(_SUBJECT, ServerConfig( |
| 2033 |
compatibility_group='application.version' |
| 2034 |
)) |
| 2035 |
|
| 2036 |
schema = { |
| 2037 |
'type': 'record', |
| 2038 |
'name': 'old', |
| 2039 |
'fields': [ |
| 2040 |
{'name': 'name', 'type': 'string'}, |
| 2041 |
{'name': 'size', 'type': 'int'}, |
| 2042 |
{'name': 'version', 'type': 'int'}, |
| 2043 |
] |
| 2044 |
} |
| 2045 |
await client.register_schema(_SUBJECT, Schema( |
| 2046 |
json.dumps(schema), |
| 2047 |
"AVRO", |
| 2048 |
[], |
| 2049 |
Metadata( |
| 2050 |
None, |
| 2051 |
MetadataProperties({"application.version": "v1"}), |
| 2052 |
None |
| 2053 |
), |
| 2054 |
None |
| 2055 |
)) |
| 2056 |
|
| 2057 |
schema = { |
| 2058 |
'type': 'record', |
| 2059 |
'name': 'new', |
| 2060 |
'fields': [ |
| 2061 |
{'name': 'name', 'type': 'string'}, |
| 2062 |
{'name': 'height', 'type': 'int'}, |
| 2063 |
{'name': 'version', 'type': 'int'}, |
| 2064 |
] |
| 2065 |
} |
| 2066 |
|
| 2067 |
rule1 = Rule( |
| 2068 |
"rule1", |
| 2069 |
"", |
| 2070 |
RuleKind.TRANSFORM, |
| 2071 |
RuleMode.UPGRADE, |
| 2072 |
"JSONATA", |
| 2073 |
None, |
| 2074 |
None, |
| 2075 |
rule1_to_2, |
| 2076 |
None, |
| 2077 |
None, |
| 2078 |
False |
| 2079 |
) |
| 2080 |
rule2 = Rule( |
| 2081 |
"rule2", |
| 2082 |
"", |
| 2083 |
RuleKind.TRANSFORM, |
| 2084 |
RuleMode.DOWNGRADE, |
| 2085 |
"JSONATA", |
| 2086 |
None, |
| 2087 |
None, |
| 2088 |
rule2_to_1, |
| 2089 |
None, |
| 2090 |
None, |
| 2091 |
False |
| 2092 |
) |
| 2093 |
await client.register_schema(_SUBJECT, Schema( |
| 2094 |
json.dumps(schema), |
| 2095 |
"AVRO", |
| 2096 |
[], |
| 2097 |
Metadata( |
| 2098 |
None, |
| 2099 |
MetadataProperties({"application.version": "v2"}), |
| 2100 |
None |
| 2101 |
), |
| 2102 |
RuleSet([rule1, rule2], None) |
| 2103 |
)) |
| 2104 |
|
| 2105 |
schema = { |
| 2106 |
'type': 'record', |
| 2107 |
'name': 'newer', |
| 2108 |
'fields': [ |
| 2109 |
{'name': 'name', 'type': 'string'}, |
| 2110 |
{'name': 'length', 'type': 'int'}, |
| 2111 |
{'name': 'version', 'type': 'int'}, |
| 2112 |
] |
| 2113 |
} |
| 2114 |
|
| 2115 |
rule3 = Rule( |
| 2116 |
"rule3", |
| 2117 |
"", |
| 2118 |
RuleKind.TRANSFORM, |
| 2119 |
RuleMode.UPGRADE, |
| 2120 |
"JSONATA", |
| 2121 |
None, |
| 2122 |
None, |
| 2123 |
rule2_to_3, |
| 2124 |
None, |
| 2125 |
None, |
| 2126 |
False |
| 2127 |
) |
| 2128 |
rule4 = Rule( |
| 2129 |
"rule4", |
| 2130 |
"", |
| 2131 |
RuleKind.TRANSFORM, |
| 2132 |
RuleMode.DOWNGRADE, |
| 2133 |
"JSONATA", |
| 2134 |
None, |
| 2135 |
None, |
| 2136 |
rule3_to_2, |
| 2137 |
None, |
| 2138 |
None, |
| 2139 |
False |
| 2140 |
) |
| 2141 |
await client.register_schema(_SUBJECT, Schema( |
| 2142 |
json.dumps(schema), |
| 2143 |
"AVRO", |
| 2144 |
[], |
| 2145 |
Metadata( |
| 2146 |
None, |
| 2147 |
MetadataProperties({"application.version": "v3"}), |
| 2148 |
None |
| 2149 |
), |
| 2150 |
RuleSet([rule3, rule4], None) |
| 2151 |
)) |
| 2152 |
|
| 2153 |
obj = { |
| 2154 |
'name': 'alice', |
| 2155 |
'size': 123, |
| 2156 |
'version': 1, |
| 2157 |
} |
| 2158 |
obj2 = { |
| 2159 |
'name': 'alice', |
| 2160 |
'height': 123, |
| 2161 |
'version': 1, |
| 2162 |
} |
| 2163 |
obj3 = { |
| 2164 |
'name': 'alice', |
| 2165 |
'length': 123, |
| 2166 |
'version': 1, |
| 2167 |
} |
| 2168 |
|
| 2169 |
ser_conf = { |
| 2170 |
'auto.register.schemas': False, |
| 2171 |
'use.latest.version': False, |
| 2172 |
'use.latest.with.metadata': { |
| 2173 |
'application.version': 'v1' |
| 2174 |
} |
| 2175 |
} |
| 2176 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 2177 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 2178 |
obj_bytes = await ser(obj, ser_ctx) |
| 2179 |
|
| 2180 |
await deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3) |
| 2181 |
|
| 2182 |
ser_conf = { |
| 2183 |
'auto.register.schemas': False, |
| 2184 |
'use.latest.version': False, |
| 2185 |
'use.latest.with.metadata': { |
| 2186 |
'application.version': 'v2' |
| 2187 |
} |
| 2188 |
} |
| 2189 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 2190 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 2191 |
obj_bytes = await ser(obj2, ser_ctx) |
| 2192 |
|
| 2193 |
await deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3) |
| 2194 |
|
| 2195 |
ser_conf = { |
| 2196 |
'auto.register.schemas': False, |
| 2197 |
'use.latest.version': False, |
| 2198 |
'use.latest.with.metadata': { |
| 2199 |
'application.version': 'v3' |
| 2200 |
} |
| 2201 |
} |
| 2202 |
ser = await AsyncAvroSerializer(client, schema_str=None, conf=ser_conf) |
| 2203 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 2204 |
obj_bytes = await ser(obj3, ser_ctx) |
| 2205 |
|
| 2206 |
await deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3) |
| 2207 |
|
| 2208 |
|
| 2209 |
async def deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3): |
| 2210 |
deser_conf = { |
| 2211 |
'use.latest.with.metadata': { |
| 2212 |
'application.version': 'v1' |
| 2213 |
} |
| 2214 |
} |
| 2215 |
deser = await AsyncAvroDeserializer(client, conf=deser_conf) |
| 2216 |
newobj = await deser(obj_bytes, ser_ctx) |
| 2217 |
assert obj == newobj |
| 2218 |
|
| 2219 |
deser_conf = { |
| 2220 |
'use.latest.with.metadata': { |
| 2221 |
'application.version': 'v2' |
| 2222 |
} |
| 2223 |
} |
| 2224 |
deser = await AsyncAvroDeserializer(client, conf=deser_conf) |
| 2225 |
newobj = await deser(obj_bytes, ser_ctx) |
| 2226 |
assert obj2 == newobj |
| 2227 |
|
| 2228 |
deser_conf = { |
| 2229 |
'use.latest.with.metadata': { |
| 2230 |
'application.version': 'v3' |
| 2231 |
} |
| 2232 |
} |
| 2233 |
deser = await AsyncAvroDeserializer(client, conf=deser_conf) |
| 2234 |
newobj = await deser(obj_bytes, ser_ctx) |
| 2235 |
assert obj3 == newobj |
| 2236 |
|
| 2237 |
|
| 2238 |
async def test_avro_reference(): |
| 2239 |
conf = {'url': _BASE_URL} |
| 2240 |
client = AsyncSchemaRegistryClient.new_client(conf) |
| 2241 |
|
| 2242 |
awarded_user, schema = await _register_avro_schemas_and_build_awarded_user_schema(client) |
| 2243 |
|
| 2244 |
await _references_test_common(client, awarded_user, schema, schema) |
| 2245 |
|
| 2246 |
|
| 2247 |
async def _register_avro_schemas_and_build_awarded_user_schema(client): |
| 2248 |
user = User('Bowie', 47, 'purple') |
| 2249 |
award_properties = AwardProperties(10, 2023) |
| 2250 |
award = Award("Best In Show", award_properties) |
| 2251 |
awarded_user = AwardedUser(award, user) |
| 2252 |
|
| 2253 |
user_schema_ref = SchemaReference("confluent.io.examples.serialization.avro.User", "user", 1) |
| 2254 |
award_properties_schema_ref = SchemaReference("confluent.io.examples.serialization.avro.AwardProperties", |
| 2255 |
"award_properties", 1) |
| 2256 |
award_schema_ref = SchemaReference("confluent.io.examples.serialization.avro.Award", "award", 1) |
| 2257 |
|
| 2258 |
await client.register_schema("user", Schema(User.schema_str, 'AVRO')) |
| 2259 |
await client.register_schema("award_properties", Schema(AwardProperties.schema_str, 'AVRO')) |
| 2260 |
await client.register_schema("award", Schema(Award.schema_str, 'AVRO', [award_properties_schema_ref])) |
| 2261 |
|
| 2262 |
references = [user_schema_ref, award_schema_ref] |
| 2263 |
schema = Schema(AwardedUser.schema_str, 'AVRO', references) |
| 2264 |
return awarded_user, schema |
| 2265 |
|
| 2266 |
|
| 2267 |
async def _references_test_common(client, awarded_user, serializer_schema, deserializer_schema): |
| 2268 |
value_serializer = await AsyncAvroSerializer( |
| 2269 |
client, serializer_schema, |
| 2270 |
lambda user, ctx: |
| 2271 |
dict( |
| 2272 |
award=dict( |
| 2273 |
name=user.award.name, |
| 2274 |
properties=dict(year=user.award.properties.year, |
| 2275 |
points=user.award.properties.points)), |
| 2276 |
user=dict( |
| 2277 |
name=user.user.name, |
| 2278 |
favorite_number=user.user.favorite_number, |
| 2279 |
favorite_color=user.user.favorite_color))) |
| 2280 |
|
| 2281 |
value_deserializer = \ |
| 2282 |
await AsyncAvroDeserializer( |
| 2283 |
client, deserializer_schema, |
| 2284 |
lambda user, ctx: |
| 2285 |
AwardedUser( |
| 2286 |
award=Award( |
| 2287 |
name=user.get('award').get('name'), |
| 2288 |
properties=AwardProperties( |
| 2289 |
year=user.get('award').get('properties').get( |
| 2290 |
'year'), |
| 2291 |
points=user.get('award').get('properties').get( |
| 2292 |
'points'))), |
| 2293 |
user=User( |
| 2294 |
name=user.get('user').get('name'), |
| 2295 |
favorite_number=user.get('user').get('favorite_number'), |
| 2296 |
favorite_color=user.get('user').get('favorite_color')))) |
| 2297 |
|
| 2298 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 2299 |
obj_bytes = await value_serializer(awarded_user, ser_ctx) |
| 2300 |
|
| 2301 |
awarded_user2 = await value_deserializer(obj_bytes, ser_ctx) |
| 2302 |
|
| 2303 |
assert awarded_user2 == awarded_user |
| 2304 |
|
| 2305 |
|
| 2306 |
class User(object): |
| 2307 |
schema_str = """ |
| 2308 |
{ |
| 2309 |
"namespace": "confluent.io.examples.serialization.avro", |
| 2310 |
"name": "User", |
| 2311 |
"type": "record", |
| 2312 |
"fields": [ |
| 2313 |
{"name": "name", "type": "string"}, |
| 2314 |
{"name": "favorite_number", "type": "int"}, |
| 2315 |
{"name": "favorite_color", "type": "string"} |
| 2316 |
] |
| 2317 |
} |
| 2318 |
""" |
| 2319 |
|
| 2320 |
def __init__(self, name, favorite_number, favorite_color): |
| 2321 |
self.name = name |
| 2322 |
self.favorite_number = favorite_number |
| 2323 |
self.favorite_color = favorite_color |
| 2324 |
|
| 2325 |
def __eq__(self, other): |
| 2326 |
return all([ |
| 2327 |
self.name == other.name, |
| 2328 |
self.favorite_number == other.favorite_number, |
| 2329 |
self.favorite_color == other.favorite_color]) |
| 2330 |
|
| 2331 |
|
| 2332 |
class AwardProperties(object): |
| 2333 |
schema_str = """ |
| 2334 |
{ |
| 2335 |
"namespace": "confluent.io.examples.serialization.avro", |
| 2336 |
"name": "AwardProperties", |
| 2337 |
"type": "record", |
| 2338 |
"fields": [ |
| 2339 |
{"name": "year", "type": "int"}, |
| 2340 |
{"name": "points", "type": "int"} |
| 2341 |
] |
| 2342 |
} |
| 2343 |
""" |
| 2344 |
|
| 2345 |
def __init__(self, points, year): |
| 2346 |
self.points = points |
| 2347 |
self.year = year |
| 2348 |
|
| 2349 |
def __eq__(self, other): |
| 2350 |
return all([ |
| 2351 |
self.points == other.points, |
| 2352 |
self.year == other.year |
| 2353 |
]) |
| 2354 |
|
| 2355 |
|
| 2356 |
class Award(object): |
| 2357 |
schema_str = """ |
| 2358 |
{ |
| 2359 |
"namespace": "confluent.io.examples.serialization.avro", |
| 2360 |
"name": "Award", |
| 2361 |
"type": "record", |
| 2362 |
"fields": [ |
| 2363 |
{"name": "name", "type": "string"}, |
| 2364 |
{"name": "properties", "type": "AwardProperties"} |
| 2365 |
] |
| 2366 |
} |
| 2367 |
""" |
| 2368 |
|
| 2369 |
def __init__(self, name, properties): |
| 2370 |
self.name = name |
| 2371 |
self.properties = properties |
| 2372 |
|
| 2373 |
def __eq__(self, other): |
| 2374 |
return all([ |
| 2375 |
self.name == other.name, |
| 2376 |
self.properties == other.properties |
| 2377 |
]) |
| 2378 |
|
| 2379 |
|
| 2380 |
class AwardedUser(object): |
| 2381 |
schema_str = """ |
| 2382 |
{ |
| 2383 |
"namespace": "confluent.io.examples.serialization.avro", |
| 2384 |
"name": "AwardedUser", |
| 2385 |
"type": "record", |
| 2386 |
"fields": [ |
| 2387 |
{"name": "award", "type": "Award"}, |
| 2388 |
{"name": "user", "type": "User"} |
| 2389 |
] |
| 2390 |
} |
| 2391 |
""" |
| 2392 |
|
| 2393 |
def __init__(self, award, user): |
| 2394 |
self.award = award |
| 2395 |
self.user = user |
| 2396 |
|
| 2397 |
def __eq__(self, other): |
| 2398 |
return all([ |
| 2399 |
self.award == other.award, |
| 2400 |
self.user == other.user |
| 2401 |
]) |