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.

97 lines
2.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env python3
  2. import json
  3. import logging
  4. import os.path
  5. import sys
  6. import time
  7. import uuid
  8. logfile= '/home/request/request_server.log'
  9. logging.basicConfig(
  10. filename= logfile,
  11. format= '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  12. level= logging.INFO,
  13. )
  14. logger = logging.getLogger('request_server')
  15. REQUESTS_PATH = "/var/lib/ca_manager/requests"
  16. RESULTS_PATH = "/var/lib/ca_manager/results"
  17. def exit_good(response):
  18. logger.info('JSON accepted, send ok')
  19. response['status'] = 'ok'
  20. print(json.dumps(response))
  21. sys.exit(0)
  22. def exit_bad(reason):
  23. logger.info('JSON rejected, send error; error %s', reason)
  24. response = {
  25. 'status': 'error',
  26. 'reason': reason,
  27. }
  28. print(json.dumps(response))
  29. sys.exit(0)
  30. def main():
  31. logger.info('Shell started')
  32. response = {}
  33. if (len(sys.argv) > 2):
  34. request_data = sys.argv[2]
  35. else:
  36. request_data = sys.stdin.read(10000)
  37. logger.info('Got request data: %s', request_data)
  38. try:
  39. metarequest = json.loads(request_data)
  40. assert 'type' in metarequest
  41. except:
  42. logger.info('"Type" key not found in request')
  43. logger.info('Stopping shell')
  44. exit_bad('bad_json')
  45. if metarequest['type'] == 'sign_request':
  46. logger.info('Got a sign request')
  47. request = metarequest['request']
  48. request_id = str(uuid.uuid4())
  49. logger.info('Request id %s', request_id)
  50. logger.info('Writing request to target directory')
  51. with open(os.path.join(REQUESTS_PATH, request_id), 'w') as stream:
  52. stream.write(json.dumps(request))
  53. logger.info('Stopping shell')
  54. exit_good({ 'requestID': request_id })
  55. elif metarequest['type'] == 'get_certificate':
  56. logger.info('Got a GET request')
  57. request_id = metarequest['requestID']
  58. logger.info('Request id: %s', request_id)
  59. result_path = os.path.join(RESULTS_PATH, request_id)
  60. while not os.path.exists(result_path):
  61. time.sleep(1)
  62. with open(result_path, 'r') as stream:
  63. result_data = stream.read()
  64. logger.info('Stopping shell')
  65. exit_good({ 'requestID': request_id, 'result': result_data })
  66. else:
  67. logger.info('Request type not supported: %s', metarequest['type'])
  68. logger.info('Stopping shell')
  69. exit_bad('unknown_type')
  70. if __name__ == '__main__':
  71. main()