Skip to content

Instantly share code, notes, and snippets.

@semipermeable
Last active December 19, 2015 14:08
Show Gist options
  • Save semipermeable/5966809 to your computer and use it in GitHub Desktop.
Save semipermeable/5966809 to your computer and use it in GitHub Desktop.
Utilities to support PostgreSQL client certificate authentication setup example.
module CertificateSetup
def generate_server_cert(name, cn)
system "openssl genrsa -des3 -passout pass:x -out #{name}.pass.key 2048"
system "openssl rsa -passin pass:x -in #{name}.pass.key -out #{name}.key"
FileUtils.rm "#{name}.pass.key"
system "openssl req -new -key #{name}.key -out #{name}.csr -subj '/C=CA/ST=California/L=San Francisco/O=Testing/CN=#{cn}'"
system "openssl x509 -req -days 365 -in #{name}.csr -signkey #{name}.key -out #{name}.crt"
end
def generate_client_cert(name, cn, root)
system "openssl genrsa -des3 -passout pass:x -out #{name}.key 1024"
system "openssl rsa -passin pass:x -in #{name}.key -out #{name}.key"
system "openssl req -new -key #{name}.key -out #{name}.csr -subj '/C=CA/ST=California/L=San Francisco/O=Testing/CN=#{cn}'"
system "openssl x509 -req -in #{name}.csr -CA #{root}.crt -CAkey #{root}.key -out #{name}.crt -CAcreateserial"
end
end
# Usage:
CertificateSetup.generate_server_cert('postgres', 'server.domain.com')
CertificateSetup.generate_client_cert('client-user', 'www-user', 'postgres')
CertificateSetup.generate_client_cert('client-repl', 'repl', 'postgres')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment