diff --git a/pkg/attestation/crafter/crafter.go b/pkg/attestation/crafter/crafter.go index f85552a76..3e43590e7 100644 --- a/pkg/attestation/crafter/crafter.go +++ b/pkg/attestation/crafter/crafter.go @@ -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 @@ -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} } diff --git a/pkg/attestation/crafter/crafter_test.go b/pkg/attestation/crafter/crafter_test.go index f36c9846f..3dd81660f 100644 --- a/pkg/attestation/crafter/crafter_test.go +++ b/pkg/attestation/crafter/crafter_test.go @@ -18,6 +18,7 @@ package crafter_test import ( "archive/tar" "archive/zip" + "bytes" "compress/gzip" "context" "crypto/sha256" @@ -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)