Skip to content

Commit 0e8c50d

Browse files
waveywavesclaude
andcommitted
fix(crafter): surface auto-discovery errors instead of masking them
Fix variable shadowing in AddMaterialContactFreeWithAutoDetectedKind: m, err := (loop-scoped) → m, err = (outer-scoped) so the error from the last failed kind probe is propagated to the caller instead of always being nil. Also break early on protovalidate.ValidationError during auto-discovery so schema-level failures (e.g. invalid material name) are surfaced immediately instead of being masked by the kind-probing loop. Fixes: #2394 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> Signed-off-by: Vibhav Bobade <[email protected]>
1 parent bb484cd commit 0e8c50d

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

pkg/attestation/crafter/crafter.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,30 +645,36 @@ func (c *Crafter) IsMaterialInContract(key string) bool {
645645
// AddMaterialContactFreeWithAutoDetectedKind adds a material to the crafting state checking the incoming material matches any of the
646646
// supported types in validation order. If the material is not found it will return an error.
647647
func (c *Crafter) AddMaterialContactFreeWithAutoDetectedKind(ctx context.Context, attestationID, name, value string, casBackend *casclient.CASBackend, runtimeAnnotations map[string]string) (*api.Attestation_Material, error) {
648-
var err error
648+
var (
649+
err error
650+
m *api.Attestation_Material
651+
)
649652
for _, kind := range schemaapi.CraftingMaterialInValidationOrder {
650-
m, err := c.AddMaterialContractFree(ctx, attestationID, kind.String(), name, value, casBackend, runtimeAnnotations)
653+
m, err = c.AddMaterialContractFree(ctx, attestationID, kind.String(), name, value, casBackend, runtimeAnnotations)
651654
if err == nil {
652-
// Successfully added material, return the kind
653655
return m, nil
654656
}
655657

656658
c.Logger.Debug().Err(err).Str("kind", kind.String()).Msg("failed to add material")
657659

658-
// Handle base error for upload and craft errors except the opening file error
659-
// TODO: have an error to detect validation error instead
660660
var policyError *policies.PolicyError
661661
if errors.Is(err, materials.ErrBaseUploadAndCraft) || errors.As(err, &policyError) {
662662
return nil, err
663663
}
664664

665-
// This is a final error, we detected the kind
666665
if v1.IsAttestationStateErrorConflict(err) {
667666
return nil, err
668667
}
668+
669+
// Proto-validation errors (e.g. invalid material name) are schema-level
670+
// failures, not kind mismatches. Stop probing immediately so the real
671+
// error is surfaced to the user instead of being masked by the loop.
672+
var valErr *protovalidate.ValidationError
673+
if errors.As(err, &valErr) {
674+
return nil, err
675+
}
669676
}
670677

671-
// Return an error if no material could be added
672678
return nil, fmt.Errorf("failed to auto-discover material kind: %w", err)
673679
}
674680

0 commit comments

Comments
 (0)