new_contents: #!/usr/bin/env python3.6
  
  """Script to generate a keystore with node and client certificates.
  
  Requires keystore and openssl to be available in $PATH
  """
  
  import os
  import argparse
  from os.path import join, splitext, basename
  from subprocess import run
  
  
  def int_or(val, default):
      if val:
          return int(val)
      return default
  
  
  def create_key_and_csr(key, csr):
      cn = splitext(basename(csr))[0]
      run([
          'openssl', 'req', '-newkey', 'rsa:2048', '-nodes',
          '-subj', f'/C=AT/ST=Dummy State/L=Dummy Country/O=Dummy Company/CN={cn}',
          '-keyout', key,
          '-out', csr
      ])
  
  
  def create_crt(csr, crt, root_ca_crt, root_ca_key):
      run(['openssl', 'x509', '-req',
           '-in', csr,
           '-CA', root_ca_crt,
           '-CAkey', root_ca_key,
           '-CAcreateserial',
           '-out', crt,
           '-sha256',
           '-days', '365'])
  
  
  def generate_for(root_ca_key, root_ca_crt, out_dir, entity, num_default):
      num = int_or(input(f'How many {entity} certs do you want to generate? [{num_default}]: '), num_default)
      certs_and_keys = []
      for i in range(num):
          name = entity + str(i + 1)
          supplied_name = input(f'Name (CN) of {entity} {i + 1} [{name}]: ')
          name = supplied_name or name
  
          key = join(out_dir, name + '.key')
          csr = join(out_dir, name + '.csr')
          crt = join(out_dir, name + '.crt')
          certs_and_keys.append((crt, key))
          print(f'Creating {entity} key, csr and cert for {name}')
          create_key_and_csr(key, csr)
          create_crt(csr, crt, root_ca_crt, root_ca_key)
      print('')
      print('')
      return certs_and_keys
  
  
  def create_certs(out_dir, keystore_pw):
      keystore = join(out_dir, 'keystore.jks')
      keystorep12 = join(out_dir, 'keystore.p12')
      ca_key = join(out_dir, 'rootCA.key')
      ca_crt = join(out_dir, 'rootCA.crt')
      print(f'Generating rootCA key: {ca_key}')
      print(f'Generating rootCA certificate: {ca_crt}')
      run([
          'openssl', 'req', '-x509', '-sha256', '-nodes',
          '-days', '365',
          '-subj', f'/C=AT/ST=Dummy State/L=Dummy Country/O=Dummy Company/CN=myCA',
          '-newkey', 'rsa:2048',
          '-keyout', ca_key,
          '-out', ca_crt
      ])
      certs_and_keys = generate_for(ca_key, ca_crt, out_dir, 'node', 1)
      print(f'Importing node certificates into keystore, Use "{keystore_pw}" as pw.')
      for (cert, key) in certs_and_keys:
          run([
              'openssl', 'pkcs12', '-export',
              '-in', cert,
              '-inkey', key,
              '-out', keystorep12,
              '-name', splitext(cert)[0],
              '-CAfile', ca_crt,
              '-caname', 'myCA',
              '-chain',
          ])
          run([
              'keytool', '-importkeystore',
              '-deststorepass', keystore_pw,
              '-destkeypass', keystore_pw,
              '-destkeystore', keystore,
              '-srckeystore', keystorep12,
              '-srcstoretype', 'PKCS12',
              '-srcstorepass', keystore_pw,
              '-alias', splitext(cert)[0]
          ])
      certs_and_keys = generate_for(ca_key, ca_crt, out_dir, 'client', 1)
      for (cert, key) in certs_and_keys:
          run(['keytool', '-importcert',
               '-storepass', keystore_pw,
               '-keystore', keystore,
               '-file', cert,
               '-alias', splitext(cert)[0]])
  
  
  def main():
      parser = argparse.ArgumentParser(description=__doc__)
      parser.add_argument('--out-dir', type=str, required=True)
      parser.add_argument('--keystore-pw', type=str, default='changeit')
      args = parser.parse_args()
      os.makedirs(args.out_dir, exist_ok=True)
      create_certs(args.out_dir, args.keystore_pw)
  
  
  if __name__ == "__main__":
      main()
  
subject: Add script to simplify cert creation for tests