- added config generation logic

- fixed typo in package name
This commit is contained in:
Kirill Zhukov 2022-12-29 12:17:21 +01:00
parent 7844a44a74
commit 48750d1377
11 changed files with 182 additions and 26 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@ venv
logs
results
*.pyc
__pycache__
__pycache__
.vscode

View File

@ -1,4 +0,0 @@
from albs_oval_erratta_diff.start import start
start()

7
albs_oval_errata_diff.py Normal file
View File

@ -0,0 +1,7 @@
"""
albs_oval_errata_diff.py is a service startup script
"""
from albs_oval_errata_diff.start import start
start()

View File

@ -0,0 +1,105 @@
'''
config.py used for generation service configuration based on input json file
'''
from datetime import datetime, date
from pathlib import Path
from typing import Dict, List
from ipaddress import IPv4Address
from pydantic import BaseModel, validator, Field # pylint: disable=import-error
import yaml
# DEFAULTS
DIFF_FILE = Path('/tmp/albs-oval-errata-diff.json')
DOWNLOAD_DIR = Path('/tmp')
LOG_FILE = Path('logs/albs-oval-errata-diff.log')
PACKAGES_EXCLUDE = []
SA_EXCLUDE = []
SERVER_PORT = 3001
SERVER_IP = IPv4Address('127.0.0.1')
# not checking anything before RHEL-9.0 release
NOT_BEFORE = datetime(2022, 5, 18)
UPDATE_INTERVAL_MINUTES = 30
class ReleaseUrls(BaseModel):
"""
ReleaseUrls represents list of RHEL/Alma Oval and Errata URLS for specific OS release
"""
rhel_oval_url: str = Field(description='URL for RHEL OVAL file')
alma_oval_url: str = Field(description='URL for Alma OVAL file')
alma_errata_url: str = Field(description='URL for Alma Errata file')
class Config(BaseModel):
"""
Config represents service configuration
"""
diff_file: Path = Field(description="file to store diff JSON in",
default=DIFF_FILE)
download_dir: Path = Field(
description='directory to download Oval/Errata files to',
default=DOWNLOAD_DIR)
log_file: Path = Field(
description='file to write logs to',
default=LOG_FILE)
packages_exclude: List[str] = Field(
description='list of RPM package names to exclude from checking',
default=PACKAGES_EXCLUDE)
releases: Dict[int, ReleaseUrls] = Field(
description='list of OS releases with Oval/Errata URLs to check')
sa_exclude: List[str] = Field(
description='list of Security Advisory IDs (ALSA-2022:5219) to exclude from checking',
default=SA_EXCLUDE)
server_port: int = Field(
description="port that will be used by websever",
default=SERVER_PORT)
server_ip: IPv4Address = Field(
description="IP that will be used by webserver",
default=SERVER_IP)
not_before: date = Field(
description='date to start checking from (YYYY-mm-dd)',
default=NOT_BEFORE)
update_interval_minutes: int = Field(
description='how often service will be running difference checks (in minutes)',
default=UPDATE_INTERVAL_MINUTES)
@validator("releases", pre=True)
@classmethod
def parse_releases(cls, value) -> Dict[int, ReleaseUrls]:
"""
parse_release converts releases attribute
Dict[int, Dict[str, str]] -> Dict[str, ReleaseUrls]
"""
result: Dict[int, ReleaseUrls] = {}
for release, urls in value.items():
result[release] = ReleaseUrls(rhel_oval_url=urls['rhel_oval_url'],
alma_oval_url=urls['alma_oval_url'],
alma_errata_url=urls['alma_errata_url'])
return result
@validator("not_before", pre=True)
@classmethod
def str_to_datetime(cls, value) -> datetime:
"""
str_to_datetime converts string attr str -> datetime
"""
return datetime.strptime(
value,
"%Y-%m-%d"
).date()
def get_config(yml_path: str) -> Config:
"""
get_config loads yml file and generates Config instance
"""
with open(yml_path, 'r', encoding='utf-8') as flr:
data = yaml.safe_load(flr)
return Config(**data)
if __name__ == "__main__":
print(get_config('./config.default.yml'))

View File

@ -1,21 +0,0 @@
from pathlib import Path
import datetime
RELEASES = {
8: {'rhel_oval_url': 'https://www.redhat.com/security/data/oval/v2/RHEL8/rhel-8.oval.xml.bz2',
'alma_oval_url': 'https://repo.almalinux.org/security/oval/org.almalinux.alsa-8.xml.bz2',
'alma_errata_url': "https://errata.almalinux.org/8/errata.full.json", },
9: {'rhel_oval_url': 'https://www.redhat.com/security/data/oval/v2/RHEL9/rhel-9.oval.xml.bz2',
'alma_oval_url': 'https://repo.almalinux.org/security/oval/org.almalinux.alsa-9.xml.bz2',
'alma_errata_url': "https://errata.almalinux.org/9/errata.full.json", }
}
LOG_FILE = Path('logs/albs-oval-errata-diff.log')
DIFF_FILE = Path('results/diff.json')
DOWNLOAD_DIR = Path('/tmp')
# not checking anything before RHEL-9.0 release
NOT_BEFORE = datetime.datetime(2022, 5, 18)
UPDATE_INTERVAL_MINUTES = 30
SERVER_PORT = 3001
SERVER_IP = "127.0.0.1"
SA_EXCLUDE = []
PACKAGES_EXCLUDE = ["dotnet-sdk-3.1-source-built-artifacts"]

68
config.default.yml Normal file
View File

@ -0,0 +1,68 @@
---
# diff_file
# file to store diff JSON in
# requred: no
# default: /tmp/albs-oval-errata-diff.json
diff_file: /tmp/albs-oval-errata-diff.json
# download_dir
# directory to download Oval/Errata files to
# required: no
# default: /tmp
download_dir: /tmp
# log_file
# file to write logs to
# requred: no
# default: logs/albs-oval-errata-diff.log
log_file: logs/albs-oval-errata-diff.log
# packages_exclude
# list of RPM package names to exclude from checking
# requred: no
# default: []
packages_exclude: []
# releases
# list of OS releases with Oval/Errata URLs to check
# required: yes
# default: N/A
releases:
8:
rhel_oval_url: https://www.redhat.com/security/data/oval/v2/RHEL8/rhel-8.oval.xml.bz2
alma_oval_url: https://repo.almalinux.org/security/oval/org.almalinux.alsa-8.xml.bz2
alma_errata_url: https://errata.almalinux.org/8/errata.full.json
9:
rhel_oval_url: https://www.redhat.com/security/data/oval/v2/RHEL9/rhel-9.oval.xml.bz2'
alma_oval_url: https://repo.almalinux.org/security/oval/org.almalinux.alsa-9.xml.bz2'
alma_errata_url: https://errata.almalinux.org/9/errata.full.json
# sa_exclude
# list of Security Advisory IDs (ALSA-2022:5219) to exclude from checking
# requred: no
# default: []
sa_exclude: []
# server_port
# port that will be used by websever
# required: no
# default: 3001
server_port: 3001
# server_ip
# IP that will be used by webserver
# required: no
# default: 127.0.0.1
server_ip: 127.0.0.1
# not_before
# date to start checking from (YYYY-mm-dd)
# required: no
# default: 2022-5-18 (Release of RHEL 9.0)
not_before: 2022-5-18
# update_interval_minutes
# how often service will be running difference checks (in minutes)
# required: no
# default: 30
update_interval_minutes: 30