-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-example.py
executable file
·109 lines (89 loc) · 3.9 KB
/
api-example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
import argparse
import sys
from pprint import pprint
# Try to import the DFIRTrack python api client
try:
import dfirtrackapi_client
from dfirtrackapi_client.api import api_api
from dfirtrackapi_client.model.systemtype import Systemtype
except ImportError as error:
print(error.__class__.__name__ + ": " + error.message)
print("Please install the difrtrack-api-python-client")
print("For details see: https://github.com/n3x77/dfirtrack-api-python-client")
sys.exit(-1)
__author__ = 'Sven Pueschel - n3x77'
__license__ = 'GNU General Public License v3.0'
__version__ = '0.0.2'
def setupCli():
""" Setup the CLI options """
parser = argparse.ArgumentParser(description='Add a new systemtype to DFIRTrack \
using the API')
parser.add_argument('-x', '--host', help='DFIRTrack URL (required)', type=str, metavar="HOST", required=True)
parser.add_argument('-s', '--systemtype', help='Systemtype that shoudl be created \
(required)', type=str, metavar="SYSTEMTYPE", required=True)
parser.add_argument('-u', '--username', help='Username that should be used for \
authentication', type=str, metavar="USERNAME", required=False)
parser.add_argument('-p', '--password', help='Password that should be used for \
authentication', type=str, metavar="PASSWORD", required=False)
parser.add_argument('-k', '--apikey', help='Use API key for authentication instead \
of username and password', type=str, metavar="APIKEY", required=False)
parser.add_argument('-i', '--insecure', help='Disable SSL verification when using \
self-signed certs.', action="store_true", required=False)
# add some additional args
parser.add_argument('--version', action="version", version="%(prog)s (version \
{version})".format(version=__version__))
# parse arguments
args = parser.parse_args()
return args
def main(args):
""" Main function of this python script """
# Setup of the dfirtrackapi_client config
configuration = dfirtrackapi_client.Configuration()
configuration.host = args.host
# Check if api key is used for authentication
if args.apikey:
configuration.username = None
configuration.password = None
configuration.access_token = args.apikey
else:
if args.username == '':
print("Please provide username or use apikey")
sys.exit(-2)
elif args.password == '':
print("Please provide password or use apikey")
sys.exit(-3)
else:
# We use HTTP basic authentication with username and password
configuration.username = args.username
configuration.password = args.password
# Disable SSL verification if needed
# Please do not use this in production!
if args.insecure:
configuration.verify_ssl = False
# We create an api client object
client = dfirtrackapi_client.ApiClient(configuration=configuration)
# (Optional): You can define the user agent
client.user_agent = "dfirtrack-api-python-client/0.47"
# We create an instance of the api client
api_instance = api_api.ApiApi(client)
# Create new Systemtype object
# The systemtype_id is not needed, this will be generated by DFIRTrack
systemtype = Systemtype(
systemtype_name=args.systemtype,
)
try:
r = api_instance.create_systemtype(systemtype=systemtype)
print("Systemtype object: {} has been successfully created.".format(r))
except dfirtrackapi_client.ApiException as e:
print(e)
# Retrieve and print out all available systemtypes
try:
r = api_instance.list_systemtypes()
pprint(r)
except dfirtrackapi_client.ApiException as e:
print("Exception when calling ApiApi->list_systemtypes: %s\n" % e)
if __name__ == "__main__":
"Run script from commandline!"
args = setupCli()
main(args=args)