-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from juliendoutre/julien.doutre/github-action…
…s-support Add new Github Action ecosystem
- Loading branch information
Showing
8 changed files
with
130 additions
and
20 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import Type | ||
|
||
from guarddog.analyzer.metadata import Detector | ||
|
||
GITHUB_ACTION_METADATA_RULES = {} | ||
|
||
classes: list[Type[Detector]] = [] | ||
|
||
for detectorClass in classes: | ||
detectorInstance = detectorClass() # type: ignore | ||
GITHUB_ACTION_METADATA_RULES[detectorInstance.get_name()] = detectorInstance |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import logging | ||
import os | ||
import pathlib | ||
import typing | ||
from urllib.parse import urlparse | ||
|
||
from guarddog.analyzer.analyzer import Analyzer | ||
from guarddog.ecosystems import ECOSYSTEM | ||
from guarddog.scanners.scanner import PackageScanner | ||
|
||
log = logging.getLogger("guarddog") | ||
|
||
|
||
class GithubActionScanner(PackageScanner): | ||
def __init__(self) -> None: | ||
super().__init__(Analyzer(ECOSYSTEM.GITHUB_ACTION)) | ||
|
||
def download_and_get_package_info(self, directory: str, package_name: str, version=None) -> typing.Tuple[dict, str]: | ||
repo = self._get_repo(package_name) | ||
tarball_url = self._get_git_tarball_url(repo, version) | ||
|
||
log.debug(f"Downloading GitHub Action source from {tarball_url}") | ||
|
||
file_extension = pathlib.Path(tarball_url).suffix | ||
if file_extension == "": | ||
file_extension = ".zip" | ||
|
||
zippath = os.path.join(directory, package_name.replace("/", "-") + file_extension) | ||
unzippedpath = zippath.removesuffix(file_extension) | ||
self.download_compressed(tarball_url, zippath, unzippedpath) | ||
|
||
return {}, unzippedpath | ||
|
||
def _get_repo(self, url: str) -> str: | ||
parsed_url = urlparse(url) | ||
|
||
if parsed_url.hostname and parsed_url.hostname != "github.com": | ||
raise ValueError("Invalid GitHub repo URL: " + url) | ||
|
||
path = parsed_url.path.removesuffix(".git").strip("/") | ||
|
||
if path.count("/") != 1: | ||
raise ValueError("Invalid GitHub repo name: " + path) | ||
|
||
return path | ||
|
||
def _get_git_tarball_url(self, repo: str, version=None) -> str: | ||
if not version: | ||
return f"https://api.github.com/repos/{repo}/zipball" | ||
else: | ||
return f"https://github.com/{repo}/archive/refs/tags/{version}.zip" |
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,22 @@ | ||
import os.path | ||
import tempfile | ||
|
||
import pytest | ||
|
||
from guarddog.scanners import GithubActionScanner | ||
|
||
|
||
def test_download_and_get_github_action_by_url(): | ||
scanner = GithubActionScanner() | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
data, path = scanner.download_and_get_package_info(tmpdirname, "https://github.com/actions/checkout.git", "v4.2.2") | ||
assert not data | ||
assert os.path.exists(os.path.join(tmpdirname, "https:--github.com-actions-checkout.git", "checkout-4.2.2", "package.json")) | ||
|
||
|
||
def test_download_and_get_github_action_by_name(): | ||
scanner = GithubActionScanner() | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
data, path = scanner.download_and_get_package_info(tmpdirname, "actions/checkout", "v4.2.2") | ||
assert not data | ||
assert os.path.exists(os.path.join(tmpdirname, "actions-checkout", "checkout-4.2.2", "package.json")) |