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"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
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-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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
existingState state.ConstellationState
|
|
|
|
client *stubRecoveryClient
|
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": {
|
|
|
|
existingState: validState,
|
|
|
|
client: &stubRecoveryClient{},
|
2022-05-06 07:56:02 -04:00
|
|
|
endpointFlag: "192.0.2.1",
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"missing flags": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-05-13 05:56:43 -04:00
|
|
|
"missing config": {
|
|
|
|
endpointFlag: "192.0.2.1",
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-13 05:56:43 -04:00
|
|
|
configFlag: "nonexistent-config",
|
|
|
|
wantErr: true,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"missing state": {
|
|
|
|
existingState: validState,
|
2022-05-06 07:56:02 -04:00
|
|
|
endpointFlag: "192.0.2.1",
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
stateless: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid cloud provider": {
|
|
|
|
existingState: invalidCSPState,
|
2022-05-06 07:56:02 -04:00
|
|
|
endpointFlag: "192.0.2.1",
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"connect fails": {
|
|
|
|
existingState: validState,
|
|
|
|
client: &stubRecoveryClient{connectErr: errors.New("connect failed")},
|
2022-05-06 07:56:02 -04:00
|
|
|
endpointFlag: "192.0.2.1",
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"pushing state key fails": {
|
|
|
|
existingState: validState,
|
|
|
|
client: &stubRecoveryClient{pushStateDiskKeyErr: errors.New("pushing key failed")},
|
2022-05-06 07:56:02 -04:00
|
|
|
endpointFlag: "192.0.2.1",
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-08-01 03:11:13 -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 {
|
|
|
|
require.NoError(fileHandler.WriteJSON(constants.StateFilename, tc.existingState, file.OptNone))
|
|
|
|
}
|
|
|
|
|
2022-06-28 05:19:03 -04:00
|
|
|
err := recover(cmd, fileHandler, tc.client)
|
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-08 08:45:27 -04:00
|
|
|
type stubRecoveryClient struct {
|
|
|
|
conn bool
|
|
|
|
connectErr error
|
|
|
|
closeErr error
|
|
|
|
pushStateDiskKeyErr error
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
pushStateDiskKeyKey []byte
|
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
func (c *stubRecoveryClient) Connect(string, atls.Validator) error {
|
|
|
|
c.conn = true
|
|
|
|
return c.connectErr
|
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
func (c *stubRecoveryClient) Close() error {
|
|
|
|
c.conn = false
|
|
|
|
return c.closeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubRecoveryClient) Recover(_ context.Context, stateDiskKey, _ []byte) error {
|
|
|
|
c.pushStateDiskKeyKey = stateDiskKey
|
|
|
|
return c.pushStateDiskKeyErr
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|