From 8f3fc02248e60eaf87e4ea9d6469d0d00492d4e8 Mon Sep 17 00:00:00 2001 From: Lars Kiesow Date: Sun, 21 Jul 2024 20:50:42 +0200 Subject: [PATCH] Add set command combining add and update The new `set` command added with this patch, combines the `add` and `update` commands. It adds the host if it is not yet in DETERRERS or updates it otherwise. Note that updates will not yet modify the registration status of the given host. --- deterrerscli/__main__.py | 44 ++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/deterrerscli/__main__.py b/deterrerscli/__main__.py index 04c6c1b..dacf6f5 100644 --- a/deterrerscli/__main__.py +++ b/deterrerscli/__main__.py @@ -17,6 +17,22 @@ def print_format(data, format: str): print(json.dumps(data, indent=4)) +def _add(ipv4, admin, profile, firewall, register, skip_scan): + '''Add IP address to DETERRERS. + ''' + deterrers.add(ipv4, admin, profile, firewall) + if profile and (auto_register if register is None else register): + skip_scan = auto_skip_scan if skip_scan is None else skip_scan + deterrers.action(ipv4, 'register', skip_scan) + + +def _update(ipv4, admin, profile, firewall): + '''Update IP address in DETERRERS. + ''' + admin = admin or None + deterrers.update(ipv4, profile, firewall, admin) + + @click.group() def cli(): global deterrers @@ -71,10 +87,7 @@ def delete(ipv4): def add(ipv4, admin, profile, firewall, register, skip_scan): '''Add IP address to DETERRERS. ''' - deterrers.add(ipv4, admin, profile, firewall) - if profile and (auto_register if register is None else register): - skip_scan = auto_skip_scan if skip_scan is None else skip_scan - deterrers.action(ipv4, 'register', skip_scan) + return _add(ipv4, admin, profile, firewall, register, skip_scan) @cli.command() @@ -88,8 +101,27 @@ def update(ipv4, admin, profile, firewall): Fields which are not specified will not be changed. The option `admin` can be used multiple times. ''' - admin = admin or None - deterrers.update(ipv4, profile, firewall, admin) + return _update(ipv4, admin, profile, firewall) + + +@cli.command() +@click.option('--admin', '-a', multiple=True, required=True) +@click.option('--profile', '-p', default='', type=types.PROFILE_TYPE) +@click.option('--firewall', '-f', default='', type=types.HOST_FIREWALL_TYPE) +@click.option('--register/--no-register', default=None, + help='If the added host should be registered immediately') +@click.option('--skip-scan/--no-skip-scan', default=None, + help='If the added host should get an initial security scan. ' + 'Only applies if it is being registered') +@click.argument('ipv4', type=types.IPV4_TYPE) +def set(ipv4, admin, profile, firewall, register, skip_scan): + '''Add IP address to DETERRERS if it is not added already. Otherwise, + update the data. Note that hosts will not be automatically registered when + updating data. + ''' + if deterrers.get(ipv4): + return _update(ipv4, admin, profile, firewall) + return _add(ipv4, admin, profile, firewall, register, skip_scan) @cli.group()