Created
October 7, 2015 05:11
-
-
Save h0ng10/23d7bf61def6f5df5471 to your computer and use it in GitHub Desktop.
Adds the certificate/private key from a OpenVPN user to the configuration file (ovpn).
This file contains hidden or 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 re | |
import os | |
import string | |
import argparse | |
def get_cert_from_file(regex, filename): | |
try: | |
cert_file = open(filename) | |
cert_content = cert_file.read() | |
search_result = re.search(regex, cert_content) | |
if search_result is None: | |
print "Unable to locate certificate/key in " + filename | |
exit(2) | |
return search_result.group(0) | |
except: | |
print "Problem with file: " + filename | |
# ------ main ------- | |
parser = argparse.ArgumentParser() | |
parser.add_argument("template", help="The OpenVPN configuration template") | |
parser.add_argument("cafile", help="The server certificate") | |
parser.add_argument("certfile", help="The client certificate") | |
parser.add_argument("keyfile", help="The private key of the client") | |
parser.add_argument("ovpnfile", help="The name of the created openvpn file") | |
arguments = parser.parse_args() | |
try: | |
template_file = open(arguments.template) | |
template = template_file.read() | |
except: | |
print "Error opening template file" | |
exit(1) | |
cert_regex = re.compile(ur'(-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE-----)', re.DOTALL) | |
key_regex = re.compile(ur'(-----BEGIN PRIVATE KEY-----.*-----END PRIVATE KEY-----)', re.DOTALL) | |
ca_cert = get_cert_from_file(cert_regex, arguments.cafile) | |
client_cert = get_cert_from_file(cert_regex, arguments.certfile) | |
client_key = get_cert_from_file(key_regex, arguments.keyfile) | |
try: | |
ovpn_file = open(arguments.ovpnfile, "w") | |
except: | |
print "Error opening ovpn file" | |
exit(1) | |
ovpn_file.write(template) | |
ovpn_file.write("\n<ca>\n") | |
ovpn_file.write(ca_cert) | |
ovpn_file.write("\n</ca>\n") | |
ovpn_file.write("<cert>\n") | |
ovpn_file.write(client_cert) | |
ovpn_file.write("\n</cert>\n") | |
ovpn_file.write("<key>\n") | |
ovpn_file.write(client_key) | |
ovpn_file.write("\n</key>\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment