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.

171 lines
4.7 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_ca(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_requests(self, l):
  25. 'List the available certification requests: LS_REQUESTS'
  26. print_available_requests(self.ca_manager)
  27. def do_describe_cas(self, l):
  28. 'Show certification authority information: DESCRIBE_CAS'
  29. ca_id = l.split()[0]
  30. ca = self.ca_manager.ca[ca_id]
  31. if ca:
  32. ca_description = """
  33. Certification authority: %s
  34. --------------------------------------------------
  35. CA type: %s
  36. CA name: %s
  37. """
  38. ca_info = (
  39. ca_id,
  40. ca.__class__.__name__,
  41. ca.name,
  42. )
  43. print(ca_description % ca_info)
  44. else:
  45. print("No CA found for id: '%s'" % request_id)
  46. def do_describe_request(self, l):
  47. 'Show sign request information: DESCRIBE_REQUEST'
  48. request_id = l.split()[0]
  49. request = self.ca_manager.request[request_id]
  50. if request:
  51. request_description = """
  52. Request %s
  53. --------------------------------------------------
  54. Request type: %s
  55. %s
  56. Key %s
  57. """
  58. request_info = (
  59. request_id,
  60. request.__class__.__name__,
  61. request.fields,
  62. request.key_data,
  63. )
  64. print(request_description % request_info)
  65. else:
  66. print('No request found for id: "%s"' % request_id)
  67. def do_drop_request(self, l):
  68. 'Delete a sign request: DROP_REQUEST'
  69. request_id = l.split()[0]
  70. del self.ca_manager.request[request_id]
  71. def do_gen_ssh(self, l):
  72. 'Generate a SSH Certification authority: GEN_SSH id name'
  73. argv = l.split(maxsplit=1)
  74. ca_id = argv[0]
  75. name = argv[1]
  76. new_auth = SSHAuthority(
  77. ca_id = ca_id,
  78. name = name,
  79. serial = 0,
  80. )
  81. new_auth.generate()
  82. new_auth.save()
  83. def do_gen_ssl(self, l):
  84. 'Generate a SSL Certification authority'
  85. argv = l.split(maxsplit=1)
  86. ca_id = argv[0]
  87. name = argv[1]
  88. new_auth = SSLAuthority(
  89. ca_id = ca_id,
  90. name = name,
  91. serial = 0,
  92. )
  93. new_auth.generate()
  94. new_auth.save()
  95. def do_sign_request(self, l):
  96. 'Sign a request using a CA: SIGN_REQUEST ca_id request_id'
  97. argv = l.split()
  98. argc = len(argv)
  99. # argument number is too low
  100. if argc < 2:
  101. if argc == 0:
  102. # print available ca
  103. print("Available authority")
  104. print_available_authorities(self.ca_manager)
  105. print("==================")
  106. # print available requests
  107. print("Available request")
  108. print_available_requests(self.ca_manager)
  109. else:
  110. authority_id, request_id = argv[0], argv[1]
  111. sign_request(self.ca_manager, request_id, authority_id)
  112. def complete_sign_request(self, text, line, begidx, endidx):
  113. ca_results = [
  114. a for a in self.ca_manager.ca if a.ca_id.startswith(text)
  115. ]
  116. req_result = [
  117. a for a in self.ca_manager.request if a.req_id.startswith(text)
  118. ]
  119. return ' '.join(results)
  120. def complete(self, text, state):
  121. results = super().complete(text, state)
  122. if results is not None:
  123. return "%s "%results
  124. return results
  125. def do_quit(self, l):
  126. 'Quit this shell'
  127. return True
  128. def print_available_authorities(ca_manager):
  129. for i, ca_item in enumerate(ca_manager.ca):
  130. print("- %d : %s" % (i , ca_item))
  131. def print_available_requests(ca_manager):
  132. for i, request in enumerate(ca_manager.request):
  133. print("- %d : %s" % (i, request))