copy_ca_based_cert

This script will copy a private key key.pem and a certificate-authority-based certificate cert.pem.

The following parameters are optional:

c

the common name (localhost if not given) of the certificate

d

the directory ($PWD if not given) the key and certificate should be copied to; will be created if it does not exit

y

assume "yes" as answer to all prompts and run non-interactively

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 -d certs"
  }
}
$ node --run cert:copy

Usage

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

$ scripts/cert/copy_ca_based_cert.sh -d ~/.local/secrets/certs/https.internal -c 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