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.

160 lines
3.8 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 shutil
  9. import sqlite3
  10. import tempfile
  11. from authority import *
  12. from certificate import *
  13. from request import *
  14. from paths import *
  15. __doc__= """
  16. Define proxy classes
  17. """
  18. class CALookup(object):
  19. """
  20. Proxy to interact with the database, get CA as element or as list
  21. """
  22. def __init__(self, ssh_ca_dir, ssl_ca_dir):
  23. """
  24. The connection attribute is setted by the CAManager instance
  25. when used
  26. """
  27. self.conn = None
  28. self.ssh_ca_dir = ssh_ca_dir
  29. self.ssl_ca_dir = ssl_ca_dir
  30. def __iter__(self):
  31. c = self.conn.cursor()
  32. c.execute("""SELECT id, name, type FROM cas""")
  33. return iter(c.fetchall())
  34. def __delitem__(self, ca_id):
  35. """
  36. Delete a specific certification authority from the database
  37. """
  38. c = self.conn.cursor()
  39. c.execute("""DELETE FROM cas WHERE id = ?""", (ca_id, ))
  40. def __getitem__(self, ca_id):
  41. """
  42. Get a specific certification authority from the database
  43. """
  44. c = self.conn.cursor()
  45. c.execute("""SELECT name, type FROM cas WHERE id = ?""", (ca_id, ))
  46. result = c.fetchone()
  47. if not result:
  48. raise IndexError('Unknown CA "%s"' % ca_id)
  49. ca_name, ca_type = result
  50. if ca_type.lower() == 'ssh':
  51. return SSHAuthority(ca_id, ca_name, self.ssh_ca_dir)
  52. elif ca_type.lower() == 'ssl':
  53. return SSLAuthority(ca_id, ca_name, self.ssl_ca_dir)
  54. def __setitem__(self, ca_id, ca_value):
  55. """
  56. Create a new certification authority, insert
  57. it into the database
  58. """
  59. ca_name, ca_type = ca_value
  60. authority = None
  61. if ca_type.lower() == 'ssh':
  62. authority = SSHAuthority(ca_id, ca_name, self.ssh_ca_dir)
  63. elif ca_type.lower() == 'ssl':
  64. authority = SSLAuthority(ca_id, ca_name, self.ssl_ca_dir)
  65. else:
  66. raise ValueError('CA type is not supported')
  67. authority.generate()
  68. c = self.conn.cursor()
  69. c.execute("""INSERT INTO cas VALUES (?, ?, ?)""",
  70. (ca_id, ca_name, ca_type.lower()))
  71. self.conn.commit()
  72. class RequestLookup(object):
  73. """
  74. Proxy to interact with the requests
  75. """
  76. def __init__(self):
  77. self.request_dir = REQUESTS_PATH
  78. self.output_dir = OUTPUT_PATH
  79. def __iter__(self):
  80. """
  81. Iterate over all certificate request in REQUEST_PATH
  82. """
  83. req_objs = []
  84. for request_id in os.listdir(self.request_dir):
  85. """
  86. request_id is formatted as uuid
  87. """
  88. with RequestLoader(request_id) as request:
  89. req_objs.append(request)
  90. return iter(req_objs)
  91. def __delitem__(self, request_id):
  92. """
  93. Delete a specific certificate request
  94. """
  95. os.unlink(os.path.join(self.request_dir, request_id))
  96. def __getitem__(self, request_id):
  97. """
  98. Get a specific certificate request
  99. """
  100. if not SignRequest(request_id):
  101. raise IndexError
  102. with RequestLoader(request_id) as request:
  103. return request
  104. @property
  105. def ssh(self):
  106. pass
  107. @property
  108. def ssl(self):
  109. pass
  110. class CertificateLookup(object):
  111. """
  112. Proxy to interact with certificates
  113. """
  114. def __iter__(self):
  115. self.cert_dir = OUTPUT_PATH
  116. def __getitem__(self, certificate_id):
  117. """
  118. Get a specific certificate from disk
  119. """
  120. if not Certificate(certificate_id):
  121. raise IndexError
  122. return Certificate(certificate_id)
  123. def __iter__(self):
  124. """
  125. Iterate over all certificate request in OUTPUT_PATH
  126. """
  127. pass