Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the certificate provided by the node.js #301

Merged
merged 4 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ report/
*.tgz
pkg/linux/pulsar-cpp/
lib/
src/cert.pem
24 changes: 24 additions & 0 deletions GenCertFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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 Client = require('./src/Client.js');

(() => {
Client.genCertFile();
})();
shibd marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const AuthenticationTls = require('./src/AuthenticationTls.js');
const AuthenticationAthenz = require('./src/AuthenticationAthenz.js');
const AuthenticationToken = require('./src/AuthenticationToken.js');
const AuthenticationOauth2 = require('./src/AuthenticationOauth2.js');
const Client = require('./src/Client.js');

const LogLevel = {
DEBUG: 0,
Expand All @@ -32,7 +33,7 @@ const LogLevel = {
};

const Pulsar = {
Client: PulsarBinding.Client,
Client,
Message: PulsarBinding.Message,
MessageId: PulsarBinding.MessageId,
AuthenticationTls,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"example": "examples"
},
"scripts": {
"install": "node-pre-gyp install --fallback-to-build",
"install": "node-pre-gyp install --fallback-to-build && node GenCertFile.js",
"configure": "node-pre-gyp configure",
"build": "npm run format && node-pre-gyp build",
"build:debug": "npm run format && node-pre-gyp rebuild --debug",
Expand Down
68 changes: 68 additions & 0 deletions src/Client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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 fs = require('fs');
const tls = require('tls');
const os = require('os');
const PulsarBinding = require('./pulsar-binding');

const certsFilePath = `${__dirname}/cert.pem`;

class Client {
constructor(params) {
if (typeof params.tlsTrustCertsFilePath === 'undefined') {
shibd marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-param-reassign
params.tlsTrustCertsFilePath = certsFilePath;
}
this.client = new PulsarBinding.Client(params);
}

createProducer(params) {
return this.client.createProducer(params);
}

subscribe(params) {
return this.client.subscribe(params);
}

createReader(params) {
return this.client.createReader(params);
}

close() {
this.client.close();
}

static setLogHandler(params) {
PulsarBinding.Client.setLogHandler(params);
}

static genCertFile() {
fs.rmSync(certsFilePath, { force: true });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you bootstrap two clients on the same machine?
Will they use the same file?

Also, we need to set permissions appropriately on this file, otherwise other users may override the contents and lead to a security hole

Cc @michaeljmarshall @lhotari @BewareMyPower

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you bootstrap two clients on the same machine?
Will they use the same file?

The file is generated only once at installation time.
If pulsar-client is the global install, they will use the same file.
If pulsar-client is not installed globally, each project generates files and uses them separately.

Also, we need to set permissions appropriately on this file, otherwise other users may override the contents and lead to a security hole

How do I set permissions? I don't think it's necessary, just like the default /etc/ssl/cert.pem file in linux, users can override it too, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The permission of ca-certificates.crt on Linux is:

$ ls -lh /etc/ssl/certs/ca-certificates.crt
-rw-r--r-- 1 root root 191K Feb  7 15:20 /etc/ssl/certs/ca-certificates.crt

But I don't think we need to change the permission. Because this file would be installed under node_modules/ in the project directory. The owner is who ran npm install. It would be that user's fault if the permission was overridden by others.

const fd = fs.openSync(certsFilePath, 'a');
try {
tls.rootCertificates.forEach((cert) => {
fs.appendFileSync(fd, cert + os.EOL);
});
} finally {
fs.closeSync(fd);
}
}
}

module.exports = Client;