@lucaapp/service-utils 4.0.1 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,6 +12,7 @@ declare class KafkaClient {
|
|
|
12
12
|
private readonly producer;
|
|
13
13
|
private readonly consumers;
|
|
14
14
|
readonly serviceIdentity: ServiceIdentity;
|
|
15
|
+
readonly encryptionEnabled: boolean;
|
|
15
16
|
constructor(parentLogger: Logger, kafkaConfig: KafkaConfiguration, topicSecrets: Partial<Record<KafkaTopic, string>>, serviceIdentity: ServiceIdentity);
|
|
16
17
|
connect: () => Promise<void>;
|
|
17
18
|
private getTopic;
|
|
@@ -88,6 +88,9 @@ class KafkaClient {
|
|
|
88
88
|
if (!value) {
|
|
89
89
|
throw (0, utils_1.logAndGetError)(this.logger, 'Invalid value argument `null | undefined` supplied.');
|
|
90
90
|
}
|
|
91
|
+
if (!this.encryptionEnabled) {
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
91
94
|
const jwe = await new jose.CompactEncrypt(new util_1.TextEncoder().encode(value));
|
|
92
95
|
jwe.setProtectedHeader({ alg: 'A256GCMKW', enc: 'A256GCM' });
|
|
93
96
|
return jwe.encrypt(this.getTopicSecret(topic));
|
|
@@ -96,16 +99,26 @@ class KafkaClient {
|
|
|
96
99
|
if (!jwe) {
|
|
97
100
|
return null;
|
|
98
101
|
}
|
|
102
|
+
if (!this.encryptionEnabled) {
|
|
103
|
+
return jwe;
|
|
104
|
+
}
|
|
99
105
|
const { plaintext } = await jose.compactDecrypt(jwe, this.getTopicSecret(topic));
|
|
100
106
|
return Buffer.from(plaintext);
|
|
101
107
|
};
|
|
102
108
|
this.generateSignature = async (value) => {
|
|
109
|
+
if (!this.encryptionEnabled) {
|
|
110
|
+
return '';
|
|
111
|
+
}
|
|
103
112
|
const privateKey = await this.serviceIdentity.getIdentityPrivateKey();
|
|
104
113
|
return await new jose.CompactSign(new util_1.TextEncoder().encode(value))
|
|
105
114
|
.setProtectedHeader({ alg: KEY_ALG })
|
|
106
115
|
.sign(privateKey);
|
|
107
116
|
};
|
|
108
117
|
this.verifySignature = async (kafkaTopic, value, headers) => {
|
|
118
|
+
if (!this.encryptionEnabled) {
|
|
119
|
+
this.logger.info('Skipping signature verification (encryption disabled)');
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
109
122
|
if (!headers || !headers.signature) {
|
|
110
123
|
throw (0, utils_1.logAndGetError)(this.logger, 'Unable to verify signature. Expected header not present');
|
|
111
124
|
}
|
|
@@ -245,6 +258,11 @@ class KafkaClient {
|
|
|
245
258
|
this.logger.error(error, 'Unable to properly disconnect kafka');
|
|
246
259
|
}
|
|
247
260
|
};
|
|
261
|
+
this.encryptionEnabled = kafkaConfig.encryptionEnabled ?? true;
|
|
262
|
+
if (kafkaConfig.encryptionEnabled &&
|
|
263
|
+
Object.keys(topicSecrets).length === 0) {
|
|
264
|
+
throw (0, utils_1.logAndGetError)(parentLogger, 'encryptionEnabled is true but no topicSecrets provided');
|
|
265
|
+
}
|
|
248
266
|
this.environment = kafkaConfig.environment;
|
|
249
267
|
this.logger = parentLogger.child({
|
|
250
268
|
kafkaClientId: kafkaConfig.clientId,
|