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.

105 lines
2.6 KiB

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