2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-02 07:21:07 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-09-08 08:45:27 -04:00
|
|
|
"context"
|
2022-05-02 07:21:07 -04:00
|
|
|
"errors"
|
2022-09-14 07:25:42 -04:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2022-05-02 07:21:07 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/cloudcmd"
|
|
|
|
"github.com/edgelesssys/constellation/disk-mapper/recoverproto"
|
2022-08-12 09:59:45 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-08-23 11:49:55 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
2022-05-02 07:21:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-08-01 03:11:13 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/crypto/testvector"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-09-14 07:25:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/atlscredentials"
|
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/dialer"
|
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/testdialer"
|
2022-05-02 07:21:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-14 07:25:42 -04:00
|
|
|
"google.golang.org/grpc"
|
2022-05-02 07:21:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecoverCmdArgumentValidation(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"no args": {[]string{}, false},
|
|
|
|
"too many arguments": {[]string{"abc"}, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewRecoverCmd()
|
2022-05-02 07:21:07 -04:00
|
|
|
err := cmd.ValidateArgs(tc.args)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecover(t *testing.T) {
|
|
|
|
validState := state.ConstellationState{CloudProvider: "GCP"}
|
|
|
|
invalidCSPState := state.ConstellationState{CloudProvider: "invalid"}
|
2022-09-14 07:25:42 -04:00
|
|
|
successActions := []func(stream recoverproto.API_RecoverServer) error{
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return stream.Send(&recoverproto.RecoverResponse{
|
|
|
|
DiskUuid: "00000000-0000-0000-0000-000000000000",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
existingState state.ConstellationState
|
2022-09-14 07:25:42 -04:00
|
|
|
recoverServerAPI *stubRecoveryServer
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret testvector.HKDF
|
2022-05-06 07:56:02 -04:00
|
|
|
endpointFlag string
|
2022-05-02 07:21:07 -04:00
|
|
|
masterSecretFlag string
|
2022-05-13 05:56:43 -04:00
|
|
|
configFlag string
|
2022-05-02 07:21:07 -04:00
|
|
|
stateless bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"works": {
|
2022-09-14 07:25:42 -04:00
|
|
|
existingState: validState,
|
|
|
|
recoverServerAPI: &stubRecoveryServer{actions: successActions},
|
|
|
|
endpointFlag: "192.0.2.1",
|
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"missing flags": {
|
2022-09-14 07:25:42 -04:00
|
|
|
recoverServerAPI: &stubRecoveryServer{actions: successActions},
|
|
|
|
wantErr: true,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
2022-05-13 05:56:43 -04:00
|
|
|
"missing config": {
|
2022-09-14 07:25:42 -04:00
|
|
|
recoverServerAPI: &stubRecoveryServer{actions: successActions},
|
|
|
|
endpointFlag: "192.0.2.1",
|
|
|
|
masterSecret: testvector.HKDFZero,
|
|
|
|
configFlag: "nonexistent-config",
|
|
|
|
wantErr: true,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"missing state": {
|
2022-09-14 07:25:42 -04:00
|
|
|
existingState: validState,
|
|
|
|
recoverServerAPI: &stubRecoveryServer{actions: successActions},
|
|
|
|
endpointFlag: "192.0.2.1",
|
|
|
|
masterSecret: testvector.HKDFZero,
|
|
|
|
stateless: true,
|
|
|
|
wantErr: true,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"invalid cloud provider": {
|
2022-09-14 07:25:42 -04:00
|
|
|
existingState: invalidCSPState,
|
|
|
|
recoverServerAPI: &stubRecoveryServer{actions: successActions},
|
|
|
|
endpointFlag: "192.0.2.1",
|
|
|
|
masterSecret: testvector.HKDFZero,
|
|
|
|
wantErr: true,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewRecoverCmd()
|
2022-09-14 07:25:42 -04:00
|
|
|
cmd.SetContext(context.Background())
|
2022-08-12 09:59:45 -04:00
|
|
|
cmd.Flags().String("config", constants.ConfigFilename, "") // register persistent flag manually
|
2022-05-02 07:21:07 -04:00
|
|
|
out := &bytes.Buffer{}
|
|
|
|
cmd.SetOut(out)
|
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
2022-05-06 07:56:02 -04:00
|
|
|
if tc.endpointFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("endpoint", tc.endpointFlag))
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
if tc.masterSecretFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("master-secret", tc.masterSecretFlag))
|
|
|
|
}
|
2022-05-13 05:56:43 -04:00
|
|
|
if tc.configFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("config", tc.configFlag))
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
2022-08-01 03:11:13 -04:00
|
|
|
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
2022-08-12 09:59:45 -04:00
|
|
|
|
2022-08-23 11:49:55 -04:00
|
|
|
config := defaultConfigWithExpectedMeasurements(t, config.Default(), cloudprovider.FromString(tc.existingState.CloudProvider))
|
2022-08-12 09:59:45 -04:00
|
|
|
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, config))
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
require.NoError(fileHandler.WriteJSON(
|
|
|
|
"constellation-mastersecret.json",
|
|
|
|
masterSecret{Key: tc.masterSecret.Secret, Salt: tc.masterSecret.Salt},
|
|
|
|
file.OptNone,
|
|
|
|
))
|
|
|
|
|
2022-05-02 07:21:07 -04:00
|
|
|
if !tc.stateless {
|
2022-09-14 07:25:42 -04:00
|
|
|
require.NoError(fileHandler.WriteJSON(
|
|
|
|
constants.StateFilename,
|
|
|
|
tc.existingState,
|
|
|
|
file.OptNone,
|
|
|
|
))
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
newDialer := func(*cloudcmd.Validator) *dialer.Dialer {
|
|
|
|
return dialer.New(nil, nil, netDialer)
|
|
|
|
}
|
|
|
|
serverCreds := atlscredentials.New(nil, nil)
|
|
|
|
recoverServer := grpc.NewServer(grpc.Creds(serverCreds))
|
|
|
|
recoverproto.RegisterAPIServer(recoverServer, tc.recoverServerAPI)
|
|
|
|
listener := netDialer.GetListener(net.JoinHostPort("192.0.2.1", strconv.Itoa(constants.RecoveryPort)))
|
|
|
|
go recoverServer.Serve(listener)
|
|
|
|
defer recoverServer.GracefulStop()
|
|
|
|
|
|
|
|
err := recover(cmd, fileHandler, newDialer)
|
2022-05-02 07:21:07 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Contains(out.String(), "Pushed recovery key.")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseRecoverFlags(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
wantFlags recoverFlags
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"no flags": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid ip": {
|
2022-09-08 08:45:27 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2:2"},
|
2022-05-02 07:21:07 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"minimal args set": {
|
2022-09-08 08:45:27 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2"},
|
2022-05-02 07:21:07 -04:00
|
|
|
wantFlags: recoverFlags{
|
2022-07-29 03:52:47 -04:00
|
|
|
endpoint: "192.0.2.1:2",
|
|
|
|
secretPath: "constellation-mastersecret.json",
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"all args set": {
|
2022-09-08 08:45:27 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2", "--config", "config-path", "--master-secret", "/path/super-secret.json"},
|
2022-05-02 07:21:07 -04:00
|
|
|
wantFlags: recoverFlags{
|
2022-07-29 03:52:47 -04:00
|
|
|
endpoint: "192.0.2.1:2",
|
|
|
|
secretPath: "/path/super-secret.json",
|
|
|
|
configPath: "config-path",
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewRecoverCmd()
|
2022-05-13 05:56:43 -04:00
|
|
|
cmd.Flags().String("config", "", "") // register persistent flag manually
|
2022-05-02 07:21:07 -04:00
|
|
|
require.NoError(cmd.ParseFlags(tc.args))
|
2022-07-29 03:52:47 -04:00
|
|
|
flags, err := parseRecoverFlags(cmd)
|
2022-05-02 07:21:07 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantFlags, flags)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
func TestDoRecovery(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
recoveryServer *stubRecoveryServer
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
recoveryServer: &stubRecoveryServer{actions: []func(stream recoverproto.API_RecoverServer) error{
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return stream.Send(&recoverproto.RecoverResponse{
|
|
|
|
DiskUuid: "00000000-0000-0000-0000-000000000000",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"error on first recv": {
|
|
|
|
recoveryServer: &stubRecoveryServer{actions: []func(stream recoverproto.API_RecoverServer) error{
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return someErr
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"error on send": {
|
|
|
|
recoveryServer: &stubRecoveryServer{actions: []func(stream recoverproto.API_RecoverServer) error{
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return someErr
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"error on second recv": {
|
|
|
|
recoveryServer: &stubRecoveryServer{actions: []func(stream recoverproto.API_RecoverServer) error{
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return stream.Send(&recoverproto.RecoverResponse{
|
|
|
|
DiskUuid: "00000000-0000-0000-0000-000000000000",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return someErr
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"final message is an error": {
|
|
|
|
recoveryServer: &stubRecoveryServer{actions: []func(stream recoverproto.API_RecoverServer) error{
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return stream.Send(&recoverproto.RecoverResponse{
|
|
|
|
DiskUuid: "00000000-0000-0000-0000-000000000000",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
func(stream recoverproto.API_RecoverServer) error {
|
|
|
|
return someErr
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
serverCreds := atlscredentials.New(nil, nil)
|
|
|
|
recoverServer := grpc.NewServer(grpc.Creds(serverCreds))
|
|
|
|
recoverproto.RegisterAPIServer(recoverServer, tc.recoveryServer)
|
|
|
|
addr := net.JoinHostPort("192.0.2.1", strconv.Itoa(constants.RecoveryPort))
|
|
|
|
listener := netDialer.GetListener(addr)
|
|
|
|
go recoverServer.Serve(listener)
|
|
|
|
defer recoverServer.GracefulStop()
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
recoverDoer := &recoverDoer{
|
|
|
|
dialer: dialer.New(nil, nil, netDialer),
|
|
|
|
endpoint: addr,
|
|
|
|
measurementSecret: []byte("measurement-secret"),
|
|
|
|
getDiskKey: func(string) ([]byte, error) {
|
|
|
|
return []byte("disk-key"), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := recoverDoer.Do(context.Background())
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
func TestDeriveStateDiskKey(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
masterSecret testvector.HKDF
|
|
|
|
}{
|
|
|
|
"all zero": {
|
|
|
|
masterSecret: testvector.HKDFZero,
|
|
|
|
},
|
|
|
|
"all 0xff": {
|
|
|
|
masterSecret: testvector.HKDF0xFF,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
getKeyFunc := getStateDiskKeyFunc(tc.masterSecret.Secret, tc.masterSecret.Salt)
|
|
|
|
stateDiskKey, err := getKeyFunc(tc.masterSecret.Info)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.masterSecret.Output, stateDiskKey)
|
|
|
|
})
|
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
type stubRecoveryServer struct {
|
|
|
|
actions []func(recoverproto.API_RecoverServer) error
|
|
|
|
recoverproto.UnimplementedAPIServer
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
func (s *stubRecoveryServer) Recover(stream recoverproto.API_RecoverServer) error {
|
|
|
|
for _, action := range s.actions {
|
|
|
|
if err := action(stream); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|