configure_repo.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python
  2. # Created by Wazuh, Inc. <info@wazuh.com>.
  3. # This program is a free software; you can redistribute it and/or modify it under the terms of GPLv2
  4. import json
  5. import sys
  6. import argparse
  7. from datetime import date, datetime, timedelta
  8. import time
  9. from os.path import dirname, abspath
  10. import re
  11. try:
  12. from elasticsearch import Elasticsearch
  13. except Exception as e:
  14. print("No module 'elasticsearch' found.")
  15. sys.exit()
  16. def read_elatic_config(auth_path):
  17. return json.load(open(auth_path))
  18. def read_snapshots_conf(conf_path):
  19. return json.load(open(conf_path))
  20. def log(msg):
  21. now_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  22. final_msg = "{0} wazuh-snapshot: {1}".format(now_date, msg)
  23. print(final_msg)
  24. if output_file:
  25. foutput.write(final_msg + "\n")
  26. def create_repo(repo_config):
  27. try:
  28. if repo_config['repository_type'] == "local":
  29. body_data = { "type": "fs", "settings": { "location": repo_config['repository'] } }
  30. elif repo_config['repository_type'] == "s3":
  31. body_data = { "type": repo_config['repository_type'], "settings": { "bucket": repo_config['bucket'], "region": repo_config['region'], "base_path": repo_config['base_path'] } }
  32. es.snapshot.create_repository(repository=repo_config['repository'],body=body_data)
  33. except Exception as e:
  34. log("Error: {0}: {1}".format(repo_config['repository'], str(e)))
  35. sys.exit()
  36. if __name__ == "__main__":
  37. # Args
  38. parser = argparse.ArgumentParser(description='Create snapshots repository Elastic')
  39. parser.add_argument('-o', '--output_file', metavar='output_file', type=str, required = False, help='Output filename.')
  40. args = parser.parse_args()
  41. # Vars
  42. current_path = dirname(abspath(__file__))
  43. SLEEP_TIME = 10
  44. LOG_ITERATIONS = 5
  45. output_file = None
  46. if args.output_file:
  47. output_file = args.output_file
  48. foutput = open(output_file, 'a')
  49. # Config
  50. config = read_snapshots_conf('{0}/snapshots_conf.json'.format(current_path))
  51. es_conf = read_elatic_config(config['es_config_path'])
  52. repo_type = config['repository_type']
  53. if es_conf['settings']['elasticsearch']['auth'] == 'True':
  54. http_auth_value=(es_conf['credentials']['elasticsearch']['user'], es_conf['credentials']['elasticsearch']['pass'])
  55. else:
  56. http_auth_value=False
  57. # Elastic connectivity
  58. es = Elasticsearch(
  59. [es_conf['settings']['elasticsearch']['hostname']],
  60. http_auth=http_auth_value,
  61. port=es_conf['settings']['elasticsearch']['port'],
  62. ca_certs=es_conf['settings']['elasticsearch']['ca_cert'],
  63. use_ssl=es_conf['settings']['elasticsearch']['use_ssl'],
  64. )
  65. try:
  66. create_repo(config)
  67. except Exception as e:
  68. print(" Elasticsearch error: {0}".format(str(e)))
  69. sys.exit(1)
  70. if output_file:
  71. foutput.close()