| 1 |
# SPDX-FileCopyrightText: 2022-2024 Greenbone AG |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
# |
| 5 |
|
| 6 |
import asyncio |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
from dataclasses import dataclass |
| 10 |
from typing import Iterable, NoReturn |
| 11 |
|
| 12 |
from rich.console import Console |
| 13 |
|
| 14 |
from greenbone.feed.sync.config import DEFAULT_VERBOSITY |
| 15 |
from greenbone.feed.sync.errors import GreenboneFeedSyncError, RsyncError |
| 16 |
from greenbone.feed.sync.helper import ( |
| 17 |
Spinner, |
| 18 |
change_user_and_group, |
| 19 |
flock_wait, |
| 20 |
is_root, |
| 21 |
) |
| 22 |
from greenbone.feed.sync.parser import CliParser |
| 23 |
from greenbone.feed.sync.rsync import Rsync |
| 24 |
|
| 25 |
__all__ = ("main",) |
| 26 |
|
| 27 |
|
| 28 |
@dataclass |
| 29 |
class Sync: |
| 30 |
""" |
| 31 |
Class to store sync information |
| 32 |
""" |
| 33 |
|
| 34 |
name: str |
| 35 |
types: Iterable[str] |
| 36 |
url: str |
| 37 |
destination: str |
| 38 |
|
| 39 |
|
| 40 |
@dataclass |
| 41 |
class SyncList: |
| 42 |
""" |
| 43 |
Class to store a list of sync information |
| 44 |
""" |
| 45 |
|
| 46 |
lock_file: str |
| 47 |
syncs: Iterable[Sync] |
| 48 |
|
| 49 |
|
| 50 |
def filter_syncs(lock_file: str, feed_type: str, *syncs: Sync) -> SyncList: |
| 51 |
""" |
| 52 |
Create a list of syncs which only match to the feed type |
| 53 |
""" |
| 54 |
return SyncList( |
| 55 |
lock_file=lock_file, |
| 56 |
syncs=[sync for sync in syncs if feed_type in sync.types], |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
def do_selftest() -> None: |
| 61 |
""" |
| 62 |
Check for rsync command. |
| 63 |
""" |
| 64 |
try: |
| 65 |
subprocess.run( |
| 66 |
["rsync", "--help"], |
| 67 |
stdout=subprocess.DEVNULL, |
| 68 |
stderr=subprocess.DEVNULL, |
| 69 |
check=True, |
| 70 |
) |
| 71 |
except (PermissionError, FileNotFoundError, subprocess.CalledProcessError): |
| 72 |
raise GreenboneFeedSyncError( |
| 73 |
"The rsync binary could not be found." |
| 74 |
) from None |
| 75 |
|
| 76 |
|
| 77 |
async def feed_sync(console: Console, error_console: Console) -> int: |
| 78 |
""" |
| 79 |
Sync the feeds |
| 80 |
""" |
| 81 |
parser = CliParser() |
| 82 |
args = parser.parse_arguments() |
| 83 |
|
| 84 |
do_selftest() |
| 85 |
|
| 86 |
if args.selftest: |
| 87 |
return 0 |
| 88 |
|
| 89 |
if args.quiet: |
| 90 |
verbose = 0 |
| 91 |
else: |
| 92 |
verbose = DEFAULT_VERBOSITY if args.verbose is None else args.verbose |
| 93 |
|
| 94 |
if is_root(): |
| 95 |
if verbose >= 1: |
| 96 |
console.print( |
| 97 |
f"Running as root. Switching to user '{args.user}' and " |
| 98 |
f"group '{args.group}'." |
| 99 |
) |
| 100 |
change_user_and_group(args.user, args.group) |
| 101 |
|
| 102 |
rsync = Rsync( |
| 103 |
private_subdir=args.private_directory, |
| 104 |
verbose=verbose >= 3, |
| 105 |
compression_level=args.compression_level, |
| 106 |
ssh_key=args.greenbone_enterprise_feed_key, |
| 107 |
) |
| 108 |
|
| 109 |
openvas_syncs = filter_syncs( |
| 110 |
args.openvas_lock_file, |
| 111 |
args.type, |
| 112 |
Sync( |
| 113 |
name="Notus files", |
| 114 |
types=("notus", "nvt", "all"), |
| 115 |
url=args.notus_url, |
| 116 |
destination=args.notus_destination, |
| 117 |
), |
| 118 |
Sync( |
| 119 |
name="NASL files", |
| 120 |
types=("nasl", "nvt", "all"), |
| 121 |
url=args.nasl_url, |
| 122 |
destination=args.nasl_destination, |
| 123 |
), |
| 124 |
) |
| 125 |
gvmd_syncs = filter_syncs( |
| 126 |
args.gvmd_lock_file, |
| 127 |
args.type, |
| 128 |
Sync( |
| 129 |
name="SCAP data", |
| 130 |
types=("scap", "all"), |
| 131 |
url=args.scap_data_url, |
| 132 |
destination=args.scap_data_destination, |
| 133 |
), |
| 134 |
Sync( |
| 135 |
name="CERT-Bund data", |
| 136 |
types=("cert", "all"), |
| 137 |
url=args.cert_data_url, |
| 138 |
destination=args.cert_data_destination, |
| 139 |
), |
| 140 |
Sync( |
| 141 |
name="gvmd data", |
| 142 |
types=("gvmd-data", "all"), |
| 143 |
url=args.gvmd_data_url, |
| 144 |
destination=args.gvmd_data_destination, |
| 145 |
), |
| 146 |
Sync( |
| 147 |
name="report formats", |
| 148 |
types=("report-format"), |
| 149 |
url=args.report_formats_url, |
| 150 |
destination=args.report_formats_destination, |
| 151 |
), |
| 152 |
Sync( |
| 153 |
name="scan configs", |
| 154 |
types=("scan-config"), |
| 155 |
url=args.scan_configs_url, |
| 156 |
destination=args.scan_configs_destination, |
| 157 |
), |
| 158 |
Sync( |
| 159 |
name="port lists", |
| 160 |
types=("port-list"), |
| 161 |
url=args.port_lists_url, |
| 162 |
destination=args.port_lists_destination, |
| 163 |
), |
| 164 |
) |
| 165 |
|
| 166 |
has_error = False |
| 167 |
wait_interval = None if args.no_wait else args.wait_interval |
| 168 |
|
| 169 |
for sync_list in (openvas_syncs, gvmd_syncs): |
| 170 |
if not sync_list.syncs: |
| 171 |
continue |
| 172 |
|
| 173 |
async with flock_wait( |
| 174 |
sync_list.lock_file, |
| 175 |
console=console if verbose else None, |
| 176 |
wait_interval=wait_interval, |
| 177 |
): |
| 178 |
for sync in sync_list.syncs: |
| 179 |
try: |
| 180 |
rsync_coro = rsync.sync( |
| 181 |
url=sync.url, destination=sync.destination |
| 182 |
) |
| 183 |
if verbose >= 3: |
| 184 |
console.print( |
| 185 |
f"Downloading {sync.name} from {sync.url} to " |
| 186 |
f"{sync.destination}" |
| 187 |
) |
| 188 |
await rsync_coro |
| 189 |
# add newline after rsync |
| 190 |
console.print() |
| 191 |
elif verbose >= 1: |
| 192 |
with Spinner( |
| 193 |
console, |
| 194 |
f"Downloading {sync.name} from {sync.url} to " |
| 195 |
f"{sync.destination}", |
| 196 |
): |
| 197 |
await rsync_coro |
| 198 |
else: |
| 199 |
await rsync_coro |
| 200 |
except RsyncError as e: |
| 201 |
has_error = True |
| 202 |
error_console.print(e.stderr) |
| 203 |
if args.fail_fast: |
| 204 |
return 1 |
| 205 |
|
| 206 |
if verbose >= 2: |
| 207 |
# add newline for grouping lock |
| 208 |
console.print() |
| 209 |
|
| 210 |
return 1 if has_error else 0 |
| 211 |
|
| 212 |
|
| 213 |
def main() -> NoReturn: |
| 214 |
""" |
| 215 |
Main CLI function |
| 216 |
""" |
| 217 |
console = Console() |
| 218 |
error_console = Console(stderr=True) |
| 219 |
|
| 220 |
try: |
| 221 |
sys.exit(asyncio.run(feed_sync(console, error_console))) |
| 222 |
except GreenboneFeedSyncError as e: |
| 223 |
error_console.print(f"[red]❌[/red]Error: {e}") |
| 224 |
sys.exit(1) |
| 225 |
except KeyboardInterrupt: |
| 226 |
sys.exit(1) |
| 227 |
|
| 228 |
|
| 229 |
if __name__ == "__main__": |
| 230 |
main() |