@@ -47,18 +47,18 @@ type StateManager interface {
4747 // Check if the state is already initialized
4848 Initialized (ctx context.Context , key string ) (bool , error )
4949 // Write the state to the manager backend
50- Write (ctx context.Context , key string , state * api. CraftingState ) error
50+ Write (ctx context.Context , key string , state * VersionedCraftingState ) error
5151 // Read the state from the manager backend
52- Read (ctx context.Context , key string , state * api. CraftingState ) error
52+ Read (ctx context.Context , key string , state * VersionedCraftingState ) error
5353 // Reset/Delete the state
5454 Reset (ctx context.Context , key string ) error
5555 // String returns a string representation of the state manager
5656 Info (ctx context.Context , key string ) string
5757}
5858
5959type Crafter struct {
60- logger * zerolog.Logger
61- CraftingState * api. CraftingState
60+ Logger * zerolog.Logger
61+ CraftingState * VersionedCraftingState
6262 Runner SupportedRunner
6363 workingDir string
6464 stateManager StateManager
@@ -67,13 +67,19 @@ type Crafter struct {
6767 validator * protovalidate.Validator
6868}
6969
70+ type VersionedCraftingState struct {
71+ * api.CraftingState
72+ // This digest is used to verify the integrity of the state during updates
73+ UpdateCheckSum string
74+ }
75+
7076var ErrAttestationStateNotLoaded = errors .New ("crafting state not loaded" )
7177
7278type NewOpt func (c * Crafter ) error
7379
7480func WithLogger (l * zerolog.Logger ) NewOpt {
7581 return func (c * Crafter ) error {
76- c .logger = l
82+ c .Logger = l
7783 return nil
7884 }
7985}
@@ -108,7 +114,7 @@ func NewCrafter(stateManager StateManager, opts ...NewOpt) (*Crafter, error) {
108114
109115 cw , _ := os .Getwd ()
110116 c := & Crafter {
111- logger : & noopLogger ,
117+ Logger : & noopLogger ,
112118 workingDir : cw ,
113119 stateManager : stateManager ,
114120 // By default we authenticate with the current user's keychain (i.e ~/.docker/config.json)
@@ -225,11 +231,13 @@ func (c *Crafter) initCraftingStateFile(
225231 return fmt .Errorf ("initializing crafting state: %w" , err )
226232 }
227233
228- if err := c .stateManager .Write (ctx , attestationID , state ); err != nil {
234+ // newState doesn't have a digest to check against
235+ newState := & VersionedCraftingState {CraftingState : state }
236+ if err := c .stateManager .Write (ctx , attestationID , newState ); err != nil {
229237 return fmt .Errorf ("failed to persist crafting state: %w" , err )
230238 }
231239
232- c .logger .Debug ().Str ("state" , c .stateManager .Info (ctx , attestationID )).Msg ("created state file" )
240+ c .Logger .Debug ().Str ("state" , c .stateManager .Info (ctx , attestationID )).Msg ("created state file" )
233241
234242 return c .LoadCraftingState (ctx , attestationID )
235243}
@@ -240,9 +248,9 @@ func (c *Crafter) Reset(ctx context.Context, stateID string) error {
240248}
241249
242250func (c * Crafter ) LoadCraftingState (ctx context.Context , attestationID string ) error {
243- c .logger .Debug ().Str ("state" , c .stateManager .Info (ctx , attestationID )).Msg ("loading state" )
251+ c .Logger .Debug ().Str ("state" , c .stateManager .Info (ctx , attestationID )).Msg ("loading state" )
244252
245- c .CraftingState = & api.CraftingState {}
253+ c .CraftingState = & VersionedCraftingState { CraftingState : & api.CraftingState {} }
246254
247255 if err := c .stateManager .Read (ctx , attestationID , c .CraftingState ); err != nil {
248256 return fmt .Errorf ("failed to load crafting state: %w" , err )
@@ -255,7 +263,7 @@ func (c *Crafter) LoadCraftingState(ctx context.Context, attestationID string) e
255263 }
256264
257265 c .Runner = NewRunner (runnerType )
258- c .logger .Debug ().Str ("state" , c .stateManager .Info (ctx , attestationID )).Msg ("loaded state" )
266+ c .Logger .Debug ().Str ("state" , c .stateManager .Info (ctx , attestationID )).Msg ("loaded state" )
259267
260268 return nil
261269}
@@ -413,7 +421,7 @@ func (c *Crafter) ResolveEnvVars(ctx context.Context, attestationID string) erro
413421 }
414422
415423 // Runner specific environment variables
416- c .logger .Debug ().Str ("runnerType" , c .Runner .ID ().String ()).Msg ("loading runner specific env variables" )
424+ c .Logger .Debug ().Str ("runnerType" , c .Runner .ID ().String ()).Msg ("loading runner specific env variables" )
417425 if ! c .Runner .CheckEnv () {
418426 errorStr := fmt .Sprintf ("couldn't detect the environment %q. Is the crafting process happening in the target env?" , c .Runner .ID ().String ())
419427 return fmt .Errorf ("%s - %w" , errorStr , ErrRunnerContextNotFound )
@@ -424,7 +432,7 @@ func (c *Crafter) ResolveEnvVars(ctx context.Context, attestationID string) erro
424432 for index , envVarDef := range c .Runner .ListEnvVars () {
425433 varNames [index ] = envVarDef .Name
426434 }
427- c .logger .Debug ().Str ("runnerType" , c .Runner .ID ().String ()).Strs ("variables" , varNames ).Msg ("list of env variables to automatically extract" )
435+ c .Logger .Debug ().Str ("runnerType" , c .Runner .ID ().String ()).Strs ("variables" , varNames ).Msg ("list of env variables to automatically extract" )
428436
429437 outputEnvVars , errors := c .Runner .ResolveEnvVars ()
430438 if len (errors ) > 0 {
@@ -437,7 +445,7 @@ func (c *Crafter) ResolveEnvVars(ctx context.Context, attestationID string) erro
437445
438446 // User-defined environment vars
439447 if len (c .CraftingState .InputSchema .EnvAllowList ) > 0 {
440- c .logger .Debug ().Strs ("allowList" , c .CraftingState .InputSchema .EnvAllowList ).Msg ("loading env variables" )
448+ c .Logger .Debug ().Strs ("allowList" , c .CraftingState .InputSchema .EnvAllowList ).Msg ("loading env variables" )
441449 }
442450 for _ , want := range c .CraftingState .InputSchema .EnvAllowList {
443451 val := os .Getenv (want )
@@ -501,7 +509,7 @@ func (c *Crafter) AddMaterialFromContract(ctx context.Context, attestationID, ke
501509
502510 // 2 - Check that it has not been set yet and warn of override
503511 if _ , found := c .CraftingState .Attestation .Materials [key ]; found {
504- c .logger .Info ().Str ("key" , key ).Str ("value" , value ).Msg ("material already set, overriding it" )
512+ c .Logger .Info ().Str ("key" , key ).Str ("value" , value ).Msg ("material already set, overriding it" )
505513 }
506514
507515 // 3 - Craft resulting material
@@ -518,7 +526,7 @@ func (c *Crafter) AddMaterialContactFreeAutomatic(ctx context.Context, attestati
518526 return kind , nil
519527 }
520528
521- c .logger .Debug ().Err (err ).Str ("kind" , kind .String ()).Msg ("failed to add material" )
529+ c .Logger .Debug ().Err (err ).Str ("kind" , kind .String ()).Msg ("failed to add material" )
522530
523531 // Handle base error for upload and craft errors except the opening file error
524532 var policyError * policies.PolicyError
@@ -534,7 +542,7 @@ func (c *Crafter) AddMaterialContactFreeAutomatic(ctx context.Context, attestati
534542// addMaterials adds the incoming material m to the crafting state
535543func (c * Crafter ) addMaterial (ctx context.Context , m * schemaapi.CraftingSchema_Material , attestationID , value string , casBackend * casclient.CASBackend , runtimeAnnotations map [string ]string ) error {
536544 // 3- Craft resulting material
537- mt , err := materials .Craft (context .Background (), m , value , casBackend , c .ociRegistryAuth , c .logger )
545+ mt , err := materials .Craft (context .Background (), m , value , casBackend , c .ociRegistryAuth , c .Logger )
538546 if err != nil {
539547 return err
540548 }
@@ -551,7 +559,7 @@ func (c *Crafter) addMaterial(ctx context.Context, m *schemaapi.CraftingSchema_M
551559 mt .Annotations [kr ] = vr
552560 } else {
553561 // NOTE: we do not allow overriding values that come from the contract
554- c .logger .Info ().Str ("key" , m .Name ).Str ("annotation" , kr ).Msg ("annotation can't be changed, skipping" )
562+ c .Logger .Info ().Str ("key" , m .Name ).Str ("annotation" , kr ).Msg ("annotation can't be changed, skipping" )
555563 }
556564 }
557565
@@ -573,13 +581,13 @@ func (c *Crafter) addMaterial(ctx context.Context, m *schemaapi.CraftingSchema_M
573581 }
574582
575583 // Validate policies
576- pv := policies .NewPolicyVerifier (c .CraftingState .InputSchema , c .logger )
584+ pv := policies .NewPolicyVerifier (c .CraftingState .InputSchema , c .Logger )
577585 policyResults , err := pv .VerifyMaterial (ctx , mt , value )
578586 if err != nil {
579587 return fmt .Errorf ("error applying policies to material: %w" , err )
580588 }
581589 // log policy violations
582- policies .LogPolicyViolations (policyResults , c .logger )
590+ policies .LogPolicyViolations (policyResults , c .Logger )
583591 // store policy results
584592 c .CraftingState .Attestation .PolicyEvaluations = append (c .CraftingState .Attestation .PolicyEvaluations , policyResults ... )
585593
@@ -594,7 +602,7 @@ func (c *Crafter) addMaterial(ctx context.Context, m *schemaapi.CraftingSchema_M
594602 return fmt .Errorf ("failed to persist crafting state: %w" , err )
595603 }
596604
597- c .logger .Debug ().Str ("key" , m .Name ).Msg ("added to state" )
605+ c .Logger .Debug ().Str ("key" , m .Name ).Msg ("added to state" )
598606 return nil
599607}
600608
0 commit comments