2022-05-02 07:21:07 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-08-01 03:11:13 -04:00
|
|
|
"strings"
|
2022-05-02 07:21:07 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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
|
|
|
diskUUIDFlag string
|
|
|
|
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
|
|
|
diskUUIDFlag: testvector.HKDFZero.Info,
|
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"uppercase disk uuid 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
|
|
|
diskUUIDFlag: strings.ToUpper(testvector.HKDF0xFF.Info),
|
|
|
|
masterSecret: testvector.HKDF0xFF,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"lowercase disk uuid results in same key": {
|
|
|
|
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
|
|
|
diskUUIDFlag: strings.ToLower(testvector.HKDF0xFF.Info),
|
|
|
|
masterSecret: testvector.HKDF0xFF,
|
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
|
|
|
diskUUIDFlag: testvector.HKDFZero.Info,
|
|
|
|
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
|
|
|
diskUUIDFlag: testvector.HKDFZero.Info,
|
|
|
|
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
|
|
|
diskUUIDFlag: testvector.HKDFZero.Info,
|
|
|
|
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
|
|
|
diskUUIDFlag: testvector.HKDFZero.Info,
|
|
|
|
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
|
|
|
diskUUIDFlag: testvector.HKDFZero.Info,
|
|
|
|
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-07-29 03:52:47 -04:00
|
|
|
cmd.Flags().String("config", "", "") // 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.diskUUIDFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("disk-uuid", tc.diskUUIDFlag))
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
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.")
|
2022-08-01 03:11:13 -04:00
|
|
|
assert.Equal(tc.masterSecret.Output, tc.client.pushStateDiskKeyKey)
|
2022-05-02 07:21:07 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseRecoverFlags(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
wantFlags recoverFlags
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"no flags": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid ip": {
|
2022-05-06 07:56:02 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2:2", "--disk-uuid", "12345678-1234-1234-1234-123456789012"},
|
2022-05-02 07:21:07 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid disk uuid": {
|
2022-05-06 07:56:02 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2", "--disk-uuid", "invalid"},
|
2022-05-02 07:21:07 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"minimal args set": {
|
2022-05-06 07:56:02 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2", "--disk-uuid", "12345678-1234-1234-1234-123456789012"},
|
2022-05-02 07:21:07 -04:00
|
|
|
wantFlags: recoverFlags{
|
2022-07-29 03:52:47 -04:00
|
|
|
endpoint: "192.0.2.1:2",
|
|
|
|
diskUUID: "12345678-1234-1234-1234-123456789012",
|
|
|
|
secretPath: "constellation-mastersecret.json",
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"all args set": {
|
2022-07-29 03:52:47 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2", "--disk-uuid", "12345678-1234-1234-1234-123456789012", "--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",
|
|
|
|
diskUUID: "12345678-1234-1234-1234-123456789012",
|
|
|
|
secretPath: "/path/super-secret.json",
|
|
|
|
configPath: "config-path",
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"uppercase disk-uuid is converted to lowercase": {
|
2022-05-06 07:56:02 -04:00
|
|
|
args: []string{"-e", "192.0.2.1:2", "--disk-uuid", "ABCDEFAB-CDEF-ABCD-ABCD-ABCDEFABCDEF"},
|
2022-05-02 07:21:07 -04:00
|
|
|
wantFlags: recoverFlags{
|
2022-07-29 03:52:47 -04:00
|
|
|
endpoint: "192.0.2.1:2",
|
|
|
|
diskUUID: "abcdefab-cdef-abcd-abcd-abcdefabcdef",
|
|
|
|
secretPath: "constellation-mastersecret.json",
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeriveStateDiskKey(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret testvector.HKDF
|
2022-05-02 07:21:07 -04:00
|
|
|
}{
|
|
|
|
"all zero": {
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDFZero,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
"all 0xff": {
|
2022-08-01 03:11:13 -04:00
|
|
|
masterSecret: testvector.HKDF0xFF,
|
2022-05-02 07:21:07 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-08-01 03:11:13 -04:00
|
|
|
stateDiskKey, err := deriveStateDiskKey(tc.masterSecret.Secret, tc.masterSecret.Salt, tc.masterSecret.Info)
|
2022-05-02 07:21:07 -04:00
|
|
|
|
|
|
|
assert.NoError(err)
|
2022-08-01 03:11:13 -04:00
|
|
|
assert.Equal(tc.masterSecret.Output, stateDiskKey)
|
2022-05-02 07:21:07 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|