-
Notifications
You must be signed in to change notification settings - Fork 0
feat: audit command #98
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
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
14f7d9e
o
keroxp 3073a8f
a
keroxp 544e3b6
a
keroxp f1b9b85
a
keroxp adacf58
Update scan.go
keroxp 97f4461
test
keroxp 1526180
Update printer.go
keroxp 7b54f9d
Update cli/cage/scan/printer.go
keroxp 822c10a
test
keroxp 7264d28
Merge branch 'feat-scan' of https://github.com/loilo-inc/canarycage i…
keroxp bc34201
Update printer_test.go
keroxp fa5cd67
test
keroxp 268af2b
tets
keroxp 92965c9
a
keroxp 732c6a0
w
keroxp 50c9d81
test
keroxp 43f643f
Update types_test.go
keroxp 74b2585
a
keroxp 7a31f5a
a
keroxp 7f960ae
a
keroxp cbba2b7
PR
keroxp b11b96d
a
keroxp 43824a3
a
keroxp 5d24307
add package name
keroxp 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package awsiface | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| ) | ||
|
|
||
| // coverage cheat: always use MustLoadConfig to avoid error handling repetition | ||
| func MustLoadConfig(ctx context.Context, opts ...func(*config.LoadOptions) error) aws.Config { | ||
| cfg, err := config.LoadDefaultConfig(ctx, opts...) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return cfg | ||
| } |
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,59 @@ | ||
| package awsiface | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| ) | ||
|
|
||
| func TestMustLoadConfig_Success(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| // This should not panic in normal circumstances | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| t.Errorf("MustLoadConfig panicked unexpectedly: %v", r) | ||
| } | ||
| }() | ||
|
|
||
| cfg := MustLoadConfig(ctx) | ||
|
|
||
| if cfg.Region == "" && cfg.Credentials == nil { | ||
| t.Log("Config loaded (region or credentials may be empty in test environment)") | ||
| } | ||
| } | ||
|
|
||
| func TestMustLoadConfig_WithOptions(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| t.Errorf("MustLoadConfig with options panicked unexpectedly: %v", r) | ||
| } | ||
| }() | ||
|
|
||
| cfg := MustLoadConfig(ctx, config.WithRegion("us-west-2")) | ||
|
|
||
| if cfg.Region != "us-west-2" { | ||
| t.Errorf("Expected region us-west-2, got %s", cfg.Region) | ||
| } | ||
| } | ||
|
|
||
| func TestMustLoadConfig_Panic(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| t.Error("Expected MustLoadConfig to panic with invalid option, but it didn't") | ||
| } | ||
| }() | ||
|
|
||
| // Pass an option that returns an error to trigger panic | ||
| invalidOpt := func(*config.LoadOptions) error { | ||
| return errors.New("forced error") | ||
| } | ||
|
|
||
| MustLoadConfig(ctx, invalidOpt) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package audit | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| ecrtypes "github.com/aws/aws-sdk-go-v2/service/ecr/types" | ||
| "github.com/loilo-inc/canarycage/logger" | ||
| ) | ||
|
|
||
| type aggregater struct { | ||
| cves map[string]ecrtypes.ImageScanFinding | ||
| cveToSeverity map[string]string | ||
| cveToContainers map[string][]string | ||
| // container name to summaries | ||
| summaries map[string][]*ScanResultSummary | ||
| } | ||
|
|
||
| func NewAggregater() *aggregater { | ||
| return &aggregater{ | ||
| cves: make(map[string]ecrtypes.ImageScanFinding), | ||
| cveToSeverity: make(map[string]string), | ||
| cveToContainers: make(map[string][]string), | ||
| summaries: make(map[string][]*ScanResultSummary)} | ||
| } | ||
|
|
||
| func (a *aggregater) Add(r *ScanResult) { | ||
| container := r.ContainerName() | ||
| if r.Err != nil { | ||
| a.summaries[container] = append(a.summaries[container], &ScanResultSummary{ | ||
| ContainerName: container, | ||
| Status: "ERROR", | ||
| }) | ||
| return | ||
| } else if r.ImageScanFindings == nil { | ||
| a.summaries[container] = append(a.summaries[container], &ScanResultSummary{ | ||
| ContainerName: container, | ||
| Status: "N/A", | ||
| }) | ||
| return | ||
| } | ||
| summary := summaryScanResult(r) | ||
| a.summaries[container] = append(a.summaries[container], summary) | ||
| for _, f := range r.ImageScanFindings.Findings { | ||
| if _, exists := a.cves[*f.Name]; !exists { | ||
| a.cves[*f.Name] = f | ||
| a.cveToSeverity[*f.Name] = string(f.Severity) | ||
| a.cveToContainers[*f.Name] = append(a.cveToContainers[*f.Name], container) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type AggregateResult struct { | ||
| CriticalCount int32 | ||
| HighCount int32 | ||
| MediumCount int32 | ||
| LowCount int32 | ||
| InfoCount int32 | ||
| TotalCount int32 | ||
| HighestSeverity ecrtypes.FindingSeverity | ||
| } | ||
|
|
||
| func (a *aggregater) SummarizeTotal() *AggregateResult { | ||
| result := &AggregateResult{} | ||
| highestServity := ecrtypes.FindingSeverityInformational | ||
|
keroxp marked this conversation as resolved.
Outdated
|
||
| for cve := range a.cves { | ||
| severity := a.cveToSeverity[cve] | ||
| switch severity { | ||
| case string(ecrtypes.FindingSeverityCritical): | ||
| result.CriticalCount++ | ||
| case string(ecrtypes.FindingSeverityHigh): | ||
| result.HighCount++ | ||
| case string(ecrtypes.FindingSeverityMedium): | ||
| result.MediumCount++ | ||
| case string(ecrtypes.FindingSeverityLow): | ||
| result.LowCount++ | ||
| case string(ecrtypes.FindingSeverityInformational): | ||
| result.InfoCount++ | ||
| } | ||
| } | ||
| if result.CriticalCount > 0 { | ||
| highestServity = ecrtypes.FindingSeverityCritical | ||
| } else if result.HighCount > 0 { | ||
| highestServity = ecrtypes.FindingSeverityHigh | ||
| } else if result.MediumCount > 0 { | ||
| highestServity = ecrtypes.FindingSeverityMedium | ||
| } else { | ||
| highestServity = ecrtypes.FindingSeverityLow | ||
| } | ||
| result.HighestSeverity = highestServity | ||
|
keroxp marked this conversation as resolved.
Outdated
|
||
| result.TotalCount = int32(len(a.cves)) | ||
| return result | ||
| } | ||
|
|
||
| type SeverityCount struct { | ||
| Severity ecrtypes.FindingSeverity | ||
| Count int | ||
| } | ||
|
|
||
| func (a *AggregateResult) SeverityCounts() []SeverityCount { | ||
| return []SeverityCount{ | ||
| {Severity: ecrtypes.FindingSeverityInformational, Count: int(a.InfoCount)}, | ||
| {Severity: ecrtypes.FindingSeverityLow, Count: int(a.LowCount)}, | ||
| {Severity: ecrtypes.FindingSeverityMedium, Count: int(a.MediumCount)}, | ||
| {Severity: ecrtypes.FindingSeverityHigh, Count: int(a.HighCount)}, | ||
| {Severity: ecrtypes.FindingSeverityCritical, Count: int(a.CriticalCount)}, | ||
| } | ||
| } | ||
|
|
||
| func (a *aggregater) TotalCVECount() int { | ||
| return len(a.cves) | ||
| } | ||
|
|
||
| func (a *aggregater) CriticalCves() []ecrtypes.ImageScanFinding { | ||
| return a.filterCvesBySeverity(ecrtypes.FindingSeverityCritical) | ||
| } | ||
|
|
||
| func (a *aggregater) HighCves() []ecrtypes.ImageScanFinding { | ||
| return a.filterCvesBySeverity(ecrtypes.FindingSeverityHigh) | ||
| } | ||
|
|
||
| func (a *aggregater) MediumCves() []ecrtypes.ImageScanFinding { | ||
| return a.filterCvesBySeverity(ecrtypes.FindingSeverityMedium) | ||
| } | ||
|
|
||
| func (a *aggregater) filterCvesBySeverity(severity ecrtypes.FindingSeverity) []ecrtypes.ImageScanFinding { | ||
| var cves []ecrtypes.ImageScanFinding | ||
| for cve, sev := range a.cveToSeverity { | ||
| if sev == string(severity) { | ||
| cves = append(cves, a.cves[cve]) | ||
| } | ||
| } | ||
| return cves | ||
| } | ||
|
|
||
| func (a *aggregater) GetVulnContainers(cveName string) []string { | ||
| containersSet, exists := a.cveToContainers[cveName] | ||
| if !exists { | ||
| return []string{} | ||
| } | ||
| return containersSet | ||
| } | ||
|
|
||
| type severityPrinter struct { | ||
| severity ecrtypes.FindingSeverity | ||
| color logger.Color | ||
| } | ||
|
|
||
| func (s *severityPrinter) Sprintf(format string, a ...any) string { | ||
| switch s.severity { | ||
| case ecrtypes.FindingSeverityCritical: | ||
| return s.color.Magentaf(format, a...) | ||
| case ecrtypes.FindingSeverityHigh: | ||
| return s.color.Redf(format, a...) | ||
| case ecrtypes.FindingSeverityMedium: | ||
| return s.color.Yellowf(format, a...) | ||
| default: | ||
| return fmt.Sprintf(format, a...) | ||
| } | ||
| } | ||
|
|
||
| func (s *severityPrinter) BSprintf(format string, a ...any) string { | ||
| return s.color.Bold(s.Sprintf(format, a...)) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.