Skip to content

Commit 09e2413

Browse files
committed
datamodel: management: reverting to absolute path for unix-socket
Warning that the unix-socket is not located in rundir.
1 parent 0837cfa commit 09e2413

File tree

6 files changed

+27
-56
lines changed

6 files changed

+27
-56
lines changed

doc/_static/config.schema.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"default": 256
5555
},
5656
"management": {
57-
"description": "Configuration of management HTTP API. By default, unix-socket is located in 'rundir'.",
57+
"description": "Configuration of management HTTP API.",
5858
"type": "object",
5959
"properties": {
6060
"unix-socket": {
@@ -75,7 +75,7 @@
7575
}
7676
},
7777
"default": {
78-
"unix_socket": "kres-api.sock",
78+
"unix_socket": "/run/knot-resolver/kres-api.sock",
7979
"interface": null
8080
}
8181
},

python/knot_resolver/client/command.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from typing import Dict, List, Optional, Set, Tuple, Type, TypeVar
55
from urllib.parse import quote
66

7-
from knot_resolver.constants import API_SOCK_FILE, API_SOCK_NAME, CONFIG_FILE, RUN_DIR
8-
from knot_resolver.datamodel.types import IPAddressPort
7+
from knot_resolver.constants import API_SOCK_FILE, CONFIG_FILE
8+
from knot_resolver.datamodel.types import IPAddressPort, WritableFilePath
99
from knot_resolver.utils.modeling import parsing
1010
from knot_resolver.utils.modeling.exceptions import DataValidationError
1111
from knot_resolver.utils.requests import SocketDesc
@@ -155,37 +155,25 @@ def get_socket_from_config(config: Path, optional_file: bool) -> Optional[Socket
155155
with open(config, "r", encoding="utf8") as f:
156156
data = parsing.try_to_parse(f.read())
157157

158-
rkey = "rundir"
159-
rundir = Path(data[rkey]) if rkey in data else RUN_DIR
160-
161158
mkey = "management"
162159
if mkey in data:
163160
management = data[mkey]
164161

162+
skey = "unix-socket"
163+
if skey in management:
164+
sock = WritableFilePath(management[skey], object_path=f"/{mkey}/{skey}")
165+
return SocketDesc(
166+
f'http+unix://{quote(str(sock), safe="")}/',
167+
f'Key "/management/unix-socket" in "{config}" file',
168+
)
165169
ikey = "interface"
166170
if ikey in data[mkey]:
167-
ip = IPAddressPort(data[mkey][ikey], object_path=f"/{mkey}/{ikey}")
171+
ip = IPAddressPort(management[ikey], object_path=f"/{mkey}/{ikey}")
168172
return SocketDesc(
169173
f"http://{ip.addr}:{ip.port}",
170174
f'Key "/management/interface" in "{config}" file',
171175
)
172-
173-
skey = "unix-socket"
174-
if skey in management:
175-
socket = Path(management[skey])
176-
if not socket.is_absolute():
177-
socket = rundir / socket
178-
return SocketDesc(
179-
f'http+unix://{quote(str(socket), safe="")}/',
180-
f'Key "/management/unix-socket" in "{config}" file',
181-
)
182-
183-
socket = rundir / API_SOCK_NAME
184-
return SocketDesc(
185-
f'http+unix://{quote(str(socket), safe="")}/',
186-
f'Key "/rundir" in "{config}" file',
187-
)
188-
176+
return None
189177
except ValueError as e:
190178
raise DataValidationError(*e.args) from e # pylint: disable=no-value-for-parameter
191179
except OSError as e:

python/knot_resolver/constants.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
USER = "knot-resolver"
66
GROUP = "knot-resolver"
77

8-
# default files names
9-
API_SOCK_NAME = "kres-api.sock"
10-
118
# default dirs paths
129
RUN_DIR = Path("/run/knot-resolver")
1310
ETC_DIR = Path("/etc/knot-resolver")
@@ -16,7 +13,7 @@
1613

1714
# default files paths
1815
CONFIG_FILE = ETC_DIR / "config.yaml"
19-
API_SOCK_FILE = RUN_DIR / API_SOCK_NAME
16+
API_SOCK_FILE = RUN_DIR / "kres-api.sock"
2017

2118
# executables paths
2219
KRESD_EXECUTABLE = SBIN_DIR / "kresd"

python/knot_resolver/constants.py.in

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ VERSION = "@version@"
55
USER = "@user@"
66
GROUP = "@group@"
77

8-
# default files names
9-
API_SOCK_NAME = "kres-api.sock"
10-
118
# default dirs paths
129
RUN_DIR = Path("@run_dir@")
1310
ETC_DIR = Path("@etc_dir@")
@@ -16,7 +13,7 @@ CACHE_DIR = Path("@cache_dir@")
1613

1714
# default files paths
1815
CONFIG_FILE = ETC_DIR / "config.yaml"
19-
API_SOCK_FILE = RUN_DIR / API_SOCK_NAME
16+
API_SOCK_FILE = RUN_DIR / "kres-api.sock"
2017

2118
# executables paths
2219
KRESD_EXECUTABLE = SBIN_DIR / "kresd"

python/knot_resolver/datamodel/config_schema.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import logging
22
import os
33
import socket
4-
from pathlib import Path
54
from typing import Any, Dict, List, Literal, Optional, Tuple, Union
65

7-
from knot_resolver.constants import API_SOCK_NAME, RUN_DIR, VERSION
6+
from knot_resolver.constants import API_SOCK_FILE, RUN_DIR, VERSION
87
from knot_resolver.datamodel.cache_schema import CacheSchema
98
from knot_resolver.datamodel.defer_schema import DeferSchema
109
from knot_resolver.datamodel.dns64_schema import Dns64Schema
@@ -96,7 +95,7 @@ class Raw(ConfigSchema):
9695
rundir: Directory where the resolver can create files and which will be it's cwd.
9796
workers: The number of running kresd (Knot Resolver daemon) workers. If set to 'auto', it is equal to number of CPUs available.
9897
max_workers: The maximum number of workers allowed. Cannot be changed in runtime.
99-
management: Configuration of management HTTP API. By default, unix-socket is located in 'rundir'.
98+
management: Configuration of management HTTP API.
10099
webmgmt: Configuration of legacy web management endpoint.
101100
options: Fine-tuning global parameters of DNS resolver operation.
102101
network: Network connections and protocols configuration.
@@ -119,7 +118,7 @@ class Raw(ConfigSchema):
119118
rundir: WritableDir = lazy_default(WritableDir, str(RUN_DIR))
120119
workers: Union[Literal["auto"], IntPositive] = IntPositive(1)
121120
max_workers: IntPositive = IntPositive(WORKERS_MAX)
122-
management: ManagementSchema = lazy_default(ManagementSchema, {"unix-socket": str(API_SOCK_NAME)})
121+
management: ManagementSchema = lazy_default(ManagementSchema, {"unix-socket": str(API_SOCK_FILE)})
123122
webmgmt: Optional[WebmgmtSchema] = None
124123
options: OptionsSchema = OptionsSchema()
125124
network: NetworkSchema = NetworkSchema()
@@ -174,14 +173,6 @@ def _workers(self, obj: Raw) -> Any:
174173
)
175174
return obj.workers
176175

