From 3a60a504c8261c4fc51afe908820f61b89b5c3ab Mon Sep 17 00:00:00 2001 From: Miguel Martinez Trivino Date: Tue, 1 Dec 2026 00:26:09 +0100 Subject: [PATCH] fix(crafter): log material replacement once on auto-detected att add When a material is added with --replace and no --kind, the kind auto-detection stages the material once per probed kind. stageMaterial logged "material already set, replacing it" before crafting, so the line was printed for every probed kind. Log it only when the material is attached to the state. Closes #3863 Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino Chainloop-Trace-Sessions: 500fcf79-527f-4bcf-95ef-2c7033bcc590 --- pkg/attestation/crafter/crafter.go | 14 ++++++++------ pkg/attestation/crafter/crafter_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) 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)