Created
May 29, 2013 03:21
-
-
Save mspaulding06/5667759 to your computer and use it in GitHub Desktop.
Create a keystore from a keypair. Useful for testing against Eucalyptus or AWS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import os | |
import pexpect | |
import subprocess | |
import sys | |
import tempfile | |
parser = argparse.ArgumentParser(description='Generate a keystore') | |
parser.add_argument('cert', metavar='certificate', | |
help='user certificate') | |
parser.add_argument('pk', metavar='private_key', | |
help='private key') | |
parser.add_argument('name', metavar='keystore_name', | |
help='name for keystore file') | |
parser.add_argument('--passwd', metavar='password', | |
help='optionally supply a password for the keystore') | |
args = parser.parse_args() | |
keystore = '{0}.jks'.format(args.name) | |
passwd = 'foobar' | |
if args.passwd: | |
passwd = args.passwd | |
tempname = tempfile.mktemp() | |
try: | |
child = pexpect.spawn('''bash -c "openssl pkcs12 -export -in {0} \ | |
-inkey {1} > {2}"'''.format(args.cert, args.pk, | |
tempname), timeout=2) | |
child.expect('Password:') | |
child.sendline(passwd) | |
child.expect('Password:') | |
child.sendline(passwd) | |
child.expect(pexpect.EOF) | |
cmd = '''keytool -importkeystore -srckeystore {0} \ | |
-destkeystore {1} -srcstoretype pkcs12'''.format(tempname, keystore) | |
child = pexpect.spawn(cmd, timeout=2) | |
child.expect('password:') | |
child.sendline(passwd) | |
child.expect('password:') | |
child.sendline(passwd) | |
child.expect('password:') | |
child.sendline(passwd) | |
child.expect(pexpect.EOF) | |
except: | |
print >> sys.stderr, 'Failed to create keystore archive.' | |
sys.exit(1) | |
finally: | |
os.remove(tempname) | |
print "Created keystore file {0} with password '{1}'".format(keystore, passwd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment