update-cert 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2016-2017 René Klomp
  5. #
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. import subprocess,sys,time
  20. cc = '/usr/local/bin/confd-client.plx'
  21. def openssl_get(domain, param):
  22. cmd_param=param
  23. if param == "altnames":
  24. cmd_param="text"
  25. cmd = "openssl x509 -noout -in /home/login/getssl/%s/%s.crt -%s" % (domain,domain,cmd_param)
  26. value = subprocess.check_output(cmd, shell=True).strip()
  27. if param in ['startdate','enddate','fingerprint','serial']:
  28. return value.split('=')[1]
  29. if param in ['issuer','subject']:
  30. return value.split('/')[-1]
  31. if param == "altnames":
  32. i = iter(value.splitlines())
  33. for line in i:
  34. if 'X509v3 Subject Alternative Name' in line:
  35. return "['%s']"% i.next().strip().replace(", ","','")
  36. return value
  37. def update_cert(domain, cert_ref):
  38. print "Writing certificate for %s to object %s" % (domain, cert_ref)
  39. cert=subprocess.check_output("/usr/bin/openssl x509 -in /home/login/getssl/%s/%s.crt -text" % (domain,domain), shell=True).replace('\n','\\n')
  40. key=open("/home/login/getssl/%s/%s.key" % (domain,domain)).read().replace('\n','\\n')
  41. # There might be a better solution for this, but I cannot find any documentation on cc.
  42. cmd = """OBJS
  43. ca
  44. host_key_cert
  45. %s
  46. certificate="%s"
  47. key="%s"
  48. write""" % (cert_ref, cert, key)
  49. cert_object = subprocess.Popen([cc, '-batch'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=cmd)[0]
  50. for line in cert_object.split("\n"):
  51. if "'meta' =>" in line:
  52. return line.split('>')[1][2:-2]
  53. def update_meta(domain, meta_ref):
  54. print "Updating certificate meta to object %s" % meta_ref
  55. cmd = """OBJS
  56. ca
  57. meta_x509
  58. %s
  59. vpn_id="%s"
  60. startdate="%s"
  61. enddate="%s"
  62. fingerprint="%s"
  63. serial="%s"
  64. issuer="%s"
  65. issuer_hash="%s"
  66. name="%s"
  67. subject="%s"
  68. subject_hash="%s"
  69. subject_alt_names=%s
  70. write""" % (
  71. meta_ref,domain,
  72. openssl_get(domain,'startdate'),
  73. openssl_get(domain,'enddate'),
  74. openssl_get(domain,'fingerprint'),
  75. openssl_get(domain,'serial'),
  76. openssl_get(domain,'issuer'),
  77. openssl_get(domain,'issuer_hash'),
  78. openssl_get(domain,'subject'),
  79. openssl_get(domain,'subject'),
  80. openssl_get(domain,'subject_hash'),
  81. openssl_get(domain,'altnames')
  82. )
  83. subprocess.Popen([cc, '-batch'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=cmd)[0]
  84. def main():
  85. if not len(sys.argv) == 3:
  86. print "Usage %s <domain> <cert_ref>" % sys.argv[0]
  87. sys.exit(1)
  88. domain = sys.argv[1]
  89. cert_ref = sys.argv[2]
  90. meta_ref = update_cert(domain,cert_ref)
  91. update_meta(domain,meta_ref)
  92. print "Done!"
  93. # Wait for a few seconds for the config to be applied so ssl check after this script will succeed
  94. time.sleep(5)
  95. if __name__ == "__main__":
  96. main()