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.

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