Skip to content
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

chore(trivy): add TRIVY_ENABLED option (closes #28) #31

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
XDEBUG_MODE: coverage
SYMFONY_DEPRECATIONS_HELPER: weak
GIT_MANAGER_DIR: /tmp/git-manager-test
TRIVY_ENABLED: false

- name: Upload coverage results to coveralls.io
if: github.ref == 'refs/heads/master' && matrix.php-version == '8.2'
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ CLI helpers to manage a set of git repositories.

## Parameters

| Name | Description | Default |
| ----------------- | ------------------------------------- | ------------------ |
| `GIT_MANAGER_DIR` | Directory containing git repositories | `/var/git-manager` |
| Name | Description | Default |
| ----------------- | ------------------------------------- | ----------------------- |
| `GIT_MANAGER_DIR` | Directory containing git repositories | `{projectDir}/var/data` |
| `TRIVY_ENABLED` | Enable/disable trivy scan | `true` |

## Setup

Expand Down
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
env(GIT_MANAGER_DIR): '%kernel.project_dir%/var/data'
env(TRIVY_ENABLED): 'true'

services:
_defaults:
Expand All @@ -8,6 +9,7 @@ services:
public: false
bind:
$dataDir: '%env(GIT_MANAGER_DIR)%'
$trivyEnabled: '%env(bool:TRIVY_ENABLED)%'

MBO\GitManager\:
resource: '../src/*'
Expand Down
16 changes: 11 additions & 5 deletions src/Git/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use MBO\GitManager\Git\Checker\LicenseChecker;
use MBO\GitManager\Git\Checker\ReadmeChecker;
use MBO\GitManager\Git\Checker\TrivyChecker;
use Psr\Log\LoggerInterface;

/**
* Analyze git repository to provide informations.
Expand All @@ -17,12 +18,14 @@ class Analyzer
*/
private $checkers;

public function __construct()
{
public function __construct(
bool $trivyEnabled,
private LoggerInterface $logger
) {
$this->checkers = [
new ReadmeChecker(),
new LicenseChecker(),
new TrivyChecker(),
new ReadmeChecker($logger),
new LicenseChecker($logger),
new TrivyChecker($trivyEnabled, $logger),
];
}

Expand All @@ -33,6 +36,9 @@ public function __construct()
*/
public function getMetadata(GitRepository $gitRepository): array
{
$this->logger->debug('[Analyser] retrieve git metadata...', [
'repository' => $gitRepository->getWorkingDir(),
]);
$metadata = [
'size' => $gitRepository->getSize() * 1024,
];
Expand Down
11 changes: 11 additions & 0 deletions src/Git/Checker/LicenseChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

use Gitonomy\Git\Repository as GitRepository;
use MBO\GitManager\Git\CheckerInterface;
use Psr\Log\LoggerInterface;

/**
* Ensure that LICENSE file is present.
*/
class LicenseChecker implements CheckerInterface
{
public function __construct(private LoggerInterface $logger)
{
}

public const LICENSE_FILENAMES = [
'LICENSE',
'LICENSE.md',
Expand All @@ -22,6 +27,12 @@ public function getName(): string

public function check(GitRepository $gitRepository): bool|string
{
$this->logger->debug('[{checker}] look for license file...', [
'checker' => $this->getName(),
'repository' => $gitRepository->getWorkingDir(),
'expected' => static::LICENSE_FILENAMES,
]);

$workingDir = $gitRepository->getWorkingDir();
foreach (static::LICENSE_FILENAMES as $filename) {
$expectedPath = $workingDir.DIRECTORY_SEPARATOR.$filename;
Expand Down
10 changes: 10 additions & 0 deletions src/Git/Checker/ReadmeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@

use Gitonomy\Git\Repository as GitRepository;
use MBO\GitManager\Git\CheckerInterface;
use Psr\Log\LoggerInterface;

/**
* Ensure that README file is present.
*/
class ReadmeChecker implements CheckerInterface
{
public function __construct(private LoggerInterface $logger)
{
}

public function getName(): string
{
return 'readme';
}

public function check(GitRepository $gitRepository): bool
{
$this->logger->debug('[{checker}] look for README.md file...', [
'checker' => $this->getName(),
'repository' => $gitRepository->getWorkingDir(),
]);

$workingDir = $gitRepository->getWorkingDir();
$readmePath = $workingDir.DIRECTORY_SEPARATOR.'README.md';

Expand Down
35 changes: 34 additions & 1 deletion src/Git/Checker/TrivyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gitonomy\Git\Repository as GitRepository;
use MBO\GitManager\Git\CheckerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

Expand All @@ -14,6 +15,15 @@ class TrivyChecker implements CheckerInterface
{
public const SEVERITIES = ['HIGH', 'CRITICAL'];

private bool $enabled;

public function __construct(
bool $trivyEnabled,
private LoggerInterface $logger
) {
$this->enabled = $trivyEnabled && $this->isAvailable();
}

public function getName(): string
{
return 'trivy';
Expand All @@ -22,6 +32,21 @@ public function getName(): string
public function check(GitRepository $gitRepository): mixed
{
$workingDir = $gitRepository->getWorkingDir();

if (!$this->enabled) {
$this->logger->debug('[{checker}] skipped (disabled)', [
'checker' => $this->getName(),
'repository' => $workingDir,
]);

return null;
}

$this->logger->debug('[{checker}] run trivy fs on repository...', [
'checker' => $this->getName(),
'repository' => $workingDir,
]);

$trivyReportPath = $workingDir.'/.trivy.json';
$process = new Process([
'trivy',
Expand Down Expand Up @@ -105,10 +130,18 @@ public function getSummary(array $vulnerabilities): array
public function isAvailable(): bool
{
try {
$this->getVersion();
$version = $this->getVersion();
$this->logger->info('[{checker}] trivy executable found (version={trivy_version})', [
'checker' => $this->getName(),
'trivy_version' => $version,
]);

return true;
} catch (\Exception $e) {
$this->logger->warning('[{checker}] trivy not found, scan disabled', [
'checker' => $this->getName(),
]);

return false;
}
}
Expand Down
Loading