copy_ca_based_cert

This script will copy the private key key.pem and the certificate cert.pem from the certificate authority to the given directory ($PWD if not given).

The given directory will be created if it does not exit yet.

The optional second parameter is the common name (localhost if not given) of the certificate to be copied.

Both key.pem and cert.pem should not be checked into version control!

If the given directory is inside a Git working tree the script will offer to modify the .gitignore file:

WARNING: key.pem and/or cert.pem is not ignored in '/Users/example/tmp/.gitignore'

Do you want me to modify your .gitignore file (Y/N)?

Related Script: git-cleanup

Copy the script into your Node.js project and add it as a custom script to your package.json file:

package.json
{
...
  "scripts": {
    "cert:copy": "scripts/copy_ca_based_cert.sh certs"
  }
}
$ npm run cert:copy

Usage

$ scripts/cert/copy_ca_based_cert.sh
$ scripts/cert/copy_ca_based_cert.sh ~/.local/secrets/certs/localhost

$ scripts/cert/copy_ca_based_cert.sh ~/.local/secrets/certs/https.internal https.internal
$ stat -f '%A %N' ~/.local/secrets/certs/https.internal/*.pem
600 /Users/example/.local/secrets/certs/https.internal/cert.pem
600 /Users/example/.local/secrets/certs/https.internal/key.pem
$ openssl x509 -ext subjectAltName -noout -in ~/.local/secrets/certs/https.internal/cert.pem
X509v3 Subject Alternative Name:
    DNS:https.internal

Prerequisites