2022-04-11 08:25:19 -04:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/nodestate"
|
2022-06-01 09:08:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/vtpm"
|
2022-06-28 12:33:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-06-28 10:51:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/logger"
|
2022-04-11 08:25:19 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-04-11 08:25:19 -04:00
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
func TestPrepareExistingDisk(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
fs afero.Afero
|
|
|
|
keyWaiter *stubKeyWaiter
|
|
|
|
mapper *stubMapper
|
|
|
|
mounter *stubMounter
|
|
|
|
openTPM vtpm.TPMOpenFunc
|
|
|
|
missingState bool
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-04-11 08:25:19 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
mounter: &stubMounter{},
|
|
|
|
openTPM: vtpm.OpenNOPTPM,
|
|
|
|
},
|
|
|
|
"WaitForDecryptionKey fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{waitErr: someErr},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
mounter: &stubMounter{},
|
|
|
|
openTPM: vtpm.OpenNOPTPM,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"MapDisk fails causes a repeat": {
|
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{},
|
|
|
|
mapper: &stubMapper{
|
|
|
|
uuid: "test",
|
|
|
|
mapDiskErr: someErr,
|
|
|
|
mapDiskRepeatedCalls: 2,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
mounter: &stubMounter{},
|
|
|
|
openTPM: vtpm.OpenNOPTPM,
|
|
|
|
wantErr: false,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"MkdirAll fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
mounter: &stubMounter{mkdirAllErr: someErr},
|
|
|
|
openTPM: vtpm.OpenNOPTPM,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"Mount fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
mounter: &stubMounter{mountErr: someErr},
|
|
|
|
openTPM: vtpm.OpenNOPTPM,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"Unmount fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
mounter: &stubMounter{unmountErr: someErr},
|
|
|
|
openTPM: vtpm.OpenNOPTPM,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"MarkNodeAsInitialized fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
mounter: &stubMounter{unmountErr: someErr},
|
|
|
|
openTPM: failOpener,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"no state file": {
|
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
keyWaiter: &stubKeyWaiter{},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
mounter: &stubMounter{},
|
|
|
|
openTPM: vtpm.OpenNOPTPM,
|
|
|
|
missingState: true,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
if !tc.missingState {
|
|
|
|
handler := file.NewHandler(tc.fs)
|
|
|
|
require.NoError(t, handler.WriteJSON(stateInfoPath, nodestate.NodeState{OwnerID: []byte("ownerID"), ClusterID: []byte("clusterID")}, file.OptMkdirAll))
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
setupManager := New(
|
|
|
|
logger.NewTest(t),
|
|
|
|
"test",
|
|
|
|
tc.fs,
|
|
|
|
tc.keyWaiter,
|
|
|
|
tc.mapper,
|
|
|
|
tc.mounter,
|
|
|
|
tc.openTPM,
|
|
|
|
)
|
2022-04-11 08:25:19 -04:00
|
|
|
|
|
|
|
err := setupManager.PrepareExistingDisk()
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 08:25:19 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.mapper.uuid, tc.keyWaiter.receivedUUID)
|
|
|
|
assert.True(tc.mapper.mapDiskCalled)
|
|
|
|
assert.True(tc.mounter.mountCalled)
|
|
|
|
assert.True(tc.mounter.unmountCalled)
|
|
|
|
assert.False(tc.mapper.formatDiskCalled)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func failOpener() (io.ReadWriteCloser, error) {
|
|
|
|
return nil, errors.New("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareNewDisk(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs afero.Afero
|
|
|
|
mapper *stubMapper
|
|
|
|
wantErr bool
|
2022-04-11 08:25:19 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
mapper: &stubMapper{uuid: "test"},
|
|
|
|
},
|
|
|
|
"creating directory fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewReadOnlyFs(afero.NewMemMapFs())},
|
|
|
|
mapper: &stubMapper{},
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"FormatDisk fails": {
|
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
mapper: &stubMapper{
|
|
|
|
uuid: "test",
|
|
|
|
formatDiskErr: someErr,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"MapDisk fails": {
|
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
mapper: &stubMapper{
|
|
|
|
uuid: "test",
|
|
|
|
mapDiskErr: someErr,
|
|
|
|
mapDiskRepeatedCalls: 1,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
setupManager := New(logger.NewTest(t), "test", tc.fs, nil, tc.mapper, nil, nil)
|
2022-04-11 08:25:19 -04:00
|
|
|
|
|
|
|
err := setupManager.PrepareNewDisk()
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 08:25:19 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.True(tc.mapper.formatDiskCalled)
|
|
|
|
assert.True(tc.mapper.mapDiskCalled)
|
|
|
|
|
|
|
|
data, err := tc.fs.ReadFile(filepath.Join(keyPath, keyFile))
|
|
|
|
require.NoError(t, err)
|
2022-06-28 12:33:27 -04:00
|
|
|
assert.Len(data, constants.RNGLengthDefault)
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadInitSecrets(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs afero.Afero
|
|
|
|
ownerID string
|
|
|
|
clusterID string
|
|
|
|
writeFile bool
|
|
|
|
wantErr bool
|
2022-04-11 08:25:19 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
ownerID: "ownerID",
|
|
|
|
clusterID: "clusterID",
|
|
|
|
writeFile: true,
|
|
|
|
},
|
|
|
|
"no state file": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"missing ownerID": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
clusterID: "clusterID",
|
|
|
|
writeFile: true,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"missing clusterID": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
ownerID: "ownerID",
|
|
|
|
writeFile: true,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"no IDs": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.Afero{Fs: afero.NewMemMapFs()},
|
|
|
|
writeFile: true,
|
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
if tc.writeFile {
|
|
|
|
handler := file.NewHandler(tc.fs)
|
|
|
|
state := nodestate.NodeState{ClusterID: []byte(tc.clusterID), OwnerID: []byte(tc.ownerID)}
|
|
|
|
require.NoError(handler.WriteJSON("/tmp/test-state.json", state, file.OptMkdirAll))
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
setupManager := New(logger.NewTest(t), "test", tc.fs, nil, nil, nil, nil)
|
2022-04-11 08:25:19 -04:00
|
|
|
|
|
|
|
ownerID, clusterID, err := setupManager.readInitSecrets("/tmp/test-state.json")
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 08:25:19 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal([]byte(tc.ownerID), ownerID)
|
|
|
|
assert.Equal([]byte(tc.clusterID), clusterID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubMapper struct {
|
|
|
|
formatDiskCalled bool
|
|
|
|
formatDiskErr error
|
|
|
|
mapDiskRepeatedCalls int
|
|
|
|
mapDiskCalled bool
|
|
|
|
mapDiskErr error
|
|
|
|
uuid string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubMapper) DiskUUID() string {
|
|
|
|
return s.uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubMapper) FormatDisk(passphrase string) error {
|
|
|
|
s.formatDiskCalled = true
|
|
|
|
return s.formatDiskErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubMapper) MapDisk(target string, passphrase string) error {
|
|
|
|
if s.mapDiskRepeatedCalls == 0 {
|
|
|
|
s.mapDiskErr = nil
|
|
|
|
}
|
|
|
|
s.mapDiskRepeatedCalls--
|
|
|
|
s.mapDiskCalled = true
|
|
|
|
return s.mapDiskErr
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubMounter struct {
|
|
|
|
mountCalled bool
|
|
|
|
mountErr error
|
|
|
|
unmountCalled bool
|
|
|
|
unmountErr error
|
|
|
|
mkdirAllErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubMounter) Mount(source string, target string, fstype string, flags uintptr, data string) error {
|
|
|
|
s.mountCalled = true
|
|
|
|
return s.mountErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubMounter) Unmount(target string, flags int) error {
|
|
|
|
s.unmountCalled = true
|
|
|
|
return s.unmountErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubMounter) MkdirAll(path string, perm fs.FileMode) error {
|
|
|
|
return s.mkdirAllErr
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubKeyWaiter struct {
|
|
|
|
receivedUUID string
|
|
|
|
decryptionKey []byte
|
|
|
|
waitErr error
|
|
|
|
waitCalled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubKeyWaiter) WaitForDecryptionKey(uuid, addr string) ([]byte, error) {
|
|
|
|
if s.waitCalled {
|
|
|
|
return nil, errors.New("wait called before key was reset")
|
|
|
|
}
|
|
|
|
s.waitCalled = true
|
|
|
|
s.receivedUUID = uuid
|
|
|
|
return s.decryptionKey, s.waitErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubKeyWaiter) ResetKey() {
|
|
|
|
s.waitCalled = false
|
|
|
|
}
|