DB rework
.check model config
This commit is contained in:
parent
055ca35e1d
commit
ad479a615a
1
.gitignore
vendored
1
.gitignore
vendored
@ -206,4 +206,3 @@ fabric.properties
|
|||||||
|
|
||||||
# Others
|
# Others
|
||||||
*.db
|
*.db
|
||||||
/test*.py
|
|
||||||
|
8
config.json
Normal file
8
config.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"db": "sqlite:///dev.db",
|
||||||
|
"subjects": {
|
||||||
|
"ro6ncuJxA_cGQ51hPKw11Q84of08j7GtOjL0Xr5GaFs=": {
|
||||||
|
"allowed": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
config.py
Normal file
27
config.py
Normal 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
26
dev-main.py
Normal 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
6
sssj-edit.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from v25.storage.dbstorage import DBStorage
|
||||||
|
|
||||||
|
# sqlite:///test.db
|
||||||
|
storage = DBStorage(input('db:'))
|
||||||
|
|
||||||
|
storage.ssssj(input('subject:'), input('json:'))
|
@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
from typing import Tuple, Optional, Iterable
|
from typing import Tuple, Optional, Iterable
|
||||||
|
|
||||||
from nacl.bindings import crypto_sign_PUBLICKEYBYTES
|
from nacl.bindings import crypto_sign_PUBLICKEYBYTES
|
||||||
@ -13,6 +14,14 @@ from v25.storage.storage import AbstractStorage
|
|||||||
Base = declarative_base()
|
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):
|
class MsgSgn(Base):
|
||||||
__tablename__ = 'msgsgns'
|
__tablename__ = 'msgsgns'
|
||||||
|
|
||||||
@ -66,8 +75,11 @@ class DBStorage(AbstractStorage):
|
|||||||
Base.metadata.create_all(self.engine)
|
Base.metadata.create_all(self.engine)
|
||||||
self.Session = sessionmaker(bind=self.engine)
|
self.Session = sessionmaker(bind=self.engine)
|
||||||
|
|
||||||
def check(self, s: Subject) -> dict:
|
def check(self, subject: Subject) -> dict:
|
||||||
return {'allowed': None}
|
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:
|
def push(self, m: Message) -> None:
|
||||||
session = self.Session()
|
session = self.Session()
|
||||||
@ -140,3 +152,12 @@ class DBStorage(AbstractStorage):
|
|||||||
msg.flags = flags
|
msg.flags = flags
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
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()
|
||||||
|
@ -10,9 +10,9 @@ class SecureStorage(AbstractStorage):
|
|||||||
self.storage = storage
|
self.storage = storage
|
||||||
self.subject = subject
|
self.subject = subject
|
||||||
|
|
||||||
def check(self, s: Subject) -> dict:
|
def check(self, subject: Subject) -> dict:
|
||||||
assert self.subject == s
|
assert self.subject == subject
|
||||||
return self.storage.check(s)
|
return self.storage.check(subject)
|
||||||
|
|
||||||
def push(self, m: Message) -> None:
|
def push(self, m: Message) -> None:
|
||||||
assert self.subject == m.sfrom
|
assert self.subject == m.sfrom
|
||||||
|
@ -6,7 +6,7 @@ from v25.messaging.subject import Subject
|
|||||||
|
|
||||||
|
|
||||||
class AbstractStorage(ABC):
|
class AbstractStorage(ABC):
|
||||||
def check(self, s: Subject) -> dict:
|
def check(self, subject: Subject) -> dict:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def push(self, m: Message) -> None:
|
def push(self, m: Message) -> None:
|
||||||
|
@ -26,8 +26,8 @@ class RemoteStorage(AbstractStorage):
|
|||||||
})
|
})
|
||||||
return req.json()
|
return req.json()
|
||||||
|
|
||||||
def check(self, s: Subject) -> dict:
|
def check(self, subject: Subject) -> dict:
|
||||||
return self.req('check', s.dumps())
|
return self.req('check', subject.dumps())
|
||||||
|
|
||||||
def push(self, m: Message) -> None:
|
def push(self, m: Message) -> None:
|
||||||
self.req('push', m.dumps())
|
self.req('push', m.dumps())
|
||||||
|
Loading…
Reference in New Issue
Block a user