Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 59cf29b

Browse files
Datatype conversion to bytes is either done implicit or in the algorithms, as pickles were python only. Making it for users impossible to work from different languages.
1 parent 8d4b410 commit 59cf29b

File tree

5 files changed

+32
-52
lines changed

5 files changed

+32
-52
lines changed

vantage/node/encryption.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,8 @@ def public_key_str(self):
9090
"""
9191
return prepare_bytes_for_transport(self.public_key_bytes)
9292

93-
def encrypt_obj_to_base64(self, msg: dict, public_key_base64: str) -> str:
94-
""" Encrypt dictonairy `msg` using `public_key_base64`.
95-
"""
96-
msg_str = pickle.dumps(msg)
97-
return self.encrypt_bytes_to_base64(msg_str, public_key_base64)
98-
99-
def encrypt_str_to_base64(self, msg: str, public_key_base64: str) -> str:
100-
""" Encrypt string `msg` using `public_key_base64`.
101-
"""
102-
msg_bytes = msg.encode(cs.STRING_ENCODING)
103-
return self.encrypt_bytes_to_base64(msg_bytes, public_key_base64)
104-
105-
def encrypt_bytes_to_base64(
106-
self, msg: bytes, public_key_base64: str) -> str:
93+
def encrypt_bytes_to_base64(self, msg: bytes,
94+
public_key_base64: str) -> str:
10795
""" Encrypt a `msg` using `public_key_base64`.
10896
10997
:param msg: message to be encrypted
@@ -189,22 +177,6 @@ def decrypt_bytes_from_base64(self, msg: str) -> bytes:
189177
return self.decrypt_bytes(msg_bytes)
190178
else:
191179
return b""
192-
193-
def decrypt_str_from_base64(self, msg: str) -> str:
194-
""" Decrypt base64 `msg` using our private key
195-
196-
:param msg: string utf-8 encoded base64 encrypted msg
197-
"""
198-
msg_bytes = self.decrypt_bytes_from_base64(msg)
199-
return msg_bytes.decode(cs.STRING_ENCODING)
200-
201-
def decrypt_obj_from_base64(self, msg: str) -> dict:
202-
""" Decrypt base64 `msg` using our private key.
203-
204-
:param msg: dict utf-8 encoded base64 encrypted msg
205-
"""
206-
msg_str = self.decrypt_bytes_from_base64(msg)
207-
return pickle.loads(msg_str)
208180

209181
def __load_private_key(self, private_key_file=None):
210182
""" Load a private key file into this instance.

vantage/node/proxy_server.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
from flask import Flask, request, jsonify
1818

19-
from vantage.util import logger_name
19+
from vantage.util import (
20+
logger_name,
21+
unpack_bytes_from_transport,
22+
prepare_bytes_for_transport
23+
)
2024
from vantage.node.server_io import ClientNodeProtocol
2125
from vantage.node.encryption import Cryptor
2226

@@ -52,7 +56,7 @@ def proxy_task():
5256
TODO we might not want to use the SERVER_IO, as we only use the
5357
encryption property of it
5458
TODO if no public key is present, we should have some sort of
55-
faalback
59+
fallback
5660
"""
5761
assert app.config["SERVER_IO"], "Server IO not initialized"
5862

@@ -78,7 +82,7 @@ def proxy_task():
7882
log.debug(f"{n_organizations} organizations, attemping to encrypt")
7983
encrypted_organizations = []
8084
for organization in organizations:
81-
input_ = organization.get("input", None)
85+
input_ = organization.get("input", b"")
8286
if not input_:
8387
log.error("No input for organization?!")
8488
return
@@ -94,8 +98,10 @@ def proxy_task():
9498

9599
public_key = response.json().get("public_key")
96100

97-
encrypted_input = server_io.cryptor.encrypt_obj_to_base64(
98-
input_, public_key)
101+
input_unpacked = unpack_bytes_from_transport(input_)
102+
encrypted_input = server_io.cryptor.encrypt_bytes_to_base64(
103+
input_unpacked, public_key)
104+
99105
log.debug(f"should be unreadable={encrypted_input}")
100106
organization["input"] = encrypted_input
101107
encrypted_organizations.append(organization)

vantage/node/server_io.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def refresh_token(self):
264264
self._access_token = response.json()["access_token"]
265265

