Skip to content

Commit fd7d93e

Browse files
authored
fix: new cas uploader digest (#50)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent 76b63c9 commit fd7d93e

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

app/controlplane/internal/biz/attestation.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ package biz
1818
import (
1919
"bytes"
2020
"context"
21-
"crypto/sha256"
2221
"encoding/json"
2322
"fmt"
2423
"io"
2524

2625
"github.com/chainloop-dev/chainloop/internal/servicelogger"
2726
"github.com/go-kratos/kratos/v2/log"
27+
28+
cr_v1 "github.com/google/go-containerregistry/pkg/v1"
2829
"github.com/secure-systems-lab/go-securesystemslib/dsse"
2930
)
3031

@@ -71,20 +72,21 @@ func (uc *AttestationUseCase) FetchFromStore(ctx context.Context, secretID, dige
7172
return &Attestation{Envelope: &envelope}, nil
7273
}
7374

74-
func (uc *AttestationUseCase) UploadToCAS(ctx context.Context, envelope *dsse.Envelope, secretID, workflowRunID string) (string, error) {
75+
func (uc *AttestationUseCase) UploadToCAS(ctx context.Context, envelope *dsse.Envelope, secretID, workflowRunID string) (*cr_v1.Hash, error) {
7576
filename := fmt.Sprintf("attestation-%s.json", workflowRunID)
7677
jsonContent, err := json.Marshal(envelope)
7778
if err != nil {
78-
return "", fmt.Errorf("marshaling the envelope: %w", err)
79+
return nil, fmt.Errorf("marshaling the envelope: %w", err)
7980
}
8081

81-
hash := sha256.New()
82-
hash.Write(jsonContent)
83-
digest := fmt.Sprintf("%x", hash.Sum(nil))
82+
h, _, err := cr_v1.SHA256(bytes.NewBuffer(jsonContent))
83+
if err != nil {
84+
return nil, fmt.Errorf("calculating the digest: %w", err)
85+
}
8486

85-
if err := uc.CASClient.Upload(ctx, secretID, bytes.NewBuffer(jsonContent), filename, digest); err != nil {
86-
return "", fmt.Errorf("uploading to CAS: %w", err)
87+
if err := uc.CASClient.Upload(ctx, secretID, bytes.NewBuffer(jsonContent), filename, h.String()); err != nil {
88+
return nil, fmt.Errorf("uploading to CAS: %w", err)
8789
}
8890

89-
return digest, nil
91+
return &h, nil
9092
}

app/controlplane/internal/biz/attestation_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
2626
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz/mocks"
27+
cr_v1 "github.com/google/go-containerregistry/pkg/v1"
2728
"github.com/google/uuid"
2829
"github.com/secure-systems-lab/go-securesystemslib/dsse"
2930
"github.com/stretchr/testify/assert"
@@ -35,32 +36,31 @@ import (
3536
var runID = uuid.NewString()
3637
var envelope = &dsse.Envelope{}
3738

38-
const expectedDigest = "f845058d865c3d4d491c9019f6afe9c543ad2cd11b31620cc512e341fb03d3d8"
39+
var expectedDigest = cr_v1.Hash{Algorithm: "sha256", Hex: "f845058d865c3d4d491c9019f6afe9c543ad2cd11b31620cc512e341fb03d3d8"}
3940

4041
func (s *attestationTestSuite) TestUploadToCAS() {
4142
ctx := context.Background()
4243
s.casClient.On(
4344
"Upload", ctx, "my-secret", mock.Anything,
44-
fmt.Sprintf("attestation-%s.json", runID), expectedDigest,
45+
fmt.Sprintf("attestation-%s.json", runID), expectedDigest.String(),
4546
).Return(nil)
4647

4748
gotDigest, err := s.uc.UploadToCAS(ctx, envelope, "my-secret", runID)
4849
assert.NoError(s.T(), err)
49-
assert.Equal(s.T(), expectedDigest, gotDigest)
50+
assert.Equal(s.T(), &expectedDigest, gotDigest)
5051
}
5152

5253
func (s *attestationTestSuite) TestFetchFromStore() {
5354
want := &biz.Attestation{Envelope: &dsse.Envelope{}}
54-
5555
ctx := context.Background()
56-
s.casClient.On("Download", ctx, "my-secret", mock.Anything, expectedDigest).Return(nil).Run(
56+
s.casClient.On("Download", ctx, "my-secret", mock.Anything, expectedDigest.String()).Return(nil).Run(
5757
func(args mock.Arguments) {
5858
buf := args.Get(2).(io.Writer)
5959
err := json.NewEncoder(buf).Encode(want)
6060
require.NoError(s.T(), err)
6161
})
6262

63-
got, err := s.uc.FetchFromStore(ctx, "my-secret", expectedDigest)
63+
got, err := s.uc.FetchFromStore(ctx, "my-secret", expectedDigest.String())
6464
assert.NoError(s.T(), err)
6565
assert.Equal(s.T(), want, got)
6666
}

app/controlplane/internal/service/attestation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (s *AttestationService) Store(ctx context.Context, req *cpAPI.AttestationSe
190190
}
191191

192192
// associate the attestation stored in the CAS with the workflow run
193-
if err := s.wrUseCase.AssociateAttestation(ctx, req.WorkflowRunId, &biz.AttestationRef{Sha256: digest, SecretRef: repo.SecretName}); err != nil {
193+
if err := s.wrUseCase.AssociateAttestation(ctx, req.WorkflowRunId, &biz.AttestationRef{Sha256: digest.Hex, SecretRef: repo.SecretName}); err != nil {
194194
return err
195195
}
196196

app/controlplane/internal/service/workflowrun.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package service
1717

1818
import (
1919
"context"
20+
"fmt"
2021

2122
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
2223
craftingpb "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
@@ -115,7 +116,7 @@ func (s *WorkflowRunService) View(ctx context.Context, req *pb.WorkflowRunServic
115116
var attestation *biz.Attestation
116117
// Download the attestation if the workflow run is successful
117118
if run.AttestationRef != nil {
118-
attestation, err = s.attestationUseCase.FetchFromStore(ctx, run.AttestationRef.SecretRef, run.AttestationRef.Sha256)
119+
attestation, err = s.attestationUseCase.FetchFromStore(ctx, run.AttestationRef.SecretRef, fmt.Sprintf("sha256:%s", run.AttestationRef.Sha256))
119120
if err != nil {
120121
// NOTE: For now we don't return an error if the attestation is not found
121122
// since we do not have a good error recovery in place for assets

0 commit comments

Comments
 (0)