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.

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