forked from jara001/ah-echo-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho-setup.py
185 lines (124 loc) · 4.92 KB
/
echo-setup.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
# echo-setup.py
"""Setup the rules for Echo test. Run from localhost."""
######################
# Imports & Globals
######################
# Arrowhead
# Requests with .p12 support
import requests_pkcs12
# Global configuration
exec(open("parameters.py").read())
client_public_key = ""
with open(CONFIG["client_pub_path"], "r") as f:
client_public_key = f.read()
client_public_key = "".join(client_public_key.split("\n")[1:-2])
server_public_key = ""
with open(CONFIG["server_pub_path"], "r") as f:
server_public_key = f.read()
server_public_key = "".join(server_public_key.split("\n")[1:-2])
######################
# Arrowhead Framework
######################
# Register provider system
providerID = -1
data = {
"systemName": CONFIG["server_name"],
"authenticationInfo": server_public_key, # required with 'CERTIFICATE' and 'TOKEN'
"address": CONFIG["server_host"],
"port": CONFIG["server_port"],
}
res = requests_pkcs12.post(CONFIG["url_sreg"] + "mgmt/systems", json=data, pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
if (res.status_code >= 400):
res = requests_pkcs12.get(CONFIG["url_sreg"] + "mgmt/systems", pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
for system in res.json()["data"]:
if system["systemName"] == data["systemName"]:
providerID = system["id"]
else:
providerID = res.json()["id"]
print ("Provider ID: %d" % providerID)
# Register consumer system
consumerID = -1
data = {
"systemName": CONFIG["client_name"],
"authenticationInfo": client_public_key, # required with 'CERTIFICATE' and 'TOKEN'
"address": CONFIG["client_ip"],
"port": 0,
}
res = requests_pkcs12.post(CONFIG["url_sreg"] + "mgmt/systems", json=data, pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
if (res.status_code >= 400):
res = requests_pkcs12.get(CONFIG["url_sreg"] + "mgmt/systems", pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
for system in res.json()["data"]:
if system["systemName"] == data["systemName"]:
consumerID = system["id"]
break
else:
consumerID = res.json()["id"]
print ("Consumer ID: %d" % consumerID)
# Define service
serviceID = -1
providerIDs = []
data = {
"serviceDefinition": "echo",
}
res = requests_pkcs12.post(CONFIG["url_sreg"] + "mgmt/services", json=data, pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
if (res.status_code >= 400):
# This works only when there is a provider for that?
#res = requests_pkcs12.get(CONFIG["url_sreg"] + "mgmt/servicedef/" + data["serviceDefinition"], pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
#
#print (res.status_code, res.text)
#
#serviceID = res.json()["data"][0]["serviceDefinition"]["id"]
#
# Note: This returns even the providers!
#for providers in res.json()["data"]:
# providerIDs.append(providers["provider"]["id"])
res = requests_pkcs12.get(CONFIG["url_sreg"] + "mgmt/services", pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
for service in res.json()["data"]:
print (service)
if service["serviceDefinition"] == data["serviceDefinition"]:
serviceID = service["id"]
break
else:
serviceID = res.json()["id"]
print ("Service ID: %d" % serviceID)
# Define interface
interfaceID = -1
data = {
"interfaceName": "HTTP-INSECURE-JSON",
}
res = requests_pkcs12.post(CONFIG["url_sreg"] + "mgmt/interfaces", json=data, pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
if (res.status_code >= 400):
res = requests_pkcs12.get(CONFIG["url_sreg"] + "mgmt/interfaces", pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)
for interface in res.json()["data"]:
print (interface)
if interface["interfaceName"] == data["interfaceName"]:
interfaceID = interface["id"]
break
else:
interfaceID = res.json()["id"]
print ("Interface ID: %d" % interfaceID)
# Authorize connection between client and server
data = {
"consumerId": consumerID,
"interfaceIds": [
interfaceID,
],
#"providerIds": providerIDs, # More dynamic solution
"providerIds": [
providerID,
],
"serviceDefinitionIds": [
serviceID,
],
}
res = requests_pkcs12.post(CONFIG["url_auth"] + "mgmt/intracloud", json=data, pkcs12_filename=CONFIG["auth_p12_path"], pkcs12_password=CONFIG["auth_p12_pass"])
print (res.status_code, res.text)