From 564bd8bab9e86068d00448f52835605b054598bf Mon Sep 17 00:00:00 2001 From: Amy Schools Date: Mon, 1 Jul 2024 11:46:44 +0200 Subject: [PATCH] WIP --- .../standalone_plugins/test_deprecate_vts.py | 10 +++-- troubadix/standalone_plugins/deprecate_vts.py | 40 +++++++++++-------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/tests/standalone_plugins/test_deprecate_vts.py b/tests/standalone_plugins/test_deprecate_vts.py index af89373b..7b6dd2d1 100644 --- a/tests/standalone_plugins/test_deprecate_vts.py +++ b/tests/standalone_plugins/test_deprecate_vts.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later # SPDX-FileCopyrightText: 2024 Greenbone AG # pylint: disable=line-too-long +# pylint: disable=protected-access import unittest from pathlib import Path from tests.plugins import TemporaryDirectory @@ -9,8 +10,8 @@ deprecate, parse_args, DeprecatedFile, - get_summary, - finalize_content, + _get_summary, + _finalize_content, update_summary, get_files, ) @@ -123,7 +124,7 @@ def test_deprecate_kb_item(self): ) def test_get_summary(self): - result = get_summary(NASL_CONTENT) + result = _get_summary(NASL_CONTENT) expected = ( "The remote host is missing an update for the 'gd'\n package(s) " "announced " @@ -132,7 +133,8 @@ def test_get_summary(self): self.assertEqual(result, expected) def test_finalize_content(self): - result = finalize_content(NASL_CONTENT) + + result = _finalize_content(NASL_CONTENT) expected = ( '...if(description)\n{\n script_oid("1.3.6.1.4.1.25623.1.0.910673");\n ' 'script_version("2024-03-12T14:15:13+0000");\n script_name("RedHat: Security Advisory for gd (RHSA-2020:5443-01)");\n script_family("Red Hat Local Security Checks");\n script_dependencies("gather-package-list.nasl");\n script_mandatory_keys("ssh/login/rhel", "ssh/login/rpms", re:"ssh/login/release=RHENT_7");\n\n script_xref(name:"RHSA", value:"2020:5443-01");\n script_xref(name:"URL", value:"https://www.redhat.com/archives/rhsa-announce/2020-December/msg00044.html");\n\n script_tag(name:"summary", value:"The remote host is missing an update for the \'gd\'\n package(s) announced via the RHSA-2020:5443-01 advisory.");\n\n script_tag(name:"deprecated", value:TRUE);\n\nexit(0);\n}\n\nexit(66);\n' diff --git a/troubadix/standalone_plugins/deprecate_vts.py b/troubadix/standalone_plugins/deprecate_vts.py index 61ce356c..4ef20d42 100644 --- a/troubadix/standalone_plugins/deprecate_vts.py +++ b/troubadix/standalone_plugins/deprecate_vts.py @@ -8,7 +8,7 @@ from pathlib import Path from typing import Iterable, Optional -from troubadix.argparser import file_type +from troubadix.argparser import file_type, directory_type from troubadix.helper.patterns import ( get_special_script_tag_pattern, get_script_tag_pattern, @@ -54,7 +54,7 @@ def update_summary(file: DeprecatedFile, deprecation_reason: str) -> str: if deprecation_reason == "duplicate": deprecate_text = "as a duplicate." - old_summary = get_summary(file.content) + old_summary = _get_summary(file.content) if old_summary: new_summary = old_summary + "\n" + deprecate_text file.content = file.content.replace(old_summary, new_summary) @@ -169,7 +169,7 @@ def deprecate( ) continue file.content = update_summary(file, deprecation_reason) - file.content = finalize_content(file.content) + file.content = _finalize_content(file.content) # Drop any unnecessary script tags like script_dependencies(), # script_require_udp_ports() or script_mandatory_keys() @@ -211,19 +211,10 @@ def parse_args(args: Iterable[str] = None) -> Namespace: "-o", "--output-path", metavar="", - type=str, + type=directory_type, required=True, help="Path where the deprecated files should be written to.", ) - parser.add_argument( - "-f", - "--files", - metavar="", - nargs="*", - default=None, - type=file_type, - help="Files to deprecate", - ) parser.add_argument( "-i", "--input-path", @@ -244,7 +235,7 @@ def parse_args(args: Iterable[str] = None) -> Namespace: "for example 'gb_rhsa_2021' to filter on the year", ) parser.add_argument( - "-d", + "-r", "--deprecation-reason", metavar="", choices=["notus", "merged", "duplicate", "defunct"], @@ -255,6 +246,24 @@ def parse_args(args: Iterable[str] = None) -> Namespace: "a still active duplicate, 'defunct': The VT is no longer " "functional.", ) + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument( + "-f", + "--files", + metavar="", + nargs="*", + default=None, + type=file_type, + help="Files to deprecate", + ) + group.add_argument( + "-i", + "--input-path", + metavar="", + default=None, + type=directory_type, + help="Path to the existing nasl script directory", + ) return parser.parse_args(args) @@ -263,8 +272,7 @@ def main(): output_path = Path(args.output_path) input_path = Path(args.input_path) if args.input_path else None single_file = Path(args.file) if args.file else None - deprecation_reason = args.deprecation_reason - filename_prefix = args.filename_prefix if args.filename_prefix else None + filename_prefix = args.filename_prefix if not input_path and not single_file: raise PathException(