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.

150 lines
3.1 KiB

  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 pickle
  9. import shutil
  10. import sqlite3
  11. import tempfile
  12. from authority import *
  13. from certificate import *
  14. from request import *
  15. from paths import *
  16. __doc__= """
  17. Define proxy classes
  18. """
  19. class CALookup(object):
  20. """
  21. Proxy to interact with authorities
  22. """
  23. allowed_auth = [
  24. SSHAuthority,
  25. SSLAuthority,
  26. ]
  27. def __init__(self):
  28. self.path = MANAGER_PATH
  29. def __iter__(self):
  30. authorities_path = os.path.join(self.path, 'pickled_cas')
  31. auth = []
  32. for authority in os.listdir(authorities_path):
  33. pickle_path = os.path.join(self.path, 'pickled_cas', authority)
  34. with open(pickle_path, 'rb') as stream:
  35. auth.append(pickle.load(stream))
  36. return iter(auth)
  37. def __getitem__(self, ca_id):
  38. if SSHAuthority(ca_id):
  39. return SSHAuthority(ca_id)
  40. elif SSLAuthority(ca_id):
  41. return SSLAuthority(ca_id)
  42. else:
  43. raise IndexError('Unknown CA "%s"' % ca_id)
  44. def __setitem__(self, ca_id, authority_class):
  45. """
  46. Create a new certification authority
  47. """
  48. if authority_class not in self.allowed_auth:
  49. raise ValueError('CA type is not supported')
  50. else:
  51. if not authority_class(ca_id):
  52. authority_class(ca_id).generate()
  53. else:
  54. raise ValueError('CA %s already exists' % ca_id)
  55. class RequestLookup(object):
  56. """
  57. Proxy to interact with the requests
  58. """
  59. def __init__(self):
  60. self.request_dir = REQUESTS_PATH
  61. self.output_dir = OUTPUT_PATH
  62. def __iter__(self):
  63. """
  64. Iterate over all certificate request in REQUEST_PATH
  65. """
  66. req_objs = []
  67. for request_id in os.listdir(self.request_dir):
  68. """
  69. request_id is formatted as uuid
  70. """
  71. with RequestLoader(request_id) as request:
  72. req_objs.append(request)
  73. return iter(req_objs)
  74. def __delitem__(self, request_id):
  75. """
  76. Delete a specific certificate request
  77. """
  78. os.unlink(SignRequest(request_id).path)
  79. def __getitem__(self, request_id):
  80. """
  81. Get a specific certificate request
  82. """
  83. if not SignRequest(request_id):
  84. raise IndexError
  85. with RequestLoader(request_id) as request:
  86. return request
  87. @property
  88. def ssh(self):
  89. pass
  90. @property
  91. def ssl(self):
  92. pass
  93. class CertificateLookup(object):
  94. """
  95. Proxy to interact with certificates
  96. """
  97. def __iter__(self):
  98. self.cert_dir = OUTPUT_PATH
  99. def __getitem__(self, certificate_id):
  100. """
  101. Get a specific certificate from disk
  102. """
  103. if not Certificate(certificate_id):
  104. raise IndexError
  105. return Certificate(certificate_id)
  106. def __iter__(self):
  107. """
  108. Iterate over all certificate request in OUTPUT_PATH
  109. """
  110. pass