changed config hierarchy

This commit is contained in:
Devaev Maxim 2019-09-11 19:11:19 +03:00
parent e17889ba42
commit 2535892723
13 changed files with 46 additions and 54 deletions

View File

@ -15,8 +15,6 @@ kvmd:
auth: !include auth.yaml
hid:
type: tty
params:
reset_pin: 4
device: /dev/kvmd-hid

View File

@ -15,8 +15,6 @@ kvmd:
auth: !include auth.yaml
hid:
type: tty
params:
reset_pin: 4
device: /dev/kvmd-hid

View File

@ -15,8 +15,6 @@ kvmd:
auth: !include auth.yaml
hid:
type: tty
params:
reset_pin: 4
device: /dev/kvmd-hid

View File

@ -15,8 +15,6 @@ kvmd:
auth: !include auth.yaml
hid:
type: tty
params:
reset_pin: 4
device: /dev/kvmd-hid

View File

@ -112,11 +112,11 @@ def _init_config(config_path: str, sections: List[str], override_options: List[s
config = make_config(raw_config, scheme)
if "kvmd" in sections:
scheme["kvmd"]["auth"]["internal"] = get_auth_service_class(config.kvmd.auth.internal_type).get_plugin_options()
if config.kvmd.auth.external_type:
scheme["kvmd"]["auth"]["external"] = get_auth_service_class(config.kvmd.auth.external_type).get_plugin_options()
scheme["kvmd"]["auth"]["internal"].update(get_auth_service_class(config.kvmd.auth.internal.type).get_plugin_options())
if config.kvmd.auth.external.type:
scheme["kvmd"]["auth"]["external"].update(get_auth_service_class(config.kvmd.auth.external.type).get_plugin_options())
scheme["kvmd"]["hid"]["params"] = get_hid_class(config.kvmd.hid.type).get_plugin_options()
scheme["kvmd"]["hid"].update(get_hid_class(config.kvmd.hid.type).get_plugin_options())
config = make_config(raw_config, scheme)
@ -164,11 +164,13 @@ def _get_config_scheme(sections: List[str]) -> Dict:
},
"auth": {
"internal_type": Option("htpasswd"),
# "internal": {},
"external_type": Option(""),
# "external": {},
"internal_users": Option([], type=valid_users_list),
"internal": {
"type": Option("htpasswd"),
"force_users": Option([], type=valid_users_list),
},
"external": {
"type": Option(""),
},
},
"info": {
@ -178,7 +180,6 @@ def _get_config_scheme(sections: List[str]) -> Dict:
"hid": {
"type": Option("tty"),
# "params": {},
},
"atx": {

View File

@ -49,7 +49,7 @@ def main(argv: Optional[List[str]]=None) -> None:
with gpio.bcm():
for (name, pin, enabled) in [
*([
("hid_reset_pin", config.hid.params.reset_pin, True),
("hid_reset_pin", config.hid.reset_pin, True),
] if config.hid.type == "tty" else []),
("atx_power_switch_pin", config.atx.power_switch_pin, config.atx.enabled),
("atx_reset_switch_pin", config.atx.reset_switch_pin, config.atx.enabled),

View File

@ -44,9 +44,9 @@ from .. import init
# =====
def _get_htpasswd_path(config: Section) -> str:
if config.kvmd.auth.internal_type != "htpasswd":
if config.kvmd.auth.internal.type != "htpasswd":
raise SystemExit(f"Error: KVMD internal auth not using 'htpasswd'"
f" (now configured {config.kvmd.auth.internal_type!r})")
f" (now configured {config.kvmd.auth.internal.type!r})")
return config.kvmd.auth.internal.file

View File

@ -53,16 +53,16 @@ def main(argv: Optional[List[str]]=None) -> None:
# pylint: disable=protected-access
Server(
auth_manager=AuthManager(
internal_type=config.auth.internal_type,
internal_kwargs=config.auth.internal._unpack(),
external_type=config.auth.external_type,
external_kwargs=(config.auth.external._unpack() if config.auth.external_type else {}),
internal_users=config.auth.internal_users,
internal_type=config.auth.internal.type,
internal_kwargs=config.auth.internal._unpack(ignore=["type", "force_users"]),
external_type=config.auth.external.type,
external_kwargs=(config.auth.external._unpack(ignore=["type"]) if config.auth.external.type else {}),
force_internal_users=config.auth.internal.force_users,
),
info_manager=InfoManager(**config.info._unpack()),
log_reader=LogReader(),
hid=get_hid_class(config.hid.type)(**config.hid.params._unpack()),
hid=get_hid_class(config.hid.type)(**config.hid._unpack(ignore=["type"])),
atx=Atx(**config.atx._unpack()),
msd=MassStorageDevice(**config.msd._unpack()),
streamer=Streamer(**config.streamer._unpack()),

View File

@ -45,7 +45,7 @@ class AuthManager:
external_type: str,
external_kwargs: Dict,
internal_users: List[str],
force_internal_users: List[str],
) -> None:
self.__internal_service = get_auth_service_class(internal_type)(**internal_kwargs)
@ -56,12 +56,12 @@ class AuthManager:
self.__external_service = get_auth_service_class(external_type)(**external_kwargs)
get_logger().info("Using external auth service %r", self.__external_service.get_plugin_name())
self.__internal_users = internal_users
self.__force_internal_users = force_internal_users
self.__tokens: Dict[str, str] = {} # {token: user}
async def authorize(self, user: str, passwd: str) -> bool:
if user not in self.__internal_users and self.__external_service:
if user not in self.__force_internal_users and self.__external_service:
service = self.__external_service
else:
service = self.__internal_service

View File

@ -71,15 +71,16 @@ class Section(dict):
dict.__init__(self)
self.__meta: Dict[str, Dict[str, Any]] = {}
def _unpack(self, _section: Optional["Section"]=None) -> Dict[str, Any]:
if _section is None:
_section = self
def _unpack(self, ignore: Optional[List[str]]=None) -> Dict[str, Any]:
if ignore is None:
ignore = []
unpacked: Dict[str, Any] = {}
for (key, value) in _section.items():
for (key, value) in self.items():
if key not in ignore:
if isinstance(value, Section):
unpacked[key] = value._unpack() # pylint: disable=protected-access
else: # Option
unpacked[_section._get_unpack_as(key)] = value # pylint: disable=protected-access
unpacked[self._get_unpack_as(key)] = value # pylint: disable=protected-access
return unpacked
def _set_meta(self, key: str, default: Any, unpack_as: str, help: str) -> None: # pylint: disable=redefined-builtin

View File

@ -7,8 +7,6 @@ kvmd:
auth: !include auth.yaml
hid:
type: tty
params:
reset_pin: 4
device: /dev/ttyS10
noop: true

View File

@ -57,7 +57,7 @@ def _htpasswd_fixture(request) -> Generator[passlib.apache.HtpasswdFile, None, N
def _run_htpasswd(cmd: List[str], htpasswd_path: str, internal_type: str="htpasswd") -> None:
cmd = ["kvmd-htpasswd", *cmd, "--set-options"]
if internal_type != "htpasswd": # By default
cmd.append("kvmd/auth/internal_type=" + internal_type)
cmd.append("kvmd/auth/internal/type=" + internal_type)
if htpasswd_path:
cmd.append("kvmd/auth/internal/file=" + htpasswd_path)
main(cmd)

View File

@ -50,7 +50,7 @@ def _make_service_kwargs(path: str) -> Dict:
async def _get_configured_manager(
internal_path: str,
external_path: str="",
internal_users: Optional[List[str]]=None,
force_internal_users: Optional[List[str]]=None,
) -> AsyncGenerator[AuthManager, None]:
manager = AuthManager(
@ -58,7 +58,7 @@ async def _get_configured_manager(
internal_kwargs=_make_service_kwargs(internal_path),
external_type=("htpasswd" if external_path else ""),
external_kwargs=(_make_service_kwargs(external_path) if external_path else {}),
internal_users=(internal_users or []),
force_internal_users=(force_internal_users or []),
)
try: