Sin descripción

upgrade_to_2.0.0.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # IRIS Source Code
  2. # Copyright (C) 2023 - DFIR-IRS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. import argparse
  19. import logging as log
  20. from pathlib import Path
  21. root_dir = Path(__file__).parent.parent
  22. LOG_FORMAT = '%(asctime)s :: %(levelname)s :: %(module)s :: %(funcName)s :: %(message)s'
  23. LOG_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
  24. log.basicConfig(level=log.INFO, format=LOG_FORMAT, datefmt=LOG_TIME_FORMAT)
  25. env_upgrade_map = {
  26. "DB_USER": "POSTGRES_ADMIN_USER",
  27. "DB_PASS": "POSTGRES_ADMIN_PASSWORD",
  28. "DB_HOST": "POSTGRES_SERVER",
  29. "DB_PORT": "POSTGRES_PORT",
  30. "SECRET_KEY": "IRIS_SECRET_KEY",
  31. "SECURITY_PASSWORD_SALT": "IRIS_SECURITY_PASSWORD_SALT",
  32. "APP_HOST": "IRIS_UPSTREAM_SERVER",
  33. "APP_PORT": "IRIS_UPSTREAM_PORT",
  34. "IRIS_WORKER": "#IRIS_WORKER"
  35. }
  36. class IrisUpgrade200:
  37. @staticmethod
  38. def handle_ports(content):
  39. if 'INTERFACE_HTTPS_PORT' not in content:
  40. log.info('What port do you want to use for HTTPS?')
  41. port = input()
  42. content += f"\n\n#IRIS Ports\nINTERFACE_HTTPS_PORT={port}"
  43. return content
  44. def handle_env(self, dry_run=False):
  45. with open(root_dir / '.env', "r") as f:
  46. content = f.read()
  47. for old, new in env_upgrade_map.items():
  48. if f"{old}=" not in content:
  49. continue
  50. if f"{new}=" in content:
  51. log.info(f'{new} already set. Skipping')
  52. continue
  53. if dry_run:
  54. log.info(f'Would have replaced {old} with {new}')
  55. else:
  56. log.info(f'Replacing {old} with {new}')
  57. content = content.replace(f"{old}=", f"{new}=")
  58. if "IRIS_WORKER=1" in content:
  59. if dry_run:
  60. log.info('Would have disabled old IRIS_WORKER')
  61. else:
  62. log.info('Old IRIS_WORKER is set. Disabling')
  63. content = content.replace("IRIS_WORKER=1", "#IRIS_WORKER=0")
  64. if "IRIS_AUTHENTICATION_METHOD=" not in content:
  65. if dry_run:
  66. log.info('Would have added IRIS_AUTHENTICATION_METHOD to .env file')
  67. else:
  68. log.info('Adding IRIS_AUTHENTICATION_METHOD to .env file')
  69. content += f"\n\n#IRIS Authentication\nIRIS_AUTHENTICATION_METHOD=local"
  70. log.warning('IRIS v2.0.0 changed the default listening port from 4433 to 443.')
  71. log.info('Do you want to change the port? (y/n)')
  72. answer = input()
  73. if answer.lower() == "y":
  74. content = self.handle_ports(content)
  75. log.info("IRIS v2.0.0 comes with new features. The following is optional.")
  76. log.info("Do you want to set the organization name? (y/n)")
  77. answer = input()
  78. if answer.lower() == "y":
  79. log.info("Please enter the organization name:")
  80. organization_name = input()
  81. if "IRIS_ORGANIZATION_NAME=" in content:
  82. log.info("IRIS_ORGANIZATION_NAME already set. Replacing it.")
  83. content = content.replace(f"IRIS_ORGANIZATION_NAME=", "#IRIS_ORGANIZATION_NAME=")
  84. content += f"\n\n#IRIS Organization\nIRIS_ORGANIZATION_NAME={organization_name}"
  85. if not dry_run:
  86. log.info('Writing new .env file')
  87. with open(root_dir / '.env', "w") as f:
  88. f.write(content)
  89. else:
  90. log.info('Dry run: would have written new .env file')
  91. print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
  92. print(content)
  93. print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
  94. def upgrade(self, dry_run=False):
  95. """
  96. Upgrade IRIS to 2.0.0
  97. """
  98. if not self.check(silent=True):
  99. log.info('Upgrade aborted')
  100. return False
  101. log.info('Checking .env file')
  102. self.handle_env(dry_run=dry_run)
  103. log.info('Upgrade done. Please check the changes. ')
  104. log.info('You can now rebuild the dockers with `docker compose build --no-cache`')
  105. log.info('And start them with `docker compose up`')
  106. @staticmethod
  107. def check(silent=False):
  108. """
  109. Check if upgrade looks doable
  110. """
  111. if not (root_dir / '.env').exists():
  112. log.error(f'No .env file found in {root_dir}')
  113. log.info('You can create one with the following command: cp .env.model .env')
  114. log.info('Then edit it to match your criteria')
  115. log.info('You do not need to run this script to upgrade to 2.0.0')
  116. return False
  117. with open(root_dir / '.env', "r") as f:
  118. content = f.read()
  119. if 'POSTGRES_USER' not in content:
  120. log.error('No POSTGRES_USER found in .env file')
  121. log.info('You can edit it to match your criteria')
  122. log.info('You do not need to run this script to upgrade to 2.0.0')
  123. return False
  124. log.info('It looks like you are coming from a previous version of IRIS')
  125. if not silent:
  126. log.info('You can run this script to upgrade to 2.0.0')
  127. return True
  128. if __name__ == "__main__":
  129. parser = argparse.ArgumentParser(description="IRIS 2.0.0 upgrade script")
  130. parser.add_argument("-i", "--install", help="Install upgrade", action="store_true", required=False)
  131. parser.add_argument("-r", "--dry-run", help="Dry run upgrade and see changes", action="store_true",
  132. required=False)
  133. parser.add_argument("-c", "--check", help="Check if upgrade looks doable",
  134. action="store_true", required=False)
  135. args = parser.parse_args()
  136. iu = IrisUpgrade200()
  137. if args.install:
  138. iu.upgrade(args.dry_run)
  139. if args.check:
  140. iu.check()