Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor VDT integration tests: feeds and scan types #2650

Merged
merged 100 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
0a2c952
add: Add a new structure to the test_alert_vulnerability_removal #2520
fedepacher Feb 9, 2022
ce2fe66
refac: Refactor fixture name to truncate_monitored_files instead of t…
fedepacher Feb 10, 2022
01aea85
refac: Make improvement to the test_alert_vulnerability_removal.py fi…
fedepacher Feb 11, 2022
0dc0dad
refac: Move vuln feeds to a generic path #2528
jmv74211 Feb 14, 2022
1bb33bb
add: Add new fixture for mocking agents #2528
jmv74211 Feb 14, 2022
925ff19
add: Add new system to the mocking data #2528
jmv74211 Feb 14, 2022
37ee028
refac: Update custom NVD feed #2528
jmv74211 Feb 14, 2022
bac04cf
refac: Disable deprecated tests to avoid feed errors #2528
jmv74211 Feb 14, 2022
50cd25f
refac: Make multiple improvement to test_alert_vulnerability_removal.…
fedepacher Feb 14, 2022
1b41300
Merge pull request #2565 from wazuh/2520-refactor-test-alert-vulnerab…
jmv74211 Feb 14, 2022
a5d2ff3
add: Update vuln feeds path #2528
jmv74211 Feb 15, 2022
d555a8c
rm: Remove unused compressed feeds #2528
jmv74211 Feb 15, 2022
fdbdc23
refac: Update default parameters values for some functions #2528
jmv74211 Feb 15, 2022
4c1d9d3
add: Add new monitor event function #2528
jmv74211 Feb 15, 2022
7bbb0c6
add: Add new custom CPE helper #2528
jmv74211 Feb 15, 2022
7a6be73
refac: Update custom NVD feed and vulnerable packages data #2528
jmv74211 Feb 15, 2022
1b42568
fix: Fix feed path #2528
jmv74211 Feb 15, 2022
4616de8
Merge branch '2519-refactor-scan-results' of github.com:wazuh/wazuh-q…
jmv74211 Feb 15, 2022
e5ddb26
add: Add test scan NVD #2528
jmv74211 Feb 15, 2022
52f0892
add: Add new test to feeds #2525
fedepacher Feb 16, 2022
302b6b0
refac: Improve vuln alert detection regex expression #2528
jmv74211 Feb 16, 2022
0a5587b
refac: Rename timeout variables #2528
jmv74211 Feb 16, 2022
e6dbcf0
add: Add a CPE helper backup process in test scan NVD #2528
jmv74211 Feb 16, 2022
6da47b9
style: Fix code style according to PEP-8 #2528
jmv74211 Feb 16, 2022
fffef32
Merge pull request #2577 from wazuh/2528-refactor-scan-nvd
jmv74211 Feb 16, 2022
fab5097
refac: Make changes to improve test #2525
fedepacher Feb 16, 2022
73dd596
refac: Make changes to improve the test #2525
fedepacher Feb 16, 2022
d0b9af2
Merge pull request #2575 from wazuh/2525-refactor-test-redhat-duplica…
jmv74211 Feb 16, 2022
7de9028
add: Add new Windows mocking systems #2531
jmv74211 Feb 17, 2022
323ccaa
add: Add new custom msu feed #2531
jmv74211 Feb 17, 2022
402308d
add: Add vendor info to the custom vulnerable packages data #2531
jmv74211 Feb 17, 2022
783d4f0
add: Add CPE indexing test module #2531
jmv74211 Feb 17, 2022
1ed46fd
refac: Update test configuration variables #2531
jmv74211 Feb 17, 2022
7ef05ae
add: Add new run on start test for vuln detector #2531
jmv74211 Feb 18, 2022
68c3e25
Merge pull request #2583 from wazuh/2531-refactor-test-cpe-indexing
jmv74211 Feb 18, 2022
465758c
add: Add new parameters when mocking agents or sys_osinfo #2529
jmv74211 Feb 21, 2022
3293b64
add: Add new generic method to update feed path configurations #2529
jmv74211 Feb 23, 2022
b05c7d3
add: Add new custom feeds #2529
jmv74211 Feb 23, 2022
b71a149
add: Add new mocking systems info #2529
jmv74211 Feb 23, 2022
488d263
add: Add new vuln detector event monitor functions #2529
jmv74211 Feb 23, 2022
ccc55f4
add: Add new module for testing providers and nvd scans #2529
jmv74211 Feb 23, 2022
e0297bb
fix: Fix an error when creating the os_info for a mocked agent #2529
jmv74211 Feb 23, 2022
c8b9b7f
fix: Fix a possible error when mocking agent packages #2594
jmv74211 Feb 24, 2022
f1301ba
add: Add new function to get the vulnerability inventory #2593 #2594 …
jmv74211 Feb 25, 2022
5ceac25
add: Add new global variables #2593 #2594 #2595
jmv74211 Feb 25, 2022
1deeea7
add: Add new conftest to setup the alert monitor #2593 #2594 #2595
jmv74211 Feb 25, 2022
89581d6
refac: Apply some changes to vuln event monitor #2593 #2594 #2595
jmv74211 Feb 25, 2022
f1457fa
add: Add new test module for partial scan type #2595
jmv74211 Feb 25, 2022
eb86384
refac: Update method to update the nvd metadata #2593 #2594 #2595
jmv74211 Feb 25, 2022
1fa01c4
add: Add new test module for full scan type #2594
jmv74211 Feb 25, 2022
c2f8ae7
add: Add new test module for baseline scans #2593
jmv74211 Feb 25, 2022
3e36969
add: Add new method to insert a vulnerability in agent inventory #2594
jmv74211 Mar 1, 2022
c093bc4
add: Add new info and methods about vulnerable packages #2593 #2594 #…
jmv74211 Mar 1, 2022
045b1f4
add: Add new monitor function to check the end of a vuln scan #2593 #…
jmv74211 Mar 1, 2022
04b8d23
add: Add new generic fixtures to setup the vuln scans #2593 #2594 #2595
jmv74211 Mar 1, 2022
e4d72ab
refac: Improve scan types vuln tests #2593 #2594 #2595
jmv74211 Mar 1, 2022
57626cb
add: Add vulnerability inventory baseline scan test module #2593
jmv74211 Mar 1, 2022
aa4918b
add: Add vulnerability inventory full scan test module #2594
jmv74211 Mar 1, 2022
bcd182d
add: Add vulnerability inventory partial scan test module #2595
jmv74211 Mar 1, 2022
86a385b
fix: Fix an error in db_interface module
jmv74211 Feb 23, 2022
71c1859
Merge pull request #2613 from wazuh/2594-refactor-full-scan-type
jmv74211 Mar 1, 2022
d3785d9
refac: Update multiple_provider_feeds test to use new custom feeds #2592
jmv74211 Mar 1, 2022
f349e0f
rm: Remove tier 5 tests to avoid duplicate testing #2592
jmv74211 Mar 1, 2022
a5f5e06
refac: Improve test_alert_vulnerability_removal #2592
jmv74211 Mar 1, 2022
91d74c4
add: Add new structure to test_download_feeds #2596
fedepacher Mar 2, 2022
29d32ba
refac: Remove dependency #2596
fedepacher Mar 2, 2022
db55c38
refac: Refact code style #2596
fedepacher Mar 2, 2022
2a92893
refac: Add style refact to test_download_feeds #2596
fedepacher Mar 2, 2022
6004f78
add: Add new structure to test_validate_feed_content #2599
fedepacher Mar 2, 2022
0f7842e
Merge pull request #2610 from wazuh/2596-refactor-test-download-feeds
jmv74211 Mar 3, 2022
24a2530
add: Add new vuln monitor functions #2598
jmv74211 Mar 3, 2022
08c3598
refac: Update error message when wazuh-modulesd is down #2598
jmv74211 Mar 3, 2022
6f12cc3
add: Add test module to check invalid feed type imports #2598
jmv74211 Mar 3, 2022
e43a7bf
refac: Temporarily disable scan provider and nvd test module #2592
jmv74211 Mar 3, 2022
a6c84cc
Merge pull request #2629 from wazuh/2598-refactor-invalid-url-feed-type
jmv74211 Mar 3, 2022
a712368
refac: Improve test_validate_feed_content module #2599
jmv74211 Mar 3, 2022
ce0c94e
Merge pull request #2628 from wazuh/2599-test_validate_feed_content
jmv74211 Mar 3, 2022
024fb58
rm: Remove old variable that caused circular import error
jmv74211 Mar 4, 2022
3f7a834
add: Add new methods to get the feeds timestamp metadata #2630
jmv74211 Mar 4, 2022
a524365
add: Add new time util function to parse datetime formats #2630
jmv74211 Mar 4, 2022
fa3b719
add: Add new function to decide if a feed is recently updated #2630
jmv74211 Mar 4, 2022
b87796c
add: Add new logic to check if a downloaded feed has been recently up…
jmv74211 Mar 4, 2022
a05b859
refac: Replace usage of datetime fromisoformat method #2630
jmv74211 Mar 4, 2022
a898789
refac: Update parse datetime formats to support python <3.7.0 #2630
jmv74211 Mar 7, 2022
eaa5dcb
Merge pull request #2634 from wazuh/2630-test-last-feeds-update
jmv74211 Mar 7, 2022
80a5b3c
refac: Refactor test_interval module #2639 -> t1
jmv74211 Mar 7, 2022
c1f93fa
refac: Update configuration template name #2592
jmv74211 Mar 7, 2022
9a26224
rm: Remove old test feed files #2592
jmv74211 Mar 7, 2022
4d0dc59
refac: Remove old event monitor functions #2639 -> t8
jmv74211 Mar 7, 2022
82ef802
rm: Remove old VDT test files #2639 -> t8
jmv74211 Mar 7, 2022
875a19f
refac: Remove old methods and variables #2639 -> t8
jmv74211 Mar 7, 2022
3d752b5
add: Add autoconfigure local_internal_options file #2639 -> t4
fedepacher Mar 7, 2022
5c1eb0d
refac: Update wazuh configuration fixture in test interval #2639 -> t4
jmv74211 Mar 7, 2022
58e00d3
doc: Improve VDT tests documentation #2639 -> t2
jmv74211 Mar 8, 2022
4d43e83
add: Add tiers information to the VDT integration tests #2639 -> t3
jmv74211 Mar 8, 2022
d084bb9
refac: Rename log monitor variables #2639 -> t5
jmv74211 Mar 8, 2022
4118fe8
refac: Move test_cpe_indexing module to test_feeds suite #2639
jmv74211 Mar 8, 2022
c2f5953
Merge branch 'master' of github.com:wazuh/wazuh-qa into 2639-vdt-refa…
jmv74211 Mar 8, 2022
9bf16f1
rm: Remove duplicated function #2639
jmv74211 Mar 9, 2022
2a37e70
refac: Update changelog
jmv74211 Mar 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Release report: TBD

