Skip to content

Commit fc5a6a3

Browse files
committed
validate commit with image
1 parent 83b7573 commit fc5a6a3

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

internal/image/image.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (i *imageValidator) ValidateImage(ctx context.Context) (*validatedImage, er
9999
attestations, _, err := cosign.VerifyImageAttestations(ctx, i.reference, &i.checkOpts)
100100
attStatements := make([]attestation, 0, len(attestations))
101101
for _, att := range attestations {
102-
attStatement, err := signatureToAttestation(ctx, att)
102+
attStatement, err := SignatureToAttestation(ctx, att)
103103
if err != nil {
104104
return nil, err
105105
}
@@ -118,7 +118,7 @@ func (i *imageValidator) ValidateImage(ctx context.Context) (*validatedImage, er
118118

119119
}
120120

121-
func signatureToAttestation(ctx context.Context, signature oci.Signature) (attestation, error) {
121+
func SignatureToAttestation(ctx context.Context, signature oci.Signature) (attestation, error) {
122122
var att attestation
123123
payload, err := policy.AttestationToPayloadJSON(ctx, "slsaprovenance", signature)
124124
if err != nil {

internal/image/validate.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package image
1818

1919
import (
2020
"context"
21+
"encoding/json"
22+
"errors"
23+
"io/ioutil"
2124

2225
conftestOutput "github.com/open-policy-agent/conftest/output"
2326
log "github.com/sirupsen/logrus"
@@ -73,6 +76,20 @@ func ValidateImage(ctx context.Context, imageRef, policyConfiguration, publicKey
7376
return nil, err
7477
}
7578

79+
attStatements := make([]attestation, 0, len(a.Attestations()))
80+
for _, att := range a.Attestations() {
81+
attStatement, err := SignatureToAttestation(ctx, att)
82+
if err != nil {
83+
return nil, err
84+
}
85+
attStatements = append(attStatements, attStatement)
86+
}
87+
88+
commitFile := "/tmp/commit.json"
89+
writeCommitData(attStatements, commitFile)
90+
91+
inputs = append(inputs, commitFile)
92+
7693
results, err := a.Evaluator.Evaluate(ctx, inputs)
7794

7895
if err != nil {
@@ -85,3 +102,33 @@ func ValidateImage(ctx context.Context, imageRef, policyConfiguration, publicKey
85102

86103
return out, nil
87104
}
105+
106+
func writeCommitData(attestations []attestation, commitFile string) error {
107+
payloads := make([]string, 0, len(attestations))
108+
for _, att := range attestations {
109+
signoffSource, err := att.NewSignOffSource()
110+
if err != nil {
111+
return err
112+
}
113+
if signoffSource == nil {
114+
return errors.New("there is no signoff source in attestation")
115+
}
116+
117+
signOff, err := signoffSource.GetSignOff()
118+
if err != nil {
119+
return err
120+
}
121+
122+
if signOff != nil {
123+
payload, err := json.MarshalIndent(signOff, "", " ")
124+
if err != nil {
125+
return err
126+
}
127+
payloads = append(payloads, string(payload))
128+
}
129+
}
130+
131+
payloadJson, _ := json.Marshal(payloads)
132+
_ = ioutil.WriteFile(commitFile, payloadJson, 0644)
133+
return nil
134+
}

0 commit comments

Comments
 (0)