@lucaapp/service-utils 4.0.0 → 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,
@@ -13,6 +13,7 @@ type KafkaConfiguration = {
13
13
  username?: string;
14
14
  password?: string;
15
15
  ssl?: boolean;
16
+ encryptionEnabled?: boolean;
16
17
  };
17
18
  type EventPayloadHandler<T extends KafkaTopic> = (message: Omit<KafkaMessage, 'value'> & {
18
19
  value: KafkaEvent<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucaapp/service-utils",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -79,7 +79,7 @@
79
79
  },
80
80
  "resolutions": {
81
81
  "yaml": "2.2.2",
82
- "semver": "7.7.1",
82
+ "semver": "7.7.2",
83
83
  "follow-redirects": "1.15.9",
84
84
  "braces": "3.0.3",
85
85
  "send": "0.19.1",
@@ -87,7 +87,7 @@
87
87
  "cookie": ">= 1.0.2",
88
88
  "string-width": "4.2.3",
89
89
  "@types/express": "4.17.23",
90
- "esbuild": "^0.25.7",
90
+ "esbuild": "^0.25.8",
91
91
  "axios": "^1.11.0",
92
92
  "vite": "6.2.5"
93
93
  }