Read a letsencrypt cert with Node.js - node.js

What might cause this? I list two certificate files in ~/TC/bin and I see them.
~/TC/bin$ ls
cert.pem key.pem
I try to read those files from ~/TC/server.js
var credentials = {
key: fs.readFileSync('bin/key.pem'),
cert: fs.readFileSync('bin/cert.pem')
};
I get an error message saying they're not found. I suspect it may be caused by file permissions though I'm not certain how to appropriately adjust it for something like a secure key asset.
Error: ENOENT: no such file or directory, open 'bin/key.pem'

when you're not providing slash at the begining you're telling node that he should look for node module (npm). Try with ./ as a start. So './bin/key.pem'

Related

node js ssh keygen giving , Cannot parse privateKey: Unsupported OpenSSH private key type: ssh-ed25519 even after pem option

My company uses ed25519 and nodejs version 10. I see the error,
"
I deleted the keys from the .ssh( this i was fine since I am starting to build new) and regenerated adding the -m pem option as I copied the .pub contents to my app for validation.
I still see this error.. Is there anything I am missing? or is it picking up the old keys from somewhere?
Any pointers would greatly help me..
thanks

How to add certificate as cafile to node js app

Today I received the following error in my node js express server: unable to get local issuer certificate.
After some research I found out, that the expression process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; allows any unauthorized certificates. This works, but I would like to add a certificate to my service.
So I found this ticket/ answer (https://stackoverflow.com/a/44726189/17330227) which explains how to add a certificate. I have done the steps one by one and am now stuck on the last one. You have to insert the certificate under cafile. For this I used the command npm config set cafile "C:\Users\...\Certificate.cer". Unfortunately I got the initial error message again and looked at the changes with npm config ls -l. At the entry cafile it says the following ; cafile = null ; overridden by user. Furthermore I get the following output cafile = "C:\Users\...Certificate.cer and ...\Certificate.cer = "".
Apparently I am doing something wrong in the "Add Certificate" step. I have tried different notations of the path, nothing works. If someone can help me, that would be great - thanks!

Azure Devops Download Secure File

I am facing an issue wherein I uploaded a server.key in Download Secure File utility but in my pipeline, it is treating it as D:a_tempserver.key - when I call it in my task using echo $(server.secureFilePath). Any idea what could be the issue?
Error:
ERROR running auth:jwt:grant: We encountered a JSON web token error, which is likely not an issue with Salesforce CLI. Here’s the error: ENOENT: no such file or directory, open 'D:\a\1\s\a_tempserver.key'
In your current situation, we recommend you can use the $(Agent.TempDirectory)/server.key instead of the $(server.secureFilePath). In the auzre devops, if we upload the secure file to pipeline, then it will put the file in the temp directory not under the source directory.
Here are some screenshots about my test, hope this will help you.
The Secure File directory:
The result of $(Agent.TempDirectory):

ENOENT, no such file or directory on fs.mkdirSync

I'm currently starting up my NodeJS application and I have the following if-statement:
Error: ENOENT, no such file or directory './realworks/objects/'
at Object.fs.mkdirSync (fs.js:654:18)
at Object.module.exports.StartScript (/home/nodeusr/huizenier.nl/realworks.js:294:7)
The weird thing, however, is that the folder exists already, but the check fails on the following snippet:
if(fs.existsSync(objectPath)) {
var existingObjects = fs.readdirSync(objectPath);
existingObjects.forEach(function (objectFile) {
var object = JSON.parse(fs.readFileSync(objectPath+objectFile));
actualObjects[object.ObjectCode] = object;
});
}else{
fs.mkdirSync(objectPath); // << this is line 294
}
I fail to understand how a no such file or directory can occur on CREATING a directory.
When any folder along the given path is missing, mkdir will throw an ENOENT.
There are 2 possible solutions (without using 3rd party packages):
Recursively call fs.mkdir for every non-existent directory along the path.
Use the recursive option, introduced in v10.12:
fs.mkdir('./path/to/dir', {recursive: true}, err => {})
Solve here How to create full path with node's fs.mkdirSync?
NodeJS version 10.12.0 has added a native support for both mkdir and mkdirSync to create a directory recursively with recursive: true option as the following:
fs.mkdirSync(targetDir, { recursive: true });
And if you prefer fs Promises API, you can write
fs.promises.mkdir(targetDir, { recursive: true });
When you are using fs.mkdir or fs.mkdirSync, while passing the path like folder1/folder2/folder3, folder1 and folder2 must exist otherwise you will get the above error.
The following worked for me:
fs.mkdir( __dirname + '/realworks/', err => {})
Problem was caused by forever running the application relative to the working directory the forever start command is called in, not the location of the application entrypoint.
Try:
fs.mkdir('./realworks/', err => {})
The reason for the error is that if any of the folders exist along the path given to fs.mkdir or fs.mkdirSync these methods will throw/callback with an ENOENT error.
ENOENT is described in the linux documentation as the following:
No such file or directory (POSIX.1-2001).
Typically, this error results when a specified path‐
name does not exist, or one of the components in the
directory prefix of a pathname does not exist, or the
specified pathname is a dangling symbolic link.
Another possible reason for ENOENT is that you lack sufficient privileges to create the directory.
This happened to me while building a docker image where I didn't have sufficient privilege to create a subfolder in the current WORKDIR. Changing the owner of the folder using --chown=user:usergroup OR changing the USER to the root user for the directive were both valid solutions to the problem.
WHAT WORKED FOR ME WAS ;
Deleting my yarn.lock, package-lock.json, and nodemodules
reinstalling with yarn build
restarting my local server
so... you probably might be using ubuntu terminal to create your react app.
It happens to me that I am testing the ubuntu terminal that windows recently launched to be installed on windows computer, like a virtual machine but actually not so messy.
I occured to have the same error as you folk, after testing all the options that the community has given before, none of them work. However, i did find a solution for my problem. It was giving me the ENOENT error, like, test file or directory not found. but I was there indeed. I was using npm start, and came up with the idea of using sudo npm start... and it worked.

Error when starting HTTPS on Node.js on Mac OSX using StartSSL cert

I am splitting hairs trying to get HTTPS server running using StartSSL cert. I got all the necessary files from them and I use them by passing them in the createServer arguments:
var options =
{
ca: FS.readFileSync('sub.class1.server.ca.pem'),
key: FS.readFileSync('ssl.key'),
cert: FS.readFileSync('ssl.crt')
};
And this is the error I got.
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Object.createCredentials (crypto.js:87:31)
at HTTPSServer.Server (tls.js:914:28)
at HTTPSServer.Server (https.js:33:14)
at HTTPSServer.HTTPSServer (/Users/myUserName/node_modules/connect/lib/https.js:34:16)
at new HTTPSServer (/Users/myUserName/node_modules/express/lib/https.js:38:23)
at Object.createServer (/Users/myUserName/node_modules/express/lib/express.js:43:12)
at Object.<anonymous> (/Users/myUserName/Sites/node.js/https/app.js:12:36)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
I thought maybe I should convert the cert to PEM. But running:
openssl x509 -in ssl.crt -out ssl.der -outform DER
...gives me similar error
unable to load certificate
67304:error:0906D06C:PEM routines:PEM_read_bio:no start line:/SourceCache/OpenSSL098/OpenSSL098-44/src/crypto/pem/pem_lib.c:648:Expecting: TRUSTED CERTIFICATE
Any idea why?
UPDATE: This only happens on OSX. I tried running the same thing on a Ubuntu server and it works.
i was having the same issue. however i can confirm that on my machine (macbook osx 10.7.3) node https now runs without error using a self-signed certificate.
that particular error means that it either cant find the files, or there's nothing in the files (you can confirm this by passing an empty string or using an invalid file path.)
firstly, try using absolute paths - e.g. FS.readFileSync(__dirname + 'ssl.crt').
also open your cert and key files and confirm that they contain data in the form: '-----BEGIN '... etc.
also notice that while your files are .cert and .key files, the documentation refers to certificate and key files with the .pem extension.
http://nodejs.org/api/https.html
from what i understand, there isn't much difference, the contents appear pretty similar to me, but these things can be fiddly.
here is a command to convert a .csr file to a .pem file:
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
taken from http://silas.sewell.org/blog/2010/06/03/node-js-https-ssl-server-example/
I think you followed this article https://tootallnate.net/setting-up-free-ssl-on-your-node-server as I did, and I got the same problem as you had. But after checking several times all the files I retrieved from StartCom, I found that I accidentally saved a certification and a private key as UTF8, not ANSI. After changing the encoding of the files to ANSI, node.js started working like a charm :)

Resources