Skip to content

Commit

Permalink
Encrypt pickled result
Browse files Browse the repository at this point in the history
  • Loading branch information
langdal committed Apr 20, 2021
1 parent 88df182 commit 5609d33
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 48 deletions.
6 changes: 3 additions & 3 deletions optimizerapi/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import base64
import io
from numbers import Number
from .securepickle import pickleToString, unpickleFromString
from securepickle import pickleToString, unpickleFromString, get_crypto

import numpy
numpy.random.seed(42)
Expand Down Expand Up @@ -112,8 +112,8 @@ def processResult(result, optimizer, dimensions, cfg, data, space):
plot_objective(result, dimensions=dimensions, usepartialdependence=False)
addPlot(response["plots"], "objective", debug=True)

print(str(response))
response["pickle"] = pickleToString(result)
prettyResult["pickled"] = pickleToString(result, get_crypto())
# print(str(response))
return response

def addPlot(result, id="generic", close=True, debug=False):
Expand Down
3 changes: 2 additions & 1 deletion optimizerapi/securepickle/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .pickler import pickleToString, unpickleFromString
from .pickler import pickleToString, unpickleFromString
from .secure import get_crypto
14 changes: 4 additions & 10 deletions optimizerapi/securepickle/pickler.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import codecs
import pickle
from .secure import create_key, load_key
from cryptography.fernet import Fernet

create_key()
key = load_key()
f = Fernet(key)

def pickleToString(obj):
pickled = codecs.encode(f.encrypt(pickle.dumps(obj)), "base64").decode()
def pickleToString(obj, crypto):
pickled = codecs.encode(crypto.encrypt(pickle.dumps(obj)), "base64").decode()
return pickled

def unpickleFromString(pickled):
unpickled = pickle.loads(f.decrypt(codecs.decode(pickled.encode(), "base64")))
def unpickleFromString(pickled, crypto):
unpickled = pickle.loads(crypto.decrypt(codecs.decode(pickled.encode(), "base64")))
return unpickled

21 changes: 9 additions & 12 deletions optimizerapi/securepickle/secure.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from cryptography.fernet import Fernet
import os

def is_initialized():
pass

def load_key():
with open('mykey.key', 'rb') as mykey:
key = mykey.read()
return key

def create_key():
key = Fernet.generate_key()
with open('mykey.key', 'wb') as mykey:
mykey.write(key)
def get_crypto(key=None):
if key == None: key = os.getenv("PICKLE_KEY", None)
if key == None:
print("No key found, generating new key")
key = Fernet.generate_key()
os.environ["PICKLE_KEY"] = key.decode("utf-8")
print("To reuse key for future server runs, set environment variable PICKLE_KEY=" + os.environ["PICKLE_KEY"])
return Fernet(key)
28 changes: 6 additions & 22 deletions tests/test_securepickle.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
from optimizerapi.securepickle import *
import pickle
from optimizerapi.securepickle import pickleToString, unpickleFromString, get_crypto

def test_pickleToString():
encoded = pickleToString("myString")
f = get_crypto()
encoded = pickleToString("myString", f)
assert encoded != "myString"

def test_unpickleFromString():
encoded = pickleToString("myString")
decoded = unpickleFromString(encoded)
f = get_crypto()
encoded = pickleToString("myString", f)
decoded = unpickleFromString(encoded, f)
assert decoded == "myString"

# def test_load_key():
# create_key()
# key = load_key()
# assert key != ""
# assert len(key) == 44

# def test_encrypt():
# create_key()
# key = load_key()
# f = Fernet(key)
# encoded = pickle.dumps("myString")
# encrypted = f.encrypt(encoded)
# assert encoded != encrypted
# assert type(encrypted) == str
# decrypted = f.decrypt(encrypted)
# assert encoded == decrypted



0 comments on commit 5609d33

Please sign in to comment.