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 list_cas, sign_request
  6. class CAManagerShell(cmd.Cmd, object):
  7. intro= """# LILiK CA Manager\n
  8. Welcome to the certification authority shell.
  9. Type help or ? to list commands.
  10. """
  11. prompt= "(CA Manager)> "
  12. def __init__(self, ca_manager):
  13. super(CAManagerShell, self).__init__()
  14. self.ca_manager= ca_manager
  15. def do_ls_ca(self, l):
  16. 'List the available certification authorities: LS_CA'
  17. list_cas(self.ca_manager)
  18. def do_ls_requests(self, l):
  19. 'List the available certification requests: LS_REQUESTS'
  20. print_available_requests(self.ca_manager)
  21. def do_describe_cas(self, l):
  22. 'Show certification authority information: DESCRIBE_CAS'
  23. raise NotImplementedError
  24. def do_gen_ca(self, l):
  25. 'Generate a certification authority: GEN_CA type id name'
  26. argv = l.split()
  27. argc = len(argv)
  28. try:
  29. if argc > 3:
  30. raise(ValueError)
  31. if argc < 1:
  32. ca_type = input("CA type> ")
  33. else:
  34. ca_type = argv[0]
  35. if argc < 2:
  36. ca_id = input("CA unique id> ")
  37. else:
  38. ca_name = argv[1]
  39. if argc < 3:
  40. ca_name = input("CA human-readable name> ")
  41. else:
  42. ca_name = argv[2]
  43. except ValueError:
  44. print("Malformed input: %s" % l)
  45. return
  46. if ca_type == "ssl":
  47. self.ca_manager.create_ssl_ca(ca_id, ca_name)
  48. elif ca_type == "ssh":
  49. self.ca_manager.create_ssh_ca(ca_id, ca_name)
  50. else:
  51. print("Invalid CA type: %s" % ca_type)
  52. return
  53. def complete_gen_ca(self, text, line, begidx, endidx):
  54. results = ''
  55. argc = len(("%send"%line).split())
  56. if argc == 2:
  57. results = [a for a in ["ssl", "ssh"] if a.startswith(text)]
  58. return results
  59. def do_sign_request(self, l):
  60. 'Sign a request using a CA: SIGN_REQUEST ca_name request_id'
  61. argv = l.split()
  62. argc = len(argv)
  63. # argument number is too low
  64. if argc < 2:
  65. if argc == 0:
  66. # print available ca
  67. print("Available authority")
  68. print_available_authorities(self.ca_manager)
  69. print("==================")
  70. # print available requests
  71. print("Available request")
  72. print_available_requests(self.ca_manager)
  73. elif argc == 1:
  74. ca_type = None
  75. try:
  76. ca_type = self.ca_manager.get_ca(argv[0]).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)"%(argv[0], 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. argc = len(("%send"%line).split())
  93. if argc == 2:
  94. results = [a[0] for a in self.ca_manager.get_cas_list() if a[0].startswith(text)]
  95. elif argc == 3:
  96. ca_type = None
  97. try:
  98. ca_type = self.ca_manager.get_ca(line.split()[1]).ca_type
  99. except Exception as e:
  100. print ("Error: %s"%e)
  101. return
  102. results = [a for a in self.ca_manager.get_requests(ca_type) if str(a).startswith(text)]
  103. return results
  104. def complete(self, text, state):
  105. results = super().complete(text, state)
  106. if results is not None:
  107. return "%s "%results
  108. return results
  109. def do_quit(self, l):
  110. 'Quit this shell'
  111. return True
  112. def print_available_authorities(ca_manager):
  113. for i, ca_item in enumerate(ca_manager.get_cas_list()):
  114. (ca_id, ca_name, ca_type) = ca_item
  115. print("- %d : [%3s] %-15s (%s)" % (i ,ca_type, ca_id, ca_name))
  116. def print_available_requests(ca_manager, ca_type=None):
  117. requests = ca_manager.get_requests(ca_type)
  118. if not requests:
  119. print("No requests")
  120. for i, request in enumerate(requests):
  121. print("- %d : %s" % (i, request))