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.

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