| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import sys |
| 19 |
import os |
| 20 |
import time |
| 21 |
import pytest |
| 22 |
|
| 23 |
from confluent_kafka.schema_registry._sync.schema_registry_client import SchemaRegistryClient |
| 24 |
from confluent_kafka.schema_registry import \ |
| 25 |
Schema, Metadata, MetadataProperties, header_schema_id_serializer |
| 26 |
from confluent_kafka.schema_registry._sync.protobuf import ProtobufSerializer, \ |
| 27 |
ProtobufDeserializer |
| 28 |
from confluent_kafka.schema_registry.protobuf import _schema_to_str |
| 29 |
from confluent_kafka.schema_registry.rules.cel.cel_executor import CelExecutor |
| 30 |
from confluent_kafka.schema_registry.rules.cel.cel_field_executor import \ |
| 31 |
CelFieldExecutor |
| 32 |
from confluent_kafka.schema_registry.rules.encryption.awskms.aws_driver import \ |
| 33 |
AwsKmsDriver |
| 34 |
from confluent_kafka.schema_registry.rules.encryption.azurekms.azure_driver import \ |
| 35 |
AzureKmsDriver |
| 36 |
from confluent_kafka.schema_registry.rules.encryption.encrypt_executor import \ |
| 37 |
FieldEncryptionExecutor, Clock, EncryptionExecutor |
| 38 |
from confluent_kafka.schema_registry.rules.encryption.gcpkms.gcp_driver import \ |
| 39 |
GcpKmsDriver |
| 40 |
from confluent_kafka.schema_registry.rules.encryption.hcvault.hcvault_driver import \ |
| 41 |
HcVaultKmsDriver |
| 42 |
from confluent_kafka.schema_registry.rules.encryption.localkms.local_driver import \ |
| 43 |
LocalKmsDriver |
| 44 |
from confluent_kafka.schema_registry.rules.jsonata.jsonata_executor import \ |
| 45 |
JsonataExecutor |
| 46 |
from confluent_kafka.schema_registry.schema_registry_client import RuleSet, \ |
| 47 |
Rule, RuleKind, RuleMode, RuleParams, ServerConfig |
| 48 |
from confluent_kafka.schema_registry.serde import RuleConditionError |
| 49 |
from confluent_kafka.serialization import SerializationContext, MessageField, SerializationError |
| 50 |
|
| 51 |
|
| 52 |
proto_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data', 'proto') |
| 53 |
if proto_path not in sys.path: |
| 54 |
sys.path.insert(0, proto_path) |
| 55 |
|
| 56 |
from tests.schema_registry.data.proto import example_pb2, nested_pb2, test_pb2, dep_pb2, cycle_pb2, \ |
| 57 |
widget_pb2, newwidget_pb2, newerwidget_pb2 |
| 58 |
|
| 59 |
|
| 60 |
class FakeClock(Clock): |
| 61 |
|
| 62 |
def __init__(self): |
| 63 |
self.fixed_now = int(round(time.time() * 1000)) |
| 64 |
|
| 65 |
def now(self) -> int: |
| 66 |
return self.fixed_now |
| 67 |
|
| 68 |
|
| 69 |
CelExecutor.register() |
| 70 |
CelFieldExecutor.register() |
| 71 |
AwsKmsDriver.register() |
| 72 |
AzureKmsDriver.register() |
| 73 |
GcpKmsDriver.register() |
| 74 |
HcVaultKmsDriver.register() |
| 75 |
JsonataExecutor.register() |
| 76 |
LocalKmsDriver.register() |
| 77 |
|
| 78 |
_BASE_URL = "mock://" |
| 79 |
|
| 80 |
_TOPIC = "topic1" |
| 81 |
_SUBJECT = _TOPIC + "-value" |
| 82 |
|
| 83 |
|
| 84 |
@pytest.fixture(autouse=True) |
| 85 |
def run_before_and_after_tests(tmpdir): |
| 86 |
"""Fixture to execute asserts before and after a test is run""" |
| 87 |
|
| 88 |
|
| 89 |
yield |
| 90 |
|
| 91 |
|
| 92 |
conf = {'url': _BASE_URL} |
| 93 |
client = SchemaRegistryClient.new_client(conf) |
| 94 |
try: |
| 95 |
client.delete_subject(_SUBJECT, True) |
| 96 |
except Exception: |
| 97 |
pass |
| 98 |
subjects = client.get_subjects() |
| 99 |
for subject in subjects: |
| 100 |
try: |
| 101 |
client.delete_subject(subject, True) |
| 102 |
except Exception: |
| 103 |
pass |
| 104 |
|
| 105 |
|
| 106 |
def test_proto_basic_serialization(): |
| 107 |
conf = {'url': _BASE_URL} |
| 108 |
client = SchemaRegistryClient.new_client(conf) |
| 109 |
ser_conf = { |
| 110 |
'auto.register.schemas': True, |
| 111 |
'use.deprecated.format': False |
| 112 |
} |
| 113 |
obj = example_pb2.Author( |
| 114 |
name='Kafka', |
| 115 |
id=123, |
| 116 |
picture=b'foobar', |
| 117 |
works=['The Castle', 'TheTrial'], |
| 118 |
oneof_string='oneof' |
| 119 |
) |
| 120 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 121 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 122 |
obj_bytes = ser(obj, ser_ctx) |
| 123 |
|
| 124 |
deser_conf = { |
| 125 |
'use.deprecated.format': False |
| 126 |
} |
| 127 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf, client) |
| 128 |
obj2 = deser(obj_bytes, ser_ctx) |
| 129 |
assert obj == obj2 |
| 130 |
|
| 131 |
|
| 132 |
def test_proto_guid_in_header(): |
| 133 |
conf = {'url': _BASE_URL} |
| 134 |
client = SchemaRegistryClient.new_client(conf) |
| 135 |
ser_conf = { |
| 136 |
'auto.register.schemas': True, |
| 137 |
'schema.id.serializer': header_schema_id_serializer |
| 138 |
} |
| 139 |
obj = example_pb2.Author( |
| 140 |
name='Kafka', |
| 141 |
id=123, |
| 142 |
picture=b'foobar', |
| 143 |
works=['The Castle', 'TheTrial'], |
| 144 |
oneof_string='oneof' |
| 145 |
) |
| 146 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 147 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE, {}) |
| 148 |
obj_bytes = ser(obj, ser_ctx) |
| 149 |
|
| 150 |
deser_conf = { |
| 151 |
} |
| 152 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf, client) |
| 153 |
obj2 = deser(obj_bytes, ser_ctx) |
| 154 |
assert obj == obj2 |
| 155 |
|
| 156 |
|
| 157 |
def test_proto_basic_deserialization_no_client(): |
| 158 |
conf = {'url': _BASE_URL} |
| 159 |
client = SchemaRegistryClient.new_client(conf) |
| 160 |
ser_conf = { |
| 161 |
'auto.register.schemas': True, |
| 162 |
'use.deprecated.format': False |
| 163 |
} |
| 164 |
obj = example_pb2.Author( |
| 165 |
name='Kafka', |
| 166 |
id=123, |
| 167 |
picture=b'foobar', |
| 168 |
works=['The Castle', 'TheTrial'], |
| 169 |
oneof_string='oneof' |
| 170 |
) |
| 171 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 172 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 173 |
obj_bytes = ser(obj, ser_ctx) |
| 174 |
|
| 175 |
deser_conf = { |
| 176 |
'use.deprecated.format': False |
| 177 |
} |
| 178 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf) |
| 179 |
obj2 = deser(obj_bytes, ser_ctx) |
| 180 |
assert obj == obj2 |
| 181 |
|
| 182 |
|
| 183 |
def test_proto_second_message(): |
| 184 |
conf = {'url': _BASE_URL} |
| 185 |
client = SchemaRegistryClient.new_client(conf) |
| 186 |
ser_conf = { |
| 187 |
'auto.register.schemas': True, |
| 188 |
'use.deprecated.format': False |
| 189 |
} |
| 190 |
obj = example_pb2.Pizza( |
| 191 |
size="large", |
| 192 |
toppings=["cheese", "pepperoni"], |
| 193 |
) |
| 194 |
ser = ProtobufSerializer(example_pb2.Pizza, client, conf=ser_conf) |
| 195 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 196 |
obj_bytes = ser(obj, ser_ctx) |
| 197 |
|
| 198 |
deser_conf = { |
| 199 |
'use.deprecated.format': False |
| 200 |
} |
| 201 |
deser = ProtobufDeserializer(example_pb2.Pizza, deser_conf, client) |
| 202 |
obj2 = deser(obj_bytes, ser_ctx) |
| 203 |
assert obj == obj2 |
| 204 |
|
| 205 |
|
| 206 |
def test_proto_nested_message(): |
| 207 |
conf = {'url': _BASE_URL} |
| 208 |
client = SchemaRegistryClient.new_client(conf) |
| 209 |
ser_conf = { |
| 210 |
'auto.register.schemas': True, |
| 211 |
'use.deprecated.format': False |
| 212 |
} |
| 213 |
obj = nested_pb2.NestedMessage.InnerMessage( |
| 214 |
id="inner", |
| 215 |
) |
| 216 |
ser = ProtobufSerializer(nested_pb2.NestedMessage.InnerMessage, client, conf=ser_conf) |
| 217 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 218 |
obj_bytes = ser(obj, ser_ctx) |
| 219 |
|
| 220 |
deser_conf = { |
| 221 |
'use.deprecated.format': False |
| 222 |
} |
| 223 |
deser = ProtobufDeserializer(nested_pb2.NestedMessage.InnerMessage, deser_conf, client) |
| 224 |
obj2 = deser(obj_bytes, ser_ctx) |
| 225 |
assert obj == obj2 |
| 226 |
|
| 227 |
|
| 228 |
def test_proto_reference(): |
| 229 |
conf = {'url': _BASE_URL} |
| 230 |
client = SchemaRegistryClient.new_client(conf) |
| 231 |
ser_conf = { |
| 232 |
'auto.register.schemas': True, |
| 233 |
'use.deprecated.format': False |
| 234 |
} |
| 235 |
msg = test_pb2.TestMessage( |
| 236 |
test_string="hi", |
| 237 |
test_bool=True, |
| 238 |
test_bytes=b'foobar', |
| 239 |
test_double=1.23, |
| 240 |
test_float=3.45, |
| 241 |
test_fixed32=67, |
| 242 |
test_fixed64=89, |
| 243 |
test_int32=100, |
| 244 |
test_int64=200, |
| 245 |
test_sfixed32=300, |
| 246 |
test_sfixed64=400, |
| 247 |
test_sint32=500, |
| 248 |
test_sint64=600, |
| 249 |
test_uint32=700, |
| 250 |
test_uint64=800, |
| 251 |
) |
| 252 |
obj = dep_pb2.DependencyMessage( |
| 253 |
is_active=True, |
| 254 |
test_message=msg |
| 255 |
) |
| 256 |
|
| 257 |
ser = ProtobufSerializer(dep_pb2.DependencyMessage, client, conf=ser_conf) |
| 258 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 259 |
obj_bytes = ser(obj, ser_ctx) |
| 260 |
|
| 261 |
deser_conf = { |
| 262 |
'use.deprecated.format': False |
| 263 |
} |
| 264 |
deser = ProtobufDeserializer(dep_pb2.DependencyMessage, deser_conf, client) |
| 265 |
obj2 = deser(obj_bytes, ser_ctx) |
| 266 |
assert obj == obj2 |
| 267 |
|
| 268 |
|
| 269 |
def test_proto_cycle(): |
| 270 |
conf = {'url': _BASE_URL} |
| 271 |
client = SchemaRegistryClient.new_client(conf) |
| 272 |
ser_conf = { |
| 273 |
'auto.register.schemas': True, |
| 274 |
'use.deprecated.format': False |
| 275 |
} |
| 276 |
inner = cycle_pb2.LinkedList( |
| 277 |
value=100 |
| 278 |
) |
| 279 |
obj = cycle_pb2.LinkedList( |
| 280 |
value=200, |
| 281 |
next=inner |
| 282 |
) |
| 283 |
|
| 284 |
ser = ProtobufSerializer(cycle_pb2.LinkedList, client, conf=ser_conf) |
| 285 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 286 |
obj_bytes = ser(obj, ser_ctx) |
| 287 |
|
| 288 |
deser_conf = { |
| 289 |
'use.deprecated.format': False |
| 290 |
} |
| 291 |
deser = ProtobufDeserializer(cycle_pb2.LinkedList, deser_conf, client) |
| 292 |
obj2 = deser(obj_bytes, ser_ctx) |
| 293 |
assert obj == obj2 |
| 294 |
|
| 295 |
|
| 296 |
def test_proto_cel_condition(): |
| 297 |
conf = {'url': _BASE_URL} |
| 298 |
client = SchemaRegistryClient.new_client(conf) |
| 299 |
ser_conf = { |
| 300 |
'auto.register.schemas': False, |
| 301 |
'use.latest.version': True, |
| 302 |
'use.deprecated.format': False |
| 303 |
} |
| 304 |
rule = Rule( |
| 305 |
"test-cel", |
| 306 |
"", |
| 307 |
RuleKind.CONDITION, |
| 308 |
RuleMode.WRITE, |
| 309 |
"CEL", |
| 310 |
None, |
| 311 |
None, |
| 312 |
"message.name == 'Kafka'", |
| 313 |
None, |
| 314 |
None, |
| 315 |
False |
| 316 |
) |
| 317 |
client.register_schema(_SUBJECT, Schema( |
| 318 |
_schema_to_str(example_pb2.Author.DESCRIPTOR.file), |
| 319 |
"PROTOBUF", |
| 320 |
[], |
| 321 |
None, |
| 322 |
RuleSet(None, [rule]) |
| 323 |
)) |
| 324 |
obj = example_pb2.Author( |
| 325 |
name='Kafka', |
| 326 |
id=123, |
| 327 |
picture=b'foobar', |
| 328 |
works=['The Castle', 'TheTrial'], |
| 329 |
oneof_string='oneof' |
| 330 |
) |
| 331 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 332 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 333 |
obj_bytes = ser(obj, ser_ctx) |
| 334 |
|
| 335 |
deser_conf = { |
| 336 |
'use.deprecated.format': False |
| 337 |
} |
| 338 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf, client) |
| 339 |
obj2 = deser(obj_bytes, ser_ctx) |
| 340 |
assert obj == obj2 |
| 341 |
|
| 342 |
|
| 343 |
def test_proto_cel_condition_fail(): |
| 344 |
conf = {'url': _BASE_URL} |
| 345 |
client = SchemaRegistryClient.new_client(conf) |
| 346 |
ser_conf = { |
| 347 |
'auto.register.schemas': False, |
| 348 |
'use.latest.version': True, |
| 349 |
'use.deprecated.format': False |
| 350 |
} |
| 351 |
rule = Rule( |
| 352 |
"test-cel", |
| 353 |
"", |
| 354 |
RuleKind.CONDITION, |
| 355 |
RuleMode.WRITE, |
| 356 |
"CEL", |
| 357 |
None, |
| 358 |
None, |
| 359 |
"message.name != 'Kafka'", |
| 360 |
None, |
| 361 |
None, |
| 362 |
False |
| 363 |
) |
| 364 |
client.register_schema(_SUBJECT, Schema( |
| 365 |
_schema_to_str(example_pb2.Author.DESCRIPTOR.file), |
| 366 |
"PROTOBUF", |
| 367 |
[], |
| 368 |
None, |
| 369 |
RuleSet(None, [rule]) |
| 370 |
)) |
| 371 |
obj = example_pb2.Author( |
| 372 |
name='Kafka', |
| 373 |
id=123, |
| 374 |
picture=b'foobar', |
| 375 |
works=['The Castle', 'TheTrial'], |
| 376 |
oneof_string='oneof' |
| 377 |
) |
| 378 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 379 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 380 |
with pytest.raises(SerializationError) as e: |
| 381 |
ser(obj, ser_ctx) |
| 382 |
assert isinstance(e.value.__cause__, RuleConditionError) |
| 383 |
|
| 384 |
|
| 385 |
def test_proto_cel_field_transform(): |
| 386 |
conf = {'url': _BASE_URL} |
| 387 |
client = SchemaRegistryClient.new_client(conf) |
| 388 |
ser_conf = { |
| 389 |
'auto.register.schemas': False, |
| 390 |
'use.latest.version': True, |
| 391 |
'use.deprecated.format': False |
| 392 |
} |
| 393 |
rule = Rule( |
| 394 |
"test-cel", |
| 395 |
"", |
| 396 |
RuleKind.TRANSFORM, |
| 397 |
RuleMode.WRITE, |
| 398 |
"CEL_FIELD", |
| 399 |
None, |
| 400 |
None, |
| 401 |
"typeName == 'STRING' ; value + '-suffix'", |
| 402 |
None, |
| 403 |
None, |
| 404 |
False |
| 405 |
) |
| 406 |
client.register_schema(_SUBJECT, Schema( |
| 407 |
_schema_to_str(example_pb2.Author.DESCRIPTOR.file), |
| 408 |
"PROTOBUF", |
| 409 |
[], |
| 410 |
None, |
| 411 |
RuleSet(None, [rule]) |
| 412 |
)) |
| 413 |
obj = example_pb2.Author( |
| 414 |
name='Kafka', |
| 415 |
id=123, |
| 416 |
picture=b'foobar', |
| 417 |
works=['The Castle', 'TheTrial'], |
| 418 |
oneof_string='oneof' |
| 419 |
) |
| 420 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 421 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 422 |
obj_bytes = ser(obj, ser_ctx) |
| 423 |
|
| 424 |
obj2 = example_pb2.Author( |
| 425 |
name='Kafka-suffix', |
| 426 |
id=123, |
| 427 |
picture=b'foobar', |
| 428 |
works=['The Castle-suffix', 'TheTrial-suffix'], |
| 429 |
oneof_string='oneof-suffix' |
| 430 |
) |
| 431 |
deser_conf = { |
| 432 |
'use.deprecated.format': False |
| 433 |
} |
| 434 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf, client) |
| 435 |
newobj = deser(obj_bytes, ser_ctx) |
| 436 |
assert obj2 == newobj |
| 437 |
|
| 438 |
|
| 439 |
def test_proto_cel_field_condition(): |
| 440 |
conf = {'url': _BASE_URL} |
| 441 |
client = SchemaRegistryClient.new_client(conf) |
| 442 |
ser_conf = { |
| 443 |
'auto.register.schemas': False, |
| 444 |
'use.latest.version': True, |
| 445 |
'use.deprecated.format': False |
| 446 |
} |
| 447 |
rule = Rule( |
| 448 |
"test-cel", |
| 449 |
"", |
| 450 |
RuleKind.CONDITION, |
| 451 |
RuleMode.WRITE, |
| 452 |
"CEL_FIELD", |
| 453 |
None, |
| 454 |
None, |
| 455 |
"name == 'name' ; value == 'Kafka'", |
| 456 |
None, |
| 457 |
None, |
| 458 |
False |
| 459 |
) |
| 460 |
client.register_schema(_SUBJECT, Schema( |
| 461 |
_schema_to_str(example_pb2.Author.DESCRIPTOR.file), |
| 462 |
"PROTOBUF", |
| 463 |
[], |
| 464 |
None, |
| 465 |
RuleSet(None, [rule]) |
| 466 |
)) |
| 467 |
obj = example_pb2.Author( |
| 468 |
name='Kafka', |
| 469 |
id=123, |
| 470 |
picture=b'foobar', |
| 471 |
works=['The Castle', 'TheTrial'], |
| 472 |
oneof_string='oneof' |
| 473 |
) |
| 474 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 475 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 476 |
obj_bytes = ser(obj, ser_ctx) |
| 477 |
|
| 478 |
deser_conf = { |
| 479 |
'use.deprecated.format': False |
| 480 |
} |
| 481 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf, client) |
| 482 |
newobj = deser(obj_bytes, ser_ctx) |
| 483 |
assert obj == newobj |
| 484 |
|
| 485 |
|
| 486 |
def test_proto_cel_field_condition_fail(): |
| 487 |
conf = {'url': _BASE_URL} |
| 488 |
client = SchemaRegistryClient.new_client(conf) |
| 489 |
ser_conf = { |
| 490 |
'auto.register.schemas': False, |
| 491 |
'use.latest.version': True, |
| 492 |
'use.deprecated.format': False |
| 493 |
} |
| 494 |
rule = Rule( |
| 495 |
"test-cel", |
| 496 |
"", |
| 497 |
RuleKind.CONDITION, |
| 498 |
RuleMode.WRITE, |
| 499 |
"CEL_FIELD", |
| 500 |
None, |
| 501 |
None, |
| 502 |
"name == 'name' ; value != 'Kafka'", |
| 503 |
None, |
| 504 |
None, |
| 505 |
False |
| 506 |
) |
| 507 |
client.register_schema(_SUBJECT, Schema( |
| 508 |
_schema_to_str(example_pb2.Author.DESCRIPTOR.file), |
| 509 |
"PROTOBUF", |
| 510 |
[], |
| 511 |
None, |
| 512 |
RuleSet(None, [rule]) |
| 513 |
)) |
| 514 |
obj = example_pb2.Author( |
| 515 |
name='Kafka', |
| 516 |
id=123, |
| 517 |
picture=b'foobar', |
| 518 |
works=['The Castle', 'TheTrial'], |
| 519 |
oneof_string='oneof' |
| 520 |
) |
| 521 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf) |
| 522 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 523 |
with pytest.raises(SerializationError) as e: |
| 524 |
ser(obj, ser_ctx) |
| 525 |
assert isinstance(e.value.__cause__, RuleConditionError) |
| 526 |
|
| 527 |
|
| 528 |
def test_proto_encryption(): |
| 529 |
executor = FieldEncryptionExecutor.register_with_clock(FakeClock()) |
| 530 |
|
| 531 |
conf = {'url': _BASE_URL} |
| 532 |
client = SchemaRegistryClient.new_client(conf) |
| 533 |
ser_conf = { |
| 534 |
'auto.register.schemas': False, |
| 535 |
'use.latest.version': True, |
| 536 |
'use.deprecated.format': False |
| 537 |
} |
| 538 |
rule_conf = {'secret': 'mysecret'} |
| 539 |
rule = Rule( |
| 540 |
"test-encrypt", |
| 541 |
"", |
| 542 |
RuleKind.TRANSFORM, |
| 543 |
RuleMode.WRITEREAD, |
| 544 |
"ENCRYPT", |
| 545 |
["PII"], |
| 546 |
RuleParams({ |
| 547 |
"encrypt.kek.name": "kek1", |
| 548 |
"encrypt.kms.type": "local-kms", |
| 549 |
"encrypt.kms.key.id": "mykey" |
| 550 |
}), |
| 551 |
None, |
| 552 |
None, |
| 553 |
"ERROR,NONE", |
| 554 |
False |
| 555 |
) |
| 556 |
client.register_schema(_SUBJECT, Schema( |
| 557 |
_schema_to_str(example_pb2.Author.DESCRIPTOR.file), |
| 558 |
"PROTOBUF", |
| 559 |
[], |
| 560 |
None, |
| 561 |
RuleSet(None, [rule]) |
| 562 |
)) |
| 563 |
obj = example_pb2.Author( |
| 564 |
name='Kafka', |
| 565 |
id=123, |
| 566 |
picture=b'foobar', |
| 567 |
works=['The Castle', 'TheTrial'], |
| 568 |
oneof_string='oneof' |
| 569 |
) |
| 570 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf, rule_conf=rule_conf) |
| 571 |
dek_client = executor.executor.client |
| 572 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 573 |
obj_bytes = ser(obj, ser_ctx) |
| 574 |
|
| 575 |
|
| 576 |
assert obj.name != 'Kafka' |
| 577 |
obj = example_pb2.Author( |
| 578 |
name='Kafka', |
| 579 |
id=123, |
| 580 |
picture=b'foobar', |
| 581 |
works=['The Castle', 'TheTrial'], |
| 582 |
oneof_string='oneof' |
| 583 |
) |
| 584 |
|
| 585 |
deser_conf = { |
| 586 |
'use.deprecated.format': False |
| 587 |
} |
| 588 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf, client, rule_conf=rule_conf) |
| 589 |
executor.executor.client = dek_client |
| 590 |
obj2 = deser(obj_bytes, ser_ctx) |
| 591 |
assert obj == obj2 |
| 592 |
|
| 593 |
|
| 594 |
def test_proto_payload_encryption(): |
| 595 |
executor = EncryptionExecutor.register_with_clock(FakeClock()) |
| 596 |
|
| 597 |
conf = {'url': _BASE_URL} |
| 598 |
client = SchemaRegistryClient.new_client(conf) |
| 599 |
ser_conf = { |
| 600 |
'auto.register.schemas': False, |
| 601 |
'use.latest.version': True, |
| 602 |
'use.deprecated.format': False |
| 603 |
} |
| 604 |
rule_conf = {'secret': 'mysecret'} |
| 605 |
rule = Rule( |
| 606 |
"test-encrypt", |
| 607 |
"", |
| 608 |
RuleKind.TRANSFORM, |
| 609 |
RuleMode.WRITEREAD, |
| 610 |
"ENCRYPT_PAYLOAD", |
| 611 |
None, |
| 612 |
RuleParams({ |
| 613 |
"encrypt.kek.name": "kek1", |
| 614 |
"encrypt.kms.type": "local-kms", |
| 615 |
"encrypt.kms.key.id": "mykey" |
| 616 |
}), |
| 617 |
None, |
| 618 |
None, |
| 619 |
"ERROR,NONE", |
| 620 |
False |
| 621 |
) |
| 622 |
client.register_schema(_SUBJECT, Schema( |
| 623 |
_schema_to_str(example_pb2.Author.DESCRIPTOR.file), |
| 624 |
"PROTOBUF", |
| 625 |
[], |
| 626 |
None, |
| 627 |
RuleSet(None, None, [rule]) |
| 628 |
)) |
| 629 |
obj = example_pb2.Author( |
| 630 |
name='Kafka', |
| 631 |
id=123, |
| 632 |
picture=b'foobar', |
| 633 |
works=['The Castle', 'TheTrial'], |
| 634 |
oneof_string='oneof' |
| 635 |
) |
| 636 |
ser = ProtobufSerializer(example_pb2.Author, client, conf=ser_conf, rule_conf=rule_conf) |
| 637 |
dek_client = executor.client |
| 638 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 639 |
obj_bytes = ser(obj, ser_ctx) |
| 640 |
|
| 641 |
deser_conf = { |
| 642 |
'use.deprecated.format': False |
| 643 |
} |
| 644 |
deser = ProtobufDeserializer(example_pb2.Author, deser_conf, client, rule_conf=rule_conf) |
| 645 |
executor.client = dek_client |
| 646 |
obj2 = deser(obj_bytes, ser_ctx) |
| 647 |
assert obj == obj2 |
| 648 |
|
| 649 |
|
| 650 |
def test_proto_jsonata_fully_compatible(): |
| 651 |
rule1_to_2 = "$merge([$sift($, function($v, $k) {$k != 'size'}), {'height': $.'size'}])" |
| 652 |
rule2_to_1 = "$merge([$sift($, function($v, $k) {$k != 'height'}), {'size': $.'height'}])" |
| 653 |
rule2_to_3 = "$merge([$sift($, function($v, $k) {$k != 'height'}), {'length': $.'height'}])" |
| 654 |
rule3_to_2 = "$merge([$sift($, function($v, $k) {$k != 'length'}), {'height': $.'length'}])" |
| 655 |
|
| 656 |
conf = {'url': _BASE_URL} |
| 657 |
client = SchemaRegistryClient.new_client(conf) |
| 658 |
|
| 659 |
client.set_config(_SUBJECT, ServerConfig( |
| 660 |
compatibility_group='application.version' |
| 661 |
)) |
| 662 |
|
| 663 |
client.register_schema(_SUBJECT, Schema( |
| 664 |
_schema_to_str(widget_pb2.Widget.DESCRIPTOR.file), |
| 665 |
"PROTOBUF", |
| 666 |
[], |
| 667 |
Metadata( |
| 668 |
None, |
| 669 |
MetadataProperties({"application.version": "v1"}), |
| 670 |
None |
| 671 |
), |
| 672 |
None |
| 673 |
)) |
| 674 |
|
| 675 |
rule1 = Rule( |
| 676 |
"rule1", |
| 677 |
"", |
| 678 |
RuleKind.TRANSFORM, |
| 679 |
RuleMode.UPGRADE, |
| 680 |
"JSONATA", |
| 681 |
None, |
| 682 |
None, |
| 683 |
rule1_to_2, |
| 684 |
None, |
| 685 |
None, |
| 686 |
False |
| 687 |
) |
| 688 |
rule2 = Rule( |
| 689 |
"rule2", |
| 690 |
"", |
| 691 |
RuleKind.TRANSFORM, |
| 692 |
RuleMode.DOWNGRADE, |
| 693 |
"JSONATA", |
| 694 |
None, |
| 695 |
None, |
| 696 |
rule2_to_1, |
| 697 |
None, |
| 698 |
None, |
| 699 |
False |
| 700 |
) |
| 701 |
client.register_schema(_SUBJECT, Schema( |
| 702 |
_schema_to_str(newwidget_pb2.NewWidget.DESCRIPTOR.file), |
| 703 |
"PROTOBUF", |
| 704 |
[], |
| 705 |
Metadata( |
| 706 |
None, |
| 707 |
MetadataProperties({"application.version": "v2"}), |
| 708 |
None |
| 709 |
), |
| 710 |
RuleSet([rule1, rule2], None) |
| 711 |
)) |
| 712 |
|
| 713 |
rule3 = Rule( |
| 714 |
"rule3", |
| 715 |
"", |
| 716 |
RuleKind.TRANSFORM, |
| 717 |
RuleMode.UPGRADE, |
| 718 |
"JSONATA", |
| 719 |
None, |
| 720 |
None, |
| 721 |
rule2_to_3, |
| 722 |
None, |
| 723 |
None, |
| 724 |
False |
| 725 |
) |
| 726 |
rule4 = Rule( |
| 727 |
"rule4", |
| 728 |
"", |
| 729 |
RuleKind.TRANSFORM, |
| 730 |
RuleMode.DOWNGRADE, |
| 731 |
"JSONATA", |
| 732 |
None, |
| 733 |
None, |
| 734 |
rule3_to_2, |
| 735 |
None, |
| 736 |
None, |
| 737 |
False |
| 738 |
) |
| 739 |
client.register_schema(_SUBJECT, Schema( |
| 740 |
_schema_to_str(newerwidget_pb2.NewerWidget.DESCRIPTOR.file), |
| 741 |
"PROTOBUF", |
| 742 |
[], |
| 743 |
Metadata( |
| 744 |
None, |
| 745 |
MetadataProperties({"application.version": "v3"}), |
| 746 |
None |
| 747 |
), |
| 748 |
RuleSet([rule3, rule4], None) |
| 749 |
)) |
| 750 |
|
| 751 |
obj = widget_pb2.Widget( |
| 752 |
name='alice', |
| 753 |
size=123, |
| 754 |
version=1 |
| 755 |
) |
| 756 |
obj2 = newwidget_pb2.NewWidget( |
| 757 |
name='alice', |
| 758 |
height=123, |
| 759 |
version=1 |
| 760 |
) |
| 761 |
obj3 = newerwidget_pb2.NewerWidget( |
| 762 |
name='alice', |
| 763 |
length=123, |
| 764 |
version=1 |
| 765 |
) |
| 766 |
|
| 767 |
ser_conf = { |
| 768 |
'auto.register.schemas': False, |
| 769 |
'use.latest.version': False, |
| 770 |
'use.latest.with.metadata': { |
| 771 |
'application.version': 'v1' |
| 772 |
}, |
| 773 |
'use.deprecated.format': False |
| 774 |
} |
| 775 |
ser = ProtobufSerializer(widget_pb2.Widget, client, conf=ser_conf) |
| 776 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 777 |
obj_bytes = ser(obj, ser_ctx) |
| 778 |
|
| 779 |
deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3) |
| 780 |
|
| 781 |
ser_conf = { |
| 782 |
'auto.register.schemas': False, |
| 783 |
'use.latest.version': False, |
| 784 |
'use.latest.with.metadata': { |
| 785 |
'application.version': 'v2' |
| 786 |
}, |
| 787 |
'use.deprecated.format': False |
| 788 |
} |
| 789 |
ser = ProtobufSerializer(newwidget_pb2.NewWidget, client, conf=ser_conf) |
| 790 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 791 |
obj_bytes = ser(obj2, ser_ctx) |
| 792 |
|
| 793 |
deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3) |
| 794 |
|
| 795 |
ser_conf = { |
| 796 |
'auto.register.schemas': False, |
| 797 |
'use.latest.version': False, |
| 798 |
'use.latest.with.metadata': { |
| 799 |
'application.version': 'v3' |
| 800 |
}, |
| 801 |
'use.deprecated.format': False |
| 802 |
} |
| 803 |
ser = ProtobufSerializer(newerwidget_pb2.NewerWidget, client, conf=ser_conf) |
| 804 |
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE) |
| 805 |
obj_bytes = ser(obj3, ser_ctx) |
| 806 |
|
| 807 |
deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3) |
| 808 |
|
| 809 |
|
| 810 |
def deserialize_with_all_versions(client, ser_ctx, obj_bytes, obj, obj2, obj3): |
| 811 |
deser_conf = { |
| 812 |
'use.latest.with.metadata': { |
| 813 |
'application.version': 'v1' |
| 814 |
}, |
| 815 |
'use.deprecated.format': False |
| 816 |
} |
| 817 |
deser = ProtobufDeserializer(widget_pb2.Widget, deser_conf, client) |
| 818 |
newobj = deser(obj_bytes, ser_ctx) |
| 819 |
assert obj.size == newobj.size |
| 820 |
|
| 821 |
deser_conf = { |
| 822 |
'use.latest.with.metadata': { |
| 823 |
'application.version': 'v2' |
| 824 |
}, |
| 825 |
'use.deprecated.format': False |
| 826 |
} |
| 827 |
deser = ProtobufDeserializer(newwidget_pb2.NewWidget, deser_conf, client) |
| 828 |
newobj = deser(obj_bytes, ser_ctx) |
| 829 |
assert obj2.height == newobj.height |
| 830 |
|
| 831 |
deser_conf = { |
| 832 |
'use.latest.with.metadata': { |
| 833 |
'application.version': 'v3' |
| 834 |
}, |
| 835 |
'use.deprecated.format': False |
| 836 |
} |
| 837 |
deser = ProtobufDeserializer(newerwidget_pb2.NewerWidget, deser_conf, client) |
| 838 |
newobj = deser(obj_bytes, ser_ctx) |
| 839 |
assert obj3.length == newobj.length |