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.

148 lines
3.8 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import os.path
  5. import sqlite3
  6. import subprocess
  7. import json
  8. from paths import *
  9. __doc__= """
  10. Module of classes to handle certificate requests
  11. """
  12. class SignRequest(object):
  13. def __init__(self, req_id):
  14. self.req_id = req_id
  15. def __repr__(self):
  16. return ( "%s %s" % ( str(self.__class__.__name__), str(self.req_id) ) )
  17. @property
  18. def name(self):
  19. raise NotImplementedError()
  20. @property
  21. def fields(self):
  22. raise NotImplementedError()
  23. @property
  24. def path(self):
  25. return os.path.join(REQUESTS_PATH, self.req_id)
  26. class RequestLoader(object):
  27. """
  28. Context manager that loads a request from a file
  29. and return a Request type
  30. """
  31. def __init__(self, request_id):
  32. self.request_id = request_id
  33. self.request_dir = REQUESTS_PATH
  34. @property
  35. def path(self):
  36. return os.path.join(self.request_dir, self.request_id)
  37. def __enter__(self):
  38. with open(self.path, 'r') as stream:
  39. request_data = json.load(
  40. stream,
  41. )
  42. requester = request_data.get('userName', None) or request_data.get('hostName', None)
  43. root_requested = request_data.get('rootRequested', False)
  44. key_data = request_data.get('keyData', None)
  45. # attribute cannot be read from
  46. # json, must add after decoding
  47. request_id = self.request_id
  48. values = request_data.values()
  49. if 'ssh_user' in values:
  50. return UserSSHRequest(
  51. request_id,
  52. requester,
  53. root_requested,
  54. key_data,
  55. )
  56. elif 'ssh_host' in values:
  57. return HostSSHRequest(
  58. request_id,
  59. requester,
  60. key_data,
  61. )
  62. elif 'ssl_host' in values:
  63. return HostSSLRequest(
  64. request_id,
  65. requester,
  66. key_data,
  67. )
  68. else:
  69. # ultimate error, cannot be decoded
  70. return SignRequest(request_id)
  71. def __exit__(self, exc_type, exc_value, traceback):
  72. if exc_type is not None:
  73. print(exc_type, exc_value)
  74. print(traceback)
  75. class UserSSHRequest(SignRequest, object):
  76. def __init__(self, req_id, user_name, root_requested, key_data):
  77. super(UserSSHRequest, self).__init__(req_id)
  78. self.user_name = user_name
  79. self.root_requested = root_requested
  80. self.key_data = key_data
  81. @property
  82. def name(self):
  83. return "User: %s [R:%d]" % (self.user_name, int(self.root_requested))
  84. @property
  85. def fields(self):
  86. return [
  87. ("User name", self.user_name),
  88. ("Root access requested", 'yes' if self.root_requested else 'no')
  89. ]
  90. class HostSSLRequest(SignRequest, object):
  91. def __init__(self, req_id, host_name, key_data):
  92. super(HostSSLRequest, self).__init__(req_id)
  93. self.host_name = host_name
  94. self.key_data = key_data
  95. @property
  96. def name(self):
  97. return "Hostname: %s" % self.host_name
  98. @property
  99. def fields(self):
  100. return [
  101. ("Hostname", self.host_name)
  102. ]
  103. class HostSSHRequest(SignRequest, object):
  104. def __init__(self, req_id, host_name, key_data):
  105. super(HostSSHRequest, self).__init__(req_id)
  106. self.host_name = host_name
  107. self.key_data = key_data
  108. @property
  109. def name(self):
  110. return "Hostname: %s" % self.host_name
  111. @property
  112. def fields(self):
  113. return [
  114. ("Hostname", self.host_name)
  115. ]