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.

208 lines
6.0 KiB

8 years ago
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import cmd
  4. import sys
  5. from ca_manager import sign_request
  6. __doc__= """
  7. Class to make a shell and interact with the user
  8. """
  9. class CAManagerShell(cmd.Cmd, object):
  10. intro= """# LILiK CA Manager\n
  11. Welcome to the certification authority shell.
  12. Type help or ? to list commands.
  13. """
  14. prompt= "(CA Manager)> "
  15. def __init__(self, ca_manager):
  16. super(CAManagerShell, self).__init__()
  17. self.ca_manager = ca_manager
  18. def do_ls_ca(self, l):
  19. 'List the available certification authorities: LS_CA'
  20. print("type - id - name")
  21. for ca_id, ca_name, ca_type in self.ca_manager.ca:
  22. print("- [%3s] %-15s (%s)" % (ca_type, ca_id, ca_name))
  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_ca(self, l):
  71. 'Generate a certification authority: GEN_CA type id name'
  72. argv = l.split()
  73. argc = len(argv)
  74. try:
  75. if argc > 3:
  76. raise(ValueError)
  77. if argc < 1:
  78. ca_type = input("CA type> ")
  79. else:
  80. ca_type = argv[0]
  81. if argc < 2:
  82. ca_id = input("CA unique id> ")
  83. else:
  84. ca_name = argv[1]
  85. if argc < 3:
  86. ca_name = input("CA human-readable name> ")
  87. else:
  88. ca_name = argv[2]
  89. except ValueError:
  90. print("Malformed input: %s" % l)
  91. return
  92. self.ca_manager.ca[ca_id] = (ca_name, ca_type)
  93. def complete_gen_ca(self, text, line, begidx, endidx):
  94. results = ''
  95. argc = len(("%send"%line).split())
  96. if argc == 2:
  97. results = [a for a in ["ssl", "ssh"] if a.startswith(text)]
  98. return results
  99. def do_sign_request(self, l):
  100. 'Sign a request using a CA: SIGN_REQUEST ca_id request_id'
  101. argv = l.split()
  102. argc = len(argv)
  103. # argument number is too low
  104. if argc < 2:
  105. if argc == 0:
  106. # print available ca
  107. print("Available authority")
  108. print_available_authorities(self.ca_manager)
  109. print("==================")
  110. # print available requests
  111. print("Available request")
  112. print_available_requests(self.ca_manager)
  113. elif argc == 1:
  114. ca_type = None
  115. ca_id = argv[0]
  116. try:
  117. ca_type = self.ca_manager.ca[ca_id].ca_type
  118. except Exception as e:
  119. print ("Error: %s"%e)
  120. return
  121. # print available requests
  122. print("Available request for CA %s (type %s)" % (ca_id, ca_type))
  123. print_available_requests(self.ca_manager, ca_type)
  124. print("==================")
  125. print("usage: sign_request autority request")
  126. else:
  127. # [request_number, authority_number] =
  128. authority_id = argv[0]
  129. request_id = " ".join(argv[1:])
  130. sign_request(self.ca_manager, request_id, authority_id)
  131. def complete_sign_request(self, text, line, begidx, endidx):
  132. results = ''
  133. #too much magic
  134. argc = len(( "%send" % line ).split() )
  135. if argc == 2:
  136. results = [a[0] for a in self.ca_manager.ca if a[0].startswith(text)]
  137. elif argc == 3:
  138. ca_type = None
  139. try:
  140. ca_id = line.split()[1]
  141. ca_type = self.ca_manager.ca[ca_id].ca_type
  142. except Exception as e:
  143. print ("Error: %s"%e)
  144. return
  145. results = [a for a in self.ca_manager.request[ca_type] if str(a).startswith(text)]
  146. return results
  147. def complete(self, text, state):
  148. results = super().complete(text, state)
  149. if results is not None:
  150. return "%s "%results
  151. return results
  152. def do_quit(self, l):
  153. 'Quit this shell'
  154. return True
  155. def print_available_authorities(ca_manager):
  156. for i, ca_item in enumerate(ca_manager.ca):
  157. (ca_id, ca_name, ca_type) = ca_item
  158. print("- %d : [%3s] %-15s (%s)" % (i ,ca_type, ca_id, ca_name))
  159. else:
  160. print("No available CA")
  161. def print_available_requests(ca_manager):
  162. for i, request in enumerate(ca_manager.request):
  163. print("- %d : %s" % (i, request))
  164. else:
  165. print("No requests")