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.

102 lines
2.5 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. __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['status'] = 'ok'
  23. print(json.dumps(response))
  24. sys.exit(0)
  25. def exit_bad(reason):
  26. logger.info('JSON rejected, send error; error %s', reason)
  27. response = {
  28. 'status': 'error',
  29. 'reason': reason,
  30. }
  31. print(json.dumps(response))
  32. sys.exit(0)
  33. def main():
  34. logger.info('Shell started')
  35. response = {}
  36. if (len(sys.argv) > 2):
  37. request_data = sys.argv[2]
  38. else:
  39. request_data = sys.stdin.read(10000)
  40. logger.info('Got request data: %s', request_data)
  41. try:
  42. metarequest = json.loads(request_data)
  43. assert 'type' in metarequest
  44. except:
  45. logger.info('"Type" key not found in request')
  46. logger.info('Stopping shell')
  47. exit_bad('bad_json')
  48. if metarequest['type'] == 'sign_request':
  49. logger.info('Got a sign request')
  50. request = metarequest['request']
  51. request_id = str(uuid.uuid4())
  52. logger.info('Request id %s', request_id)
  53. logger.info('Writing request to target directory')
  54. with open(os.path.join(REQUESTS_PATH, request_id), 'w') as stream:
  55. stream.write(json.dumps(request))
  56. logger.info('Stopping shell')
  57. exit_good({ 'requestID': request_id })
  58. elif metarequest['type'] == 'get_certificate':
  59. logger.info('Got a GET request')
  60. request_id = metarequest['requestID']
  61. logger.info('Request id: %s', request_id)
  62. result_path = os.path.join(RESULTS_PATH, request_id)
  63. while not os.path.exists(result_path):
  64. time.sleep(1)
  65. with open(result_path, 'r') as stream:
  66. result_data = stream.read()
  67. logger.info('Stopping shell')
  68. exit_good({ 'requestID': request_id, 'result': result_data })
  69. else:
  70. logger.info('Request type not supported: %s', metarequest['type'])
  71. logger.info('Stopping shell')
  72. exit_bad('unknown_type')
  73. if __name__ == '__main__':
  74. main()