-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcsfle-schemaregistry.ts
More file actions
155 lines (128 loc) · 4.31 KB
/
csfle-schemaregistry.ts
File metadata and controls
155 lines (128 loc) · 4.31 KB
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
import {
AvroSerializer, AvroSerializerConfig, SerdeType,
AvroDeserializer, ClientConfig,
SchemaRegistryClient, SchemaInfo, Rule, RuleMode,
RuleRegistry, FieldEncryptionExecutor, AwsKmsDriver, RuleSet
} from "@confluentinc/schemaregistry";
import { CreateAxiosDefaults } from "axios";
import { KafkaJS } from '@confluentinc/kafka-javascript';
import {
basicAuthCredentials, clusterApiKey, clusterApiSecret,
clusterBootstrapUrl, baseUrl
} from "./constants";
FieldEncryptionExecutor.register();
AwsKmsDriver.register();
async function csfle() {
const schemaString: string = JSON.stringify({
type: 'record',
name: 'User',
fields: [
{ name: 'name', type: 'string' },
{ name: 'age', type: 'int' },
{
name: 'address', type: 'string',
"confluent:tags": ["PII"]
},
],
});
const createAxiosDefaults: CreateAxiosDefaults = {
timeout: 10000
};
const clientConfig: ClientConfig = {
baseURLs: [baseUrl],
createAxiosDefaults: createAxiosDefaults,
cacheCapacity: 512,
cacheLatestTtlSecs: 60,
basicAuthCredentials: basicAuthCredentials
};
const schemaRegistryClient = new SchemaRegistryClient(clientConfig);
const kafka: KafkaJS.Kafka = new KafkaJS.Kafka({
kafkaJS: {
brokers: [clusterBootstrapUrl],
ssl: true,
sasl: {
mechanism: 'plain',
username: clusterApiKey,
password: clusterApiSecret,
},
},
});
const producer: KafkaJS.Producer = kafka.producer({
kafkaJS: {
allowAutoTopicCreation: true,
acks: 1,
compression: KafkaJS.CompressionTypes.GZIP,
}
});
let encRule: Rule = {
name: 'test-encrypt',
kind: 'TRANSFORM',
mode: RuleMode.WRITEREAD,
type: 'ENCRYPT',
tags: ['PII'],
params: {
'encrypt.kek.name': 'csfle-example',
'encrypt.kms.type': 'aws-kms',
'encrypt.kms.key.id': 'your-key-id',
},
onFailure: 'ERROR,NONE'
};
let ruleSet: RuleSet = {
domainRules: [encRule]
};
const schemaInfo: SchemaInfo = {
schemaType: 'AVRO',
schema: schemaString,
ruleSet: ruleSet
};
const userInfo = { name: 'Alice N Bob', age: 30, address: '369 Main St' };
const userTopic = 'csfle-topic';
await schemaRegistryClient.register(userTopic+"-value", schemaInfo);
const serializerConfig: AvroSerializerConfig = { useLatestVersion: true };
const serializer: AvroSerializer = new AvroSerializer(schemaRegistryClient, SerdeType.VALUE, serializerConfig);
const outgoingMessage = {
key: "1",
value: await serializer.serialize(userTopic, userInfo)
};
console.log("Outgoing Message:", outgoingMessage);
await producer.connect();
await producer.send({
topic: userTopic,
messages: [outgoingMessage]
});
await producer.disconnect();
const consumer: KafkaJS.Consumer = kafka.consumer({
kafkaJS: {
groupId: 'demo-group',
fromBeginning: true,
partitionAssigners: [KafkaJS.PartitionAssigners.roundRobin],
},
});
await consumer.connect();
const deserializer: AvroDeserializer = new AvroDeserializer(schemaRegistryClient, SerdeType.VALUE, {});
await consumer.subscribe({ topic: userTopic });
let messageRcvd = false;
await consumer.run({
eachMessage: async ({ message }) => {
console.log("Message value", message.value);
const decodedMessage = {
...message,
value: await deserializer.deserialize(userTopic, message.value as Buffer)
};
console.log("Decoded message", decodedMessage);
let registry = new RuleRegistry();
const weakDeserializer: AvroDeserializer = new AvroDeserializer(schemaRegistryClient, SerdeType.VALUE, {}, registry);
const weakDecodedMessage = {
...message,
value: await weakDeserializer.deserialize(userTopic, message.value as Buffer)
};
console.log("Weak decoded message", weakDecodedMessage);
messageRcvd = true;
},
});
while (!messageRcvd) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
await consumer.disconnect();
}
csfle();