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.

209 lines
5.8 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import cmd
  4. import sys
  5. from models.ssh import SSHAuthority
  6. from models.ssl import SSLAuthority
  7. from manager import sign_request
  8. __doc__= """
  9. Class to make a shell and interact with the user
  10. """
  11. class CAManagerShell(cmd.Cmd):
  12. intro= """# LILiK CA Manager #
  13. Welcome to the certification authority shell.
  14. Type help or ? to list commands.
  15. """
  16. prompt= "(CA Manager)> "
  17. def __init__(self, ca_manager):
  18. super(CAManagerShell, self).__init__()
  19. self.ca_manager = ca_manager
  20. def do_ls_cas(self, l):
  21. 'List the available certification authorities: LS_CA'
  22. for i, authority in enumerate(self.ca_manager.ca):
  23. print('- %d - %s' % (i, authority))
  24. def do_ls_certificates(self, l):
  25. 'List the issued certificates: LS_CERTIFICATE'
  26. for i, cert in enumerate(self.ca_manager.certificate):
  27. print('- %d - %s' % (i, cert))
  28. def do_ls_requests(self, l):
  29. 'List the available certification requests: LS_REQUESTS'
  30. print_available_requests(self.ca_manager)
  31. def do_describe_cas(self, l):
  32. 'Show certification authority information: DESCRIBE_CAS'
  33. ca_id = l.split()[0]
  34. ca = self.ca_manager.ca[ca_id]
  35. if ca:
  36. ca_description = """
  37. Certification authority: %s
  38. --------------------------------------------------
  39. CA type: %s
  40. CA name: %s
  41. Serial: %s
  42. """
  43. ca_info = (
  44. ca_id,
  45. ca.__class__.__name__,
  46. ca.name,
  47. ca.serial,
  48. )
  49. print(ca_description % ca_info)
  50. else:
  51. print("No CA found for id: '%s'" % request_id)
  52. def do_describe_certificate(self, l):
  53. 'Show certificate information: DESCRIBE_CERTIFICATE'
  54. certificate_id = l.split()[0]
  55. cert = self.ca_manager.certificate[certificate_id]
  56. if cert:
  57. cert_description = """
  58. Certificate %s
  59. --------------------------------------------------
  60. Signin authority: %s
  61. Signed on: %s
  62. Receiver: %s
  63. Certificate Serial: %s
  64. Validity Interval: %s
  65. """
  66. request_info = (
  67. certificate_id,
  68. cert.signed_by,
  69. cert.date_issued,
  70. cert.receiver,
  71. cert.serial_number,
  72. cert.validity_interval,
  73. )
  74. print(cert_description % cert_info)
  75. else:
  76. print('No certificate found for id: "%s"' % cert_id)
  77. pass
  78. def do_describe_request(self, l):
  79. 'Show sign request information: DESCRIBE_REQUEST'
  80. request_id = l.split()[0]
  81. request = self.ca_manager.request[request_id]
  82. if request:
  83. request_description = """
  84. Request %s
  85. --------------------------------------------------
  86. Request type: %s
  87. %s
  88. Key %s
  89. """
  90. request_info = (
  91. request_id,
  92. request.__class__.__name__,
  93. request.fields,
  94. request.key_data,
  95. )
  96. print(request_description % request_info)
  97. else:
  98. print('No request found for id: "%s"' % request_id)
  99. def do_drop_request(self, l):
  100. 'Delete a sign request: DROP_REQUEST'
  101. request_id = l.split()[0]
  102. del self.ca_manager.request[request_id]
  103. def do_gen_ssh(self, l):
  104. 'Generate a SSH Certification authority: GEN_SSH id name'
  105. argv = l.split(maxsplit=1)
  106. ca_id = argv[0]
  107. name = argv[1]
  108. new_auth = SSHAuthority(
  109. ca_id = ca_id,
  110. name = name,
  111. serial = 0,
  112. )
  113. new_auth.generate()
  114. new_auth.save()
  115. def do_gen_ssl(self, l):
  116. 'Generate a SSL Certification authority'
  117. argv = l.split(maxsplit=1)
  118. ca_id = argv[0]
  119. name = argv[1]
  120. new_auth = SSLAuthority(
  121. ca_id = ca_id,
  122. name = name,
  123. serial = 0,
  124. )
  125. new_auth.generate()
  126. new_auth.save()
  127. def do_sign_request(self, l):
  128. 'Sign a request using a CA: SIGN_REQUEST ca_id request_id'
  129. argv = l.split()
  130. argc = len(argv)
  131. # argument number is too low
  132. if argc < 2:
  133. if argc == 0:
  134. # print available ca
  135. print("Available authority")
  136. print_available_authorities(self.ca_manager)
  137. print("==================")
  138. # print available requests
  139. print("Available request")
  140. print_available_requests(self.ca_manager)
  141. else:
  142. authority_id, request_id = argv[0], argv[1]
  143. sign_request(self.ca_manager, request_id, authority_id)
  144. def complete_sign_request(self, text, line, begidx, endidx):
  145. ca_results = [
  146. a for a in self.ca_manager.ca if a.ca_id.startswith(text)
  147. ]
  148. req_result = [
  149. a for a in self.ca_manager.request if a.req_id.startswith(text)
  150. ]
  151. return ' '.join(results)
  152. def complete(self, text, state):
  153. results = super().complete(text, state)
  154. if results is not None:
  155. return "%s "%results
  156. return results
  157. def do_quit(self, l):
  158. 'Quit this shell'
  159. return True
  160. def print_available_authorities(ca_manager):
  161. for i, ca_item in enumerate(ca_manager.ca):
  162. print("- %d : %s" % (i , ca_item))
  163. def print_available_requests(ca_manager):
  164. for i, request in enumerate(ca_manager.request):
  165. print("- %d : %s" % (i, request))