Skip to content

Commit cac5f24

Browse files
committed
refactor: use protojson for policy evaluations bundle serialization
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent d0e5434 commit cac5f24

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

app/cli/pkg/action/attestation_push.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
protobundle "github.com/sigstore/protobuf-specs/gen/pb-go/bundle/v1"
3939
"google.golang.org/grpc"
4040
"google.golang.org/protobuf/encoding/protojson"
41-
"google.golang.org/protobuf/proto"
4241
"google.golang.org/protobuf/types/known/timestamppb"
4342
)
4443

@@ -365,15 +364,15 @@ func uploadPolicyEvaluationsBundle(ctx context.Context, evaluations []*v1.Policy
365364
}
366365

367366
bundle := &v1.PolicyEvaluationBundle{Evaluations: evaluations}
368-
data, err := proto.Marshal(bundle)
367+
data, err := protojson.Marshal(bundle)
369368
if err != nil {
370369
return nil, fmt.Errorf("marshaling policy evaluation bundle: %w", err)
371370
}
372371

373372
hexDigest := fmt.Sprintf("%x", sha256.Sum256(data))
374373
digest := fmt.Sprintf("sha256:%s", hexDigest)
375374

376-
if _, err := uploader.Upload(ctx, bytes.NewReader(data), "policy-evaluations.pb", digest); err != nil {
375+
if _, err := uploader.Upload(ctx, bytes.NewReader(data), "policy-evaluations.json", digest); err != nil {
377376
return nil, fmt.Errorf("uploading policy evaluation bundle: %w", err)
378377
}
379378

app/cli/pkg/action/attestation_push_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/stretchr/testify/assert"
2929
"github.com/stretchr/testify/mock"
3030
"github.com/stretchr/testify/require"
31-
"google.golang.org/protobuf/proto"
31+
"google.golang.org/protobuf/encoding/protojson"
3232
)
3333

3434
func TestUploadPolicyEvaluationsBundle(t *testing.T) {
@@ -64,9 +64,9 @@ func TestUploadPolicyEvaluationsBundle(t *testing.T) {
6464
uploader: func(t *testing.T) casclient.Uploader {
6565
t.Helper()
6666
m := casclientmock.NewUploader(t)
67-
m.On("Upload", mock.Anything, mock.Anything, "policy-evaluations.pb", mock.MatchedBy(func(digest string) bool {
67+
m.On("Upload", mock.Anything, mock.Anything, "policy-evaluations.json", mock.MatchedBy(func(digest string) bool {
6868
return len(digest) > 7 && digest[:7] == "sha256:"
69-
})).Return(&casclient.UpDownStatus{Filename: "policy-evaluations.pb"}, nil)
69+
})).Return(&casclient.UpDownStatus{Filename: "policy-evaluations.json"}, nil)
7070
return m
7171
},
7272
wantRef: true,
@@ -113,7 +113,7 @@ func TestUploadPolicyEvaluationsBundle(t *testing.T) {
113113

114114
// Verify the digest matches what we'd expect from serializing the bundle
115115
bundle := &v1.PolicyEvaluationBundle{Evaluations: tc.evaluations}
116-
data, err := proto.Marshal(bundle)
116+
data, err := protojson.Marshal(bundle)
117117
require.NoError(t, err)
118118
expectedDigest := fmt.Sprintf("%x", sha256.Sum256(data))
119119
assert.Equal(t, expectedDigest, ref.Digest["sha256"])

pkg/attestation/renderer/chainloop/v02.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
// Replace custom material type with https://github.com/in-toto/attestation/blob/main/spec/v1.0/resource_descriptor.md
3939
const PredicateTypeV02 = "chainloop.dev/attestation/v0.2"
4040
const AttPolicyEvaluation = "CHAINLOOP.ATTESTATION"
41-
const PolicyEvaluationsBundleMediaType = "application/vnd.chainloop.policy-evaluations.v1+proto"
41+
const PolicyEvaluationsBundleMediaType = "application/vnd.chainloop.policy-evaluations.v1+json"
4242

4343
type ProvenancePredicateV02 struct {
4444
*ProvenancePredicateCommon

0 commit comments

Comments
 (0)