Skip to content

Commit

Permalink
added delete and select/list method to keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
monty committed Jun 4, 2024
1 parent 3d44f86 commit c3bc972
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 23 deletions.
15 changes: 9 additions & 6 deletions my_scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from monstr.ident.persist import MemoryProfileStore
from monstr.ident.profile import Profile

# logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)


async def convert_store():
Expand All @@ -21,8 +21,8 @@ async def get_key() -> str:
return input('keystore key: ')

my_enc = KeyDataEncrypter(get_key=get_key)
new_store = SQLiteKeyStore(new_file,
encrypter=my_enc)
new_store = FileKeyStore(new_file,
encrypter=my_enc)

await new_store.convert_memstore(old_file)

Expand All @@ -37,17 +37,20 @@ async def get_key() -> str:

my_enc = KeyDataEncrypter(get_key=get_key)

new_store = SQLiteKeyStore(new_file,
new_store = FileKeyStore(new_file,
encrypter=my_enc)

await new_store.add(NamedKeys('moobs'))
# await new_store.add(NamedKeys('moobs'))
await new_store.delete('zoddy')
for c_k in await new_store.select():
print(c_k)

# print(await new_store.get('monty_test'))
#
# from monstr.encrypt import Keys
# await new_store.update(Keys(), 'monty_test')

asyncio.run(convert_store())
asyncio.run(test_store())



Expand Down
85 changes: 68 additions & 17 deletions src/monstr/ident/keystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,21 @@ async def get_store_key(cls, k: NamedKeys, encrypter: KeyDataEncrypter = None) -
async def add(self, k: Keys | NamedKeys, name: str = None) -> NamedKeys:
"""
add a new k, name map to the store
will error if name already exists
should error if name already exists
"""

@abstractmethod
async def update(self, k: Keys | NamedKeys, name: str = None) -> NamedKeys:
"""
update an existing map in the store
will error if name does not exist
should error if name does not exist
"""

@abstractmethod
async def delete(self, name: str = None) -> NamedKeys:
"""
delete from store
should error if name does not exist
"""

@abstractmethod
Expand Down Expand Up @@ -189,7 +196,10 @@ async def get(self, name: str) -> NamedKeys:
return ret

async def select(self, filter: list | dict = None) -> [NamedKeys]:
raise KeyStoreException('FileKeyStore::select: method not implemented select')
# make sure store is loaded
await self._init_store()
ret = [self._store[k] for k in self._store.keys()]
return ret

async def add(self, k: Keys | NamedKeys, name: str = None) -> NamedKeys:
k = self.named_keys(k, name)
Expand Down Expand Up @@ -223,6 +233,19 @@ async def update(self, k: Keys | NamedKeys, name: str = None) -> NamedKeys:

return ret

async def delete(self, name: str = None) -> NamedKeys:
ret = await self.get(name)
if ret is None:
raise KeyStoreException(f'FileKeyStore::delete: {name} not found to delete')

# in mem
del self._store[ret.name]
# and file - note requires the whole store to be rewritten!!
await self.save()

return ret


async def save(self, file_name: str = None):
# nothing to save yet!
if self._store is None:
Expand Down Expand Up @@ -302,35 +325,48 @@ async def _init_store(self):

# this will force an encrypt action, which will stop us having a db ks encrpted with
# different passwords
await self._key_from_rs(rs)
if rs:
await self._key_from_row(rs[0])

async def _key_from_rs(self, rs) -> NamedKeys:
async def _key_from_row(self, row) -> NamedKeys:
ret = None
if rs:
name = rs[0]['name']
key_str = rs[0]['key']

# decrypt if required
if self._encrypter is not None:
key_str = await self._encrypter.decrypt_data(key_str)
name = row['name']
key_str = row['key']

k = Keys.get_key(key_str)
ret = NamedKeys(name=name,
priv_k=k.private_key_hex(),
pub_k=k.public_key_hex())
# decrypt if required
if self._encrypter is not None:
key_str = await self._encrypter.decrypt_data(key_str)

k = Keys.get_key(key_str)
ret = NamedKeys(name=name,
priv_k=k.private_key_hex(),
pub_k=k.public_key_hex())
return ret

async def get(self, name: str) -> NamedKeys:
ret = None
# make sure store is loaded
await self._init_store()

rs = await self._db.select_sql(sql='select name,key from name_key_map where name=?',
args=[name])
if rs:
ret = await self._key_from_row(rs[0])

return await self._key_from_rs(rs)
return ret

async def select(self, filter: list | dict = None) -> [NamedKeys]:
raise KeyStoreException('FileKeyStore::select: method not implemented select')
# at the moment the filter is ignored and this just returns everything

# make sure store is loaded
await self._init_store()

rs = await self._db.select_sql(sql='select name,key from name_key_map')

ret = [await self._key_from_row(c_row) for c_row in rs]

return ret

async def add(self, k: Keys | NamedKeys, name: str = None) -> NamedKeys:
ret = self.named_keys(k, name)
Expand Down Expand Up @@ -365,3 +401,18 @@ async def update(self, k: Keys | NamedKeys, name: str = None) -> NamedKeys:

return ret

async def delete(self, name: str = None) -> NamedKeys:
ret = await self.get(name)
if ret is None:
raise KeyStoreException(f'SQLiteKeyStore::delete: {name} not found to delete')

# try and add name, key in db will fail if it does not already exist
try:
# now do the update
await self._db.execute_sql(sql='delete from name_key_map where name=?',
args=[ret.name])
except Exception as e:
raise KeyStoreException(f'SQLiteKeyStore::delete: {e}')

return ret

0 comments on commit c3bc972

Please sign in to comment.