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.

109 lines
2.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env python3
  2. from fqdn import FQDN
  3. import json
  4. import logging
  5. import os.path
  6. import sys
  7. import time
  8. import uuid
  9. from ca_manager.paths import *
  10. __doc__ = """
  11. Procedure to spawn a shell for automation, used by Ansible
  12. """
  13. logfile = os.path.join(REQUEST_USER_HOME, 'request_server.log')
  14. logging.basicConfig(
  15. filename=logfile,
  16. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  17. level=logging.INFO,
  18. )
  19. logger = logging.getLogger('request_server')
  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. if request['keyType'].endswith('_host'):
  57. if not FQDN(request['hostName']).is_valid:
  58. exit_bad('bad FQDN: <%s>' % (request['hostName'],))
  59. logger.info('Writing request to target directory')
  60. with open(os.path.join(REQUESTS_PATH, request_id), 'w') as stream:
  61. stream.write(json.dumps(request))
  62. logger.info('Stopping shell')
  63. exit_good({'requestID': request_id})
  64. elif metarequest['type'] == 'get_certificate':
  65. logger.info('Got a GET request')
  66. request_id = metarequest['requestID']
  67. logger.info('Request id: %s', (request_id,))
  68. result_path = os.path.join(RESULTS_PATH, request_id)
  69. while not os.path.exists(result_path):
  70. time.sleep(1)
  71. with open(result_path, 'r') as stream:
  72. result_data = stream.read()
  73. logger.info('Stopping shell')
  74. exit_good({'requestID': request_id, 'result': result_data})
  75. else:
  76. logger.info('Request type not supported: %s', metarequest['type'])
  77. logger.info('Stopping shell')
  78. exit_bad('unknown_type')
  79. if __name__ == '__main__':
  80. main()