Skip to content

Commit

Permalink
Merge pull request #44 from iriusrisk/mdavidsonIR-patch-1
Browse files Browse the repository at this point in the history
Adding a READ ME file, config and library script
  • Loading branch information
Jayarr03 authored May 3, 2024
2 parents 7b14ebe + 89e4c3d commit 3f0693e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Integrations/Tenant True Scripts/README.md
Original file line number Diff line number Diff line change
@@ -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://<insert_domain_name>.iriusrisk.com' # source domain
start_sub_url = '' #initialise
start_apitoken = '<insert_adpi_token>' #insert api token value
start_head = {'api-token': start_apitoken}

post_domain = 'https://<insert_domain_name>.iriusrisk.com' # target domain
post_sub_url = ''
post_apitoken = '<insert_adpi_token>'
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
15 changes: 15 additions & 0 deletions Integrations/Tenant True Scripts/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pip._vendor.requests as requests
import sys
import json

#-------------INITIALISE ENVIRONMENT-----------------#
#set request and head
start_domain = 'https://<insert_domain_name>.iriusrisk.com' # source domain
start_sub_url = '' #initialise
start_apitoken = '<insert_adpi_token>' #insert api token value
start_head = {'api-token': start_apitoken}

post_domain = 'https://<insert_domain_name>.iriusrisk.com' # target domain
post_sub_url = ''
post_apitoken = '<insert_adpi_token>'
post_head = {'api-token': post_apitoken}
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3f0693e

Please sign in to comment.