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.

175 lines
5.2 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import cmd
  4. import sys
  5. from authority import SSHAuthority, SSLAuthority
  6. from ca_manager import sign_request
  7. __doc__= """
  8. Class to make a shell and interact with the user
  9. """
  10. class CAManagerShell(cmd.Cmd, object):
  11. intro= """# LILiK CA Manager #
  12. Welcome to the certification authority shell.
  13. Type help or ? to list commands.
  14. """
  15. prompt= "(CA Manager)> "
  16. def __init__(self, ca_manager):
  17. super(CAManagerShell, self).__init__()
  18. self.ca_manager = ca_manager
  19. def do_ls_ca(self, l):
  20. 'List the available certification authorities: LS_CA'
  21. for i, authority in enumerate(self.ca_manager.ca):
  22. print('- %d - %s' % (i, authority))
  23. def do_ls_requests(self, l):
  24. 'List the available certification requests: LS_REQUESTS'
  25. print_available_requests(self.ca_manager)
  26. def do_describe_cas(self, l):
  27. 'Show certification authority information: DESCRIBE_CAS'
  28. ca_id = l.split()[0]
  29. ca = self.ca_manager.ca[ca_id]
  30. if ca:
  31. ca_description = """
  32. Certification authority: %s
  33. --------------------------------------------------
  34. CA type: %s
  35. CA name: %s
  36. """
  37. ca_info = (
  38. ca_id,
  39. ca.__class__.__name__,
  40. ca.name,
  41. )
  42. print(ca_description % ca_info)
  43. else:
  44. print("No CA found for id: '%s'" % request_id)
  45. def do_describe_request(self, l):
  46. 'Show sign request information: DESCRIBE_REQUEST'
  47. request_id = l.split()[0]
  48. request = self.ca_manager.request[request_id]
  49. if request:
  50. request_description = """
  51. Request %s
  52. --------------------------------------------------
  53. Request type: %s
  54. %s
  55. Key %s
  56. """
  57. request_info = (
  58. request_id,
  59. request.__class__.__name__,
  60. request.fields,
  61. request.key_data,
  62. )
  63. print(request_description % request_info)
  64. else:
  65. print("No request found for id: '%s'" % request_id)
  66. def do_drop_request(self, l):
  67. 'Delete a sign request: DROP_REQUEST'
  68. request_id = l.split()[0]
  69. del self.ca_manager.request[request_id]
  70. def do_gen_ssh(self, l):
  71. 'Generate a SSH Certification authority'
  72. argv = l.split()
  73. ca_id = argv[0]
  74. self.ca_manager.ca[ca_id] = SSHAuthority
  75. def do_gen_ssl(self, l):
  76. 'Generate a SSL Certification authority'
  77. argv = l.split()
  78. ca_id = argv[0]
  79. self.ca_manager.ca[ca_id] = SSLAuthority
  80. def do_sign_request(self, l):
  81. 'Sign a request using a CA: SIGN_REQUEST ca_id request_id'
  82. argv = l.split()
  83. argc = len(argv)
  84. # argument number is too low
  85. if argc < 2:
  86. if argc == 0:
  87. # print available ca
  88. print("Available authority")
  89. print_available_authorities(self.ca_manager)
  90. print("==================")
  91. # print available requests
  92. print("Available request")
  93. print_available_requests(self.ca_manager)
  94. elif argc == 1:
  95. ca_type = None
  96. ca_id = argv[0]
  97. try:
  98. ca_type = self.ca_manager.ca[ca_id].ca_type
  99. except Exception as e:
  100. print ("Error: %s"%e)
  101. return
  102. # print available requests
  103. print("Available request for CA %s (type %s)" % (ca_id, ca_type))
  104. print_available_requests(self.ca_manager, ca_type)
  105. print("==================")
  106. print("usage: sign_request autority request")
  107. else:
  108. authority_id, request_id = argv[0], argv[1]
  109. sign_request(self.ca_manager, request_id, authority_id)
  110. def complete_sign_request(self, text, line, begidx, endidx):
  111. results = ''
  112. #too much magic
  113. argc = len(( "%send" % line ).split() )
  114. if argc == 2:
  115. results = [a[0] for a in self.ca_manager.ca if a[0].startswith(text)]
  116. elif argc == 3:
  117. ca_type = None
  118. try:
  119. ca_id = line.split()[1]
  120. ca_type = self.ca_manager.ca[ca_id].ca_type
  121. except Exception as e:
  122. print ("Error: %s"%e)
  123. return
  124. results = [a for a in self.ca_manager.request[ca_type] if str(a).startswith(text)]
  125. return results
  126. def complete(self, text, state):
  127. results = super().complete(text, state)
  128. if results is not None:
  129. return "%s "%results
  130. return results
  131. def do_quit(self, l):
  132. 'Quit this shell'
  133. return True
  134. def print_available_authorities(ca_manager):
  135. for i, ca_item in enumerate(ca_manager.ca):
  136. print("- %d : %s" % (i , ca_item))
  137. def print_available_requests(ca_manager):
  138. for i, request in enumerate(ca_manager.request):
  139. print("- %d : %s" % (i, request))