Skip to content

Commit

Permalink
feat: Add configuration option to enable/disable Docker Scout
Browse files Browse the repository at this point in the history
- Integrated ConfigManager to retrieve 'enable_docker_scout' setting.
- Modified DockerScoutManager to respect the configuration option, allowing users to enable or disable Docker Scout functionality.
- Updated init_hook to conditionally check Docker Scout installation based on configuration.
- Added logic to short-circuit Docker Scout operations if disabled via configuration.
  • Loading branch information
willis89pr committed Jan 21, 2025
1 parent eb2cb9c commit 0a48473
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions surfactant/infoextractors/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@

import surfactant.plugin
from surfactant.sbomtypes import SBOM, Software
from surfactant.configmanager import ConfigManager


class DockerScoutManager:
def __init__(self) -> None:
self.disable_docker_scout = True
# Initialize ConfigManager
config_manager = ConfigManager()

# Retrieve the configuration option
enable_docker_scout = config_manager.get("docker", "enable_docker_scout", True)

# Set disable_docker_scout based on the configuration
self.disable_docker_scout = not enable_docker_scout
self.docker_scout_installed = False

def check_docker_scout_installed(self) -> None:
"""Check if Docker Scout is installed and update the state accordingly."""
if self.disable_docker_scout:
return # Do nothing if Docker Scout is disabled by config

try:
result = subprocess.run(["docker", "scout"], capture_output=True, check=False)
self.docker_scout_installed = result.returncode == 0
Expand All @@ -29,10 +40,16 @@ def check_docker_scout_installed(self) -> None:

self.disable_docker_scout = not self.docker_scout_installed
if not self.docker_scout_installed:
logger.warning("Install Docker Scout to scan containers for additional information")
logger.warning(
"Install Docker Scout to scan containers for additional information. "
"You can also disable this check by running 'surfactant config docker.enable_docker_scout false'."
)

def run_docker_scout(self, filename: str) -> object:
"""Run Docker Scout on the given file and return the results."""
if self.disable_docker_scout:
return {} # Do nothing if Docker Scout is disabled by config

try:
result = subprocess.run(
["docker", "scout", "sbom", "--format", "spdx", f"fs://{filename}"],
Expand Down Expand Up @@ -80,5 +97,5 @@ def extract_docker_info(filetype: str, filename: str) -> object:

@surfactant.plugin.hookimpl
def init_hook(command_name: Optional[str] = None) -> None:
if command_name != "update-db":
if command_name != "update-db" and not dsManager.disable_docker_scout:
dsManager.check_docker_scout_installed()

0 comments on commit 0a48473

Please sign in to comment.