177-
def _management(self, obj: Raw) -> Any:
178-
if obj.management.unix_socket:
179-
soc = Path(obj.management.unix_socket.serialize())
180-
if soc.is_absolute():
181-
return obj.management
182-
return ManagementSchema({"unix-socket": str(obj.rundir.to_path() / soc)})
183-
return obj.management
184-
185176
def _dnssec(self, obj: Raw) -> Any:
186177
if obj.dnssec is True:
187178
return DnssecSchema()
@@ -193,6 +184,14 @@ def _dns64(self, obj: Raw) -> Any:
193184
return obj.dns64
194185

195186
def _validate(self) -> None:
187+
# warn about '/management/unix-socket' not located in '/rundir'
188+
if self.management.unix_socket and self.management.unix_socket.to_path().parent != self.rundir.to_path():
189+
logger.warning(
190+
f"The management API unix-socket '{self.management.unix_socket}'"
191+
f" is not located in the resolver's rundir '{self.rundir}'."
192+
" This can lead to permissions issues."
193+
)
194+
196195
# enforce max-workers config
197196
workers_max = _workers_max_count()
198197
if int(self.workers) > workers_max:

tests/manager/datamodel/test_config_schema.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
from typing import Any, Dict, Type, cast
55

6-
from knot_resolver.constants import API_SOCK_FILE, API_SOCK_NAME, RUN_DIR
6+
from knot_resolver.constants import API_SOCK_FILE
77
from knot_resolver.datamodel import KresConfig
88
from knot_resolver.datamodel.lua_schema import LuaSchema
99
from knot_resolver.utils.modeling import BaseSchema
@@ -58,16 +58,6 @@ def test_config_defaults():
5858
assert config.dns64 == False
5959

6060

61-
def test_management_unix_socket():
62-
cwd = os.getcwd()
63-
config = KresConfig({"rundir": cwd})
64-
assert str(config.management.unix_socket) == f"{cwd}/{API_SOCK_NAME}"
65-
66-
my_soc = "my-new.soc"
67-
config = KresConfig({"management": {"unix-socket": my_soc}})
68-
assert str(config.management.unix_socket) == f"{RUN_DIR}/{my_soc}"
69-
70-
7161
def test_management_interface():
7262
cwd = os.getcwd()
7363
config = KresConfig({"rundir": cwd, "management": {"interface": "127.0.0.1@5000"}})

0 commit comments

Comments
 (0)