Skip to content

Commit cb25b83

Browse files
committed
Custom Capabilities in Candore
1 parent 5ab760b commit cb25b83

File tree

7 files changed

+53
-1
lines changed

7 files changed

+53
-1
lines changed

candore/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from candore.modules.extractor import Extractor
1212
from candore.modules.finder import Finder
1313
from candore.modules.report import Reporting
14+
from candore.config import candore_settings
1415

1516

1617
class Candore:
@@ -39,6 +40,8 @@ async def save_all_entities(self, mode, output_file, full, max_pages=None, skip_
3940
extractor.max_pages = max_pages
4041
extractor.skip_percent = skip_percent
4142
data = await extractor.extract_all_entities()
43+
if hasattr(self.settings, 'rpms'):
44+
data.update({'installed_rpms': await extractor.extract_all_rpms()})
4245

4346
if not data:
4447
click.echo("Entities data is not data found!")

candore/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def candore_settings(option_settings_file=None, option_components_file=None):
2323
core_loaders=["YAML"],
2424
envvar_prefix="CANDORE",
2525
settings_files=[settings_file, components_file],
26+
preload=["conf/*.yaml"],
2627
envless_mode=True,
2728
lowercase_read=True,
2829
)

candore/modules/extractor.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio # noqa: F401
22
import math
33
from functools import cached_property
4-
4+
from candore.modules.ssh import Session
5+
import re
56
import aiohttp
67

78
# Max observed request duration in testing was approximately 888 seconds
@@ -188,3 +189,15 @@ async def extract_all_entities(self):
188189
comp_entities = await self.process_entities(endpoints=endpoints)
189190
all_data[component] = comp_entities
190191
return all_data
192+
193+
async def extract_all_rpms(self):
194+
"""Extracts all installed RPMs from server"""
195+
with Session() as ssh_client:
196+
rpms = ssh_client.execute('rpm -qa').stdout
197+
rpms = rpms.splitlines()
198+
name_version_pattern = rf'{self.settings.rpms.regex_pattern}'
199+
rpms_matches = [
200+
re.compile(name_version_pattern).match(rpm) for rpm in rpms
201+
]
202+
rpms_list = [rpm_match.groups()[:-1] for rpm_match in rpms_matches if rpm_match]
203+
return dict(rpms_list)

candore/modules/ssh.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from hussh import Connection
2+
from functools import cached_property
3+
from candore.config import candore_settings
4+
from urllib.parse import urlparse
5+
6+
7+
class Session:
8+
9+
def __init__(self):
10+
self.settings = candore_settings()
11+
self.hostname = urlparse(self.settings.candore.base_url).hostname
12+
self.username = self.settings.candore.ssh.username or 'root'
13+
14+
@cached_property
15+
def auth(self):
16+
auth_kwargs = {}
17+
if self.settings.candore.ssh.private_key:
18+
auth_kwargs["private_key"] = self.settings.candore.ssh.private_key
19+
elif self.settings.candore.ssh.password:
20+
auth_kwargs["password"] = self.settings.candore.ssh.password
21+
return auth_kwargs
22+
23+
def __enter__(self):
24+
self.client = Connection(self.hostname, username=self.username, **self.auth)
25+
return self.client
26+
27+
def __exit__(self, exc_type, exc_val, exc_tb):
28+
pass

conf/rpms.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rpms:
2+
regex_pattern: '([a-zA-Z0-9_-]+)-([\d._-]+)\.(.*?)$'

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dependencies = [
4646
"flask",
4747
"requests",
4848
"PyYAML",
49+
"hussh",
4950
]
5051

5152
[project.optional-dependencies]

settings.yaml.template

+4
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ candore:
88
parser: apipie
99
var_file: "@jinja {{this.candore.product_version | replace('.', '_')}}_variations.yaml"
1010
constant_File: "@jinja {{this.candore.product_version | replace('.', '_')}}_constants.yaml"
11+
ssh:
12+
username:
13+
private_key:
14+
password:
1115
# The maximum number of concurrent (asynchronous) connections allowed against the host
1216
max_connections: 20

0 commit comments

Comments
 (0)