Skip to content

Commit

Permalink
Use functionalAccounts.json
Browse files Browse the repository at this point in the history
  • Loading branch information
jl-wynen committed Jan 19, 2024
1 parent 5215b18 commit 399fab8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/scitacean/testing/backend/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import time
from copy import deepcopy
from pathlib import Path
from typing import Any, Dict, Union
from urllib.parse import urljoin

Expand Down Expand Up @@ -35,7 +36,9 @@ def _docker_compose_template() -> Dict[str, Any]:
return template # type: ignore[no-any-return]


def _apply_config(template: Dict[str, Any]) -> Dict[str, Any]:
def _apply_config(
template: Dict[str, Any], account_config_path: Path
) -> Dict[str, Any]:
res = deepcopy(template)
scicat = res["services"]["scicat"]
ports = scicat["ports"][0].split(":")
Expand All @@ -46,6 +49,10 @@ def _apply_config(template: Dict[str, Any]) -> Dict[str, Any]:
env["PID_PREFIX"] = config.PID_PREFIX
env["SITE"] = config.SITE

scicat["volumes"] = [
f"/home/node/app/functionalAccounts.json:{account_config_path}",
]

return res


Expand All @@ -57,7 +64,9 @@ def configure(target_path: _PathLike) -> None:
target_path:
Generate a docker-compose file at this path.
"""
c = yaml.dump(_apply_config(_docker_compose_template()))
account_config_path = Path(target_path).parent / "functionalAccounts.json"
config.dump_account_config(account_config_path)
c = yaml.dump(_apply_config(_docker_compose_template(), account_config_path))
if "PLACEHOLDER" in c:
raise RuntimeError("Incorrect config")

Expand Down
21 changes: 19 additions & 2 deletions src/scitacean/testing/backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# Copyright (c) 2024 SciCat Project (https://github.com/SciCatProject/scitacean)
"""Backend configuration."""

import json
from dataclasses import dataclass
from typing import Dict
from pathlib import Path


@dataclass
Expand All @@ -23,7 +24,7 @@ class SciCatUser:
group: str

@property
def credentials(self) -> Dict[str, str]:
def credentials(self) -> dict[str, str]:
"""Return login credentials for this user.
User as
Expand All @@ -37,6 +38,16 @@ def credentials(self) -> Dict[str, str]:
"password": self.password,
}

def dump(self) -> dict[str, str]:
"""Return a dict that can be serialized to functionalAccounts.json."""
return {
"username": self.username,
"password": self.password,
"email": self.email,
"role": self.group,
"global": False,
}


# see https://github.com/SciCatProject/scicat-backend-next/blob/master/src/config/configuration.ts
USERS = {
Expand Down Expand Up @@ -116,3 +127,9 @@ def local_access(user: str) -> SciCatAccess:
Parameters for the local SciCat backend.
"""
return SciCatAccess(url=f"http://localhost:{SCICAT_PORT}/api/v3/", user=USERS[user])


def dump_account_config(path: Path) -> None:
"""Write a functional account config for the backend."""
with path.open("w") as f:
json.dump([user.dump() for user in USERS.values()], f)

0 comments on commit 399fab8

Please sign in to comment.