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.

152 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_name 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. try:
  75. ca_type = self.ca_manager.get_ca(argv[0]).ca_type
  76. except Exception as e:
  77. print ("Error: %s"%e)
  78. return
  79. # print available requests
  80. print("Available request for CA %s (type %s)"%(argv[0], ca_type))
  81. print_available_requests(self.ca_manager, ca_type)
  82. print("==================")
  83. print("usage: sign_request autority request")
  84. else:
  85. # [request_number, authority_number] =
  86. authority_name = argv[0]
  87. request_name = " ".join(argv[1:])
  88. sign_request(self.ca_manager, request_name, authority_name)
  89. def complete_sign_request(self, text, line, begidx, endidx):
  90. results = ''
  91. argc = len(("%send"%line).split())
  92. if argc == 2:
  93. results = [a[0] for a in self.ca_manager.get_cas_list() if a[0].startswith(text)]
  94. elif argc == 3:
  95. ca_type = None
  96. try:
  97. ca_type = self.ca_manager.get_ca(line.split()[1]).ca_type
  98. except Exception as e:
  99. print ("Error: %s"%e)
  100. return
  101. results = [a for a in self.ca_manager.get_requests(ca_type) if str(a).startswith(text)]
  102. return results
  103. def complete(self, text, state):
  104. results = super().complete(text, state)
  105. if results is not None:
  106. return "%s "%results
  107. return results
  108. def do_quit(self, l):
  109. 'Quit this shell'
  110. return True
  111. def print_available_authorities(ca_manager):
  112. for i, ca_item in enumerate(ca_manager.get_cas_list()):
  113. (ca_id, ca_name, ca_type) = ca_item
  114. print("- %d : [%3s] %-15s (%s)" % (i ,ca_type, ca_id, ca_name))
  115. def print_available_requests(ca_manager, ca_type=None):
  116. requests = ca_manager.get_requests(ca_type)
  117. if not requests:
  118. print("No requests")
  119. for i, request in enumerate(requests):
  120. print("- %d : %s" % (i, request))