1 import os

2 from dataclasses import dataclass

3 from pathlib import Path

4

5

6@dataclass

7 class NetworkContext:

8 name: str

9 endpoint: str

10 interface: str = "nebula1"

11 active: bool = False

12

13

14@dataclass

15 class MeshConfig:

16 keycloak_base_url: str

17 keycloak_realm: str

18 keycloak_admin_client: str

19 keycloak_issuer: str

20 keycloak_device_auth_url: str

21 keycloak_token_url: str

22 private_key: Path

23 private_net_key_file: Path

24 public_net_key_file: Path

25 config_path: Path

26 authentication_path: Path

27 contexts_file: Path

28 networks_dir: Path

29

30

31 def load_config(base_config_path: Path):

32 KEYCLOAK_BASE_URL = os.getenv(

33 "KEYCLOAK_BASE_URL", "https://auth.dmeshadmin.hydo.ch"

34 )

35 KEYCLOAK_REALM = os.getenv("KEYCLOAK_REALM", "meshadmin")

36 KEYCLOAK_ADMIN_CLIENT = os.getenv("KEYCLOAK_ADMIN_CLIENT", "admin-cli")

37 KEYCLOAK_ISSUER = f"{KEYCLOAK_BASE_URL}/realms/{KEYCLOAK_REALM}"

38 KEYCLOAK_DEVICE_AUTH_URL = f"{KEYCLOAK_ISSUER}/protocol/openid-connect/auth/device"

39 KEYCLOAK_TOKEN_URL = f"{KEYCLOAK_ISSUER}/protocol/openid-connect/token"

40

41 return MeshConfig(

42 keycloak_base_url=KEYCLOAK_BASE_URL,

43 keycloak_realm=KEYCLOAK_REALM,

44 keycloak_admin_client=KEYCLOAK_ADMIN_CLIENT,

45 keycloak_issuer=KEYCLOAK_ISSUER,

46 keycloak_device_auth_url=KEYCLOAK_DEVICE_AUTH_URL,

47 keycloak_token_url=KEYCLOAK_TOKEN_URL,

48 private_key=Path("auth.key"),

49 private_net_key_file=Path("host.key"),

50 public_net_key_file=Path("host.pub"),

51 config_path=Path("config.yaml"),

52 authentication_path=Path("auth.json"),

53 contexts_file=base_config_path / "contexts.yaml",

54 networks_dir=base_config_path / "networks",

55 )

56

57

58_config = None

59

60

61 def get_config():

62 global _config

63 return _config

64

65

66 def set_config(config: MeshConfig):

67 global _config

68 _config = config