Skip to content

Commit

Permalink
Merge branch 'topic/dopheide/ipv6-controller-option' of github.com:/d…
Browse files Browse the repository at this point in the history
…opheide-esnet/zeek-client

* 'topic/dopheide/ipv6-controller-option' of github.com:/dopheide-esnet/zeek-client:
  Fixup pre-commit
  chore: linting/pre-commit
  Supporting IPv6 addresses for the --controller option
  • Loading branch information
ckreibich committed Dec 13, 2024
2 parents 7b782d4 + 83c2438 commit 0aa6f27
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
36 changes: 36 additions & 0 deletions tests/test_config_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ def test_update_from_args_controller_host(self):
self.assertEqual(self.config.get("controller", "host"), "foo")
self.assertEqual(self.config.getint("controller", "port"), 2149)

parser = zeekclient.cli.create_parser()
args = parser.parse_args(["--controller", "127.0.0.1"])
self.config.update_from_args(args)
self.assertEqual(self.config.get("controller", "host"), "127.0.0.1")
self.assertEqual(self.config.getint("controller", "port"), 2149)

parser = zeekclient.cli.create_parser()
args = parser.parse_args(["--controller", "127.0.0.1:"])
self.config.update_from_args(args)
self.assertEqual(self.config.get("controller", "host"), "127.0.0.1")
self.assertEqual(self.config.getint("controller", "port"), 2149)

parser = zeekclient.cli.create_parser()
args = parser.parse_args(["--controller", "[fe80::1]"])
self.config.update_from_args(args)
self.assertEqual(self.config.get("controller", "host"), "[fe80::1]")
self.assertEqual(self.config.getint("controller", "port"), 2149)

parser = zeekclient.cli.create_parser()
args = parser.parse_args(["--controller", "[fe80::1]:"])
self.config.update_from_args(args)
self.assertEqual(self.config.get("controller", "host"), "[fe80::1]")
self.assertEqual(self.config.getint("controller", "port"), 2149)

def test_update_from_args_controller_port(self):
parser = zeekclient.cli.create_parser()
args = parser.parse_args(["--controller", ":2222"])
Expand All @@ -76,3 +100,15 @@ def test_update_from_args_controller_hostport(self):
self.config.update_from_args(args)
self.assertEqual(self.config.get("controller", "host"), "foo")
self.assertEqual(self.config.getint("controller", "port"), 2222)

parser = zeekclient.cli.create_parser()
args = parser.parse_args(["--controller", "127.0.0.1:2222"])
self.config.update_from_args(args)
self.assertEqual(self.config.get("controller", "host"), "127.0.0.1")
self.assertEqual(self.config.getint("controller", "port"), 2222)

parser = zeekclient.cli.create_parser()
args = parser.parse_args(["--controller", "[fe80::1]:2222"])
self.config.update_from_args(args)
self.assertEqual(self.config.get("controller", "host"), "[fe80::1]")
self.assertEqual(self.config.getint("controller", "port"), 2222)
28 changes: 21 additions & 7 deletions zeekclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,30 @@ def update_from_args(self, args):
# The `--controller` argument is a shortcut for two `--set` arguments that
# set controller host and port, so update these manually:
if args.controller:
host_port = args.controller.split(":", 1)
if len(host_port) != 2 or not host_port[1]:
if ":" not in args.controller:
host = args.controller
port = ""
else:
(host, _, port) = args.controller.rpartition(":")
if host.count(":") >= 1:
# We likely have an IPv6 address
if not host.startswith("["):
raise ValueError(
"IPv6 addresses must be surrounded by brackets: [<ipv6>]:<port>"
)
if port.endswith("]"):
host = host + ":" + port
port = ""

if port == "":
# It's just a hostname
self.set("controller", "host", host_port[0])
elif not host_port[0]:
self.set("controller", "host", host)
elif host == "":
# It's just a port (as ":<port>")
self.set("controller", "port", host_port[1])
self.set("controller", "port", port)
else:
self.set("controller", "host", host_port[0])
self.set("controller", "port", host_port[1])
self.set("controller", "host", host)
self.set("controller", "port", port)

# --verbose/-v/-vvv etc set a numeric verbosity level:
if args.verbose:
Expand Down

0 comments on commit 0aa6f27

Please sign in to comment.