forked from jara001/ah-echo-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho-client.py
164 lines (123 loc) · 4.73 KB
/
echo-client.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
#!/usr/bin/env python3
# echo-client.py
"""Simple echo client that is Arrowhead Compliant."""
######################
# Imports & Globals
######################
# Arrowhead
# Requests with .p12 support
import requests_pkcs12
# Socket communication
import socket
# Error output
import sys
# Global configuration
exec(open("parameters.py").read())
# Reading out the public key (as we need it in plaintext)
public_key = ""
with open(CONFIG["client_pub_path"], "r") as f:
public_key = f.read()
public_key = "".join(public_key.split("\n")[1:-2])
######################
# Arrowhead Framework
######################
def registerConsumer():
"""Register the client in the Arrowhead Framework."""
print ("Registering the system...")
data = {
# *Who are we?
# 'systemName': name of the client
# 'authenticationInfo' is required with 'CERTIFICATE' and 'TOKEN'
# - For 'CERTIFICATE' I put there public key (so it should be asymmetric encryption).
# 'address': IP address of the client
# 'port': port is not used, so zero (we are consuming)
"systemName": CONFIG["client_name"],
"authenticationInfo": public_key,
"address": CONFIG["client_ip"],
"port": 0,
}
res = requests_pkcs12.post(
CONFIG["url_sreg"]
+ "register-system",
json=data, pkcs12_filename=CONFIG["client_p12_path"], pkcs12_password=CONFIG["client_p12_pass"], verify=CONFIG["ca_path"])
print (res.status_code, res.text)
if (res.status_code >= 400):
print ("Unable to create the system.", file=sys.stderr)
exit (1)
print ("System registered.\nConsumer ID: %d" % res.json()["id"])
return True
def findServer():
"""Find the echo server using Arrowhead Framework."""
global CONFIG
print ("Looking for echo server...")
data = {
# *Who are we?
# Here we introduce the system asking the service.
# 'systemName' should be same as the name in the certificate.
# - Otherwise, we get an SSL error.
# 'authenticationInfo' is required with 'CERTIFICATE' and 'TOKEN'
# - For 'CERTIFICATE' I put there public key (so it should be asymmetric encryption).
# 'address' is an IP address / name? of the system
# 'port' is port used for the communication
"requesterSystem": {
"systemName": CONFIG["client_name"],
"authenticationInfo": public_key,
"address": CONFIG["client_ip"],
"port": 0, # I assume that this means that we are not listening
},
# Dynamic Orchestration
# - By passing this value we say that we want to find the counterpart dynamically,
# skipping any pre-set configuration in the Orchestrator.
"orchestrationFlags": {
"overrideStore": "true"
},
# Which service do we want?
# Since the dynamic orchestration is enabled, this is mandatory*.
"requestedService": {
# Which interface we want to use?
# - This is optional.
# In here, you can use 'interfaceRequirement' (string). But it is not supported by Orchestrator (anymore?).
"interfaceRequirements": [
"HTTP-INSECURE-JSON"
],
# *What is the name of the service?
"serviceDefinitionRequirement": "echo",
}
}
res = requests_pkcs12.post(
CONFIG["url_orch"]
+ "orchestration",
json=data, pkcs12_filename=CONFIG["client_p12_path"], pkcs12_password=CONFIG["client_p12_pass"], verify=CONFIG["ca_path"])
print (res.status_code, res.text)
if (res.status_code >= 400):
print ("Client is not authorized for communication with the Orchestrator.", file=sys.stderr)
print ("Trying to register it instead...")
registerConsumer()
exit (1)
else:
for provider in res.json()["response"]:
CONFIG["client_host"] = provider["provider"]["address"]
CONFIG["client_port"] = provider["provider"]["port"]
print (provider)
break
else:
return False
return True
######################
# Echo Client
######################
def sendData():
"""Open up socket to the server, send data and obtain the response."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((CONFIG["client_host"], CONFIG["client_port"]))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
######################
# Main
######################
if __name__ == "__main__":
if findServer():
sendData()
else:
print ("Unable to find the server. Is it registered and running?", file=sys.stderr)