-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Snyk scan suggestion when building
Signed-off-by: Ulysses Souza <[email protected]>
- Loading branch information
1 parent
84afa51
commit 5773a11
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import json | ||
import logging | ||
import os | ||
from distutils.util import strtobool | ||
|
||
from docker.constants import IS_WINDOWS_PLATFORM | ||
from docker.utils.config import find_config_file | ||
|
||
|
||
SCAN_BINARY_NAME = "docker-scan" + (".exe" if IS_WINDOWS_PLATFORM else "") | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ScanConfig: | ||
def __init__(self, dict): | ||
self.optin = False | ||
vars(self).update(dict) | ||
|
||
|
||
def display_scan_suggest_msg(): | ||
if environment_scan_avoid_suggest() or \ | ||
scan_already_invoked() or \ | ||
not scan_available(): | ||
return | ||
log.info("Use 'docker scan' to run Snyk tests against images to find vulnerabilities " | ||
"and learn how to fix them") | ||
|
||
|
||
def environment_scan_avoid_suggest(): | ||
return os.getenv('DOCKER_SCAN_SUGGEST', 'true').lower() == 'false' | ||
|
||
|
||
def scan_already_invoked(): | ||
docker_folder = docker_config_folder() | ||
if docker_folder is None: | ||
return False | ||
|
||
scan_config_file = os.path.join(docker_folder, 'scan', "config.json") | ||
if not os.path.exists(scan_config_file): | ||
return False | ||
|
||
data = '' | ||
with open(scan_config_file) as f: | ||
data = f.read() | ||
scan_config = json.loads(data, object_hook=ScanConfig) | ||
return scan_config.optin if isinstance(scan_config.optin, bool) else strtobool(scan_config.optin) | ||
|
||
|
||
def scan_available(): | ||
docker_folder = docker_config_folder() | ||
scan_config_file = os.path.join(docker_folder, 'cli-plugins', SCAN_BINARY_NAME) | ||
return os.path.isfile(scan_config_file) or os.path.islink(scan_config_file) | ||
|
||
|
||
def docker_config_folder(): | ||
docker_config_file = find_config_file() | ||
return None if not docker_config_file \ | ||
else os.path.dirname(os.path.abspath(docker_config_file)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters