2020-09-03 21:11:58 +00:00
|
|
|
"""
|
|
|
|
Storage for both keys and data
|
|
|
|
"""
|
2020-09-06 17:59:38 +00:00
|
|
|
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
|
|
|
|
from komrade import *
|
2020-09-03 21:11:58 +00:00
|
|
|
from simplekv.fs import FilesystemStore
|
|
|
|
from simplekv.memory.redisstore import RedisStore
|
|
|
|
import redis
|
|
|
|
import hashlib,os
|
|
|
|
import zlib
|
2020-09-06 17:59:38 +00:00
|
|
|
|
2020-09-03 21:11:58 +00:00
|
|
|
|
|
|
|
|
2020-09-07 07:09:32 +00:00
|
|
|
LOG_GET_SET = True
|
2020-09-03 21:11:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-04 12:46:22 +00:00
|
|
|
class Crypt(Logger):
|
2020-09-10 09:00:11 +00:00
|
|
|
def __init__(self,name=None,fn=None,cell=None,init_d=None,use_secret=CRYPT_USE_SECRET,path_secret=PATH_CRYPT_SECRET):
|
2020-09-03 21:11:58 +00:00
|
|
|
if not name and fn: name=os.path.basename(fn).replace('.','_')
|
|
|
|
|
2020-09-10 09:00:11 +00:00
|
|
|
if use_secret and path_secret:
|
|
|
|
if not os.path.exists(path_secret):
|
|
|
|
self.secret = get_random_binary_id()
|
|
|
|
self.log('shhh! creating secret:',self.secret)
|
|
|
|
with open(path_secret,'wb') as of:
|
|
|
|
of.write(self.secret)
|
|
|
|
else:
|
|
|
|
with open(path_secret,'rb') as f:
|
|
|
|
self.secret = f.read()
|
|
|
|
else:
|
|
|
|
self.secret = b''
|
|
|
|
|
2020-09-03 21:11:58 +00:00
|
|
|
self.name,self.fn,self.cell = name,fn,cell
|
|
|
|
self.store = FilesystemStore(self.fn)
|
2020-09-06 17:09:56 +00:00
|
|
|
if init_d:
|
|
|
|
for k,v in init_d.items():
|
2020-09-06 17:30:23 +00:00
|
|
|
try:
|
|
|
|
self.store.put(k,v)
|
|
|
|
except OSError as e:
|
|
|
|
self.log('!!',e)
|
|
|
|
self.log('!! key ->',k)
|
|
|
|
self.log('!! val ->',v)
|
|
|
|
raise KomradeException()
|
|
|
|
|
2020-09-04 12:46:22 +00:00
|
|
|
|
|
|
|
def log(self,*x):
|
|
|
|
if LOG_GET_SET:
|
|
|
|
super().log(*x)
|
2020-09-03 21:11:58 +00:00
|
|
|
|
|
|
|
def hash(self,binary_data):
|
2020-09-10 09:00:11 +00:00
|
|
|
return hashlib.sha256(binary_data + self.secret).hexdigest()
|
2020-09-03 21:11:58 +00:00
|
|
|
# return zlib.adler32(binary_data)
|
|
|
|
|
|
|
|
def force_binary(self,k_b):
|
2020-09-06 18:40:56 +00:00
|
|
|
if k_b is None: return None
|
2020-09-03 21:11:58 +00:00
|
|
|
if type(k_b)==str: k_b=k_b.encode()
|
2020-09-06 14:45:40 +00:00
|
|
|
if type(k_b)!=bytes: k_b=k_b.decode()
|
2020-09-03 21:11:58 +00:00
|
|
|
return k_b
|
|
|
|
|
2020-09-04 00:06:47 +00:00
|
|
|
def package_key(self,k,prefix=''):
|
2020-09-06 18:43:18 +00:00
|
|
|
if not k: return b''
|
2020-09-03 21:11:58 +00:00
|
|
|
k_b = self.force_binary(k)
|
2020-09-06 18:12:18 +00:00
|
|
|
k_b2 = self.force_binary(prefix) + k_b
|
2020-09-06 18:24:51 +00:00
|
|
|
return k_b2
|
2020-09-03 21:11:58 +00:00
|
|
|
|
|
|
|
def package_val(self,k):
|
|
|
|
k_b = self.force_binary(k)
|
2020-09-04 00:06:47 +00:00
|
|
|
if self.cell is not None: k_b = self.cell.encrypt(k_b)
|
2020-09-03 21:11:58 +00:00
|
|
|
return k_b
|
|
|
|
|
|
|
|
def unpackage_val(self,k_b):
|
|
|
|
try:
|
2020-09-04 00:06:47 +00:00
|
|
|
if self.cell is not None: k_b = self.cell.decrypt(k_b)
|
2020-09-03 21:11:58 +00:00
|
|
|
except ThemisError:
|
2020-09-04 00:06:47 +00:00
|
|
|
pass
|
|
|
|
return k_b
|
2020-09-03 21:11:58 +00:00
|
|
|
|
2020-09-10 09:00:11 +00:00
|
|
|
def has(self,k,prefix=''):
|
|
|
|
k_b=self.package_key(k,prefix=prefix)
|
|
|
|
k_b_hash = self.hash(k_b)
|
|
|
|
try:
|
|
|
|
v=self.store.get(k_b_hash)
|
|
|
|
return True
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
2020-09-03 21:11:58 +00:00
|
|
|
|
2020-09-04 00:06:47 +00:00
|
|
|
def set(self,k,v,prefix=''):
|
2020-09-10 09:00:11 +00:00
|
|
|
if self.has(k,prefix=prefix):
|
|
|
|
self.log("I'm afraid I can't let you do that, overwrite someone's data!")
|
|
|
|
return False
|
|
|
|
|
2020-09-04 00:06:47 +00:00
|
|
|
k_b=self.package_key(k,prefix=prefix)
|
2020-09-06 18:24:51 +00:00
|
|
|
k_b_hash = self.hash(k_b)
|
2020-09-03 21:11:58 +00:00
|
|
|
v_b=self.package_val(v)
|
2020-09-06 18:25:44 +00:00
|
|
|
self.log(f'set(\n\t{prefix}{k},\n\t{k_b}\n\t{k_b_hash}\n\t\n\t{v_b}\n)\n')
|
2020-09-10 09:00:11 +00:00
|
|
|
|
|
|
|
# store
|
|
|
|
self.store.put(k_b_hash,v_b)
|
|
|
|
return True
|
2020-09-03 21:11:58 +00:00
|
|
|
|
2020-09-05 14:09:31 +00:00
|
|
|
def exists(self,k,prefix=''):
|
2020-09-10 09:00:11 +00:00
|
|
|
return self.has(k,prefix=prefix)
|
2020-09-05 14:09:31 +00:00
|
|
|
|
2020-09-04 00:06:47 +00:00
|
|
|
def get(self,k,prefix=''):
|
|
|
|
k_b=self.package_key(k,prefix=prefix)
|
2020-09-06 18:32:03 +00:00
|
|
|
k_b_hash = self.hash(k_b)
|
2020-09-04 00:06:47 +00:00
|
|
|
try:
|
2020-09-06 18:32:03 +00:00
|
|
|
v=self.store.get(k_b_hash)
|
2020-09-04 00:06:47 +00:00
|
|
|
except KeyError:
|
|
|
|
return None
|
2020-09-03 21:11:58 +00:00
|
|
|
v_b=self.unpackage_val(v)
|
|
|
|
return v_b
|
|
|
|
|
|
|
|
|
|
|
|
class KeyCrypt(Crypt):
|
|
|
|
def __init__(self):
|
2020-09-06 09:33:46 +00:00
|
|
|
return super().__init__(name=PATH_CRYPT_CA_KEYS.replace('.','_'))
|
2020-09-03 21:11:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DataCrypt(Crypt):
|
|
|
|
def __init__(self):
|
2020-09-06 09:33:46 +00:00
|
|
|
return super().__init__(name=PATH_CRYPT_CA_DATA.replace('.','_'))
|
2020-09-03 21:11:58 +00:00
|
|
|
|
|
|
|
|
2020-09-06 16:51:25 +00:00
|
|
|
from collections import defaultdict
|
2020-09-06 16:48:47 +00:00
|
|
|
class CryptMemory(Crypt):
|
|
|
|
def __init__(self):
|
|
|
|
self.data = defaultdict(None)
|
|
|
|
self.crypt = defaultdict(None)
|
2020-09-06 16:52:53 +00:00
|
|
|
self.cell = None
|
2020-09-06 16:48:47 +00:00
|
|
|
|
|
|
|
def set(self,k,v,prefix=''):
|
|
|
|
k_b=self.package_key(k,prefix=prefix)
|
|
|
|
v_b=self.package_val(v)
|
|
|
|
self.data[k]=v_b
|
|
|
|
self.crypt[k_b]=v_b
|
|
|
|
|
|
|
|
|
2020-09-03 21:11:58 +00:00
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
crypt = Crypt('testt')
|
|
|
|
|
|
|
|
print(crypt.set('hellothere',b'ryan'))
|
|
|
|
|
|
|
|
# print(crypt.get(b'hello there'))
|