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.

154 lines
3.9 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. def __bool__(self):
  18. return os.path.exists(self.path)
  19. @property
  20. def name(self):
  21. raise NotImplementedError()
  22. @property
  23. def fields(self):
  24. raise NotImplementedError()
  25. @property
  26. def path(self):
  27. return os.path.join(REQUESTS_PATH, self.req_id)
  28. @property
  29. def destination(self):
  30. return os.path.join(OUTPUT_PATH, self.req_id + '.pub')
  31. class RequestLoader(object):
  32. """
  33. Context manager that loads a request from a file
  34. and return a Request type
  35. """
  36. def __init__(self, request_id):
  37. self.request_id = request_id
  38. self.request_dir = REQUESTS_PATH
  39. @property
  40. def path(self):
  41. return os.path.join(self.request_dir, self.request_id)
  42. def __enter__(self):
  43. with open(self.path, 'r') as stream:
  44. request_data = json.load(
  45. stream,
  46. )
  47. requester = request_data.get('userName', None) or request_data.get('hostName', None)
  48. root_requested = request_data.get('rootRequested', False)
  49. key_data = request_data.get('keyData', None)
  50. # attribute cannot be read from
  51. # json, must add after decoding
  52. request_id = self.request_id
  53. values = request_data.values()
  54. if 'ssh_user' in values:
  55. return UserSSHRequest(
  56. request_id,
  57. requester,
  58. root_requested,
  59. key_data,
  60. )
  61. elif 'ssh_host' in values:
  62. return HostSSHRequest(
  63. request_id,
  64. requester,
  65. key_data,
  66. )
  67. elif 'ssl_host' in values:
  68. return HostSSLRequest(
  69. request_id,
  70. requester,
  71. key_data,
  72. )
  73. else:
  74. # ultimate error, cannot be decoded
  75. return SignRequest(request_id)
  76. def __exit__(self, exc_type, exc_value, traceback):
  77. if exc_type is not None:
  78. print(exc_type, exc_value)
  79. print(traceback)
  80. class UserSSHRequest(SignRequest, object):
  81. def __init__(self, req_id, user_name, root_requested, key_data):
  82. super(UserSSHRequest, self).__init__(req_id)
  83. self.user_name = user_name
  84. self.root_requested = root_requested
  85. self.key_data = key_data
  86. @property
  87. def name(self):
  88. return "User: %s [R:%d]" % (self.user_name, int(self.root_requested))
  89. @property
  90. def fields(self):
  91. return [
  92. ("User name", self.user_name),
  93. ("Root access requested", 'yes' if self.root_requested else 'no')
  94. ]
  95. class HostSSLRequest(SignRequest, object):
  96. def __init__(self, req_id, host_name, key_data):
  97. super(HostSSLRequest, self).__init__(req_id)
  98. self.host_name = host_name
  99. self.key_data = key_data
  100. @property
  101. def name(self):
  102. return "Hostname: %s" % self.host_name
  103. @property
  104. def fields(self):
  105. return [
  106. ("Hostname", self.host_name)
  107. ]
  108. class HostSSHRequest(SignRequest, object):
  109. def __init__(self, req_id, host_name, key_data):
  110. super(HostSSHRequest, self).__init__(req_id)
  111. self.host_name = host_name
  112. self.key_data = key_data
  113. @property
  114. def name(self):
  115. return "Hostname: %s" % self.host_name
  116. @property
  117. def fields(self):
  118. return [
  119. ("Hostname", self.host_name)
  120. ]