| 1 |
# SPDX-FileCopyrightText: 2023-2024 Greenbone AG |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
# |
| 5 |
|
| 6 |
from argparse import ArgumentParser, Namespace |
| 7 |
from pathlib import Path |
| 8 |
from typing import Any, Optional, Sequence |
| 9 |
|
| 10 |
import shtab |
| 11 |
|
| 12 |
from greenbone.feed.sync.__version__ import __version__ |
| 13 |
from greenbone.feed.sync.config import ( |
| 14 |
DEFAULT_CONFIG_FILE, |
| 15 |
DEFAULT_USER_CONFIG_FILE, |
| 16 |
Config, |
| 17 |
ConfigDict, |
| 18 |
EnterpriseSettings, |
| 19 |
maybe_int, |
| 20 |
) |
| 21 |
from greenbone.feed.sync.errors import ConfigFileError |
| 22 |
|
| 23 |
|
| 24 |
def _to_defaults(values: ConfigDict) -> dict[str, Any]: |
| 25 |
defaults = {} |
| 26 |
|
| 27 |
for key, value in values.items(): |
| 28 |
defaults[key.replace("-", "_")] = value |
| 29 |
|
| 30 |
return defaults |
| 31 |
|
| 32 |
|
| 33 |
def feed_type(value: str) -> str: |
| 34 |
""" |
| 35 |
Converts to a specific feed type |
| 36 |
""" |
| 37 |
|
| 38 |
value = value.replace("_", "-").lower() |
| 39 |
|
| 40 |
if value in ("nvts", "report-formats", "port-lists", "scan-configs"): |
| 41 |
return value[:-1] |
| 42 |
|
| 43 |
return value |
| 44 |
|
| 45 |
|
| 46 |
class CliParser: |
| 47 |
""" |
| 48 |
An ArgumentParser for the feed sync CLI |
| 49 |
""" |
| 50 |
|
| 51 |
def __init__(self) -> None: |
| 52 |
parser = ArgumentParser(add_help=False) |
| 53 |
shtab.add_argument_to(parser) |
| 54 |
parser.add_argument( |
| 55 |
"--version", |
| 56 |
help="Print version then exit.", |
| 57 |
action="version", |
| 58 |
version=f"%(prog)s {__version__}", |
| 59 |
) |
| 60 |
parser.add_argument( |
| 61 |
"-h", |
| 62 |
"--help", |
| 63 |
help="Show this help message and exit.", |
| 64 |
action="store_true", |
| 65 |
) |
| 66 |
parser.add_argument( |
| 67 |
"--selftest", |
| 68 |
help="Perform self-test and set exit code", |
| 69 |
action="store_true", |
| 70 |
) |
| 71 |
output_group = parser.add_mutually_exclusive_group() |
| 72 |
output_group.add_argument( |
| 73 |
"--verbose", |
| 74 |
"-v", |
| 75 |
action="count", |
| 76 |
help="Set log verbosity. `-vvv` for maximum verbosity. " |
| 77 |
"(Default: %(default)s)", |
| 78 |
) |
| 79 |
output_group.add_argument( |
| 80 |
"--quiet", action="store_true", help="Disable all log output." |
| 81 |
) |
| 82 |
|
| 83 |
parser.add_argument( |
| 84 |
"-c", |
| 85 |
"--config", |
| 86 |
help="Configuration file path. If not set %(prog)s " |
| 87 |
f"tries to load {DEFAULT_USER_CONFIG_FILE} or " |
| 88 |
f"{DEFAULT_CONFIG_FILE}.", |
| 89 |
) |
| 90 |
parser.add_argument( |
| 91 |
"--private-directory", |
| 92 |
help="(Sub-)Directory to exclude from the sync which will never " |
| 93 |
"get deleted automatically.", |
| 94 |
type=Path, |
| 95 |
) |
| 96 |
parser.add_argument( |
| 97 |
"--compression-level", |
| 98 |
type=int, |
| 99 |
choices=range(0, 10), |
| 100 |
help="Rsync compression level (0-9). (Default: %(default)s)", |
| 101 |
) |
| 102 |
parser.add_argument( |
| 103 |
"--type", |
| 104 |
choices=[ |
| 105 |
"all", |
| 106 |
"nvt", |
| 107 |
"gvmd-data", |
| 108 |
"scap", |
| 109 |
"cert", |
| 110 |
"notus", |
| 111 |
"nasl", |
| 112 |
"report-format", |
| 113 |
"scan-config", |
| 114 |
"port-list", |
| 115 |
], |
| 116 |
default="all", |
| 117 |
type=feed_type, |
| 118 |
help="Select which feed should be synced. (Default: %(default)s)", |
| 119 |
) |
| 120 |
parser.add_argument( |
| 121 |
"--feed-version", |
| 122 |
help="Deprecated. Use --feed-release instead.", |
| 123 |
) |
| 124 |
parser.add_argument( |
| 125 |
"--feed-release", |
| 126 |
help="Release series of the feed to download for example 22.04 or 24.10. (Default: %(default)s)", |
| 127 |
) |
| 128 |
parser.add_argument( |
| 129 |
"--destination-prefix", |
| 130 |
type=Path, |
| 131 |
help="Prefix for the destination directories. (Default: %(default)s)", |
| 132 |
) |
| 133 |
parser.add_argument( |
| 134 |
"--gvmd-data-destination", |
| 135 |
type=Path, |
| 136 |
help="Destination of the downloaded gvmd data. " |
| 137 |
"(Default: %(default)s)", |
| 138 |
) |
| 139 |
parser.add_argument( |
| 140 |
"--gvmd-data-url", |
| 141 |
help="URL to download the gvmd data from. (Default: %(default)s)", |
| 142 |
) |
| 143 |
nvts_destination_group = parser.add_argument_group() |
| 144 |
nvts_destination_group.add_argument( |
| 145 |
"--notus-destination", |
| 146 |
type=Path, |
| 147 |
help="Destination of the downloaded notus data. " |
| 148 |
"(Default: %(default)s)", |
| 149 |
) |
| 150 |
nvts_url_group = parser.add_argument_group() |
| 151 |
nvts_url_group.add_argument( |
| 152 |
"--notus-url", |
| 153 |
help="URL to download the notus data from. (Default: %(default)s)", |
| 154 |
) |
| 155 |
nvts_destination_group.add_argument( |
| 156 |
"--nasl-destination", |
| 157 |
type=Path, |
| 158 |
help="Destination of the downloaded nasl data. " |
| 159 |
"(Default: %(default)s)", |
| 160 |
) |
| 161 |
nvts_url_group.add_argument( |
| 162 |
"--nasl-url", |
| 163 |
help="URL to download the nasl data from. (Default: %(default)s)", |
| 164 |
) |
| 165 |
|
| 166 |
secinfo_destination_group = parser.add_argument_group() |
| 167 |
secinfo_destination_group.add_argument( |
| 168 |
"--scap-data-destination", |
| 169 |
type=Path, |
| 170 |
help="Destination of the downloaded SCAP data. " |
| 171 |
"(Default: %(default)s)", |
| 172 |
) |
| 173 |
secinfo_url_group = parser.add_argument_group() |
| 174 |
secinfo_url_group.add_argument( |
| 175 |
"--scap-data-url", |
| 176 |
help="URL to download the SCAP data from. (Default: %(default)s)", |
| 177 |
) |
| 178 |
secinfo_destination_group.add_argument( |
| 179 |
"--cert-data-destination", |
| 180 |
type=Path, |
| 181 |
help="Destination of the downloaded CERT data. " |
| 182 |
"(Default: %(default)s)", |
| 183 |
) |
| 184 |
secinfo_url_group.add_argument( |
| 185 |
"--cert-data-url", |
| 186 |
help="URL to download the CERT data from. (Default: %(default)s)", |
| 187 |
) |
| 188 |
|
| 189 |
data_objects_destination_group = parser.add_argument_group() |
| 190 |
data_objects_destination_group.add_argument( |
| 191 |
"--report-formats-destination", |
| 192 |
type=Path, |
| 193 |
help="Destination of the downloaded report format data " |
| 194 |
"(Default: %(default)s)", |
| 195 |
) |
| 196 |
data_objects_url_group = parser.add_argument_group() |
| 197 |
data_objects_url_group.add_argument( |
| 198 |
"--report-formats-url", |
| 199 |
help="URL to download the report format data from. " |
| 200 |
"(Default: %(default)s)", |
| 201 |
) |
| 202 |
data_objects_destination_group.add_argument( |
| 203 |
"--scan-configs-destination", |
| 204 |
type=Path, |
| 205 |
help="Destination of the downloaded scan config data. " |
| 206 |
"(Default: %(default)s)", |
| 207 |
) |
| 208 |
data_objects_url_group.add_argument( |
| 209 |
"--scan-configs-url", |
| 210 |
help="URL to download the scan config data from. " |
| 211 |
"(Default: %(default)s)", |
| 212 |
) |
| 213 |
data_objects_destination_group.add_argument( |
| 214 |
"--port-lists-destination", |
| 215 |
type=Path, |
| 216 |
help="Destination of the downloaded port list data. " |
| 217 |
"(Default: %(default)s)", |
| 218 |
) |
| 219 |
data_objects_url_group.add_argument( |
| 220 |
"--port-lists-url", |
| 221 |
help="URL to download the port list data from. " |
| 222 |
"(Default: %(default)s)", |
| 223 |
) |
| 224 |
|
| 225 |
lock_file_group = parser.add_argument_group() |
| 226 |
lock_file_group.add_argument( |
| 227 |
"--gvmd-lock-file", |
| 228 |
type=Path, |
| 229 |
help="File to use for locking the feed synchronization for data " |
| 230 |
"loaded by the gvmd daemon. Used to avoid that more then one " |
| 231 |
"process accesses the feed data at the same time. " |
| 232 |
"(Default: %(default)s)", |
| 233 |
) |
| 234 |
lock_file_group.add_argument( |
| 235 |
"--openvas-lock-file", |
| 236 |
type=Path, |
| 237 |
help="File to use for locking the feed synchronization for data " |
| 238 |
"loaded by the openvas scanner. Used to avoid that more then one " |
| 239 |
"process accesses the feed data at the same time. " |
| 240 |
"(Default: %(default)s)", |
| 241 |
) |
| 242 |
parser.add_argument( |
| 243 |
"--fail-fast", |
| 244 |
"--failfast", |
| 245 |
action="store_true", |
| 246 |
help="Stop after a first error has occurred. Otherwise the script " |
| 247 |
"tries to download additional data if specified.", |
| 248 |
) |
| 249 |
|
| 250 |
wait_group = parser.add_mutually_exclusive_group() |
| 251 |
wait_group.add_argument( |
| 252 |
"--no-wait", |
| 253 |
action="store_true", |
| 254 |
help="Fail directly if the lock file can't be acquired.", |
| 255 |
) |
| 256 |
wait_group.add_argument( |
| 257 |
"--wait-interval", |
| 258 |
type=int, |
| 259 |
help="Time to wait in seconds after failed lock attempt before" |
| 260 |
"re-trying to lock the file. (Default: %(default)s seconds)", |
| 261 |
) |
| 262 |
|
| 263 |
parser.add_argument( |
| 264 |
"--rsync-timeout", |
| 265 |
type=int, |
| 266 |
help="Maximum I/O timeout in seconds used for rsync. If no data is " |
| 267 |
"transferred for the specified time then rsync will exit. By " |
| 268 |
"default no timeout is set and the rsync default will be used.", |
| 269 |
) |
| 270 |
|
| 271 |
permissions_group = parser.add_argument_group() |
| 272 |
permissions_group.add_argument( |
| 273 |
"--user", |
| 274 |
type=maybe_int, |
| 275 |
help="If started as root, use this user name or ID to run the " |
| 276 |
"script. (Default: %(default)s)", |
| 277 |
) |
| 278 |
permissions_group.add_argument( |
| 279 |
"--group", |
| 280 |
type=maybe_int, |
| 281 |
help="If started as root, use this group name or ID to run the " |
| 282 |
"script. (Default: %(default)s)", |
| 283 |
) |
| 284 |
|
| 285 |
parser.add_argument( |
| 286 |
"--greenbone-enterprise-feed-key", |
| 287 |
type=Path, |
| 288 |
help="File to read the Greenbone Enterprise Feed key from. " |
| 289 |
"The key gives access to additional vulnerability tests for " |
| 290 |
"enterprise software among other advantages. See " |
| 291 |
"https://www.greenbone.net/en/feed-comparison/ for more details." |
| 292 |
"The default URLs are adjusted according to the data in the key." |
| 293 |
"If the key file does not exist it is ignored. " |
| 294 |
"(Default: %(default)s)", |
| 295 |
) |
| 296 |
|
| 297 |
self.parser = parser |
| 298 |
|
| 299 |
def _determine_enterprise_settings( |
| 300 |
self, enterprise_key: Path |
| 301 |
) -> Optional[EnterpriseSettings]: |
| 302 |
if not enterprise_key or not enterprise_key.exists(): |
| 303 |
return None |
| 304 |
|
| 305 |
return EnterpriseSettings.from_key(enterprise_key) |
| 306 |
|
| 307 |
def _load_config(self, config_file: str) -> Config: |
| 308 |
config_path = None |
| 309 |
|
| 310 |
if config_file is None: |
| 311 |
for file in [DEFAULT_USER_CONFIG_FILE, DEFAULT_CONFIG_FILE]: |
| 312 |
path = Path(file).expanduser().resolve() |
| 313 |
if path.exists(): |
| 314 |
config_path = path |
| 315 |
break |
| 316 |
|
| 317 |
else: |
| 318 |
config_path = Path(config_file).expanduser().resolve() |
| 319 |
if not config_path.exists(): |
| 320 |
raise ConfigFileError( |
| 321 |
f"Config file {config_file} does not exist." |
| 322 |
) |
| 323 |
|
| 324 |
config = Config() |
| 325 |
|
| 326 |
if config_path: |
| 327 |
config.load_from_config_file(config_path) |
| 328 |
|
| 329 |
return config |
| 330 |
|
| 331 |
def _set_defaults(self, config: ConfigDict) -> None: |
| 332 |
self.parser.set_defaults(**_to_defaults(config)) |
| 333 |
|
| 334 |
def parse_arguments( |
| 335 |
self, args: Optional[Sequence[str]] = None |
| 336 |
) -> Namespace: |
| 337 |
""" |
| 338 |
Parse CLI arguments |
| 339 |
""" |
| 340 |
# Parse args to get the config file path passed as option |
| 341 |
known_args, _ = self.parser.parse_known_args(args) |
| 342 |
|
| 343 |
# Load the defaults from the config file if it exists. |
| 344 |
config = self._load_config(known_args.config) |
| 345 |
|
| 346 |
# set greenbone enterprise feed key in config if user passed one to load |
| 347 |
# desired key for determining the feed url |
| 348 |
if known_args.greenbone_enterprise_feed_key: |
| 349 |
config["greenbone-enterprise-feed-key"] = ( |
| 350 |
known_args.greenbone_enterprise_feed_key |
| 351 |
) |
| 352 |
|
| 353 |
if known_args.feed_version: |
| 354 |
config["feed-release"] = known_args.feed_version |
| 355 |
|
| 356 |
if known_args.feed_release: |
| 357 |
config["feed-release"] = known_args.feed_release |
| 358 |
|
| 359 |
if known_args.destination_prefix: |
| 360 |
config["destination-prefix"] = known_args.destination_prefix |
| 361 |
|
| 362 |
if self.parser.prog == "greenbone-nvt-sync": |
| 363 |
config["type"] = "nvt" |
| 364 |
elif self.parser.prog == "greenbone-scapdata-sync": |
| 365 |
config["type"] = "scap" |
| 366 |
elif self.parser.prog == "greenbone-certdata-sync": |
| 367 |
config["type"] = "cert" |
| 368 |
|
| 369 |
# apply defaults in config |
| 370 |
config.apply_settings() |
| 371 |
|
| 372 |
# check if a enterprise feed key is available |
| 373 |
enterprise_settings = self._determine_enterprise_settings( |
| 374 |
config["greenbone-enterprise-feed-key"] |
| 375 |
) |
| 376 |
|
| 377 |
# override feed url from key |
| 378 |
if enterprise_settings: |
| 379 |
config["feed-url"] = enterprise_settings.feed_url() |
| 380 |
|
| 381 |
# apply other config defaults |
| 382 |
config.apply_dependent_settings() |
| 383 |
|
| 384 |
# set config as defaults for CLI to make them visible in --help |
| 385 |
self._set_defaults(config) |
| 386 |
|
| 387 |
if known_args.help: |
| 388 |
self.parser.print_help() |
| 389 |
self.parser.exit(0) |
| 390 |
|
| 391 |
return self.parser.parse_args(args) |