BewareMyPower commented on code in PR #445:
URL:
https://github.com/apache/pulsar-client-node/pull/445#discussion_r2634691686
##########
src/Message.cc:
##########
@@ -156,6 +165,57 @@ Napi::Value Message::GetProducerName(const
Napi::CallbackInfo &info) {
return Napi::String::New(env,
pulsar_message_get_producer_name(this->cMessage.get()));
}
+Napi::Value Message::GetEncryptionContext(const Napi::CallbackInfo &info) {
+ Napi::Env env = info.Env();
+ if (!ValidateCMessage(env)) {
+ return env.Null();
+ }
+
+ auto encCtxOpt = this->cMessage.get()->message.getEncryptionContext();
+ if (!encCtxOpt) {
+ return env.Null();
+ }
+
+ // getEncryptionContext returns std::optional<const EncryptionContext*>
+ const pulsar::EncryptionContext *encCtxPtr = *encCtxOpt;
+ if (!encCtxPtr) {
+ return env.Null();
+ }
+ const pulsar::EncryptionContext &encCtx = *encCtxPtr;
+
+ if (encCtx.keys().empty() && encCtx.param().empty() &&
encCtx.algorithm().empty()) {
Review Comment:
First, `keys()` should never be empty, which is guaranteed by the C++ client
implementation:
https://github.com/apache/pulsar-client-cpp/blob/d040039e3a351056d0b3be31adbad4a0b72fee09/lib/ConsumerImpl.cc#L559-L561
Second, from existing implementation, `algorithm()` is always empty.
Regarding the `param()`, I'm not sure if it's allowed to be empty for now.
Anyway, we should expose the original encryption context to the application
once it's encrypted. If an empty `param` is an incorrect behavior, we should
also expose it to users for the wrong context.
Hence, it's better to remove this check.
##########
tests/encryption.test.js:
##########
@@ -0,0 +1,154 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const path = require('path');
+const fs = require('fs');
+const Pulsar = require('../index');
+
+class MyCryptoKeyReader extends Pulsar.CryptoKeyReader {
+ constructor(publicKeys, privateKeys) {
+ super();
+ this.publicKeys = publicKeys;
+ this.privateKeys = privateKeys;
+ }
+
+ getPublicKey(keyName, _metadata) {
+ const keyPath = this.publicKeys[keyName];
+ if (keyPath) {
+ try {
+ const key = fs.readFileSync(keyPath);
+ return { key, _metadata };
+ } catch (e) {
+ return null;
+ }
+ }
+ return null;
+ }
+
+ getPrivateKey(keyName, _metadata) {
+ const keyPath = this.privateKeys[keyName];
+ if (keyPath) {
+ try {
+ const key = fs.readFileSync(keyPath);
+ return { key, _metadata };
+ } catch (e) {
+ return null;
+ }
+ }
+ return null;
+ }
+}
+
+(() => {
+ describe('Encryption', () => {
+ let client;
+ const publicKeyPath = path.join(__dirname,
'certificate/public-key.client-rsa.pem');
+ const privateKeyPath = path.join(__dirname,
'certificate/private-key.client-rsa.pem');
+
+ beforeAll(() => {
+ client = new Pulsar.Client({
+ serviceUrl: 'pulsar://localhost:6650',
+ operationTimeoutSeconds: 30,
+ });
+ });
+
+ afterAll(async () => {
+ await client.close();
+ });
+
+ test('End-to-End Encryption', async () => {
+ const topic =
`persistent://public/default/test-encryption-${Date.now()}`;
+
+ const cryptoKeyReader = new MyCryptoKeyReader(
+ { 'my-key': publicKeyPath },
+ { 'my-key': privateKeyPath },
+ );
+
+ const producer = await client.createProducer({
+ topic,
+ encryptionKeys: ['my-key'],
+ cryptoKeyReader,
+ cryptoFailureAction: 'FAIL',
+ });
+
+ const consumer = await client.subscribe({
+ topic,
+ subscription: 'sub-encryption',
+ cryptoKeyReader,
+ cryptoFailureAction: 'CONSUME',
+ subscriptionInitialPosition: 'Earliest',
+ });
+
+ const msgContent = 'my-secret-message';
+ await producer.send({
+ data: Buffer.from(msgContent),
+ });
+
+ const msg = await consumer.receive();
Review Comment:
You should test the encryption context as well, e.g. `isDecryptionFailed`
should be false here
##########
index.d.ts:
##########
@@ -198,6 +202,22 @@ export interface TopicMetadata {
*/
export type MessageRouter = (message: Message, topicMetadata: TopicMetadata)
=> number;
+export interface EncryptionKey {
+ key: string;
+ value: Buffer;
+ metadata: { [key: string]: string };
+}
+
+export interface EncryptionContext {
+ keys: EncryptionKey[];
+ param: Buffer;
+ algorithm: string;
+ compressionType: CompressionType;
+ uncompressedMessageSize: number;
+ batchSize: number;
Review Comment:
These fields are not tested, you can create a producer with compression and
batching enabled to verify they're converted correctly from the C++ result.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]