Easy CA management
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import hashlib
  4. import os
  5. import os.path
  6. import shutil
  7. from playhouse.gfk import *
  8. from lookup import CALookup, RequestLookup, CertificateLookup
  9. from models.ssh import SSHAuthority
  10. from models.ssl import SSLAuthority
  11. from models.certificate import Certificate
  12. from paths import *
  13. __doc__ = """
  14. Define classes to interact with certificate requests and Certification Authority
  15. """
  16. class CAManager(object):
  17. """
  18. Middleware to interact with ssh-keygen
  19. """
  20. def __init__(self, path):
  21. self.path = path
  22. self.ca = CALookup()
  23. self.request = RequestLookup()
  24. self.certificate = CertificateLookup()
  25. # Create tables
  26. SSHAuthority.create_table(fail_silently=True)
  27. SSLAuthority.create_table(fail_silently=True)
  28. Certificate.create_table(fail_silently=True)
  29. @property
  30. def ssh_ca_dir(self):
  31. return os.path.join(self.path, 'ssh_cas')
  32. @property
  33. def ssl_ca_dir(self):
  34. return os.path.join(self.path, 'ssl_cas')
  35. def init_manager(paths):
  36. """
  37. Initiate the manager by creating the
  38. directories to store CAs and requests.
  39. Create a database to store the information
  40. """
  41. directories = ['ssh_cas', 'ssl_cas', ]
  42. # ensure the directories needed by CAManager
  43. # exists
  44. for dirpath in paths:
  45. if not os.path.exists(dirpath):
  46. os.makedirs(dirpath)
  47. # ensure ssh_cas ad ssl_cas directories
  48. # exists in MANAGER_PATH
  49. for dirname in directories:
  50. dirpath = os.path.join(paths[0], dirname)
  51. if not os.path.exists(dirpath):
  52. os.mkdir(dirpath)
  53. def sign_request(ca_manager, request_id, authority_id):
  54. authority, request = None, None
  55. try:
  56. authority = ca_manager.ca[authority_id]
  57. except IndexError:
  58. print("Could not find CA '%d'" % authority_id)
  59. return
  60. try:
  61. request = ca_manager.request[request_id]
  62. except IndexError:
  63. print("Could not find request '%d'" % request_id)
  64. h = hashlib.sha256()
  65. h.update(request.key_data.encode('utf-8'))
  66. print("Request hash: %s" % h.hexdigest())
  67. print("You are about to sign the following request:\n %s\nwith the following CA:\n %s"%(request, authority))
  68. confirm = input('Proceed? (type yes)> ')
  69. if confirm != 'yes':
  70. print ("user abort")
  71. return
  72. cert_path = authority.sign(request)
  73. del ca_manager.request[request_id]
  74. shutil.copy(cert_path, os.path.join(RESULTS_PATH, request.req_id))
  75. if __name__ == '__main__':
  76. from shell import CAManagerShell
  77. init_manager([
  78. MANAGER_PATH,
  79. REQUESTS_PATH,
  80. OUTPUT_PATH,
  81. RESULTS_PATH,
  82. ])
  83. ca_manager = CAManager(MANAGER_PATH)
  84. CAManagerShell(ca_manager).cmdloop()