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.

146 lines
3.7 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from itertools import chain
  4. import json
  5. import os
  6. import os.path
  7. from models.ssh import SSHAuthority, UserSSHRequest, HostSSHRequest
  8. from models.ssl import SSLAuthority, HostSSLRequest, CASSLRequest
  9. from models.certificate import Certificate
  10. from models.request import SignRequest
  11. from paths import *
  12. class CALookup:
  13. """
  14. Proxy to interact with authorities
  15. """
  16. allowed_auth = [
  17. SSHAuthority,
  18. SSLAuthority,
  19. ]
  20. def __init__(self):
  21. self.path = MANAGER_PATH
  22. def __iter__(self):
  23. all_the_authorities = [ auth.select().iterator() for auth in self.allowed_auth]
  24. return chain.from_iterable(all_the_authorities)
  25. def __getitem__(self, ca_id):
  26. for authority_type in self.allowed_auth:
  27. try:
  28. ca = authority_type.get(authority_type.ca_id == ca_id)
  29. return ca
  30. except authority_type.DoesNotExist:
  31. continue
  32. class RequestLookup:
  33. """
  34. Proxy to interact with the requests
  35. """
  36. def __init__(self):
  37. self.request_dir = REQUESTS_PATH
  38. self.output_dir = OUTPUT_PATH
  39. def __iter__(self):
  40. """
  41. Iterate over all certificate request in REQUEST_PATH
  42. """
  43. for request_id in os.listdir(self.request_dir):
  44. """
  45. request_id is formatted as uuid
  46. """
  47. yield self[request_id]
  48. def __delitem__(self, request_id):
  49. """
  50. Delete a specific certificate request
  51. """
  52. os.unlink(SignRequest(request_id).path)
  53. def __getitem__(self, request_id):
  54. """
  55. Get a specific certificate request
  56. """
  57. with open(SignRequest(request_id).path, 'r') as stream:
  58. request_data = json.load(
  59. stream,
  60. )
  61. requester = request_data.get('userName', None) or request_data.get('hostName', None) or request_data.get('caName', None)
  62. root_requested = request_data.get('rootRequested', False)
  63. key_data = request_data.get('keyData', None)
  64. values = request_data.values()
  65. if 'ssh_user' in values:
  66. return UserSSHRequest(
  67. request_id,
  68. requester,
  69. root_requested,
  70. key_data,
  71. )
  72. elif 'ssh_host' in values:
  73. return HostSSHRequest(
  74. request_id,
  75. requester,
  76. key_data,
  77. )
  78. elif 'ssl_host' in values:
  79. return HostSSLRequest(
  80. request_id,
  81. requester,
  82. key_data,
  83. )
  84. elif 'ssl_ca' in values:
  85. return CASSLRequest(
  86. request_id,
  87. requester,
  88. key_data,
  89. )
  90. else:
  91. return SignRequest(request_id)
  92. @property
  93. def ssh(self):
  94. pass
  95. @property
  96. def ssl(self):
  97. pass
  98. class CertificateLookup:
  99. """
  100. Proxy to interact with certificates
  101. """
  102. def __iter__(self):
  103. self.cert_dir = OUTPUT_PATH
  104. def __getitem__(self, certificate_id):
  105. """
  106. Get a specific certificate from disk
  107. """
  108. try:
  109. return Certificate.get(Certificate.cert_id == certificate_id)
  110. except Certificate.DoesNotExist:
  111. raise IndexError()
  112. def __iter__(self):
  113. """
  114. Iterate over all certificate request in OUTPUT_PATH
  115. """
  116. return Certificate.select().iterator()