266266
def post_task(self, name:str, image:str, collaboration_id:int,
267-
input_:str='', description='', organization_ids:list=[]) -> dict:
267+
input_:bytes=b'', description='', organization_ids:list=[]) -> dict:
268268
""" Post a new task at the server.
269269
270270
It will also encrypt `input_` for each receiving
@@ -281,28 +281,23 @@ def post_task(self, name:str, image:str, collaboration_id:int,
281281
"""
282282
assert self.cryptor, "Encryption has not yet been setup!"
283283

284-
encryption_method = self.cryptor.encrypt_obj_to_base64
285-
if type(input_) == str:
286-
encryption_method = self.cryptor.encrypt_str_to_base64
287-
elif type(input_) == dict:
288-
encryption_method = self.cryptor.encrypt_obj_to_base64
289-
290284
organization_json_list = []
291285
for org_id in organization_ids:
292286
pub_key = self.request(f"organization/{org_id}").get("public_key")
293287
pub_key = unpack_bytes_from_transport(pub_key)
294288
organization_json_list.append(
295289
{
296290
"id": org_id,
297-
"input": encryption_method(input_, pub_key)
291+
"input": self.cryptor.encrypt_bytes_to_base64(
292+
input_, pub_key)
298293
}
299294
)
300295

301296
return self.request('task', method='post', json={
302297
"name": name,
303298
"image": image,
304299
"collaboration_id": collaboration_id,
305-
"input": input_,
300+
"input": input_, # TODO remove this
306301
"description": description,
307302
"organizations": organization_json_list
308303
})
@@ -673,7 +668,6 @@ def request_token_for_container(self, task_id: int, image: str):
673668
container-token (a task results in a algorithm-
674669
container at the node)
675670
:param image: image-name of the task
676-
677671
"""
678672
self.log.debug(
679673
f"requesting container token for task_id={task_id} "
@@ -792,8 +786,11 @@ def patch_results(self, id: int, initiator_id: int, result: dict):
792786

793787
self.log.debug(public_key)
794788

795-
result["result"] = self.cryptor.encrypt_obj_to_base64(
796-
result["result"], public_key
789+
results_unpacked = unpack_bytes_from_transport(
790+
result["result"])
791+
792+
result["result"] = self.cryptor.encrypt_bytes_to_base64(
793+
results_unpacked, public_key
797794
)
798795
self.log.debug("Sending encrypted results to server")
799796

vantage/tests/test_node_encryption.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import yaml
44
import bcrypt
55
import datetime
6+
import json
67

78
# from sqlalchemy import create_engine
89
# from sqlalchemy.orm import Session
@@ -71,14 +72,14 @@ def test_unpacking_transport_key(self):
7172
)
7273

7374
def test_encryption_decryption(self):
74-
msg = {"msg":"some message!"}
75-
encrypted = self.cryptor.encrypt_obj_to_base64(
75+
msg = json.dumps({"msg":"some message!"}).encode("ascii")
76+
encrypted = self.cryptor.encrypt_bytes_to_base64(
7677
msg,
7778
self.cryptor.public_key_str
7879
)
7980
self.assertNotEqual(msg, encrypted)
8081

81-
unencrypted = self.cryptor.decrypt_obj_from_base64(
82+
unencrypted = self.cryptor.decrypt_bytes_from_base64(
8283
encrypted
8384
)
8485
self.assertEqual(msg, unencrypted)

vantage/tests/test_proxy_server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
from vantage.node.encryption import Cryptor
1818

1919
from vantage.constants import PACAKAGE_FOLDER, APPNAME, DATA_FOLDER, VERSION
20-
from vantage.util import unpack_bytes_from_transport
20+
from vantage.util import (
21+
unpack_bytes_from_transport,
22+
prepare_bytes_for_transport
23+
)
2124

2225

2326
log = logging.getLogger(__name__.split(".")[-1])
@@ -154,13 +157,14 @@ def test_task(self):
154157
if not self.headers:
155158
self.login()
156159

160+
input_ = prepare_bytes_for_transport("bla".encode("ascii"))
157161
proxy_test = self.app.post(
158162
"task",
159163
headers=self.headers,
160164
json={
161165
"organizations":[{
162166
"id":1,
163-
"input": "bla"
167+
"input": input_
164168
}],
165169
"image": "some-image",
166170
"collaboration_id": 1

0 commit comments

Comments
 (0)