From 89e4c3dcf20d9c639a984ec5287bb41813656718 Mon Sep 17 00:00:00 2001 From: mdavidsonIR <117912155+mdavidsonIR@users.noreply.github.com> Date: Thu, 2 May 2024 16:19:46 +0100 Subject: [PATCH] Adding a READ ME file, config and library script Adding sufficient documentation for usage & configuration of script. Adding a config file to help centralize configuration (adaptations to existing scripts to come) & adding a library migration script from one environment to another. --- Integrations/Tenant True Scripts/README.md | 42 +++++++++++++ Integrations/Tenant True Scripts/config.py | 15 +++++ .../tenant_config_migration_libraries.py | 63 +++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 Integrations/Tenant True Scripts/README.md create mode 100644 Integrations/Tenant True Scripts/config.py create mode 100644 Integrations/Tenant True Scripts/tenant_config_migration_libraries.py diff --git a/Integrations/Tenant True Scripts/README.md b/Integrations/Tenant True Scripts/README.md new file mode 100644 index 0000000..d27a36e --- /dev/null +++ b/Integrations/Tenant True Scripts/README.md @@ -0,0 +1,42 @@ +# Tenant True Up Scripts + +This collection of Python scripts is designed to migrate data or configurations from one environment to another. + +## Configuration + +Before running the scripts, configure your environment settings in `config.py`. +We need to insert values for the domains & api tokens for both environment, where start is your source domain & post is your target domain. +This file should be set up as follows: + +```python +import pip._vendor.requests as requests +import sys +import json + +#-------------INITIALISE ENVIRONMENT-----------------# +#set request and head +start_domain = 'https://.iriusrisk.com' # source domain +start_sub_url = '' #initialise +start_apitoken = '' #insert api token value +start_head = {'api-token': start_apitoken} + +post_domain = 'https://.iriusrisk.com' # target domain +post_sub_url = '' +post_apitoken = '' +post_head = {'api-token': post_apitoken} +``` + +Important: +Replace 'api-token': '' with your actual API tokens for start_head and post_head to authenticate your API requests. + +Usage +To run the scripts, execute the following command from your terminal: + + +```bash +python script_name.py +``` + +## Usage notes +We can run scripts in any order, but please make sure to note any dependencies which may dictate the preferential order these are executed. +For example, it would be advised that we run security classification creation before assets, as the assets have a dependency on the security classification \ No newline at end of file diff --git a/Integrations/Tenant True Scripts/config.py b/Integrations/Tenant True Scripts/config.py new file mode 100644 index 0000000..3d13e8a --- /dev/null +++ b/Integrations/Tenant True Scripts/config.py @@ -0,0 +1,15 @@ +import pip._vendor.requests as requests +import sys +import json + +#-------------INITIALISE ENVIRONMENT-----------------# +#set request and head +start_domain = 'https://.iriusrisk.com' # source domain +start_sub_url = '' #initialise +start_apitoken = '' #insert api token value +start_head = {'api-token': start_apitoken} + +post_domain = 'https://.iriusrisk.com' # target domain +post_sub_url = '' +post_apitoken = '' +post_head = {'api-token': post_apitoken} \ No newline at end of file diff --git a/Integrations/Tenant True Scripts/tenant_config_migration_libraries.py b/Integrations/Tenant True Scripts/tenant_config_migration_libraries.py new file mode 100644 index 0000000..0dba1c2 --- /dev/null +++ b/Integrations/Tenant True Scripts/tenant_config_migration_libraries.py @@ -0,0 +1,63 @@ +import pip._vendor.requests as requests +import sys +import os +import config +import logging + +# Set up logging +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + +def fetch_libraries(url, headers): + #Fetch and log library data from API. + response = requests.get(url, headers=headers) + if response.status_code == 200: + logging.info("Libraries fetched successfully.") + return response.json() + logging.error(f"Fetch failed. Status code: {response.status_code}") + return None + +def process_libraries_data(libraries_data): + #Extract and return custom libraries from API response. + return [ + {'id': item['id'], 'name': item['name'], 'referenceId': item.get('referenceId'), 'filePath': None} + for item in libraries_data.get('_embedded', {}).get('items', []) + if item.get('type', '').lower() == 'custom' + ] + +def export_libraries_to_xml(libraries_info, domain, headers): + #Export libraries to XML, save files locally. + if not os.path.exists('exports'): + os.makedirs('exports') + for library in libraries_info: + export_url = f'{domain}/api/v2/libraries/{library["id"]}/export' + response = requests.get(export_url, headers=headers) + if response.status_code == 200: + file_path = os.path.join('exports', f"{library['id']}.xml") + with open(file_path, 'wb') as file: + file.write(response.content) + library['filePath'] = file_path + logging.info(f"Exported {library['id']}.") + else: + logging.error(f"Export failed for {library['id']}. Status: {response.status_code}") + +def import_libraries(libraries_info, url, headers): + #Import libraries from local XML files to the target system. + for library in libraries_info: + if library.get('filePath') and os.path.exists(library['filePath']): + with open(library['filePath'], 'rb') as file: + files = {'file': (os.path.basename(library['filePath']), file)} + data = {"referenceId": library['referenceId'], "name": library['name']} + response = requests.post(url, headers=headers, data=data, files=files) + if response.status_code == 200: + logging.info(f"Imported {library['name']}.") + else: + logging.error(f"Import failed for {library['name']}. Status: {response.status_code}") + +if __name__ == "__main__": + sys.stdout.reconfigure(encoding='utf-8') + start_url = f"{config.start_domain}/api/v2/libraries?size=10000" + libraries_data = fetch_libraries(start_url, config.start_head) + if libraries_data: + libraries_info = process_libraries_data(libraries_data) + export_libraries_to_xml(libraries_info, config.start_domain, config.start_head) + import_libraries(libraries_info, f"{config.post_domain}/api/v2/libraries/import", config.post_head)