Skip to content

Commit

Permalink
Track the policy data by source group.
Browse files Browse the repository at this point in the history
Only collect the policy data once for each source
group. Before this the data was duplicated for
each component.

https://issues.redhat.com/browse/EC-1027
  • Loading branch information
joejstuart committed Nov 21, 2024
1 parent eceee3c commit 738ed22
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
17 changes: 13 additions & 4 deletions cmd/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
type result struct {
err error
component applicationsnapshot.Component
data []evaluator.Data
data map[string]evaluator.Data
policyInput []byte
}

Expand Down Expand Up @@ -343,6 +343,7 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
}

log.Debugf("Worker %d got a component %q", id, comp.ContainerImage)

out, err := validate(ctx, comp, data.spec, data.policy, evaluators, data.info)
res := result{
err: err,
Expand All @@ -366,8 +367,8 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
res.component.Signatures = out.Signatures
res.component.Attestations = out.Attestations
res.component.ContainerImage = out.ImageURL
res.data = out.Data
res.component.Attestations = out.Attestations
res.data = out.Data
res.policyInput = out.PolicyInput
}
res.component.Success = err == nil && len(res.component.Violations) == 0
Expand Down Expand Up @@ -400,7 +401,9 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
close(jobs)

var components []applicationsnapshot.Component
var manyData [][]evaluator.Data
var manyData []evaluator.Data
// we don't want to accumulate the data from each source group for each component
sgData := make(map[string]evaluator.Data)
var manyPolicyInput [][]byte
var allErrors error = nil
for i := 0; i < numComponents; i++ {
Expand All @@ -409,12 +412,17 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
e := fmt.Errorf("error validating image %s of component %s: %w", r.component.ContainerImage, r.component.Name, r.err)
allErrors = errors.Join(allErrors, e)
} else {
for key, val := range r.data {
sgData[key] = val
}

Check warning on line 417 in cmd/validate/image.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/image.go#L416-L417

Added lines #L416 - L417 were not covered by tests
components = append(components, r.component)
manyData = append(manyData, r.data)
manyPolicyInput = append(manyPolicyInput, r.policyInput)
}
}
close(results)
for _, val := range sgData {
manyData = append(manyData, val)
}

Check warning on line 425 in cmd/validate/image.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/image.go#L424-L425

Added lines #L424 - L425 were not covered by tests
if allErrors != nil {
return allErrors
}
Expand All @@ -432,6 +440,7 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
if err != nil {
return err
}

p := format.NewTargetParser(applicationsnapshot.JSON, format.Options{ShowSuccesses: showSuccesses}, cmd.OutOrStdout(), utils.FS(cmd.Context()))
utils.SetColorEnabled(data.noColor, data.forceColor)
if err := report.WriteAll(data.output, p); err != nil {
Expand Down
13 changes: 10 additions & 3 deletions cmd/validate/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
type result struct {
err error
input input.Input
data []evaluator.Data
data map[string]evaluator.Data

Check warning on line 115 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L115

Added line #L115 was not covered by tests
policyInput []byte
}

Expand Down Expand Up @@ -166,20 +166,27 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
close(ch)

var inputs []input.Input
var manyData [][]evaluator.Data
var manyData []evaluator.Data

Check warning on line 169 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L169

Added line #L169 was not covered by tests
var manyPolicyInput [][]byte
var allErrors error = nil
// we don't want to accumulate the data from each source group for each component
sgData := make(map[string]evaluator.Data)

Check warning on line 173 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L172-L173

Added lines #L172 - L173 were not covered by tests

for r := range ch {
if r.err != nil {
e := fmt.Errorf("error validating file %s: %w", r.input.FilePath, r.err)
allErrors = errors.Join(allErrors, e)
} else {
inputs = append(inputs, r.input)
manyData = append(manyData, r.data)
for key, val := range r.data {
sgData[key] = val
}

Check warning on line 183 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L181-L183

Added lines #L181 - L183 were not covered by tests
manyPolicyInput = append(manyPolicyInput, r.policyInput)
}
}
for _, val := range sgData {
manyData = append(manyData, val)
}

Check warning on line 189 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L187-L189

Added lines #L187 - L189 were not covered by tests
if allErrors != nil {
return allErrors
}
Expand Down
2 changes: 1 addition & 1 deletion features/__snapshots__/validate_image.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@ Error: success criteria not met
---

[Custom rule data:${TMPDIR}/custom-rule-data.yaml - 1]
- - config:
- config:
default_sigstore_opts:
certificate_identity: ""
certificate_identity_regexp: ""
Expand Down
9 changes: 6 additions & 3 deletions internal/image/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package image
import (
"context"
"encoding/json"
"fmt"
"runtime/trace"
"sort"
"time"
Expand All @@ -45,7 +46,7 @@ func ValidateImage(ctx context.Context, comp app.SnapshotComponent, snap *app.Sn

log.Debugf("Validating image %s", comp.ContainerImage)

out := &output.Output{ImageURL: comp.ContainerImage, Detailed: detailed, Policy: p}
out := &output.Output{ImageURL: comp.ContainerImage, Detailed: detailed, Policy: p, Data: make(map[string]evaluator.Data)}
a, err := application_snapshot_image.NewApplicationSnapshotImage(ctx, comp, p, *snap)
if err != nil {
log.Debug("Failed to create application snapshot image!")
Expand Down Expand Up @@ -114,7 +115,7 @@ func ValidateImage(ctx context.Context, comp app.SnapshotComponent, snap *app.Sn

var allResults []evaluator.Outcome

for _, e := range evaluators {
for idx, e := range evaluators {
// Todo maybe: Handle each one concurrently
target := evaluator.EvaluationTarget{Inputs: []string{inputPath}}
if digest, err := a.ResolveDigest(ctx); err != nil {
Expand All @@ -130,7 +131,9 @@ func ValidateImage(ctx context.Context, comp app.SnapshotComponent, snap *app.Sn
return nil, err
}
allResults = append(allResults, results...)
out.Data = append(out.Data, data)

key := fmt.Sprintf("%d", idx)
out.Data[key] = data
}

out.PolicyInput = inputJSON
Expand Down
2 changes: 1 addition & 1 deletion internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type Output struct {
Attestations []attestation.Attestation `json:"attestations,omitempty"`
ImageURL string `json:"-"`
Detailed bool `json:"-"`
Data []evaluator.Data `json:"-"`
Data map[string]evaluator.Data `json:"-"`
Policy policy.Policy `json:"-"`
PolicyInput []byte `json:"-"`
}
Expand Down

0 comments on commit 738ed22

Please sign in to comment.