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.

94 lines
2.3 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. exit_bad('bad_json')
  44. if metarequest['type'] == 'sign_request':
  45. logger.info('Got a sign request')
  46. request = metarequest['request']
  47. request_id = str(uuid.uuid4())
  48. logger.info('Request id %s', request_id)
  49. logger.info('Writing request to target directory')
  50. with open(os.path.join(REQUESTS_PATH, request_id), 'w') as stream:
  51. stream.write(json.dumps(request))
  52. exit_good({ 'requestID': request_id })
  53. elif metarequest['type'] == 'get_certificate':
  54. logger.info('Got a GET request')
  55. request_id = metarequest['requestID']
  56. logger.info('Request id: %s', request_id)
  57. result_path = os.path.join(RESULTS_PATH, request_id)
  58. while not os.path.exists(result_path):
  59. time.sleep(1)
  60. with open(result_path, 'r') as stream:
  61. result_data = stream.read()
  62. exit_good({ 'requestID': request_id, 'result': result_data })
  63. else:
  64. logger.info('Request type not supported: %s', metarequest['type'])
  65. exit_bad('unknown_type')
  66. logger.info('Stopping shell')
  67. if __name__ == '__main__':
  68. main()