2022-03-22 11:03:15 -04:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-24 10:17:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/mount/cryptmapper"
|
|
|
|
"github.com/edgelesssys/constellation/mount/kms"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-05-04 06:04:31 -04:00
|
|
|
"k8s.io/klog/v2"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DevicePath string = "testDevice"
|
|
|
|
DeviceName string = "testDeviceName"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setup() {
|
2022-08-17 07:50:43 -04:00
|
|
|
_ = exec.Command("/bin/dd", "if=/dev/zero", fmt.Sprintf("of=%s", DevicePath), "bs=64M", "count=1").Run()
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-19 02:47:17 -04:00
|
|
|
func teardown(devicePath string) {
|
2022-08-17 07:50:43 -04:00
|
|
|
_ = exec.Command("/bin/rm", "-f", devicePath).Run()
|
2022-05-19 02:47:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func copy(source, target string) error {
|
|
|
|
return exec.Command("cp", source, target).Run()
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
func resize() {
|
2022-08-17 07:50:43 -04:00
|
|
|
_ = exec.Command("/bin/dd", "if=/dev/zero", fmt.Sprintf("of=%s", DevicePath), "bs=32M", "count=1", "oflag=append", "conv=notrunc").Run()
|
2022-05-10 04:43:48 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if os.Getuid() != 0 {
|
2022-05-10 04:43:48 -04:00
|
|
|
fmt.Printf("This test suite requires root privileges, as libcryptsetup uses the kernel's device mapper.\n")
|
2022-03-22 11:03:15 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
klog.InitFlags(nil)
|
|
|
|
defer klog.Flush()
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
result := m.Run()
|
|
|
|
os.Exit(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenAndClose(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
setup()
|
2022-05-19 02:47:17 -04:00
|
|
|
defer teardown(DevicePath)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
kms := kms.NewStaticKMS()
|
2022-03-24 10:21:19 -04:00
|
|
|
mapper := cryptmapper.New(kms, &cryptmapper.CryptDevice{})
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
newPath, err := mapper.OpenCryptDevice(context.Background(), DevicePath, DeviceName, false)
|
|
|
|
require.NoError(err)
|
2022-05-10 04:43:48 -04:00
|
|
|
assert.Equal("/dev/mapper/"+DeviceName, newPath)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// assert crypt device got created
|
|
|
|
_, err = os.Stat(newPath)
|
|
|
|
assert.NoError(err)
|
|
|
|
// assert no integrity device got created
|
|
|
|
_, err = os.Stat(newPath + "_dif")
|
|
|
|
assert.True(os.IsNotExist(err))
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
// Resize the device
|
|
|
|
resize()
|
|
|
|
|
|
|
|
resizedPath, err := mapper.ResizeCryptDevice(context.Background(), DeviceName)
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal("/dev/mapper/"+DeviceName, resizedPath)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(mapper.CloseCryptDevice(DeviceName))
|
|
|
|
|
|
|
|
// assert crypt device got removed
|
|
|
|
_, err = os.Stat(newPath)
|
|
|
|
assert.True(os.IsNotExist(err))
|
2022-05-19 02:47:17 -04:00
|
|
|
|
|
|
|
// check if we can reopen the device
|
|
|
|
_, err = mapper.OpenCryptDevice(context.Background(), DevicePath, DeviceName, true)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NoError(mapper.CloseCryptDevice(DeviceName))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenAndCloseIntegrity(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
setup()
|
2022-05-19 02:47:17 -04:00
|
|
|
defer teardown(DevicePath)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
kms := kms.NewStaticKMS()
|
2022-03-24 10:21:19 -04:00
|
|
|
mapper := cryptmapper.New(kms, &cryptmapper.CryptDevice{})
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
newPath, err := mapper.OpenCryptDevice(context.Background(), DevicePath, DeviceName, true)
|
|
|
|
require.NoError(err)
|
2022-05-10 04:43:48 -04:00
|
|
|
assert.Equal("/dev/mapper/"+DeviceName, newPath)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// assert crypt device got created
|
|
|
|
_, err = os.Stat(newPath)
|
|
|
|
assert.NoError(err)
|
|
|
|
// assert integrity device got created
|
|
|
|
_, err = os.Stat(newPath + "_dif")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
// integrity devices do not support resizing
|
|
|
|
resize()
|
|
|
|
_, err = mapper.ResizeCryptDevice(context.Background(), DeviceName)
|
|
|
|
assert.Error(err)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(mapper.CloseCryptDevice(DeviceName))
|
|
|
|
|
|
|
|
// assert crypt device got removed
|
|
|
|
_, err = os.Stat(newPath)
|
|
|
|
assert.True(os.IsNotExist(err))
|
|
|
|
// assert integrity device got removed
|
|
|
|
_, err = os.Stat(newPath + "_dif")
|
|
|
|
assert.True(os.IsNotExist(err))
|
2022-05-19 02:47:17 -04:00
|
|
|
|
|
|
|
// check if we can reopen the device
|
|
|
|
_, err = mapper.OpenCryptDevice(context.Background(), DevicePath, DeviceName, true)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NoError(mapper.CloseCryptDevice(DeviceName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeviceCloning(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
setup()
|
|
|
|
defer teardown(DevicePath)
|
|
|
|
|
|
|
|
mapper := cryptmapper.New(&dynamicKMS{}, &cryptmapper.CryptDevice{})
|
|
|
|
|
|
|
|
_, err := mapper.OpenCryptDevice(context.Background(), DevicePath, DeviceName, false)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
require.NoError(copy(DevicePath, DevicePath+"-copy"))
|
|
|
|
defer teardown(DevicePath + "-copy")
|
|
|
|
|
|
|
|
_, err = mapper.OpenCryptDevice(context.Background(), DevicePath+"-copy", DeviceName+"-copy", false)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
assert.NoError(mapper.CloseCryptDevice(DeviceName))
|
|
|
|
assert.NoError(mapper.CloseCryptDevice(DeviceName + "-copy"))
|
|
|
|
}
|
|
|
|
|
|
|
|
type dynamicKMS struct{}
|
|
|
|
|
|
|
|
func (k *dynamicKMS) GetDEK(ctx context.Context, dekID string, dekSize int) ([]byte, error) {
|
|
|
|
key := make([]byte, dekSize)
|
|
|
|
for i := range key {
|
|
|
|
key[i] = 0x41 ^ dekID[i%len(dekID)]
|
|
|
|
}
|
|
|
|
return key, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|