Skip to content

Commit b50f1a1

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
sambacc: add ability to specify <host>:<port> for kmip servers
Add helper properties that extract the port from "hostname" fields (it's mostly internal - I'm not renaming it now). Take some care not to break on ipv6 addresses w/o ports. Signed-off-by: John Mulligan <[email protected]>
1 parent 7827da0 commit b50f1a1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

sambacc/config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import json
2525
import sys
2626
import typing
27+
import urllib
2728

2829
from .opener import Opener, FileOpener
2930

@@ -614,6 +615,20 @@ def subname(self) -> str:
614615
def hostnames(self) -> list[str]:
615616
return list(self._cfg.get("hostnames", []))
616617

618+
@property
619+
def host_ports(self) -> list[tuple[str, int]]:
620+
default_port = self.port
621+
out: list[tuple[str, int]] = []
622+
for host in self.hostnames:
623+
host, port = _safe_split_host_port(host)
624+
if port <= 0 and default_port <= 0:
625+
raise ValueError("no host port and no default port")
626+
elif port <= 0:
627+
out.append((host, default_port))
628+
else:
629+
out.append((host, int(port)))
630+
return out
631+
617632
@property
618633
def port(self) -> int:
619634
return int(self._cfg.get("port", -1))
@@ -772,3 +787,13 @@ def _globals_data(gconfig: GlobalConfig, iconfig: dict) -> list:
772787
except KeyError:
773788
return []
774789
return [gconfig.data["globals"][n] for n in gnames]
790+
791+
792+
def _safe_split_host_port(host: str, scheme: str = "https") -> tuple[str, int]:
793+
if host.count(":") > 1 and "[" not in host:
794+
# assume this is an undecorated ipv6 and pass it thru w/o port
795+
return (host, -1)
796+
# otherwise let urllib do the work for us
797+
parsed = urllib.parse.urlparse(f"{scheme}://{host}")
798+
assert parsed.hostname, "invalid hostname" # should be impossible
799+
return parsed.hostname, int(parsed.port or -1)

0 commit comments

Comments
 (0)