Continuing the discussion from Export and Import Workflows via API!:
Along with the NEW Import/Export Workflow API functionality, we have provided a few useful pythons scripts to get you started with Import & Export Workflow!
Export Workflow: This interactive script requires the ID of the workflow you want to export and an optional argument for a file name. Returns and saves to file the raw json of the exported workflow.
Note: This requires a user api key
import requests, json, re
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import maskpass
from werkzeug.utils import secure_filename
def main():
# Parse command line arguments
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
required = parser.add_argument_group('required arguments')
required.add_argument("-i", "--id", help="The id of the workflow wanted to export", required=True)
parser.add_argument("-f", "--file", help="The file name where the exported workflow will live")
args = vars(parser.parse_args())
# Set up User API key
userKey = maskpass.askpass(prompt="X-API-KEY:", mask="*")
headers = {'X-API-KEY': userKey}
# Build URL
workflowID = args["id"]
regionInput = input("What region?\n1) eu\n2) us\n3) ap\n4) ca\n5) au\n6) us2\n7) us3\nEnter a number 1-7: ")
if regionInput == "1":
region = "eu"
elif regionInput == "2":
region = "us"
elif regionInput == "3":
region = "ap"
elif regionInput == "4":
region = "ca"
elif regionInput == "5":
region = "au"
elif regionInput == "6":
region = "us2"
elif regionInput == "7":
region = "us3"
else:
print("A region must be provided. Please try again.")
return
# Export Workflow V2 API
exportWorkflowUrl = "https://{0}.api.insight.rapid7.com/connect/v2/workflows/{1}/export".format(region, workflowID)
r = requests.get(exportWorkflowUrl, headers=headers)
print("Print the url to check the URL has been correctly encoded or not!")
print("URL: ", r.url)
print("Status Code: ", r.status_code)
body = r.json()
exportWorkflowResponse = json.dumps(r.json(), indent=4, ensure_ascii=False)
print(exportWorkflowResponse)
# Save exported workflow to a file
fileName = args["file"]
if fileName == None or fileName == "":
for key, data in body.items():
for k in data:
if k == "name":
fileName = data["name"]
fileName = fileName.replace(" ", "_") + ".json"
fileName = secure_filename(fileName)
f = open(fileName, "w")
f.write(exportWorkflowResponse)
f.close()
print("The exported workflow has successfully saved to " + fileName)
main()
Import Workflow: This script imports a workflow from a specified file.
import requests, json, re
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import maskpass
def main():
# Parse command line arguments
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
required = parser.add_argument_group('required arguments')
required.add_argument("-f", "--file", help="The file of the workflow wanted to import", required=True)
args = vars(parser.parse_args())
# Set up User API key
userKey = maskpass.askpass(prompt="X-API-KEY:", mask="*")
headers = {'X-API-KEY': userKey}
# Build URL
regionInput = input("What region?\n1) eu\n2) us\n3) ap\n4) ca\n5) au\n6) us2\n7) us3\nEnter a number 1-7: ")
if regionInput == "1":
region = "eu"
elif regionInput == "2":
region = "us"
elif regionInput == "3":
region = "ap"
elif regionInput == "4":
region = "ca"
elif regionInput == "5":
region = "au"
elif regionInput == "6":
region = "us2"
elif regionInput == "7":
region = "us3"
else:
print("A region must be provided. Please try again.")
return
# Open file name and read the json
fileName = args["file"]
with open(fileName) as f:
body = json.load(f)
# Import Workflow V2 API
importWorkflowUrl = "https://{0}.api.insight.rapid7.com/connect/v2/workflows/import".format(region)
r = requests.post(importWorkflowUrl, headers=headers, json=body)
print("Print the url to check the URL has been correctly encoded or not!")
print("URL: ", r.url)
print("Status Code: ", r.status_code)
importWorkflowResponse = json.dumps(r.json(), indent=4, ensure_ascii=False)
print(importWorkflowResponse)
main()
Duplicate Workflow: This interactive script requires the ID of the workflow you want to duplicate. It exports the workflow, allows the user to rename the duplicated workflow and imports the workflow with the new name.
Note: This requires a user api key
import requests, json, re
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import maskpass
def main():
# Parse command line arguments
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
required = parser.add_argument_group('required arguments')
required.add_argument("-i", "--id", help="The id of the workflow wanted to export", required=True)
args = vars(parser.parse_args())
# Set up User API key
userKey = maskpass.askpass(prompt="X-API-KEY:", mask="*")
headers = {'X-API-KEY': userKey}
# Build URL
workflowID = args["id"]
regionInput = input("What region?\n1) eu\n2) us\n3) ap\n4) ca\n5) au\n6) us2\n7) us3\nEnter a number 1-7: ")
if regionInput == "1":
region = "eu"
elif regionInput == "2":
region = "us"
elif regionInput == "3":
region = "ap"
elif regionInput == "4":
region = "ca"
elif regionInput == "5":
region = "au"
elif regionInput == "6":
region = "us2"
elif regionInput == "7":
region = "us3"
else:
print("A region must be provided. Please try again.")
return
# Export Workflow V2 API
exportWorkflowUrl = "https://{0}.api.insight.rapid7.com/connect/v2/workflows/{1}/export".format(region, workflowID)
r = requests.get(exportWorkflowUrl, headers=headers)
print("Print the url to check the URL has been correctly encoded or not!")
print("URL: ", r.url)
print("Status Code: ", r.status_code)
body = r.json()
exportWorkflowResponse = json.dumps(body, indent=4, ensure_ascii=False)
print(exportWorkflowResponse)
# Bail out of Export Workflow returns anything besides a 200
if r.status_code != 200:
return
# Import file with new workflow name
response = input("Do you want to give this workflow a new name? Y/N? ").upper()
# Replace name in json
if response == "Y":
newName = input("What is the new name of the imported workflow? ")
if newName and not newName.isspace():
for key, data in body.items():
for k in data:
if k == "name":
data["name"] = newName
# Import Workflow V2 API
importWorkflowUrl = "https://{0}.api.insight.rapid7.com/connect/v2/workflows/import".format(region)
r = requests.post(importWorkflowUrl, headers=headers, json=body)
print("Print the url to check the URL has been correctly encoded or not!")
print("URL: ", r.url)
print("Status Code: ", r.status_code)
importWorkflowResponse = json.dumps(r.json(), indent=4, ensure_ascii=False)
print(importWorkflowResponse)
main()