How do I add a certifcate of a third party api to my app in Heroku?

Viewed 255

My backend service calls a third-party API (Kamer van koophandel) in order to retrieve some data. However this API requires me to set a certificate. Locally it is working but when I push it to Heroku the following error occurs:

Warning: Ignoring extra certs from `Private_G1_chain.pem`, load failed: error:02001002:system library:fopen:No such file or directory
   Warning: Ignoring extra certs from `Private_G1_chain.pem`, load failed: error:02001002:system library:fopen:No such file or directory
   Warning: Ignoring extra certs from `Private_G1_chain.pem`, load failed: error:02001002:system library:fopen:No such file or directory
   Warning: Ignoring extra certs from `Private_G1_chain.pem`, load failed: error:02001002:system library:fopen:No such file or directory
   gyp: Call to 'node -p "require('node-addon-api').include"' returned exit status 0 while in binding.gyp. while trying to load binding.gyp
   gyp ERR! configure error 
   gyp ERR! stack Error: `gyp` failed with exit code: 1
   gyp ERR! stack     at ChildProcess.onCpExit (/tmp/build_e15be358/.heroku/node/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
   gyp ERR! stack     at ChildProcess.emit (events.js:376:20)
   gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
   gyp ERR! System Linux 4.4.0-1088-aws
   gyp ERR! command "/tmp/build_e15be358/.heroku/node/bin/node" "/tmp/build_e15be358/.heroku/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/tmp/build_e15be358/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/tmp/build_e15be358/node_modules/bcrypt/lib/binding/napi-v3" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=3" "--node_napi_label=napi-v3"
   gyp ERR! cwd /tmp/build_e15be358/node_modules/bcrypt
   gyp ERR! node -v v14.17.0
   gyp ERR! node-gyp -v v5.1.0
   gyp ERR! not ok 
   node-pre-gyp ERR! build error 
   node-pre-gyp ERR! stack Error: Failed to execute '/tmp/build_e15be358/.heroku/node/bin/node /tmp/build_e15be358/.heroku/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/tmp/build_e15be358/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node --module_name=bcrypt_lib --

In Heroku I set the real config var: NODE_EXTRA_CA_CERTS = Private_G1_chain.pem

The strange part is: if I first push my application to Heroku and then set NODE_EXTRA_CA_CERTS it works, but if NODE_EXTRA_CA_CERTS is set and then I push my application to Heroku I get the error above..

My application btw is built-in: node/express and using typescript.

thanks in advance!

1 Answers

The Ignoring extra certs error is because Node is getting more than one certificate from your .pem file. To resolve that problem you have to remove the white spaces and line brakes from your .pem file.

To do so follow these steps:

awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' cert-name.pem

Run this command in the same directory where your certificate file exists. This command will simply add \n in the required places.

You will get output something like this:

-----BEGIN RSA PRIVATE KEY-----\njlQvt9WdR9Vpg3WQT5+C3HU17bUOwvhp/r0+viMcBUCRW85UqI2BJJKTi1IwQQ4c\ntyTrhYJYOP+A6JXt5BzDzZy/B7tjEMDBosPiwH2m4MaP+6wTbi1qR1pFDL3fXYDr\n

After this save this obtained string in the .env file and then use that parameter instead of using the file Private_G1_chain.pem.

Related