Skip to content

Commit 614ae0f

Browse files
committed
Add tests
1 parent f2c677c commit 614ae0f

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

src/Confluent.SchemaRegistry.Encryption/KmsClientWrapper.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,32 @@ private IList<string> GetKmsKeyIds()
7272
{
7373
IList<string> kmsKeyIds = new List<string>();
7474
kmsKeyIds.Add(Kek.KmsKeyId);
75-
if (Kek.KmsProps != null)
75+
string alternateKmsKeyIds = null;
76+
if (Kek.KmsProps != null && Kek.KmsProps.TryGetValue(EncryptionExecutor.EncryptAlternateKmsKeyIds, out alternateKmsKeyIds))
7677
{
77-
if (Kek.KmsProps.TryGetValue(EncryptionExecutor.EncryptAlternateKmsKeyIds, out string alternateKmsKeyIds))
78+
char[] separators = { ',' };
79+
string[] ids = alternateKmsKeyIds.Split(separators, StringSplitOptions.RemoveEmptyEntries);
80+
foreach (string id in ids) {
81+
if (!string.IsNullOrEmpty(id)) {
82+
kmsKeyIds.Add(id);
83+
}
84+
}
85+
} else
86+
{
87+
var kvp = Configs.FirstOrDefault(x =>
88+
x.Key == EncryptionExecutor.EncryptAlternateKmsKeyIds);
89+
if (!kvp.Equals(default(KeyValuePair<string, string>)))
7890
{
79-
char[] separators = { ',' };
80-
string[] ids = alternateKmsKeyIds.Split(separators, StringSplitOptions.RemoveEmptyEntries);
81-
foreach (string id in ids) {
82-
if (!string.IsNullOrEmpty(id)) {
83-
kmsKeyIds.Add(id);
84-
}
91+
alternateKmsKeyIds = kvp.Value;
92+
}
93+
}
94+
if (alternateKmsKeyIds != null)
95+
{
96+
char[] separators = { ',' };
97+
string[] ids = alternateKmsKeyIds.Split(separators, StringSplitOptions.RemoveEmptyEntries);
98+
foreach (string id in ids) {
99+
if (!string.IsNullOrEmpty(id)) {
100+
kmsKeyIds.Add(id);
85101
}
86102
}
87103
}

test/Confluent.SchemaRegistry.Serdes.UnitTests/SerializeDeserialize.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,69 @@ public void ISpecificRecordPayloadEncryption()
649649
Assert.True(pic.SequenceEqual(result.picture));
650650
}
651651

652+
[Fact]
653+
public void ISpecificRecordEncryptionAlternateKeks()
654+
{
655+
var schemaStr = "{\"type\":\"record\",\"name\":\"UserWithPic\",\"namespace\":\"Confluent.Kafka.Examples.AvroSpecific" +
656+
"\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\"," +
657+
"\"type\":[\"int\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}," +
658+
"{\"name\":\"picture\",\"type\":[\"null\",\"bytes\"],\"default\":null}]}";
659+
660+
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Avro, null);
661+
schema.Metadata = new Metadata(new Dictionary<string, ISet<string>>
662+
{
663+
["Confluent.Kafka.Examples.AvroSpecific.UserWithPic.name"] = new HashSet<string> { "PII" },
664+
["Confluent.Kafka.Examples.AvroSpecific.UserWithPic.picture"] = new HashSet<string> { "PII" }
665+
666+
}, new Dictionary<string, string>(), new HashSet<string>()
667+
);
668+
schema.RuleSet = new RuleSet(new List<Rule>(), new List<Rule>(),
669+
new List<Rule>
670+
{
671+
new Rule("encryptPII", RuleKind.Transform, RuleMode.WriteRead, "ENCRYPT_PAYLOAD", null,
672+
new Dictionary<string, string>
673+
{
674+
["encrypt.kek.name"] = "kek1",
675+
["encrypt.kms.type"] = "local-kms",
676+
["encrypt.kms.key.id"] = "mykey"
677+
})
678+
}
679+
);
680+
store[schemaStr] = 1;
681+
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
682+
var config = new AvroSerializerConfig
683+
{
684+
AutoRegisterSchemas = false,
685+
UseLatestVersion = true
686+
};
687+
config.Set("rules.secret", "mysecret");
688+
config.Set("rules.encrypt.alternate.kms.key.ids", "mykey2,mykey3");
689+
RuleRegistry ruleRegistry = new RuleRegistry();
690+
IRuleExecutor ruleExecutor = new EncryptionExecutor(dekRegistryClient, clock);
691+
ruleRegistry.RegisterExecutor(ruleExecutor);
692+
var serializer = new AvroSerializer<UserWithPic>(schemaRegistryClient, config, ruleRegistry);
693+
var deserializer = new AvroDeserializer<UserWithPic>(schemaRegistryClient, null, ruleRegistry);
694+
695+
var pic = new byte[] { 1, 2, 3 };
696+
var user = new UserWithPic()
697+
{
698+
favorite_color = "blue",
699+
favorite_number = 100,
700+
name = "awesome",
701+
picture = pic
702+
};
703+
704+
Headers headers = new Headers();
705+
var bytes = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
706+
var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
707+
708+
// The user name has been modified
709+
Assert.Equal("awesome", result.name);
710+
Assert.Equal(user.favorite_color, result.favorite_color);
711+
Assert.Equal(user.favorite_number, result.favorite_number);
712+
Assert.True(pic.SequenceEqual(result.picture));
713+
}
714+
652715
[Fact]
653716
public void ISpecificRecordFieldEncryptionDekRotation()
654717
{

0 commit comments

Comments
 (0)