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

Track the policy data by source group #2181

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion acceptance/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func setupKeys(ctx context.Context, vars map[string]string, environment []string

vars[name+"_PUBLIC_KEY"] = key.Name()
// Handle some variations in indentation
vars[fmt.Sprintf("__________%s_PUBLIC_KEY", name)] = snaps.Indent(publicKey, 10)
vars[fmt.Sprintf("________%s_PUBLIC_KEY", name)] = snaps.Indent(publicKey, 10)

vars[name+"_PUBLIC_KEY_JSON"] = strings.ReplaceAll(publicKey, "\n", "\\n")

Expand Down
18 changes: 14 additions & 4 deletions cmd/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
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 @@
}

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 @@
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 @@
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,18 @@
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 {
// one copy will do. each evaluator runs for every component
if len(sgData) == 0 {
sgData = r.data
}
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 426 in cmd/validate/image.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/image.go#L425-L426

Added lines #L425 - L426 were not covered by tests
if allErrors != nil {
return allErrors
}
Expand All @@ -432,6 +441,7 @@
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 @@
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 @@
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
56 changes: 28 additions & 28 deletions features/__snapshots__/validate_image.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2383,34 +2383,34 @@ Error: success criteria not met
---

[Custom rule data:${TMPDIR}/custom-rule-data.yaml - 1]
- - config:
default_sigstore_opts:
certificate_identity: ""
certificate_identity_regexp: ""
certificate_oidc_issuer: ""
certificate_oidc_issuer_regexp: ""
ignore_rekor: false
public_key: |
${__________known_PUBLIC_KEY}
rekor_url: ${REKOR}
policy:
when_ns: 1401494400000000000
rule_data__configuration__:
custom: data1
- config:
default_sigstore_opts:
certificate_identity: ""
certificate_identity_regexp: ""
certificate_oidc_issuer: ""
certificate_oidc_issuer_regexp: ""
ignore_rekor: false
public_key: |
${__________known_PUBLIC_KEY}
rekor_url: ${REKOR}
policy:
when_ns: 1401494400000000000
rule_data__configuration__:
other: data2
- config:
default_sigstore_opts:
certificate_identity: ""
certificate_identity_regexp: ""
certificate_oidc_issuer: ""
certificate_oidc_issuer_regexp: ""
ignore_rekor: false
public_key: |
${________known_PUBLIC_KEY}
rekor_url: ${REKOR}
policy:
when_ns: 1401494400000000000
rule_data__configuration__:
custom: data1
- config:
default_sigstore_opts:
certificate_identity: ""
certificate_identity_regexp: ""
certificate_oidc_issuer: ""
certificate_oidc_issuer_regexp: ""
ignore_rekor: false
public_key: |
${________known_PUBLIC_KEY}
rekor_url: ${REKOR}
policy:
when_ns: 1401494400000000000
rule_data__configuration__:
other: data2

---

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
Loading