### Changed

- Refactor VDT integration tests: feeds and scan types ([#2650](https://github.com/wazuh/wazuh-qa/pull/2650))
- Refactor: FIM `test_synchronization` according to new standard. Phase 1. ([#2358](https://github.com/wazuh/wazuh-qa/pull/2358))
- Refactor: FIM `test_registry_file_limit` and `test_registry_report_changes`. ([#2478](https://github.com/wazuh/wazuh-qa/pull/2478))
- Refactor: FIM `test_files/test_file_limit` and updated imports to new standard. ([#2501](https://github.com/wazuh/wazuh-qa/pull/2501))
Expand Down Expand Up @@ -150,9 +151,9 @@ Release report: https://github.com/wazuh/wazuh-qa/issues/2500

## [v4.2.0]
### Added
- Add agent labels to agent simulator tool [#1153](https://github.com/wazuh/wazuh-qa/pull/1153)
- Add agent labels to agent simulator tool [#1153](https://github.com/wazuh/wazuh-qa/pull/1153)
- Add the capability to know which CVE’s affect an agent [#7479](https://github.com/wazuh/wazuh/issues/7479)
- Add new tests for Wazuh-DB insert commands in agents' CVEs table [#1092](https://github.com/wazuh/wazuh-qa/pull/1092)
- Add new tests for Wazuh-DB insert commands in agents' CVEs table [#1092](https://github.com/wazuh/wazuh-qa/pull/1092)
- Add integration tests for syslog [#1086](https://github.com/wazuh/wazuh-qa/pull/1086)
- Add remoted integration tests: basic configuration tests [#1073](https://github.com/wazuh/wazuh-qa/pull/1073)
- Add the tier 0 integration tests for wazuh-remoted [#1024](https://github.com/wazuh/wazuh-qa/issues/1024)
Expand Down
3 changes: 2 additions & 1 deletion deps/wazuh_testing/wazuh_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
WAZUH_DB_SOCKET_PATH = os.path.join(QUEUE_DB_PATH, 'wdb')
CVE_DB_PATH = os.path.join(WAZUH_PATH, 'queue', 'vulnerabilities', 'cve.db')
LOG_FILE_PATH = os.path.join(WAZUH_PATH, 'logs', 'ossec.log')

ALERTS_JSON_PATH = os.path.join(WAZUH_PATH, 'logs', 'alerts', 'alerts.json')
CPE_HELPER_PATH = os.path.join(WAZUH_PATH, 'queue', 'vulnerabilities', 'dictionaries', 'cpe_helper.json')

UDP = 'UDP'
TCP = 'TCP'
Expand Down
2 changes: 0 additions & 2 deletions deps/wazuh_testing/wazuh_testing/db_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ def query_wdb(command):
retry = 0
# Wait if the wdb socket is not still alive (due to wazuh-db restarts). Max 3 seconds
while not os.path.exists(WAZUH_DB_SOCKET_PATH) and retry < max_retries:
print("Retrying ...")
sleep(0.5)
retry += 1

# Restart wazuh-db in case of wdb socket is not yet up.
if not os.path.exists(WAZUH_DB_SOCKET_PATH):
print("Restarting wazuh-db ...")
control_service('restart', daemon='wazuh-db')

# Raise custom exception if the socket is not up in the expected time, even restarting wazuh-db
Expand Down
199 changes: 149 additions & 50 deletions deps/wazuh_testing/wazuh_testing/db_interface/agent_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from time import time

from wazuh_testing.db_interface import query_wdb
from wazuh_testing.modules.vulnerability_detector import DEFAULT_PACKAGE_NAME


def clean_table(agent_id, table):
Expand All @@ -17,7 +16,7 @@ def clean_table(agent_id, table):


def update_last_full_scan(last_scan=0, agent_id='000'):
"""Update the last scan of an agent.
"""Update the last full scan of an agent.

Args:
last_scan (int): Last scan ID. This is compute by casting to int the result of time().
Expand All @@ -27,6 +26,17 @@ def update_last_full_scan(last_scan=0, agent_id='000'):
query_wdb(query_string)


def update_last_partial_scan(last_scan=0, agent_id='000'):
"""Update the last partial scan of an agent.

Args:
last_scan (int): Last scan ID. This is compute by casting to int the result of time().
agent_id (str): Agent ID.
"""
query_string = f"agent {agent_id} sql UPDATE vuln_metadata SET LAST_PARTIAL_SCAN={last_scan}"
query_wdb(query_string)


def insert_hotfix(agent_id='000', scan_id=int(time()), scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
hotfix='000000', checksum='dummychecksum'):
"""Insert a hotfix.
Expand All @@ -44,9 +54,10 @@ def insert_hotfix(agent_id='000', scan_id=int(time()), scan_time=datetime.dateti


def insert_os_info(agent_id='000', scan_id=int(time()), scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
hostname='centos8', architecture='x86_64', os_name='CentOS Linux', os_version='8.4', os_major='8',
os_minor='4', os_build='', version='', os_release='', os_patch='', release='',
checksum='dummychecksum'):
hostname='centos8', architecture='x64', os_name='CentOS Linux', os_version='8.4', os_codename= '',
os_major='8', os_minor='4', os_patch='', os_build='', os_platform='centos', sysname='Linux',
release='', version='', os_release='', checksum='dummychecksum', os_display_version='', triaged=0,
reference=''):
"""Insert the OS information in the agent database.

Args:
Expand All @@ -57,28 +68,36 @@ def insert_os_info(agent_id='000', scan_id=int(time()), scan_time=datetime.datet
architecture (str): Architecture of the host.
os_name (str): Complete name of the OS.
os_version (str): Version of the OS.
os_codename (str): OS codename.
os_major (str): Major version of the OS.
os_minor (str): Minor version of the OS.
os_patch (str): Current patch of the OS.
os_build (str): Build id of the OS.
os_platform (str): OS platform.
sysname (str): System name.
release (str): Release of the OS.
version (str): Version of the OS.
os_release (str): Release of the OS.
os_patch (str): Current patch of the OS.
release (str): Release of the OS.
checksum (str): Checksum of the OS.
os_display_version (str): Os displayed version
triaged (int): Triaged.
reference (str): OS reference.
"""
query_string = f"agent {agent_id} sql INSERT OR REPLACE INTO sys_osinfo (scan_id, scan_time, hostname, " \
'architecture, os_name, os_version, os_major, os_minor, os_patch, os_build, release, version, ' \
f"os_release, checksum) VALUES ('{scan_id}', '{scan_time}', '{hostname}', '{architecture}', " \
f"'{os_name}', '{os_version}', '{os_major}', '{os_minor}', '{os_patch}', '{os_build}', " \
f"'{release}', '{version}', '{os_release}', '{checksum}')"
'architecture, os_name, os_version, os_codename, os_major, os_minor, os_patch, os_build, ' \
'os_platform, sysname, release, version, os_release, os_display_version, checksum, reference, ' \
f"triaged) VALUES ({scan_id}, '{scan_time}', '{hostname}', '{architecture}', '{os_name}', " \
f"'{os_version}', '{os_codename}', '{os_major}', '{os_minor}', '{os_patch}', '{os_build}', " \
f"'{os_platform}', '{sysname}', '{release}', '{version}', '{os_release}', '{os_display_version}', " \
f"'{checksum}', '{reference}', {triaged})"

query_wdb(query_string)


def insert_package(agent_id='000', scan_id=int(time()), format='rpm', name=DEFAULT_PACKAGE_NAME,
priority='', section='Unspecified', size=99, vendor='wazuhintegrationtests', version='1.0.0-1.el7',
architecture='x86_64', multiarch='', description='Wazuh Integration tests mock package',
source='Wazuh Integration tests mock package', location='', triaged=0,
install_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
def insert_package(agent_id='000', scan_id=int(time()), format='rpm', name='custom-package-0',
priority='', section='Unspecified', size=99, vendor='wazuh-mocking', version='1.0.0-1.el7',
architecture='x64', multiarch='', description='Wazuh mocking packages', source='Wazuh QA tests',
location='', triaged=0, install_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"), checksum='dummychecksum',
item_id='dummyitemid'):
"""Insert a package in the agent DB.
Expand Down Expand Up @@ -119,7 +138,6 @@ def insert_package(agent_id='000', scan_id=int(time()), format='rpm', name=DEFAU
f"{arguments['description']}, {arguments['location']}, {arguments['triaged']}, {arguments['checksum']},"
f"{arguments['item_id']})")


def update_sync_info(agent_id='000', component='syscollector-packages', last_attempt=1, last_completion=1,
n_attempts=0, n_completions=0, last_agent_checksum=''):
"""Update the sync_info table of the specified agent for the selected component.
Expand Down Expand Up @@ -197,59 +215,140 @@ def delete_os_info(agent_id='000'):


def update_os_info(agent_id='000', scan_id=int(time()), scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
hostname='centos8', architecture='x86_64', os_name='CentOS Linux', os_version='8.4', os_major='8',
os_minor='4', os_build='', version='', os_release='', os_patch='', release='',
checksum='dummychecksum'):
hostname='centos8', architecture='x64', os_name='CentOS Linux', os_version='8.4', os_codename= '',
os_major='8', os_minor='4', os_patch='', os_build='', os_platform='centos', sysname='Linux',
release='', version='', os_release='', checksum='dummychecksum', os_display_version='', triaged=0,
reference=''):
"""Update the sys_osinfo data from a specific agent.

Args:
agent_id (str): Agent ID.
scan_id (int): Id of the last scan.
scan_time (str): Date of the scan with this format "%Y/%m/%d %H:%M:%S".
hostname (str): Name of the host.
architecture (str): Architecture of the host.
os_name (str): Complete name of the OS.
os_version (str): Version of the OS.
os_codename (str): OS codename.
os_major (str): Major version of the OS.
os_minor (str): Minor version of the OS.
os_patch (str): Current patch of the OS.
os_build (str): Build id of the OS.
os_platform (str): OS platform.
sysname (str): System name.
release (str): Release of the OS.
version (str): Version of the OS.
os_release (str): Release of the OS.
checksum (str): Checksum of the OS.
os_display_version (str): Os displayed version
triaged (int): Triaged.
reference (str): OS reference.
"""
delete_os_info(agent_id)
insert_os_info(**locals())


def check_vulnerability_scan_inventory(agent_id, package, version, arch, cve, condition, severity='-', cvss2=0,
cvss3=0):
"""Check the existence or lack of a vulnerability in the agent's DB.
def clean_sys_programs(agent_id='000'):
"""Clean all the agent packages data from the DB

Args:
agent_id (str): Agent ID.
"""
clean_table(agent_id, 'sys_programs')


def get_vulnerability_status(agent_id='000', package=''):
"""Check the status of a vulnerability in the agent database table.

Args:
agent_id (str): Agent ID.
package (str): Package name.
version (str): Package version.
arch (str): Package architecture.
cve (str): Vulnerability ID associated to the vulnerable package.
condition (str): This parameter is used to check if the vulnerability exists ('inserted') or
not ('removed') in the inventory.
package (str): Package to be checked.
"""
query = f"agent {agent_id} sql SELECT status FROM vuln_cves WHERE name = '{package}'"

result = query_wdb(query)[0]['status']

return result


def get_packages_number(agent_id='000', package=''):
"""Check the number of packages in the agent database table.

Args:
agent_id (str): Agent ID.
package (str): Package to be checked.
"""
query = f"agent {agent_id} sql SELECT count(*) FROM sys_programs WHERE name = '{package}'"

result = query_wdb(query)[0]['count(*)']

return result


def get_vulnerability_inventory_data(agent_id='000', name=None, status=None, cve=None, version=None, type=None,
architecture=None, severity=None, cvss2_score=None, cvss3_score=None):
"""Get the vulnerability inventory data according to the specified parameters.

Args:
agent_id (str): Agent ID.
name (str): Vulnerability name.
status (str): Vulnerability status.
cve (str): Vulnerability CVE.
version (str): Version.
type (str): Vulnerability type.
architecture (str): Architecture.
severity (str): Vulnerability severity.
cvss2 (str): CVSS2 score of the vulnerable package.
cvss3 (str): CVSS3 score of the vulnerable package.
cvss2_score (float): CVSS2 score.
cvss3_score (float): CVSS3 score

Returns:
list(dict): Data in the DB.

Raises:
Exception: If the condition has unexpected value.
"""
if condition != 'inserted' and condition != 'removed':
raise Exception(f'The "condition" parameter has an unexpected value: {condition}')
# Build a dictionary with local variables
query_parameters = locals()

if condition == 'inserted':
query = f"agent {agent_id} sql SELECT CASE WHEN EXISTS (select 1 FROM vuln_cves WHERE cve = '{cve}' AND " \
f"name = '{package}' AND version = '{version}' AND architecture = '{arch} AND severity = ' " \
f"'{severity}' AND cvss2_score = {cvss2} AND cvss3_score = {cvss3}) THEN 'true' ELSE 'false' END " \
"as 'result'"
else:
query = f"agent {agent_id} sql SELECT CASE WHEN NOT EXISTS (select 1 FROM vuln_cves WHERE cve = '{cve}' " \
f"AND name = '{package}' AND version = '{version}' AND architecture = '{arch}') THEN 'true' " \
f"ELSE 'false' END as 'result'"
# Remove non query parameters
del query_parameters['agent_id']

result = query_wdb(query)[0]['result']
# Define the initial query string
query = f"agent {agent_id} sql SELECT * FROM vuln_cves"

return result
# Build the query string according to the specified parameters
first_parameter = True
for item, value in query_parameters.items():
if value is not None:
formated_value = f"'{value}'" if isinstance(value, str) else value

if first_parameter:
query += f" WHERE {item}={formated_value}"
first_parameter = False
else:
query += f" AND {item}={formated_value}"

def clean_sys_programs(agent_id='000'):
"""Clean all the agent packages data from the DB
return query_wdb(query)

Args:

def insert_vulnerability_in_agent_inventory(agent_id='000', name='', status='PENDING', cve='', version='',
type='PACKAGE', architecture='', severity='-', cvss2_score=0,
cvss3_score=0, reference='', detection_time=''):
"""Insert a vulnerability in the agent vulnerabilities inventory.

Args:
agent_id (str): Agent ID.
name (str): Vulnerability name.
status (str): Vulnerability status.
cve (str): Vulnerability CVE.
version (str): Version.
type (str): Vulnerability type.
architecture (str): Architecture.
severity (str): Vulnerability severity.
cvss2_score (float): CVSS2 score.
cvss3_score (float): CVSS3 score
reference (str): Vulnerability reference.
detection_time (str): Vulnerability detection time.
"""
clean_table(agent_id, 'sys_programs')
query_wdb(f"agent {agent_id} sql INSERT OR REPLACE INTO vuln_cves (name, version, architecture, cve, reference, " \
f"type, status, severity, cvss2_score, cvss3_score, detection_time) VALUES ('{name}', '{version}', " \
f"'{architecture}', '{cve}', '{reference}', '{type}', '{status}', '{severity}', {cvss2_score}, " \
f"{cvss3_score}, '{detection_time}')")
Loading