2022-04-11 10:35:17 +02:00
|
|
|
package nodestate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-05-16 17:32:00 +02:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-08-26 09:42:40 +00:00
|
|
|
"github.com/edgelesssys/constellation/internal/role"
|
2022-04-11 10:35:17 +02: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 10:58:39 +02:00
|
|
|
Role role.Role
|
|
|
|
MeasurementSalt []byte
|
2022-04-11 10:35:17 +02: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 14:04:30 +00:00
|
|
|
return nil, fmt.Errorf("loading node state: %w", err)
|
2022-04-11 10:35:17 +02:00
|
|
|
}
|
|
|
|
return nodeState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToFile writes a NodeState to disk.
|
|
|
|
func (nodeState *NodeState) ToFile(fileHandler file.Handler) error {
|
2022-04-13 09:18:32 +02:00
|
|
|
return fileHandler.WriteJSON(nodeStatePath, nodeState, file.OptMkdirAll)
|
2022-04-11 10:35:17 +02:00
|
|
|
}
|