Нет описания

iris.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # IRIS Source Code
  2. # Copyright (C) 2023 - DFIR-IRIS
  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. from pathlib import Path
  19. from docker_compose import DockerCompose
  20. from rest_api import RestApi
  21. from user import User
  22. from uuid import uuid4
  23. API_URL = 'http://127.0.0.1:8000'
  24. # TODO SSOT: this could be directly read from the .env file
  25. _API_KEY = 'B8BA5D730210B50F41C06941582D7965D57319D5685440587F98DFDC45A01594'
  26. _IRIS_PATH = Path('..')
  27. _TEST_DATA_PATH = Path('./data')
  28. _ADMINISTRATOR_USER_IDENTIFIER = 1
  29. _INITIAL_DEMO_CASE_IDENTIFIER = 1
  30. class Iris:
  31. def __init__(self):
  32. self._docker_compose = DockerCompose(_IRIS_PATH, 'docker-compose.dev.yml')
  33. # TODO remove this field and use _administrator instead
  34. self._api = RestApi(API_URL, _API_KEY)
  35. self._administrator = User(API_URL, _API_KEY, _ADMINISTRATOR_USER_IDENTIFIER)
  36. def create(self, path, body, query_parameters=None):
  37. return self._api.post(path, body, query_parameters)
  38. def get(self, path, query_parameters=None):
  39. return self._api.get(path, query_parameters=query_parameters)
  40. def update(self, path, body):
  41. return self._api.put(path, body)
  42. def delete(self, path):
  43. return self._api.delete(path)
  44. def _create_user(self, user_name):
  45. body = {
  46. 'user_name': user_name,
  47. 'user_login': user_name,
  48. 'user_email': f'{user_name}@aa.eu',
  49. 'user_password': 'aA.1234567890'
  50. }
  51. user = self._api.post('/manage/users/add', body).json()
  52. return User(API_URL, user['data']['user_api_key'], user['data']['id'])
  53. def create_dummy_user(self):
  54. return self._create_user(f'user{uuid4()}')
  55. def create_dummy_case(self):
  56. body = {
  57. 'case_name': 'case name',
  58. 'case_description': 'description',
  59. 'case_customer': 1,
  60. 'case_soc_id': ''
  61. }
  62. response = self._api.post('/api/v2/cases', body).json()
  63. return response['case_id']
  64. def execute_graphql_query(self, payload):
  65. return self._administrator.execute_graphql_query(payload)
  66. def clear_database(self):
  67. cases = self.get('/api/v2/cases', query_parameters={'per_page': 1000000000}).json()
  68. for case in cases['data']:
  69. identifier = case['case_id']
  70. if identifier == _INITIAL_DEMO_CASE_IDENTIFIER:
  71. continue
  72. self.delete(f'/api/v2/cases/{identifier}')
  73. groups = self.get('/manage/groups/list').json()
  74. for group in groups['data']:
  75. identifier = group['group_id']
  76. self.create(f'/manage/groups/delete/{identifier}', {})
  77. users = self.get('/manage/users/list').json()
  78. for user in users['data']:
  79. identifier = user['user_id']
  80. self.get(f'/manage/users/deactivate/{identifier}')
  81. self.create(f'/manage/users/delete/{identifier}', {})