DB rework

.check model
config
This commit is contained in:
AF 2020-08-06 01:18:16 +03:00
parent 055ca35e1d
commit ad479a615a
9 changed files with 96 additions and 9 deletions

1
.gitignore vendored
View File

@ -206,4 +206,3 @@ fabric.properties
# Others
*.db
/test*.py

8
config.json Normal file
View File

@ -0,0 +1,8 @@
{
"db": "sqlite:///dev.db",
"subjects": {
"ro6ncuJxA_cGQ51hPKw11Q84of08j7GtOjL0Xr5GaFs=": {
"allowed": null
}
}
}

27
config.py Normal file
View File

@ -0,0 +1,27 @@
import json
from typing import Dict, Any, Union
from v25.storage.dbstorage import DBStorage
_d_type = Dict[Any, Union[str, Dict[str, Any]]]
def get_config() -> _d_type:
with open('config.json') as f:
return json.load(f)
def from_config(d: _d_type):
storage = DBStorage(d["db"])
subjects = d["subjects"]
for subject in subjects:
storage.ssssj(subject, json.dumps(subjects[subject]))
def main():
from_config(get_config())
if __name__ == '__main__':
main()

26
dev-main.py Normal file
View File

@ -0,0 +1,26 @@
import json
from typing import Any, Dict
from flask import Flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware
import config
from v25.storage.dbstorage import DBStorage
from v25.web.server.api import API
def simple(_env, resp):
resp(b'404 OK', [])
return []
with open('config.json') as f:
d: Dict[str, Any] = json.load(f)
config.from_config(d)
app = Flask(__name__)
app.wsgi_app = DispatcherMiddleware(simple, {
'/api': API(__name__, DBStorage(d['db']))
})
app.config['ENV'] = 'development'
app.run(port=5013)

6
sssj-edit.py Normal file
View File

@ -0,0 +1,6 @@
from v25.storage.dbstorage import DBStorage
# sqlite:///test.db
storage = DBStorage(input('db:'))
storage.ssssj(input('subject:'), input('json:'))

View File

@ -1,3 +1,4 @@
import json
from typing import Tuple, Optional, Iterable
from nacl.bindings import crypto_sign_PUBLICKEYBYTES
@ -13,6 +14,14 @@ from v25.storage.storage import AbstractStorage
Base = declarative_base()
class SSSJ(Base):
"""Subject Status Storage JSON"""
__tablename__ = 'ssse'
subject = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES), primary_key=True)
status = Column(String, nullable=False, default='{}')
class MsgSgn(Base):
__tablename__ = 'msgsgns'
@ -66,8 +75,11 @@ class DBStorage(AbstractStorage):
Base.metadata.create_all(self.engine)
self.Session = sessionmaker(bind=self.engine)
def check(self, s: Subject) -> dict:
return {'allowed': None}
def check(self, subject: Subject) -> dict:
session = self.Session()
status = json.loads(session.query(SSSJ).filter_by(subject=subject.vkey.encode()).one_or_none().status) or {}
session.close()
return status
def push(self, m: Message) -> None:
session = self.Session()
@ -140,3 +152,12 @@ class DBStorage(AbstractStorage):
msg.flags = flags
session.commit()
session.close()
def ssssj(self, subject: str, status: str):
"""set SSSJ"""
subject = Subject(Encoding.decode(subject)).vkey.encode()
session = self.Session()
session.query(SSSJ).filter_by(subject=subject).delete()
session.add(SSSJ(subject=subject, status=status))
session.commit()
session.close()

View File

@ -10,9 +10,9 @@ class SecureStorage(AbstractStorage):
self.storage = storage
self.subject = subject
def check(self, s: Subject) -> dict:
assert self.subject == s
return self.storage.check(s)
def check(self, subject: Subject) -> dict:
assert self.subject == subject
return self.storage.check(subject)
def push(self, m: Message) -> None:
assert self.subject == m.sfrom

View File

@ -6,7 +6,7 @@ from v25.messaging.subject import Subject
class AbstractStorage(ABC):
def check(self, s: Subject) -> dict:
def check(self, subject: Subject) -> dict:
raise NotImplementedError
def push(self, m: Message) -> None:

View File

@ -26,8 +26,8 @@ class RemoteStorage(AbstractStorage):
})
return req.json()
def check(self, s: Subject) -> dict:
return self.req('check', s.dumps())
def check(self, subject: Subject) -> dict:
return self.req('check', subject.dumps())
def push(self, m: Message) -> None:
self.req('push', m.dumps())