2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-11 04:35:17 -04:00
|
|
|
package nodestate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/role"
|
2022-04-11 04:35:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const nodeStatePath = "/run/state/constellation/node_state.json"
|
|
|
|
|
|
|
|
// NodeState is the state of a constellation node that is required to recover from a reboot.
|
|
|
|
// Can be persisted to disk and reloaded later.
|
|
|
|
type NodeState struct {
|
2022-07-26 04:58:39 -04:00
|
|
|
Role role.Role
|
|
|
|
MeasurementSalt []byte
|
2022-04-11 04:35:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromFile reads a NodeState from disk.
|
|
|
|
func FromFile(fileHandler file.Handler) (*NodeState, error) {
|
|
|
|
nodeState := &NodeState{}
|
|
|
|
if err := fileHandler.ReadJSON(nodeStatePath, nodeState); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return nil, fmt.Errorf("loading node state: %w", err)
|
2022-04-11 04:35:17 -04:00
|
|
|
}
|
|
|
|
return nodeState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToFile writes a NodeState to disk.
|
|
|
|
func (nodeState *NodeState) ToFile(fileHandler file.Handler) error {
|
2022-04-13 03:18:32 -04:00
|
|
|
return fileHandler.WriteJSON(nodeStatePath, nodeState, file.OptMkdirAll)
|
2022-04-11 04:35:17 -04:00
|
|
|
}
|