Skip to content

Commit 1efe92c

Browse files
committed
Add test
1 parent 7e1d0a6 commit 1efe92c

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

schemaregistry/rules/encryption/encrypt-executor.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,15 @@ export class KmsClientWrapper implements KmsClient {
658658

659659
getKmsKeyIds(): string[] {
660660
let kmsKeyIds = [this.kek.kmsKeyId!]
661+
let alternateKmsKeyIds: string | undefined
661662
if (this.kek.kmsProps != null) {
662-
let alternateKmsKeyIds = this.kek.kmsProps[ENCRYPT_ALTERNATE_KMS_KEY_IDS]
663-
if (alternateKmsKeyIds != null) {
664-
kmsKeyIds = kmsKeyIds.concat(alternateKmsKeyIds.split(',').map(id => id.trim()))
665-
}
663+
alternateKmsKeyIds = this.kek.kmsProps[ENCRYPT_ALTERNATE_KMS_KEY_IDS]
664+
}
665+
if (alternateKmsKeyIds == null) {
666+
alternateKmsKeyIds = this.config.get(ENCRYPT_ALTERNATE_KMS_KEY_IDS)
667+
}
668+
if (alternateKmsKeyIds != null) {
669+
kmsKeyIds = kmsKeyIds.concat(alternateKmsKeyIds.split(',').map(id => id.trim()))
666670
}
667671
return kmsKeyIds
668672
}

schemaregistry/test/serde/avro.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,69 @@ describe('AvroSerializer', () => {
11931193
expect(obj2.boolField).toEqual(obj.boolField);
11941194
expect(obj2.bytesField).toEqual(obj.bytesField);
11951195
})
1196+
it('encryption with alternate keks', async () => {
1197+
let conf: ClientConfig = {
1198+
baseURLs: [baseURL],
1199+
cacheCapacity: 1000
1200+
}
1201+
let client = SchemaRegistryClient.newClient(conf)
1202+
let serConfig: AvroSerializerConfig = {
1203+
useLatestVersion: true,
1204+
ruleConfig: {
1205+
secret: 'mysecret',
1206+
'encrypt.alternate.kms.key.ids': 'mykey2,mykey3'
1207+
}
1208+
}
1209+
let ser = new AvroSerializer(client, SerdeType.VALUE, serConfig)
1210+
let dekClient = encryptionExecutor.client!
1211+
1212+
let encRule: Rule = {
1213+
name: 'test-encrypt',
1214+
kind: 'TRANSFORM',
1215+
mode: RuleMode.WRITEREAD,
1216+
type: 'ENCRYPT_PAYLOAD',
1217+
params: {
1218+
'encrypt.kek.name': 'kek1',
1219+
'encrypt.kms.type': 'local-kms',
1220+
'encrypt.kms.key.id': 'mykey',
1221+
},
1222+
onFailure: 'ERROR,NONE'
1223+
}
1224+
let ruleSet: RuleSet = {
1225+
encodingRules: [encRule]
1226+
}
1227+
1228+
let info: SchemaInfo = {
1229+
schemaType: 'AVRO',
1230+
schema: demoSchema,
1231+
ruleSet
1232+
}
1233+
1234+
await client.register(subject, info, false)
1235+
1236+
let obj = {
1237+
intField: 123,
1238+
doubleField: 45.67,
1239+
stringField: 'hi',
1240+
boolField: true,
1241+
bytesField: Buffer.from([1, 2]),
1242+
}
1243+
let bytes = await ser.serialize(topic, obj)
1244+
1245+
let deserConfig: AvroDeserializerConfig = {
1246+
ruleConfig: {
1247+
secret: 'mysecret'
1248+
}
1249+
}
1250+
let deser = new AvroDeserializer(client, SerdeType.VALUE, deserConfig)
1251+
encryptionExecutor.client = dekClient
1252+
let obj2 = await deser.deserialize(topic, bytes)
1253+
expect(obj2.intField).toEqual(obj.intField);
1254+
expect(obj2.doubleField).toBeCloseTo(obj.doubleField, 0.001);
1255+
expect(obj2.stringField).toEqual(obj.stringField);
1256+
expect(obj2.boolField).toEqual(obj.boolField);
1257+
expect(obj2.bytesField).toEqual(obj.bytesField);
1258+
})
11961259
it('deterministic encryption', async () => {
11971260
let conf: ClientConfig = {
11981261
baseURLs: [baseURL],

0 commit comments

Comments
 (0)