-
Notifications
You must be signed in to change notification settings - Fork 7
Generate composer.lock files #471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
faizanH
wants to merge
37
commits into
main
Choose a base branch
from
feature/generate-php-lockfile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
0314d9f
adding composer.lock download functionality to trivy sca
faizanH df03719
add test case
faizanH 627c5e8
formatting
faizanH e96fe48
updating dind to have php and curl
faizanH deeaae7
adding required packages for PHP
faizanH 8dcd3fe
adding one more PHP requirement
faizanH 18ac459
trying a multi stage build
faizanH 47ec973
testing
faizanH 8fafd6c
adding more php depdencies
faizanH 0416d29
removing incorrect package
faizanH 7dc3078
removing improper import
faizanH 8d62552
invoke the script
faizanH bf65480
attempting to create docker container in plugin directly
faizanH ac6c8f8
remove php from dockerfile
faizanH bce7432
adding config in settings.py
faizanH f165e10
adding logs for debugging
faizanH dfe927a
commenting out return
faizanH acb39fb
trying to get logs to appear to show file directory
faizanH 880247c
Merge branch 'main' into feature/generate-php-lockfile
faizanH 32ab4e2
tring to enter container
faizanH b849677
slight tweaks
faizanH a60c99d
debug
faizanH 22eb123
adding more debugs
faizanH 4ebbe82
remove possible error causing
faizanH 83b314c
adding more debugging
faizanH 14eac8c
using different temp volume
faizanH 8cab6cf
trying different approach
faizanH 2107bcf
drawing inspo from other plugin
faizanH d8e3de7
Merge branch 'main' into feature/generate-php-lockfile
faizanH 83de2b2
debugging
mdfleury-wbd 9d9750a
testing working dir
mdfleury-wbd d21e59c
changing path again
mdfleury-wbd 1e73976
another try
mdfleury-wbd 0d26b9b
adding missed file
mdfleury-wbd 3b53779
renaming npm file
mdfleury-wbd c077fba
using mnt
mdfleury-wbd 76be831
trying working mnt
mdfleury-wbd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
backend/engine/plugins/lib/trivy_common/generate_composer_locks.py
This file contains hidden or 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,101 @@ | ||
| import os | ||
| from glob import glob | ||
| from engine.plugins.lib import utils | ||
| import docker | ||
| import uuid | ||
| from typing import Optional | ||
|
|
||
| logger = utils.setup_logging("trivy_sca") | ||
| docker_client = docker.from_env() | ||
|
|
||
|
|
||
| def install_package_files(path: str, include_dev: bool, sub_path: str, working_src: str, root_path: str): | ||
| # sub_path: absolute path to the composer project inside the parent container (e.g. /tmp/work/foo/bar) | ||
| # temp_vol_name: Docker volume name (e.g. artemis-plugin-temp-xxxx) | ||
| # temp_vol_mount: mount path inside the plugin container (e.g. /tmp/work) | ||
| # root_path: the original root for logging | ||
|
|
||
| rel_subdir = os.path.relpath(sub_path, path) | ||
| abs_path_in_container = os.path.join("/app", rel_subdir) | ||
| logger.info(f"Mounting volume: {working_src} to /app in composer container") | ||
| logger.info(f"Target subdir in container: {abs_path_in_container}") | ||
| logger.info(f"composer.json: {os.path.join(sub_path, 'composer.json')}") | ||
| logger.info(f"composer.json exists: {os.path.exists(os.path.join(sub_path, 'composer.json'))}") | ||
|
|
||
| composer_cmd = ( | ||
| "composer --version && " | ||
| "ls -l && " | ||
| "cat composer.json && " | ||
| "composer install --no-scripts -q" | ||
| " && ls -l composer.lock && ls -l" | ||
| ) | ||
|
|
||
| # if not include_dev: | ||
| # composer_cmd += " --no-dev" | ||
|
|
||
| COMPOSER_IMG = "composer:2.8.11" | ||
| container_name = f"composer_runner_{uuid.uuid4().hex[:8]}" | ||
| container_mount_path = "/app" | ||
|
|
||
| try: | ||
| container = docker_client.containers.run( | ||
| COMPOSER_IMG, | ||
| name=container_name, | ||
| command=["sh", "-c", composer_cmd], | ||
| volumes={ | ||
| working_src: {"bind": container_mount_path, "mode": "rw"}, | ||
| }, | ||
| working_dir=abs_path_in_container, | ||
| auto_remove=False, | ||
| stdout=True, | ||
| stderr=True, | ||
| detach=True, | ||
| ) | ||
|
|
||
| result = container.wait() | ||
| logs = container.logs(stdout=True, stderr=True).decode("utf-8") | ||
| logger.info(f"Container logs for {sub_path.replace(root_path, '')}:\n{logs}") | ||
| logger.info(f"Container exit code: {result.get('StatusCode')}") | ||
| container.remove() | ||
| except Exception as e: | ||
| logger.error(f"Error running composer install in Docker: {e}") | ||
|
|
||
| # Check if composer.lock was created | ||
| lockfile = os.path.join(sub_path, "composer.lock") | ||
| if not os.path.exists(lockfile): | ||
| logger.error(f"composer.lock was not created in {sub_path}") | ||
|
|
||
| return | ||
|
|
||
|
|
||
| def check_composer_package_files( | ||
| path: str, working_src: str, include_dev: bool, root_path: Optional[str] = None | ||
| ) -> tuple: | ||
| """ | ||
| Find all composer.json files in the repo and build lock files for them if missing. | ||
| """ | ||
| errors = [] | ||
| alerts = [] | ||
| logger.info("Searching %s for composer files", path) | ||
| files = glob(f"{path}/**/composer.json", recursive=True) | ||
| logger.info("Found %d composer.json files", len(files)) | ||
|
|
||
| if len(files) == 0: | ||
| return errors, alerts | ||
|
|
||
| paths = set() | ||
| for filename in files: | ||
| paths.add(os.path.dirname(filename)) | ||
|
|
||
| for sub_path in paths: | ||
| lockfile = os.path.join(sub_path, "composer.lock") | ||
| lockfile_missing = not os.path.exists(lockfile) | ||
| if lockfile_missing: | ||
| msg = ( | ||
| f"No composer.lock file was found in path {sub_path.replace(path, '')}." | ||
| " Please consider creating a composer.lock file for this project." | ||
| ) | ||
| logger.warning(msg) | ||
| alerts.append(msg) | ||
| install_package_files(path, include_dev, sub_path, working_src, root_path or working_src) | ||
| return errors, alerts |
File renamed without changes.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor) Since our JSON results can be quite large, we should switch from using
json.dumps(which renders the whole JSON to a string first) tojson.dumpthat renders the output directly to the output, e.g.: