test/convenience_scripts/keygen_for_test.py
author Volker Birk <vb@pep.foundation>
Tue, 07 Aug 2018 14:24:15 +0200
changeset 2803 fadf1e8e22ff
parent 2740 cd666a204266
child 2949 99359f6aa025
child 2995 6f2f9a7c060e
permissions -rw-r--r--
fixing ENGINE-453
     1 import argparse
     2 import gnupg
     3 import os
     4 import subprocess
     5 
     6 parser = argparse.ArgumentParser()
     7 parser.add_argument("num_keys", type=int)
     8 parser.add_argument("real_name_prefix")
     9 parser.add_argument("email_address_prefix")
    10 parser.add_argument("output_root", help="root of where to stick the keys (keys go into pub/ and priv/ accordingly)")
    11 parser.add_argument("--no_suffix", "-x", help="Use name and email address as is - do not create incremental ones based on the input", action='store_true')
    12 parser.add_argument("--hgadd", "-a", help="hg add the created keyfiles", action='store_true')
    13 
    14 args = parser.parse_args()
    15 
    16 pub_path = os.path.join(args.output_root, "pub")
    17 priv_path = os.path.join(args.output_root, "priv")
    18 
    19 homedir = os.path.join(os.path.expanduser('~'),"gnupg")
    20 print("GNUPGHOME=" + homedir + "\n")
    21 
    22 try:
    23     gpg = gnupg.GPG(gnupghome=homedir) 
    24 except TypeError:
    25     gpg = gnupg.GPG(homedir=homedir)
    26 
    27 name = args.real_name_prefix
    28 email = args.email_address_prefix
    29     
    30 suffix = not args.no_suffix
    31 
    32 name_prefix = args.real_name_prefix + " "     
    33 e_split = args.email_address_prefix.split('@')
    34 e_split_len = len(e_split)
    35 
    36 if (e_split_len > 2):
    37     for j in range(e_split_len - 1):
    38         email_0 = email_0 + e_split[j] + "@"
    39     email_0 = email_0 + _ + i_str + e_split[e_split_len - 1]    
    40     email_1 = e_split_len[e_split_len - 1]
    41     e_split = [email_0, email_1]
    42     e_split_len = 2
    43 elif (e_split_len == 0):
    44         email_0 = "doge"    
    45         email_1 = "dogepile.me"
    46         e_split = [email_0, email_1]
    47         e_split_len = 2
    48     
    49 num_keys = args.num_keys
    50     
    51 for i in range(num_keys):
    52     i_str = str(i)
    53 
    54     if suffix:
    55         
    56         name = name_prefix + i_str
    57         
    58         if e_split_len == 1:
    59             email = e_split[0] + "_" + i_str
    60         elif e_split_len == 2:
    61             email = e_split[0] + "_" + i_str + "@" + e_split[1]
    62 
    63     print("Generating key data for " + name + " " + email + "\n")
    64     input_data = gpg.gen_key_input(key_type="RSA", key_length=2048, subkey_type="RSA", subkey_length=2048, expire_date=0, name_real=name, name_email=email, password="")
    65     if not input_data:
    66         raise Exception('Input data not created in iteration ' + str(i))
    67     
    68     print(input_data)
    69     key = None
    70     try:
    71         key = gpg.gen_key(input_data)
    72         if not key:
    73             raise Exception('Key not created in iteration ' + str(i))
    74     except ValueError:
    75         pass
    76 
    77 
    78     pubkey = None
    79     privkey = None
    80     
    81     fpr = key.fingerprint
    82     print("Generated " + fpr)
    83     key_filename_prefix = e_split[0] + "_" + i_str + "-0x" + fpr[-8:] + "_"
    84 
    85     
    86     pubkey = gpg.export_keys(fpr)
    87     privkey = gpg.export_keys(fpr, True, passphrase="")
    88 
    89     pubkey_filename = os.path.join(pub_path, key_filename_prefix + "pub.asc")
    90     privkey_filename = os.path.join(priv_path, key_filename_prefix + "priv.asc")    
    91     
    92     # Write to file
    93     pubkey_file = open(pubkey_filename,'w')
    94     pubkey_file.write(pubkey)
    95     pubkey_file.close()
    96     privkey_file = open(privkey_filename,'w')    
    97     privkey_file.write(privkey)
    98     privkey_file.close()
    99         
   100     # Delete keys from keyring
   101     gpg.delete_keys(fpr, True, passphrase="") # True => private keys
   102     gpg.delete_keys(fpr)
   103 
   104     if (args.hgadd):
   105         subprocess.run(["hg", "add", pubkey_filename])
   106         subprocess.run(["hg", "add", privkey_filename])