move nodestate and role

Signed-off-by: Fabian Kammel <fk@edgeless.systems>
This commit is contained in:
Fabian Kammel 2022-08-26 09:42:40 +00:00 committed by Malte Poll
parent 26e9c67a00
commit 22c912a56d
30 changed files with 29 additions and 29 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi"
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/kubectl"
"github.com/edgelesssys/constellation/bootstrapper/internal/logging"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/atls"
"github.com/edgelesssys/constellation/internal/attestation/azure"
"github.com/edgelesssys/constellation/internal/attestation/gcp"
@ -29,6 +28,7 @@ import (
"github.com/edgelesssys/constellation/internal/iproute"
"github.com/edgelesssys/constellation/internal/logger"
"github.com/edgelesssys/constellation/internal/oid"
"github.com/edgelesssys/constellation/internal/role"
"github.com/spf13/afero"
"go.uber.org/zap"
)

View file

@ -4,9 +4,9 @@ import (
"context"
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/cloud/metadata"
"github.com/edgelesssys/constellation/internal/logger"
"github.com/edgelesssys/constellation/internal/role"
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
)

View file

@ -10,8 +10,6 @@ import (
"github.com/edgelesssys/constellation/bootstrapper/initproto"
"github.com/edgelesssys/constellation/bootstrapper/internal/diskencryption"
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
"github.com/edgelesssys/constellation/bootstrapper/nodestate"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/atls"
"github.com/edgelesssys/constellation/internal/attestation"
"github.com/edgelesssys/constellation/internal/crypto"
@ -19,6 +17,8 @@ import (
"github.com/edgelesssys/constellation/internal/grpc/atlscredentials"
"github.com/edgelesssys/constellation/internal/grpc/grpclog"
"github.com/edgelesssys/constellation/internal/logger"
"github.com/edgelesssys/constellation/internal/nodestate"
"github.com/edgelesssys/constellation/internal/role"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"

View file

@ -12,13 +12,13 @@ import (
"github.com/edgelesssys/constellation/bootstrapper/internal/diskencryption"
"github.com/edgelesssys/constellation/bootstrapper/internal/kubelet"
"github.com/edgelesssys/constellation/bootstrapper/nodestate"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/attestation"
"github.com/edgelesssys/constellation/internal/cloud/metadata"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/edgelesssys/constellation/internal/file"
"github.com/edgelesssys/constellation/internal/logger"
"github.com/edgelesssys/constellation/internal/nodestate"
"github.com/edgelesssys/constellation/internal/role"
"github.com/edgelesssys/constellation/joinservice/joinproto"
"github.com/spf13/afero"
"go.uber.org/zap"

View file

@ -9,7 +9,6 @@ import (
"testing"
"time"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/cloud/metadata"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/edgelesssys/constellation/internal/file"
@ -17,6 +16,7 @@ import (
"github.com/edgelesssys/constellation/internal/grpc/dialer"
"github.com/edgelesssys/constellation/internal/grpc/testdialer"
"github.com/edgelesssys/constellation/internal/logger"
"github.com/edgelesssys/constellation/internal/role"
"github.com/edgelesssys/constellation/joinservice/joinproto"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"

View file

@ -9,12 +9,12 @@ import (
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi"
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/bootstrapper/util"
"github.com/edgelesssys/constellation/internal/cloud/metadata"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/edgelesssys/constellation/internal/iproute"
"github.com/edgelesssys/constellation/internal/logger"
"github.com/edgelesssys/constellation/internal/role"
"github.com/edgelesssys/constellation/internal/versions"
"github.com/spf13/afero"
"go.uber.org/zap"

View file

@ -10,11 +10,11 @@ import (
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi"
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/cloud/metadata"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/edgelesssys/constellation/internal/kubernetes"
"github.com/edgelesssys/constellation/internal/logger"
"github.com/edgelesssys/constellation/internal/role"
"github.com/edgelesssys/constellation/internal/versions"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

View file

@ -1,31 +0,0 @@
package nodestate
import (
"fmt"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/file"
)
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 {
Role role.Role
MeasurementSalt []byte
}
// FromFile reads a NodeState from disk.
func FromFile(fileHandler file.Handler) (*NodeState, error) {
nodeState := &NodeState{}
if err := fileHandler.ReadJSON(nodeStatePath, nodeState); err != nil {
return nil, fmt.Errorf("loading node state: %w", err)
}
return nodeState, nil
}
// ToFile writes a NodeState to disk.
func (nodeState *NodeState) ToFile(fileHandler file.Handler) error {
return fileHandler.WriteJSON(nodeStatePath, nodeState, file.OptMkdirAll)
}

View file

@ -1,105 +0,0 @@
package nodestate
import (
"path/filepath"
"testing"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/file"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func TestFromFile(t *testing.T) {
testCases := map[string]struct {
fileContents string
wantState *NodeState
wantErr bool
}{
"nodestate exists": {
fileContents: `{ "Role": "ControlPlane", "MeasurementSalt": "U2FsdA==" }`,
wantState: &NodeState{
Role: role.ControlPlane,
MeasurementSalt: []byte("Salt"),
},
},
"nodestate file does not exist": {
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
fs := afero.NewMemMapFs()
if tc.fileContents != "" {
require.NoError(fs.MkdirAll(filepath.Dir(nodeStatePath), 0o755))
require.NoError(afero.WriteFile(fs, nodeStatePath, []byte(tc.fileContents), 0o644))
}
fileHandler := file.NewHandler(fs)
state, err := FromFile(fileHandler)
if tc.wantErr {
assert.Error(err)
return
}
require.NoError(err)
assert.Equal(tc.wantState, state)
})
}
}
func TestToFile(t *testing.T) {
testCases := map[string]struct {
precreateFile bool
state *NodeState
wantFile string
wantErr bool
}{
"writing works": {
state: &NodeState{
Role: role.ControlPlane,
MeasurementSalt: []byte("Salt"),
},
wantFile: `{
"Role": "ControlPlane",
"MeasurementSalt": "U2FsdA=="
}`,
},
"file exists already": {
precreateFile: true,
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
fs := afero.NewMemMapFs()
if tc.precreateFile {
require.NoError(fs.MkdirAll(filepath.Dir(nodeStatePath), 0o755))
require.NoError(afero.WriteFile(fs, nodeStatePath, []byte("pre-existing"), 0o644))
}
fileHandler := file.NewHandler(fs)
err := tc.state.ToFile(fileHandler)
if tc.wantErr {
assert.Error(err)
return
}
require.NoError(err)
fileContents, err := afero.ReadFile(fs, nodeStatePath)
require.NoError(err)
assert.Equal(tc.wantFile, string(fileContents))
})
}
}

View file

@ -1,42 +0,0 @@
package role
import (
"encoding/json"
"strings"
)
//go:generate stringer -type=Role
// Role is a peer's role.
type Role uint
const (
Unknown Role = iota
ControlPlane
Worker
Admin
)
// MarshalJSON marshals the Role to JSON string.
func (r Role) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
}
// UnmarshalJSON unmarshals the Role from JSON string.
func (r *Role) UnmarshalJSON(b []byte) error {
var roleString string
if err := json.Unmarshal(b, &roleString); err != nil {
return err
}
switch strings.ToLower(roleString) {
case "controlplane":
*r = ControlPlane
case "worker":
*r = Worker
case "admin":
*r = Admin
default:
*r = Unknown
}
return nil
}

View file

@ -1,26 +0,0 @@
// Code generated by "stringer -type=Role"; DO NOT EDIT.
package role
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[Unknown-0]
_ = x[ControlPlane-1]
_ = x[Worker-2]
_ = x[Admin-3]
}
const _Role_name = "UnknownControlPlaneWorkerAdmin"
var _Role_index = [...]uint8{0, 7, 19, 25, 30}
func (i Role) String() string {
if i >= Role(len(_Role_index)-1) {
return "Role(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Role_name[_Role_index[i]:_Role_index[i+1]]
}

View file

@ -1,113 +0,0 @@
package role
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func TestMarshal(t *testing.T) {
testCases := map[string]struct {
role Role
wantJSON string
wantErr bool
}{
"controlePlane role": {
role: ControlPlane,
wantJSON: `"ControlPlane"`,
},
"node role": {
role: Worker,
wantJSON: `"Worker"`,
},
"admin role": {
role: Admin,
wantJSON: `"Admin"`,
},
"unknown role": {
role: Unknown,
wantJSON: `"Unknown"`,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
jsonRole, err := tc.role.MarshalJSON()
if tc.wantErr {
assert.Error(err)
return
}
require.NoError(err)
assert.Equal(tc.wantJSON, string(jsonRole))
})
}
}
func TestUnmarshal(t *testing.T) {
testCases := map[string]struct {
json string
wantRole Role
wantErr bool
}{
"ControlPlane can be unmarshaled": {
json: `"ControlPlane"`,
wantRole: ControlPlane,
},
"lowercase controlPlane can be unmarshaled": {
json: `"controlPlane"`,
wantRole: ControlPlane,
},
"Worker can be unmarshaled": {
json: `"Worker"`,
wantRole: Worker,
},
"lowercase worker can be unmarshaled": {
json: `"worker"`,
wantRole: Worker,
},
"Admin can be unmarshaled": {
json: `"Admin"`,
wantRole: Admin,
},
"lowercase admin can be unmarshaled": {
json: `"admin"`,
wantRole: Admin,
},
"other strings unmarshal to the unknown role": {
json: `"anything"`,
wantRole: Unknown,
},
"invalid json fails": {
json: `"unterminated string literal`,
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
var role Role
err := role.UnmarshalJSON([]byte(tc.json))
if tc.wantErr {
assert.Error(err)
return
}
require.NoError(err)
assert.Equal(tc.wantRole, role)
})
}
}