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.

162 lines
4.8 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. raise NotImplementedError
  29. def do_drop_request(self, l):
  30. 'Delete a sign request: DROP_REQUEST'
  31. request_id = l.split()[0]
  32. del self.ca_manager.request[request_id]
  33. def do_gen_ca(self, l):
  34. 'Generate a certification authority: GEN_CA type id name'
  35. argv = l.split()
  36. argc = len(argv)
  37. try:
  38. if argc > 3:
  39. raise(ValueError)
  40. if argc < 1:
  41. ca_type = input("CA type> ")
  42. else:
  43. ca_type = argv[0]
  44. if argc < 2:
  45. ca_id = input("CA unique id> ")
  46. else:
  47. ca_name = argv[1]
  48. if argc < 3:
  49. ca_name = input("CA human-readable name> ")
  50. else:
  51. ca_name = argv[2]
  52. except ValueError:
  53. print("Malformed input: %s" % l)
  54. return
  55. self.ca_manager.ca[ca_id] = (ca_name, ca_type)
  56. def complete_gen_ca(self, text, line, begidx, endidx):
  57. results = ''
  58. argc = len(("%send"%line).split())
  59. if argc == 2:
  60. results = [a for a in ["ssl", "ssh"] if a.startswith(text)]
  61. return results
  62. def do_sign_request(self, l):
  63. 'Sign a request using a CA: SIGN_REQUEST ca_id request_id'
  64. argv = l.split()
  65. argc = len(argv)
  66. # argument number is too low
  67. if argc < 2:
  68. if argc == 0:
  69. # print available ca
  70. print("Available authority")
  71. print_available_authorities(self.ca_manager)
  72. print("==================")
  73. # print available requests
  74. print("Available request")
  75. print_available_requests(self.ca_manager)
  76. elif argc == 1:
  77. ca_type = None
  78. ca_id = argv[0]
  79. try:
  80. ca_type = self.ca_manager.ca[ca_id].ca_type
  81. except Exception as e:
  82. print ("Error: %s"%e)
  83. return
  84. # print available requests
  85. print("Available request for CA %s (type %s)" % (ca_id, ca_type))
  86. print_available_requests(self.ca_manager, ca_type)
  87. print("==================")
  88. print("usage: sign_request autority request")
  89. else:
  90. # [request_number, authority_number] =
  91. authority_id = argv[0]
  92. request_id = " ".join(argv[1:])
  93. sign_request(self.ca_manager, request_id, authority_id)
  94. def complete_sign_request(self, text, line, begidx, endidx):
  95. results = ''
  96. #too much magic
  97. argc = len(( "%send" % line ).split() )
  98. if argc == 2:
  99. results = [a[0] for a in self.ca_manager.ca if a[0].startswith(text)]
  100. elif argc == 3:
  101. ca_type = None
  102. try:
  103. ca_id = line.split()[1]
  104. ca_type = self.ca_manager.ca[ca_id].ca_type
  105. except Exception as e:
  106. print ("Error: %s"%e)
  107. return
  108. results = [a for a in self.ca_manager.request[ca_type] if str(a).startswith(text)]
  109. return results
  110. def complete(self, text, state):
  111. results = super().complete(text, state)
  112. if results is not None:
  113. return "%s "%results
  114. return results
  115. def do_quit(self, l):
  116. 'Quit this shell'
  117. return True
  118. def print_available_authorities(ca_manager):
  119. for i, ca_item in enumerate(ca_manager.ca):
  120. (ca_id, ca_name, ca_type) = ca_item
  121. print("- %d : [%3s] %-15s (%s)" % (i ,ca_type, ca_id, ca_name))
  122. else:
  123. print("No available CA")
  124. def print_available_requests(ca_manager):
  125. for i, request in enumerate(ca_manager.request):
  126. print("- %d : %s" % (i, request))
  127. else:
  128. print("No requests")