Skip to content

Commit 8625aea

Browse files
authored
fix(dispatcher): only download materials that are in the CAS (#145)
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent e25e6eb commit 8625aea

17 files changed

Lines changed: 115 additions & 129 deletions

File tree

app/cli/api/attestation/v1/crafting_state.pb.go

Lines changed: 69 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/cli/api/attestation/v1/crafting_state.pb.validate.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/cli/api/attestation/v1/crafting_state.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ message Attestation {
4141
google.protobuf.Timestamp added_at = 5;
4242
workflowcontract.v1.CraftingSchema.Material.MaterialType material_type = 6;
4343

44+
// Whether the material has been uploaded to the CAS
45+
bool uploaded_to_cas = 7;
46+
4447
message KeyVal {
4548
string id = 1 [(validate.rules).string.min_len = 1];
4649
string value = 2 [(validate.rules).string.min_len = 1];

app/controlplane/internal/data/workflowrun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (r *WorkflowRunRepo) List(ctx context.Context, orgID, workflowID uuid.UUID,
138138

139139
workflowRuns, err := wfRunsQuery.All(ctx)
140140
if err != nil {
141-
return
141+
return nil, "", err
142142
}
143143

144144
for i, wr := range workflowRuns {

app/controlplane/internal/dispatcher/dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (d *FanOutDispatcher) Run(ctx context.Context, envelope *dsse.Envelope, org
199199
// Retrieve material content
200200
content := []byte(material.Value)
201201
// It's a downloadable so we retrieve and override the content variable
202-
if material.Hash != nil {
202+
if material.Hash != nil && material.UploadedToCAS {
203203
digest := material.Hash.String()
204204
d.log.Infow("msg", "downloading material", "workflowID", workflowID, "materialType", material.Type, "name", material.Name)
205205
buf := bytes.NewBuffer(nil)

internal/attestation/crafter/materials/artifact.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ package materials
1818
import (
1919
"context"
2020
"fmt"
21-
"time"
2221

2322
api "github.com/chainloop-dev/chainloop/app/cli/api/attestation/v1"
2423
schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
2524
"github.com/chainloop-dev/chainloop/internal/casclient"
2625
"github.com/rs/zerolog"
27-
"google.golang.org/protobuf/types/known/timestamppb"
2826
)
2927

3028
type ArtifactCrafter struct {
@@ -43,21 +41,5 @@ func NewArtifactCrafter(schema *schemaapi.CraftingSchema_Material, uploader casc
4341

4442
// Craft will calculate the digest of the artifact, simulate an upload and return the material definition
4543
func (i *ArtifactCrafter) Craft(ctx context.Context, artifactPath string) (*api.Attestation_Material, error) {
46-
result, err := i.uploader.UploadFile(ctx, artifactPath)
47-
if err != nil {
48-
i.logger.Debug().Err(err)
49-
return nil, err
50-
}
51-
52-
res := &api.Attestation_Material{
53-
AddedAt: timestamppb.New(time.Now()),
54-
MaterialType: i.input.Type,
55-
M: &api.Attestation_Material_Artifact_{
56-
Artifact: &api.Attestation_Material_Artifact{
57-
Id: i.input.Name, Digest: result.Digest, Name: result.Filename, IsSubject: i.input.Output,
58-
},
59-
},
60-
}
61-
62-
return res, nil
44+
return uploadAndCraft(ctx, i.input, i.uploader, artifactPath)
6345
}

internal/attestation/crafter/materials/artifact_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func TestArtifactCraft(t *testing.T) {
8787
got, err := crafter.Craft(context.TODO(), "file.txt")
8888
assert.NoError(err)
8989
assert.Equal(contractAPI.CraftingSchema_Material_ARTIFACT.String(), got.MaterialType.String())
90+
assert.True(got.UploadedToCas)
9091
assert.WithinDuration(time.Now(), got.AddedAt.AsTime(), 5*time.Second)
9192

9293
// The result includes the digest reference

internal/attestation/crafter/materials/cyclonedxjson.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ import (
1919
"context"
2020
"fmt"
2121
"os"
22-
"time"
2322

2423
cdx "github.com/CycloneDX/cyclonedx-go"
2524
api "github.com/chainloop-dev/chainloop/app/cli/api/attestation/v1"
2625
schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
2726
"github.com/chainloop-dev/chainloop/internal/casclient"
2827
"github.com/rs/zerolog"
29-
"google.golang.org/protobuf/types/known/timestamppb"
3028
)
3129

3230
type CyclonedxJSONCrafter struct {
@@ -64,21 +62,5 @@ func (i *CyclonedxJSONCrafter) Craft(ctx context.Context, filePath string) (*api
6462
return nil, fmt.Errorf("invalid cyclonedx sbom file: %w", ErrInvalidMaterialType)
6563
}
6664

67-
result, err := i.uploader.UploadFile(ctx, filePath)
68-
if err != nil {
69-
i.logger.Debug().Err(err)
70-
return nil, err
71-
}
72-
73-
res := &api.Attestation_Material{
74-
AddedAt: timestamppb.New(time.Now()),
75-
MaterialType: i.input.Type,
76-
M: &api.Attestation_Material_Artifact_{
77-
Artifact: &api.Attestation_Material_Artifact{
78-
Id: i.input.Name, Digest: result.Digest, Name: "sbom.cyclonedx.json",
79-
},
80-
},
81-
}
82-
83-
return res, nil
65+
return uploadAndCraft(ctx, i.input, i.uploader, filePath)
8466
}

internal/attestation/crafter/materials/cyclonedxjson_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ func TestCyclonedxJSONCraft(t *testing.T) {
121121
require.NoError(t, err)
122122
assert.Equal(contractAPI.CraftingSchema_Material_SBOM_CYCLONEDX_JSON.String(), got.MaterialType.String())
123123
assert.WithinDuration(time.Now(), got.AddedAt.AsTime(), 5*time.Second)
124+
assert.True(got.UploadedToCas)
124125

125-
// // The result includes the digest reference
126+
// The result includes the digest reference
126127
assert.Equal(got.GetArtifact(), &attestationApi.Attestation_Material_Artifact{
127128
Id: "test", Digest: "deadbeef", Name: "sbom.cyclonedx.json",
128129
})

internal/attestation/crafter/materials/junit_xml_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
//nolint:dupl
1617
package materials_test
1718

1819
import (
@@ -63,6 +64,7 @@ func TestNewJUnitXMLCrafter(t *testing.T) {
6364
})
6465
}
6566
}
67+
6668
func TestJUnitXMLCraft(t *testing.T) {
6769
testCases := []struct {
6870
name string
@@ -95,6 +97,7 @@ func TestJUnitXMLCraft(t *testing.T) {
9597
Name: "test",
9698
Type: contractAPI.CraftingSchema_Material_JUNIT_XML,
9799
}
100+
98101
l := zerolog.Nop()
99102
for _, tc := range testCases {
100103
t.Run(tc.name, func(t *testing.T) {
@@ -119,6 +122,7 @@ func TestJUnitXMLCraft(t *testing.T) {
119122

120123
require.NoError(t, err)
121124
assert.Equal(contractAPI.CraftingSchema_Material_JUNIT_XML.String(), got.MaterialType.String())
125+
assert.True(got.UploadedToCas)
122126
assert.WithinDuration(time.Now(), got.AddedAt.AsTime(), 5*time.Second)
123127

124128
// The result includes the digest reference

0 commit comments

Comments
 (0)