Merge branch 'main' of ssh://gitea.gchq.icu:7171/IoSL/service-aware-frontend

This commit is contained in:
Georg-Stahn
2023-12-11 19:31:18 +01:00
28 changed files with 580 additions and 180 deletions

0
pkgs/clan-cli/bin/clan Executable file → Normal file
View File

0
pkgs/clan-cli/bin/clan-config Executable file → Normal file
View File

0
pkgs/clan-cli/bin/gen-openapi Executable file → Normal file
View File

View File

@@ -22,6 +22,12 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--host", type=str, default="localhost", help="Host to listen on"
)
parser.add_argument(
"--populate",
action="store_true",
help="Populate the database with dummy data",
default=False,
)
parser.add_argument(
"--no-open", action="store_true", help="Don't open the browser", default=False
)

View File

@@ -35,10 +35,11 @@ def setup_app() -> FastAPI:
# bind sql engine
# TODO comment aut and add flag to run with pupulated data rm *.sql run pytest with marked then start clan webui
# https://docs.pytest.org/en/7.1.x/example/markers.html
sql_models.Base.metadata.drop_all(engine)
# sql_models.Base.metadata.drop_all(engine)
sql_models.Base.metadata.create_all(bind=engine)
app = FastAPI(lifespan=lifespan)
app = FastAPI(lifespan=lifespan, swagger_ui_parameters={"tryItOutEnabled": True})
app.add_middleware(
CORSMiddleware,
allow_origins=origins,

View File

@@ -25,7 +25,7 @@ def sql_error_handler(request: Request, exc: SQLAlchemyError) -> JSONResponse:
def clan_error_handler(request: Request, exc: ClanError) -> JSONResponse:
log.error("ClanError: %s", exc)
log.exception(exc)
detail = [
{
"loc": [],

View File

@@ -1,3 +1,4 @@
import logging
import time
from typing import List, Optional
@@ -5,6 +6,7 @@ import httpx
from fastapi import APIRouter, BackgroundTasks, Depends
from sqlalchemy.orm import Session
from ...errors import ClanError
from .. import sql_crud, sql_db, sql_models
from ..schemas import (
Consumer,
@@ -15,11 +17,15 @@ from ..schemas import (
ProducerCreate,
Repository,
RepositoryCreate,
Resolution,
ResolutionCreate,
)
from ..tags import Tags
router = APIRouter()
log = logging.getLogger(__name__)
#########################
# #
@@ -57,6 +63,15 @@ def get_producer(
return producer
@router.delete("/api/v1/delete_producer", tags=[Tags.producers])
def delete_producer(
entity_did: str = "did:sov:test:1234",
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
sql_crud.delete_producer_by_entity_did(db, entity_did)
return {"message": "Producer deleted"}
#########################
# #
# Consumer #
@@ -93,6 +108,15 @@ def get_consumer(
return consumer
@router.delete("/api/v1/delete_consumer", tags=[Tags.consumers])
def delete_consumer(
entity_did: str = "did:sov:test:1234",
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
sql_crud.delete_consumer_by_entity_did(db, entity_did)
return {"message": "Consumer deleted"}
#########################
# #
# REPOSITORY #
@@ -129,10 +153,19 @@ def get_repository(
limit: int = 100,
db: Session = Depends(sql_db.get_db),
) -> List[sql_models.Repository]:
repository = sql_crud.get_repository_by_did(db, did=entity_did)
repository = sql_crud.get_repository_by_entity_did(db, did=entity_did)
return repository
@router.delete("/api/v1/delete_repository", tags=[Tags.repositories])
def delete_repository(
entity_did: str = "did:sov:test:1234",
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
sql_crud.delete_repository_by_entity_did(db, did=entity_did)
return {"message": "Repository deleted"}
#########################
# #
# Entity #
@@ -141,11 +174,7 @@ def get_repository(
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
def create_entity(
entity: EntityCreate, db: Session = Depends(sql_db.get_db)
) -> EntityCreate | str:
# todo checken ob schon da ...
if sql_crud.get_entity_by_did(db, did=entity.did):
print("did already exsists")
return "Error did already exsists in db"
) -> EntityCreate:
return sql_crud.create_entity(db, entity)
@@ -178,21 +207,19 @@ def get_attached_entities(
return entities
@router.get("/api/v1/detach")
@router.post("/api/v1/detach", response_model=Entity, tags=[Tags.entities])
async def detach(
background_tasks: BackgroundTasks,
entity_did: str = "did:sov:test:1234",
skip: int = 0,
limit: int = 100,
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
background_tasks.add_task(
sql_crud.set_attached_by_entity_did, db, entity_did, False
)
return {"message": "Detaching in the background"}
) -> sql_models.Entity:
entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
return entity
@router.get("/api/v1/attach")
@router.post("/api/v1/attach", tags=[Tags.entities])
async def attach(
background_tasks: BackgroundTasks,
entity_did: str = "did:sov:test:1234",
@@ -200,27 +227,86 @@ async def attach(
limit: int = 100,
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
if sql_crud.get_entity_by_did(db, entity_did) is None:
raise ClanError(f"Entity with did '{entity_did}' not found")
background_tasks.add_task(attach_entity, db, entity_did)
return {"message": "Attaching in the background"}
# TODO
def attach_entity(db: Session, entity_did: str) -> None:
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, True)
try:
if db_entity is not None:
while db_entity.attached:
# query status endpoint
# https://www.python-httpx.org/
response = httpx.get(f"http://{db_entity.ip}", timeout=2)
print(response)
# test with:
# while true ; do printf 'HTTP/1.1 200 OK\r\n\r\ncool, thanks' | nc -l -N localhost 5555 ; done
# client test (apt install python3-httpx):
# httpx http://localhost:5555
# except not reached set false
time.sleep(1)
except Exception as e:
print(e)
if db_entity is not None:
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
while db_entity.attached:
# query status endpoint
# https://www.python-httpx.org/
response = httpx.get(f"http://{db_entity.ip}", timeout=2)
print(response)
# test with:
# while true ; do printf 'HTTP/1.1 200 OK\r\n\r\ncool, thanks' | nc -l -N localhost 5555 ; done
# client test (apt install python3-httpx):
# httpx http://localhost:5555
# except not reached set false
time.sleep(1)
except Exception:
log.warning(f"Entity {entity_did} not reachable. Setting attached to false")
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
@router.delete("/api/v1/delete_entity_recursive", tags=[Tags.entities])
def delete_entity(
entity_did: str = "did:sov:test:1234",
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
sql_crud.delete_entity_by_did_recursive(db, did=entity_did)
return {"message": "Entity deleted and all relations to that entity"}
#########################
# #
# Resolution #
# #
#########################
@router.post(
"/api/v1/create_resolution", response_model=Resolution, tags=[Tags.resolutions]
)
def create_resolution(
resolution: ResolutionCreate,
db: Session = Depends(sql_db.get_db),
) -> sql_models.Resolution:
return sql_crud.create_resolution(db, resolution)
@router.get(
"/api/v1/get_resolutions", response_model=List[Resolution], tags=[Tags.resolutions]
)
def get_resolutions(
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
) -> List[sql_models.Resolution]:
resolutions = sql_crud.get_resolutions(db, skip=skip, limit=limit)
return resolutions
@router.get(
"/api/v1/get_resolution", response_model=List[Resolution], tags=[Tags.resolutions]
)
def get_resolution(
requester_did: str = "did:sov:test:1122",
skip: int = 0,
limit: int = 100,
db: Session = Depends(sql_db.get_db),
) -> List[sql_models.Resolution]:
resolution = sql_crud.get_resolution_by_requester_did(
db, requester_did=requester_did
)
return resolution
@router.delete("/api/v1/delete_resolution", tags=[Tags.resolutions])
def delete_resolution(
requester_did: str = "did:sov:test:1122",
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
sql_crud.delete_resolution_by_requester_did(db, requester_did=requester_did)
return {"message": "Resolution deleted"}

View File

@@ -27,7 +27,7 @@ class ProducerBase(BaseModel):
service_type: str = "3D Printing"
endpoint_url: str = "http://127.0.0.1:8000"
status: str = "unknown"
other: dict = {"test": "test"}
other: dict = {"action": ["register", "deregister", "delete", "create"]}
class ProducerCreate(ProducerBase):
@@ -91,7 +91,11 @@ class EntityBase(BaseModel):
name: str = "C1"
ip: str = "127.0.0.1"
attached: bool = False
other: dict = {"test": "test"}
visible: bool = True
other: dict = {
"network": "Carlo's Home Network",
"roles": ["service repository", "service prosumer"],
}
class EntityCreate(EntityBase):
@@ -105,3 +109,29 @@ class Entity(EntityCreate):
class Config:
orm_mode = True
#########################
# #
# Resolution #
# #
#########################
class ResolutionBase(BaseModel):
requester_name: str = "C1"
requester_did: str = "did:sov:test:1122"
resolved_did: str = "did:sov:test:1234"
other: dict = {"test": "test"}
class ResolutionCreate(ResolutionBase):
pass
class Resolution(ResolutionCreate):
timestamp: datetime
id: int
class Config:
orm_mode = True

View File

@@ -105,6 +105,27 @@ def start_server(args: argparse.Namespace) -> None:
if not args.no_open:
Thread(target=open_browser, args=(base_url, args.sub_url)).start()
# DELETE all data from the database
from . import sql_models
from .sql_db import engine
sql_models.Base.metadata.drop_all(engine)
if args.populate:
test_dir = Path(__file__).parent.parent.parent / "tests"
if not test_dir.is_dir():
raise ClanError(f"Could not find test dir: {test_dir}")
test_db_api = test_dir / "test_db_api.py"
if not test_db_api.is_file():
raise ClanError(f"Could not find test db api: {test_db_api}")
import subprocess
cmd = ["pytest", "-s", "-n0", str(test_db_api)]
subprocess.run(cmd, check=True)
uvicorn.run(
"clan_cli.webui.app:app",
host=args.host,

View File

@@ -3,6 +3,7 @@ from typing import List, Optional
from sqlalchemy.orm import Session
from sqlalchemy.sql.expression import true
from ..errors import ClanError
from . import schemas, sql_models
#########################
@@ -40,6 +41,13 @@ def get_producers_by_entity_did(
)
def delete_producer_by_entity_did(db: Session, entity_did: str) -> None:
db.query(sql_models.Producer).filter(
sql_models.Producer.entity_did == entity_did
).delete()
db.commit()
#########################
# #
# Consumer #
@@ -75,6 +83,13 @@ def get_consumers_by_entity_did(
)
def delete_consumer_by_entity_did(db: Session, entity_did: str) -> None:
db.query(sql_models.Consumer).filter(
sql_models.Consumer.entity_did == entity_did
).delete()
db.commit()
#########################
# #
# REPOSITORY #
@@ -104,7 +119,7 @@ def get_repository_by_uuid(db: Session, uuid: str) -> Optional[sql_models.Reposi
)
def get_repository_by_did(
def get_repository_by_entity_did(
db: Session, did: str, skip: int = 0, limit: int = 100
) -> List[sql_models.Repository]:
return (
@@ -116,6 +131,13 @@ def get_repository_by_did(
)
def delete_repository_by_entity_did(db: Session, did: str) -> None:
db.query(sql_models.Repository).filter(
sql_models.Repository.entity_did == did
).delete()
db.commit()
#########################
# #
# Entity #
@@ -153,21 +175,70 @@ def get_attached_entities(
)
# set attached
# None if did not found
# Returns same entity if setting didnt changed something
def set_attached_by_entity_did(
db: Session, entity_did: str, value: bool
) -> Optional[sql_models.Entity]:
# ste attached to true
) -> sql_models.Entity:
db_entity = get_entity_by_did(db, entity_did)
if db_entity is not None:
# db_entity.attached = Column(True)
setattr(db_entity, "attached", value)
# save changes in db
db.add(db_entity)
db.commit()
db.refresh(db_entity)
return db_entity
else:
return db_entity
if db_entity is None:
raise ClanError(f"Entity with did '{entity_did}' not found")
setattr(db_entity, "attached", value)
# save changes in db
db.add(db_entity)
db.commit()
db.refresh(db_entity)
return db_entity
def delete_entity_by_did(db: Session, did: str) -> None:
db.query(sql_models.Entity).filter(sql_models.Entity.did == did).delete()
db.commit()
def delete_entity_by_did_recursive(db: Session, did: str) -> None:
delete_producer_by_entity_did(db, did)
delete_consumer_by_entity_did(db, did)
delete_repository_by_entity_did(db, did)
delete_entity_by_did(db, did)
#########################
# #
# Resolution #
# #
#########################
def create_resolution(
db: Session, resolution: schemas.ResolutionCreate
) -> sql_models.Resolution:
db_resolution = sql_models.Resolution(**resolution.dict())
db.add(db_resolution)
db.commit()
db.refresh(db_resolution)
return db_resolution
def get_resolutions(
db: Session, skip: int = 0, limit: int = 100
) -> List[sql_models.Resolution]:
return db.query(sql_models.Resolution).offset(skip).limit(limit).all()
def get_resolution_by_requester_did(
db: Session, requester_did: str, skip: int = 0, limit: int = 100
) -> List[sql_models.Resolution]:
return (
db.query(sql_models.Resolution)
.filter(sql_models.Resolution.requester_did == requester_did)
.offset(skip)
.limit(limit)
.all()
)
def delete_resolution_by_requester_did(db: Session, requester_did: str) -> None:
db.query(sql_models.Resolution).filter(
sql_models.Resolution.requester_did == requester_did
).delete()
db.commit()

View File

@@ -26,6 +26,7 @@ class Entity(Base):
name = Column(String, index=True)
ip = Column(String, index=True)
attached = Column(Boolean, index=True)
visible = Column(Boolean, index=True)
## Non queryable body ##
# In here we deposit: Network, Roles, Visible, etc.
@@ -106,4 +107,5 @@ class Resolution(Base):
requester_name = Column(String, index=True)
requester_did = Column(String, index=True)
resolved_did = Column(String, index=True)
timestamp = Column(DateTime, index=True)
other = Column(JSON)
timestamp = Column(DateTime(timezone=True), server_default=func.now())

View File

@@ -7,6 +7,7 @@ class Tags(Enum):
consumers = "consumers"
entities = "entities"
repositories = "repositories"
resolutions = "resolution"
def __str__(self) -> str:
return self.value
@@ -29,4 +30,8 @@ tags_metadata: List[Dict[str, Any]] = [
"name": str(Tags.repositories),
"description": "Operations on a repository.",
},
{
"name": str(Tags.resolutions),
"description": "Operations on a resolution.",
},
]

View File

@@ -38,8 +38,11 @@ def make_test_post_and_get(
headers={"Content-Type": "application/json"},
)
assert response.status_code == 200
if paramter == "repository":
assert_extra_info(["time_created"], request_body, response.json())
elif paramter == "resolution":
assert_extra_info(["timestamp", "id"], request_body, response.json())
elif paramter == "consumer":
assert_extra_info(["id"], request_body, response.json())
elif paramter == "entity":
@@ -55,6 +58,8 @@ def make_test_post_and_get(
assert response.status_code == 200
if paramter == "repository":
assert_extra_info(["time_created"], request_body, response.json()[0])
elif paramter == "resolution":
assert_extra_info(["timestamp", "id"], request_body, response.json()[0])
elif paramter == "consumer":
assert_extra_info(["id"], request_body, response.json()[0])
elif paramter == "entity":
@@ -77,7 +82,7 @@ def test_producer(api: TestClient) -> None:
"service_type": "3D Printing",
"endpoint_url": "http://127.0.0.1:8000",
"status": "unknown",
"other": {"test": "test"},
"other": {"action": ["register", "deregister", "delete", "create"]},
"entity_did": default_entity_did,
}
paramter = "producer"
@@ -151,7 +156,7 @@ def test_producer2(api: TestClient) -> None:
"service_type": "Fax",
"endpoint_url": "http://127.0.0.1:8001",
"status": "unknown",
"other": {"faxen": "dicke"},
"other": {"action": ["register", "deregister", "delete", "create"]},
"entity_did": default_entity_did2,
}
paramter = "producer"
@@ -166,7 +171,7 @@ def test_producer3(api: TestClient) -> None:
"service_type": "VR-Stream",
"endpoint_url": "http://127.0.0.1:8002",
"status": "unknown",
"other": {"oculos": "rift"},
"other": {"action": ["register", "deregister", "delete", "create"]},
"entity_did": default_entity_did3,
}
paramter = "producer"
@@ -181,7 +186,7 @@ def test_producer4(api: TestClient) -> None:
"service_type": "gallary",
"endpoint_url": "http://127.0.0.1:8003",
"status": "unknown",
"other": {"nice": "pics"},
"other": {"action": ["register", "deregister", "delete", "create"]},
"entity_did": default_entity_did4,
}
paramter = "producer"
@@ -196,7 +201,7 @@ def test_producer5(api: TestClient) -> None:
"service_type": "Game-Shop",
"endpoint_url": "http://127.0.0.1:8004",
"status": "unknown",
"other": {"war": "games"},
"other": {"action": ["register", "deregister", "delete", "create"]},
"entity_did": default_entity_did5,
}
paramter = "producer"
@@ -389,7 +394,11 @@ def test_entity(api: TestClient) -> None:
"name": "C1",
"ip": "127.0.0.1",
"attached": False,
"other": {"test": "test"},
"visible": True,
"other": {
"network": "Carlo1's Home Network",
"roles": ["service repository", "service consumer"],
},
}
paramter = "entity"
# get_request = "entity_did=did%3Asov%3Atest%3A1234"
@@ -402,8 +411,53 @@ def test_entity2(api: TestClient) -> None:
"name": "C2",
"ip": "127.0.0.2",
"attached": False,
"other": {"test": "test"},
"visible": True,
"other": {
"network": "Carlo2's Home Network",
"roles": ["service repository", "service prosumer"],
},
}
paramter = "entity"
get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request)
#########################
# #
# Resolution #
# #
#########################
def test_resolution(api: TestClient) -> None:
request_body = {
"requester_did": default_entity_did2,
"requester_name": "C2",
"resolved_did": default_entity_did,
"other": {"test": "test"},
}
paramter = "resolution"
get_request = "requester_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_resolution2(api: TestClient) -> None:
request_body = {
"requester_did": default_entity_did3,
"requester_name": "C3",
"resolved_did": default_entity_did,
"other": {"test": "test"},
}
paramter = "resolution"
get_request = "requester_did=" + url.quote(default_entity_did3)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_resolution3(api: TestClient) -> None:
request_body = {
"requester_did": default_entity_did4,
"requester_name": "C4",
"resolved_did": default_entity_did,
"other": {"test": "test"},
}
paramter = "resolution"
get_request = "requester_did=" + url.quote(default_entity_did4)
make_test_post_and_get(api, request_body, paramter, get_request)