11//
2- // Copyright 2024 The Chainloop Authors.
2+ // Copyright 2024-2025 The Chainloop Authors.
33//
44// Licensed under the Apache License, Version 2.0 (the "License");
55// you may not use this file except in compliance with the License.
@@ -21,10 +21,10 @@ import (
2121 "fmt"
2222
2323 pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
24- schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
2524 "github.com/chainloop-dev/chainloop/internal/casclient"
2625 "github.com/chainloop-dev/chainloop/internal/grpcconn"
2726 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter"
27+ api "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1"
2828 "google.golang.org/grpc"
2929)
3030
@@ -73,22 +73,22 @@ func NewAttestationAdd(cfg *AttestationAddOpts) (*AttestationAdd, error) {
7373
7474var ErrAttestationNotInitialized = errors .New ("attestation not yet initialized" )
7575
76- func (action * AttestationAdd ) Run (ctx context.Context , attestationID , materialName , materialValue , materialType string , annotations map [string ]string ) error {
76+ func (action * AttestationAdd ) Run (ctx context.Context , attestationID , materialName , materialValue , materialType string , annotations map [string ]string ) ( * AttestationStatusMaterial , error ) {
7777 // initialize the crafter. If attestation-id is provided we assume the attestation is performed using remote state
7878 crafter , err := newCrafter (& newCrafterStateOpts {enableRemoteState : (attestationID != "" ), localStatePath : action .localStatePath }, action .CPConnection , action .newCrafterOpts .opts ... )
7979 if err != nil {
80- return fmt .Errorf ("failed to load crafter: %w" , err )
80+ return nil , fmt .Errorf ("failed to load crafter: %w" , err )
8181 }
8282
8383 if initialized , err := crafter .AlreadyInitialized (ctx , attestationID ); err != nil {
84- return fmt .Errorf ("checking if attestation is already initialized: %w" , err )
84+ return nil , fmt .Errorf ("checking if attestation is already initialized: %w" , err )
8585 } else if ! initialized {
86- return ErrAttestationNotInitialized
86+ return nil , ErrAttestationNotInitialized
8787 }
8888
8989 if err := crafter .LoadCraftingState (ctx , attestationID ); err != nil {
9090 action .Logger .Err (err ).Msg ("loading existing attestation" )
91- return err
91+ return nil , err
9292 }
9393
9494 // Default to inline CASBackend and override if we are not in dry-run mode
@@ -106,11 +106,11 @@ func (action *AttestationAdd) Run(ctx context.Context, attestationID, materialNa
106106 },
107107 )
108108 if err != nil {
109- return err
109+ return nil , fmt . Errorf ( "getting upload creds: %w" , err )
110110 }
111111 b := creds .GetResult ().GetBackend ()
112112 if b == nil {
113- return fmt .Errorf ("no backend found in upload creds" )
113+ return nil , fmt .Errorf ("no backend found in upload creds" )
114114 }
115115 casBackend .Name = b .Provider
116116 casBackend .MaxSize = b .GetLimits ().MaxBytes
@@ -127,7 +127,7 @@ func (action *AttestationAdd) Run(ctx context.Context, attestationID, materialNa
127127
128128 artifactCASConn , err := grpcconn .New (action .casURI , creds .Result .Token , opts ... )
129129 if err != nil {
130- return err
130+ return nil , fmt . Errorf ( "creating CAS connection: %w" , err )
131131 }
132132 defer artifactCASConn .Close ()
133133
@@ -141,37 +141,42 @@ func (action *AttestationAdd) Run(ctx context.Context, attestationID, materialNa
141141 // 2. If materialName is not empty, check if the material is in the contract. If it is, add material from contract
142142 // 2.1. If materialType is empty, try to guess the material kind with auto-detected kind and materialName
143143 // 3. If materialType is not empty, add material contract free with materialType and materialName
144- var kind schemaapi. CraftingSchema_Material_MaterialType
144+ var mt * api. Attestation_Material
145145 switch {
146146 case materialName == "" && materialType == "" :
147- kind , err = crafter .AddMaterialContactFreeWithAutoDetectedKind (ctx , attestationID , "" , materialValue , casBackend , annotations )
147+ mt , err = crafter .AddMaterialContactFreeWithAutoDetectedKind (ctx , attestationID , "" , materialValue , casBackend , annotations )
148148 if err != nil {
149- return fmt .Errorf ("adding material: %w" , err )
149+ return nil , fmt .Errorf ("adding material: %w" , err )
150150 }
151- action .Logger .Info ().Str ("kind" , kind .String ()).Msg ("material kind detected" )
151+ action .Logger .Info ().Str ("kind" , mt . MaterialType .String ()).Msg ("material kind detected" )
152152 case materialName != "" :
153153 switch {
154154 // If the material is in the contract, add it from the contract
155155 case crafter .IsMaterialInContract (materialName ):
156- err = crafter .AddMaterialFromContract (ctx , attestationID , materialName , materialValue , casBackend , annotations )
156+ mt , err = crafter .AddMaterialFromContract (ctx , attestationID , materialName , materialValue , casBackend , annotations )
157157 // If the material is not in the contract and the materialType is not provided, add material contract free with auto-detected kind, guessing the kind
158158 case materialType == "" :
159- kind , err = crafter .AddMaterialContactFreeWithAutoDetectedKind (ctx , attestationID , materialName , materialValue , casBackend , annotations )
159+ mt , err = crafter .AddMaterialContactFreeWithAutoDetectedKind (ctx , attestationID , materialName , materialValue , casBackend , annotations )
160160 if err != nil {
161- return fmt .Errorf ("adding material: %w" , err )
161+ return nil , fmt .Errorf ("adding material: %w" , err )
162162 }
163- action .Logger .Info ().Str ("kind" , kind .String ()).Msg ("material kind detected" )
163+ action .Logger .Info ().Str ("kind" , mt . MaterialType .String ()).Msg ("material kind detected" )
164164 // If the material is not in the contract and has a materialType, add material contract free with the provided materialType
165165 default :
166- err = crafter .AddMaterialContractFree (ctx , attestationID , materialType , materialName , materialValue , casBackend , annotations )
166+ mt , err = crafter .AddMaterialContractFree (ctx , attestationID , materialType , materialName , materialValue , casBackend , annotations )
167167 }
168168 default :
169- err = crafter .AddMaterialContractFree (ctx , attestationID , materialType , materialName , materialValue , casBackend , annotations )
169+ mt , err = crafter .AddMaterialContractFree (ctx , attestationID , materialType , materialName , materialValue , casBackend , annotations )
170170 }
171171
172172 if err != nil {
173- return fmt .Errorf ("adding material: %w" , err )
173+ return nil , fmt .Errorf ("adding material: %w" , err )
174174 }
175175
176- return nil
176+ materialResult , err := attMaterialToAction (mt )
177+ if err != nil {
178+ return nil , fmt .Errorf ("converting material to action: %w" , err )
179+ }
180+
181+ return materialResult , nil
177182}
0 commit comments