Skip to content
Merged
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
14 changes: 8 additions & 6 deletions pkg/attestation/crafter/crafter.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,9 @@ func (c *Crafter) stageMaterial(ctx context.Context, m *schemaapi.CraftingSchema
}

// Check before crafting so that nothing is uploaded for a rejected material.
if _, found := c.CraftingState.GetAttestation().GetMaterials()[m.Name]; found {
if !addOptions.replace {
return nil, fmt.Errorf("%w: %q", ErrMaterialAlreadyExists, m.Name)
}

c.Logger.Info().Str("name", m.Name).Msg("material already set, replacing it")
_, exists := c.CraftingState.GetAttestation().GetMaterials()[m.Name]
if exists && !addOptions.replace {
return nil, fmt.Errorf("%w: %q", ErrMaterialAlreadyExists, m.Name)
}

// 3- Craft resulting material
Expand Down Expand Up @@ -911,6 +908,11 @@ func (c *Crafter) stageMaterial(ctx context.Context, m *schemaapi.CraftingSchema
policies.LogPolicyEvaluations(policyResults, c.Logger)

// 5 - Attach it to state
// Log only here, once the material is replaced: the kind auto-detection
// calls this once per probed kind, and the failed probes must not log.
if exists {
c.Logger.Info().Str("name", m.Name).Msg("material already set, replacing it")
}
if c.CraftingState.Attestation.Materials == nil {
c.CraftingState.Attestation.Materials = map[string]*api.Attestation_Material{m.Name: mt}
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/attestation/crafter/crafter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package crafter_test
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
Expand Down Expand Up @@ -1577,8 +1578,21 @@ func (s *crafterSuite) TestAddMaterialDuplicateName() {
_, err = add("first-value")
require.NoError(s.T(), err)

// Capture the logs of the second add only
var logs bytes.Buffer
logger := zerolog.New(&logs)
c.Logger = &logger

_, err = add("second-value", crafter.WithReplace(tc.replace))

// The replace line is logged once, only when the material is replaced,
// not once per kind probed by the auto-detection.
wantReplaceLogs := 0
if tc.replace {
wantReplaceLogs = 1
}
assert.Equal(s.T(), wantReplaceLogs, strings.Count(logs.String(), "material already set, replacing it"))

got := c.CraftingState.Attestation.Materials[tc.materialName]
require.NotNil(s.T(), got)

Expand Down
Loading