Skip to content

Commit

Permalink
feat(reposcan): add CSAF sync task
Browse files Browse the repository at this point in the history
  • Loading branch information
michalslomczynski committed Jan 23, 2024
1 parent 6961630 commit 971fc92
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/reposcan.env
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ SYNC_REPOS=yes
SYNC_CVE_MAP=yes
SYNC_CPE=yes
SYNC_OVAL=yes
SYNC_CSAF=yes
1 change: 1 addition & 0 deletions vmaas/reposcan/mnm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@

CSAF_FAILED_DOWNLOAD = Counter('vmaas_reposcan_failed_csaf_download', '# of failed csaf-download attempts')
CSAF_FAILED_DELETE = Counter('vmaas_reposcan_failed_csaf_delete', '# of failed csaf-delete attempts')
CSAF_FAILED_IMPORT = Counter('vmaas_reposcan_failed_csaf_import', '# of failed csaf-import attempts')

REPOS_TO_CLEANUP = Gauge('vmaas_reposcan_repos_cleanup', '# of repos to cleanup from DB')
38 changes: 37 additions & 1 deletion vmaas/reposcan/reposcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
from vmaas.reposcan.dbchange import DbChangeAPI
from vmaas.reposcan.exporter import main as export_data, fetch_latest_dump
from vmaas.reposcan.mnm import ADMIN_REQUESTS, FAILED_AUTH, FAILED_IMPORT_CVE, FAILED_IMPORT_CPE, OVAL_FAILED_IMPORT, \
FAILED_IMPORT_REPO, REPOS_TO_CLEANUP, REGISTRY
CSAF_FAILED_IMPORT, FAILED_IMPORT_REPO, REPOS_TO_CLEANUP, REGISTRY
from vmaas.reposcan.pkgtree import main as export_pkgtree, PKGTREE_FILE
from vmaas.reposcan.redhatcpe.cpe_controller import CpeController
from vmaas.reposcan.redhatcsaf.csaf_controller import CsafController
from vmaas.reposcan.redhatcve.cvemap_controller import CvemapController
from vmaas.reposcan.redhatoval.oval_controller import OvalController
from vmaas.reposcan.repodata.repository_controller import RepositoryController
Expand Down Expand Up @@ -67,6 +68,7 @@
SYNC_CVE_MAP = strtobool(os.getenv("SYNC_CVE_MAP", "yes"))
SYNC_CPE = strtobool(os.getenv("SYNC_CPE", "yes"))
SYNC_OVAL = strtobool(os.getenv("SYNC_OVAL", "yes"))
SYNC_CSAF = strtobool(os.getenv("SYNC_CSAF", "yes"))


class TaskStatusResponse(dict):
Expand Down Expand Up @@ -740,6 +742,39 @@ def metrics():
return generate_latest(REGISTRY), 200, {'Content-Type': 'text/plain; charset=utf-8'}


class CsafSyncHandler(SyncHandler):
"""Handler for CSAF sync API."""

task_type = "Sync CSAF metadata"

@classmethod
def put(cls, **kwargs):
"""Sync CSAF metadata."""

status_code, status_msg = cls.start_task()
return status_msg, status_code

@staticmethod
def run_task(*args, **kwargs):
"""Function to start syncing CSAFs."""
try:
init_logging()
init_db()
controller = CsafController()
controller.store()
except Exception as err: # pylint: disable=broad-except
msg = "Internal server error <%s>" % hash(err)
LOGGER.exception(msg)
CSAF_FAILED_IMPORT.inc()
DatabaseHandler.rollback()
if isinstance(err, DatabaseError):
return "DB_ERROR"
return "ERROR"
finally:
DatabaseHandler.close_connection()
return "OK"


def all_sync_handlers() -> list:
"""Return all sync-handlers selected using env vars."""
handlers = []
Expand All @@ -748,6 +783,7 @@ def all_sync_handlers() -> list:
handlers.extend([CvemapSyncHandler] if SYNC_CVE_MAP else [])
handlers.extend([CpeSyncHandler] if SYNC_CPE else [])
handlers.extend([OvalSyncHandler] if SYNC_OVAL else [])
handlers.extend([CsafSyncHandler] if SYNC_CSAF else [])
return handlers


Expand Down
10 changes: 10 additions & 0 deletions vmaas/reposcan/reposcan.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ paths:
tags:
- Sync

/sync/csaf:
put:
summary: Sync CSAF metadata
operationId: vmaas.reposcan.reposcan.CsafSyncHandler.put
<<: *secured
responses:
<<: *sync_responses
tags:
- Sync

/export/pkgtree:
put:
summary: Export package tree
Expand Down

0 comments on commit 971fc92

Please sign in to comment.