forked from confluentinc/confluent-kafka-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtoSerializeDeserialize.cs
406 lines (348 loc) · 16 KB
/
ProtoSerializeDeserialize.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2020 Confluent Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Refer to LICENSE for more information.
// ConstructValueSubjectName is still used a an internal implementation detail.
#pragma warning disable CS0618
using System;
using Xunit;
using System.Collections.Generic;
using System.Linq;
using Confluent.Kafka;
using Confluent.SchemaRegistry.Encryption;
using Example;
using Google.Protobuf;
namespace Confluent.SchemaRegistry.Serdes.UnitTests
{
public class ProtobufSerializeDeserializeTests : BaseSerializeDeserializeTests
{
public ProtobufSerializeDeserializeTests() : base()
{
}
[Fact]
public void ParseSchema()
{
string schema = @"syntax = ""proto3"";
package io.confluent.kafka.serializers.protobuf.test;
import ""ref.proto"";
import ""confluent/meta.proto"";
message ReferrerMessage {
string root_id = 1 [(confluent.field_meta) = { doc: ""PII"" }];
ReferencedMessage ref = 2 [(confluent.field_meta).doc = ""PII""];
}";
string import = @"syntax = ""proto3"";
package io.confluent.kafka.serializers.protobuf.test;
message ReferencedMessage {
string ref_id = 1;
bool is_active = 2;
}
";
IDictionary<string, string> imports = new Dictionary<string, string>();
imports["ref.proto"] = import;
imports["confluent/meta.proto"] = "doesn't matter, will be overwritten anyway";
var fds = ProtobufUtils.Parse(schema, imports);
Assert.Equal(4, fds.Files.Count);
var fileNames = fds.Files.Select(s => s.Name).ToHashSet();
Assert.Contains("__root.proto", fileNames);
Assert.Contains("ref.proto", fileNames);
Assert.Contains("confluent/meta.proto", fileNames);
Assert.Contains("google/protobuf/descriptor.proto", fileNames);
var rootFile = fds.Files.First(s => s.Name == "__root.proto");
Assert.Equal(1, rootFile.MessageTypes.Count);
Assert.Equal("ReferrerMessage", rootFile.MessageTypes.First().Name);
}
[Fact]
public void Null()
{
var protoSerializer = new ProtobufSerializer<UInt32Value>(schemaRegistryClient);
var protoDeserializer = new ProtobufDeserializer<UInt32Value>(schemaRegistryClient);
var bytes = protoSerializer.SerializeAsync(null, new SerializationContext(MessageComponentType.Value, testTopic)).Result;
Assert.Null(bytes);
Assert.Null(protoDeserializer.DeserializeAsync(bytes, true, new SerializationContext(MessageComponentType.Value, testTopic)).Result);
}
[Fact]
public void UInt32SerDe()
{
var protoSerializer = new ProtobufSerializer<UInt32Value>(schemaRegistryClient);
var protoDeserializer = new ProtobufDeserializer<UInt32Value>();
var v = new UInt32Value { Value = 1234 };
var bytes = protoSerializer.SerializeAsync(v, new SerializationContext(MessageComponentType.Value, testTopic)).Result;
Assert.Equal(v.Value, protoDeserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result.Value);
}
[Fact]
public void CELCondition()
{
string schemaStr = @"syntax = ""proto3"";
import ""confluent/meta.proto"";
package example;
message Person {
string favorite_color = 1;
int32 favorite_number = 2;
string name = 3;
}";
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Protobuf, null);
schema.RuleSet = new RuleSet(new List<Rule>(),
new List<Rule>
{
new Rule("testCEL", RuleKind.Condition, RuleMode.Write, "CEL", null, null,
"message.name == 'awesome'", null, null, false)
}
);
store[schemaStr] = 1;
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
var config = new ProtobufSerializerConfig
{
AutoRegisterSchemas = false,
UseLatestVersion = true
};
var serializer = new ProtobufSerializer<Person>(schemaRegistryClient, config);
var deserializer = new ProtobufDeserializer<Person>(schemaRegistryClient, null);
var user = new Person
{
FavoriteColor = "blue",
FavoriteNumber = 100,
Name = "awesome"
};
Headers headers = new Headers();
var bytes = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
Assert.Equal("awesome", result.Name);
Assert.Equal(user.FavoriteColor, result.FavoriteColor);
Assert.Equal(user.FavoriteNumber, result.FavoriteNumber);
}
[Fact]
public void CELConditionFail()
{
string schemaStr = @"syntax = ""proto3"";
import ""confluent/meta.proto"";
package example;
message Person {
string favorite_color = 1;
int32 favorite_number = 2;
string name = 3;
}";
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Protobuf, null);
schema.RuleSet = new RuleSet(new List<Rule>(),
new List<Rule>
{
new Rule("testCEL", RuleKind.Condition, RuleMode.Write, "CEL", null, null,
"message.name != 'awesome'", null, null, false)
}
);
store[schemaStr] = 1;
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
var config = new ProtobufSerializerConfig
{
AutoRegisterSchemas = false,
UseLatestVersion = true
};
var serializer = new ProtobufSerializer<Person>(schemaRegistryClient, config);
var user = new Person
{
FavoriteColor = "blue",
FavoriteNumber = 100,
Name = "awesome"
};
Headers headers = new Headers();
Assert.Throws<AggregateException>(() => serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result);
}
[Fact]
public void CELFieldTransform()
{
string schemaStr = @"syntax = ""proto3"";
import ""confluent/meta.proto"";
package example;
message Person {
string favorite_color = 1;
int32 favorite_number = 2;
string name = 3;
}";
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Protobuf, null);
schema.RuleSet = new RuleSet(new List<Rule>(),
new List<Rule>
{
new Rule("testCEL", RuleKind.Transform, RuleMode.Write, "CEL_FIELD", null, null,
"typeName == 'STRING' ; value + '-suffix'", null, null, false)
}
);
store[schemaStr] = 1;
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
var config = new ProtobufSerializerConfig
{
AutoRegisterSchemas = false,
UseLatestVersion = true
};
var serializer = new ProtobufSerializer<Person>(schemaRegistryClient, config);
var deserializer = new ProtobufDeserializer<Person>(schemaRegistryClient, null);
var user = new Person
{
FavoriteColor = "blue",
FavoriteNumber = 100,
Name = "awesome"
};
Headers headers = new Headers();
var bytes = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
Assert.Equal("awesome-suffix", result.Name);
Assert.Equal("blue-suffix", result.FavoriteColor);
Assert.Equal(user.FavoriteNumber, result.FavoriteNumber);
}
[Fact]
public void CELFieldCondition()
{
string schemaStr = @"syntax = ""proto3"";
import ""confluent/meta.proto"";
package example;
message Person {
string favorite_color = 1;
int32 favorite_number = 2;
string name = 3;
}";
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Protobuf, null);
schema.RuleSet = new RuleSet(new List<Rule>(),
new List<Rule>
{
new Rule("testCEL", RuleKind.Condition, RuleMode.Write, "CEL_FIELD", null, null,
"name == 'name' ; value == 'awesome'", null, null, false)
}
);
store[schemaStr] = 1;
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
var config = new ProtobufSerializerConfig
{
AutoRegisterSchemas = false,
UseLatestVersion = true
};
var serializer = new ProtobufSerializer<Person>(schemaRegistryClient, config);
var deserializer = new ProtobufDeserializer<Person>(schemaRegistryClient, null);
var user = new Person
{
FavoriteColor = "blue",
FavoriteNumber = 100,
Name = "awesome"
};
Headers headers = new Headers();
var bytes = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
Assert.Equal("awesome", result.Name);
Assert.Equal(user.FavoriteColor, result.FavoriteColor);
Assert.Equal(user.FavoriteNumber, result.FavoriteNumber);
}
[Fact]
public void CELFieldConditionFail()
{
string schemaStr = @"syntax = ""proto3"";
import ""confluent/meta.proto"";
package example;
message Person {
string favorite_color = 1;
int32 favorite_number = 2;
string name = 3;
}";
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Protobuf, null);
schema.RuleSet = new RuleSet(new List<Rule>(),
new List<Rule>
{
new Rule("testCEL", RuleKind.Condition, RuleMode.Write, "CEL_FIELD", null, null,
"name == 'name' ; value != 'awesome'", null, null, false)
}
);
store[schemaStr] = 1;
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
var config = new ProtobufSerializerConfig
{
AutoRegisterSchemas = false,
UseLatestVersion = true
};
var serializer = new ProtobufSerializer<Person>(schemaRegistryClient, config);
var user = new Person
{
FavoriteColor = "blue",
FavoriteNumber = 100,
Name = "awesome"
};
Headers headers = new Headers();
Assert.Throws<AggregateException>(() => serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result);
}
[Fact]
public void FieldEncryption()
{
string schemaStr = @"syntax = ""proto3"";
import ""confluent/meta.proto"";
package example;
message PersonWithPic {
string favorite_color = 1;
int32 favorite_number = 2;
string name = 3 [(.confluent.field_meta) = { tags: ""PII"" }];
bytes picture = 4 [(.confluent.field_meta) = { tags: ""PII"" }];
}";
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Protobuf, null);
schema.Metadata = new Metadata(new Dictionary<string, ISet<string>>
{
["example.PersonWithPic.name"] = new HashSet<string> { "PII" },
["example.PersonWithPic.picture"] = new HashSet<string> { "PII" }
}, new Dictionary<string, string>(), new HashSet<string>()
);
schema.RuleSet = new RuleSet(new List<Rule>(),
new List<Rule>
{
new Rule("encryptPII", RuleKind.Transform, RuleMode.WriteRead, "ENCRYPT", new HashSet<string>
{
"PII"
}, new Dictionary<string, string>
{
["encrypt.kek.name"] = "kek1",
["encrypt.kms.type"] = "local-kms",
["encrypt.kms.key.id"] = "mykey"
})
}
);
store[schemaStr] = 1;
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
var config = new ProtobufSerializerConfig
{
AutoRegisterSchemas = false,
UseLatestVersion = true
};
config.Set("rules.secret", "mysecret");
RuleRegistry ruleRegistry = new RuleRegistry();
IRuleExecutor ruleExecutor = new FieldEncryptionExecutor(dekRegistryClient, clock);
ruleRegistry.RegisterExecutor(ruleExecutor);
var serializer = new ProtobufSerializer<PersonWithPic>(schemaRegistryClient, config, ruleRegistry);
var deserializer = new ProtobufDeserializer<PersonWithPic>(schemaRegistryClient, null, ruleRegistry);
var pic = new byte[] { 1, 2, 3 };
var user = new PersonWithPic
{
FavoriteColor = "blue",
FavoriteNumber = 100,
Name = "awesome",
Picture = ByteString.CopyFrom(pic)
};
Headers headers = new Headers();
var bytes = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
// The user name has been modified
Assert.Equal("awesome", result.Name);
Assert.Equal(user.FavoriteColor, result.FavoriteColor);
Assert.Equal(user.FavoriteNumber, result.FavoriteNumber);
Assert.True(pic.SequenceEqual(result.Picture));
}
[Fact]
public void ProtobufDeserializerWithoutSchemaRegistry()
{
new ProtobufDeserializer<Person>();
Assert.True(true); // if constructor does not throw exception we're ok
}
}
}