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.

108 lines
2.7 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. @property
  81. def fields(self):
  82. return [
  83. ("Hostname", self.host_name)
  84. ]