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.

88 lines
2.6 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import cmd
  4. from ca_manager import list_cas, sign_request
  5. class CAManagerShell(cmd.Cmd, object):
  6. intro= """# LILiK CA Manager\n
  7. Welcome to the certification authority shell.
  8. Type help or ? to list commands.
  9. """
  10. prompt= "(CA Manager)> "
  11. def __init__(self, ca_manager):
  12. super(CAManagerShell, self).__init__()
  13. self.ca_manager= ca_manager
  14. def do_ls(self, l):
  15. 'List the available certification authorities: LS'
  16. list_cas(self.ca_manager)
  17. def do_requests(self, l):
  18. 'List the available certification requests: REQUESTS'
  19. print_available_requests(self.ca_manager)
  20. def do_show_ca(self, l):
  21. 'Show certification authority information: SHOW_CA'
  22. raise NotImplementedError()
  23. def do_gen_ssh_ca(self, l):
  24. 'Generate a SSH certification authority: GEN_SSH_CA id name'
  25. try:
  26. [ca_id, ca_name] = l.split(" ", 2)[:2]
  27. self.ca_manager.create_ssh_ca(ca_id, ca_name)
  28. except ValueError:
  29. print "Malformed input: %s" % l
  30. def do_gen_ssl_ca(self, l):
  31. 'Generate a SSL certification authority: GEN_SSL_CA id name'
  32. try:
  33. [ca_id, ca_name] = l.split(" ", 2)[:2]
  34. self.ca_manager.create_ssl_ca(ca_id, ca_name)
  35. except ValueError:
  36. print "Malformed input: %s" % l
  37. def do_sign_request(self, l):
  38. 'Sign a certificate from a request'
  39. # argument number is too low
  40. if len(l) < 2:
  41. # print available requests
  42. print "Available request"
  43. print_available_requests(self.ca_manager)
  44. print "=================="
  45. # print available ca
  46. print "Available authority"
  47. print_available_authorities(self.ca_manager)
  48. print "=================="
  49. # print usage
  50. print "usage: sign_request {{ n }} {{ m }}"
  51. else:
  52. [request_number, authority_number] = l.split(" ", 2)[:2]
  53. sign_request(self.ca_manager, request_number, authority_number)
  54. def do_quit(self, l):
  55. 'Quit this shell'
  56. return True
  57. def print_available_authorities(ca_manager):
  58. for i, ca_item in enumerate(ca_manager.get_cas_list()):
  59. (ca_id, ca_name, ca_type) = ca_item
  60. print("- %d : [%3s] %-15s (%s)" % (i ,ca_type, ca_id, ca_name))
  61. def print_available_requests(ca_manager):
  62. requests = ca_manager.get_requests()
  63. for i, request in enumerate(requests):
  64. print("- %d : %s" % (i, request))
  65. else:
  66. print("No requests")