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.

135 lines
3.0 KiB

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