forked from The-Shadowserver-Foundation/api_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call-api.py
107 lines (75 loc) · 2.37 KB
/
call-api.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
#!/usr/bin/env python3
"""
call-api.py : Shadowserver Foundation API Utility
This script requires your API details to be stored in ~/.shadowserver.api
with the following contents:
--
[api]
key = 123456798
secret = MySecret
uri = https://transform.shadowserver.org/api2/
--
This script may be called with two or three arguments:
call-api.py <method> <request> [pretty|binary]
The request must be a valid JSON object.
Simple usage:
$ ./call-api.py test/ping '{}'
{"pong":"2020-10-26 23:06:37"}
Pretty output:
$ ./call-api.py test/ping '{}' pretty
{
"pong": "2020-10-26 23:06:42"
}
"""
import os
import sys
import hmac
import hashlib
import json
import configparser
from urllib.request import urlopen, Request
config = configparser.ConfigParser()
config.read(os.environ['HOME'] + "/.shadowserver.api")
TIMEOUT = 45
def api_call(method, request):
"""
Call the specified api method with a request dictionary.
"""
url = config.get('api', 'uri') + method
request['apikey'] = config.get('api', 'key')
request_string = json.dumps(request)
secret_bytes = bytes(str(config.get('api', 'secret')), 'utf-8')
request_bytes = bytes(request_string, 'utf-8')
hmac_generator = hmac.new(secret_bytes, request_bytes, hashlib.sha256)
hmac2 = hmac_generator.hexdigest()
ua_request = Request(url, data=request_bytes, headers={'HMAC2': hmac2})
response = urlopen(ua_request, timeout=TIMEOUT)
return response.read()
if __name__ == '__main__':
if (len(sys.argv) < 3):
exit("Usage: call-api.py method json [pretty|binary]")
try:
api_request = json.loads(sys.argv[2])
except Exception as e:
exit("JSON Exception: " + format(e))
try:
config.get('api', 'key')
except configparser.NoSectionError:
exit("Exception: " + os.environ['HOME'] + "/.shadowserver.api could "
"not be found. Exiting.")
try:
res = api_call(sys.argv[1], api_request)
except Exception as e:
exit("API Exception: " + format(e))
if (len(sys.argv) > 3):
if (sys.argv[3] == "pretty"):
try:
print(json.dumps(json.loads(res), indent=4))
except:
print(res.decode('utf-8'))
elif (sys.argv[3] == "binary"):
os.write(1, res);
else:
exit("Unknown option " + sys.argv[3])
else:
print(res.decode('utf-8'))