2023-05-17 11:36:31 -04:00
|
|
|
//go:build integration && linux && cgo
|
2022-04-07 10:39:21 -04:00
|
|
|
|
2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2023-07-18 10:20:03 -04:00
|
|
|
"encoding/json"
|
2022-08-02 06:35:23 -04:00
|
|
|
"flag"
|
2022-04-05 09:12:20 -04:00
|
|
|
"fmt"
|
2024-02-08 09:20:01 -05:00
|
|
|
"log/slog"
|
2022-04-05 09:12:20 -04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-12-05 05:19:18 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2022-04-05 09:12:20 -04:00
|
|
|
"testing"
|
|
|
|
|
2023-12-05 05:19:18 -05:00
|
|
|
"github.com/bazelbuild/rules_go/go/runfiles"
|
2023-07-17 07:55:31 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/internal/diskencryption"
|
2023-07-18 10:20:03 -04:00
|
|
|
ccryptsetup "github.com/edgelesssys/constellation/v2/internal/cryptsetup"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-07-18 10:20:03 -04:00
|
|
|
cryptsetup "github.com/martinjungblut/go-cryptsetup"
|
2022-04-05 09:12:20 -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-04-05 09:12:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
devicePath = "testDevice"
|
|
|
|
mappedDevice = "mappedDevice"
|
|
|
|
)
|
|
|
|
|
2022-08-02 06:35:23 -04:00
|
|
|
var diskPath = flag.String("disk", "", "Path to the disk to use for the benchmark")
|
|
|
|
|
2024-03-12 06:43:38 -04:00
|
|
|
var toolsEnvs = []string{"DD", "RM"}
|
2023-12-05 05:19:18 -05:00
|
|
|
|
|
|
|
// addToolsToPATH is used to update the PATH to contain necessary tool binaries for
|
|
|
|
// coreutils.
|
|
|
|
func addToolsToPATH() error {
|
|
|
|
path := ":" + os.Getenv("PATH") + ":"
|
|
|
|
for _, tool := range toolsEnvs {
|
|
|
|
toolPath := os.Getenv(tool)
|
|
|
|
if toolPath == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toolPath, err := runfiles.Rlocation(toolPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pathComponent := filepath.Dir(toolPath)
|
|
|
|
if strings.Contains(path, ":"+pathComponent+":") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path = ":" + pathComponent + path
|
|
|
|
}
|
|
|
|
path = strings.Trim(path, ":")
|
|
|
|
os.Setenv("PATH", path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-02 06:35:23 -04:00
|
|
|
func setup(sizeGB int) error {
|
2023-12-05 05:19:18 -05:00
|
|
|
return exec.Command("dd", "if=/dev/random", fmt.Sprintf("of=%s", devicePath), "bs=1G", fmt.Sprintf("count=%d", sizeGB)).Run()
|
2022-04-05 09:12:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func teardown() error {
|
2023-12-05 05:19:18 -05:00
|
|
|
return exec.Command("rm", "-f", devicePath).Run()
|
2022-04-05 09:12:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-08-02 06:35:23 -04:00
|
|
|
flag.Parse()
|
|
|
|
|
2023-12-05 05:19:18 -05:00
|
|
|
// try to become root (best effort)
|
|
|
|
_ = syscall.Setuid(0)
|
2022-04-05 09:12:20 -04:00
|
|
|
if os.Getuid() != 0 {
|
|
|
|
fmt.Printf("This test suite requires root privileges, as libcrypsetup uses the kernel's device mapper.\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-12-05 05:19:18 -05:00
|
|
|
if err := addToolsToPATH(); err != nil {
|
|
|
|
fmt.Printf("Failed to add tools to PATH: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-04-05 09:12:20 -04:00
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/issues/1262
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
2024-01-16 11:45:35 -05:00
|
|
|
goleak.IgnoreAnyFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"),
|
2022-06-30 09:24:36 -04:00
|
|
|
)
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
result := m.Run()
|
|
|
|
os.Exit(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapper(t *testing.T) {
|
2022-08-02 06:35:23 -04:00
|
|
|
cryptsetup.SetDebugLevel(cryptsetup.CRYPT_LOG_ERROR)
|
|
|
|
cryptsetup.SetLogCallback(func(_ int, message string) { fmt.Println(message) })
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
2022-08-02 06:35:23 -04:00
|
|
|
require.NoError(setup(1), "failed to setup test disk")
|
2022-04-05 09:12:20 -04:00
|
|
|
defer func() { require.NoError(teardown(), "failed to delete test disk") }()
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
mapper, free, err := diskencryption.New(devicePath, logger.NewTextLogger(slog.LevelInfo))
|
2022-04-05 09:12:20 -04:00
|
|
|
require.NoError(err, "failed to initialize crypt device")
|
2023-07-17 07:55:31 -04:00
|
|
|
defer free()
|
2022-04-05 09:12:20 -04:00
|
|
|
|
2023-07-18 10:20:03 -04:00
|
|
|
assert.False(mapper.IsInitialized())
|
2022-04-12 08:24:36 -04:00
|
|
|
|
|
|
|
// Format and map disk
|
2022-04-05 09:12:20 -04:00
|
|
|
passphrase := "unit-test"
|
|
|
|
require.NoError(mapper.FormatDisk(passphrase), "failed to format disk")
|
|
|
|
require.NoError(mapper.MapDisk(mappedDevice, passphrase), "failed to map disk")
|
|
|
|
require.NoError(mapper.UnmapDisk(mappedDevice), "failed to remove disk mapping")
|
2022-04-12 08:24:36 -04:00
|
|
|
|
2023-07-18 10:20:03 -04:00
|
|
|
// Make sure token was set
|
|
|
|
ccrypt := ccryptsetup.New()
|
|
|
|
freeDevice, err := ccrypt.Init(devicePath)
|
|
|
|
require.NoError(err, "failed to initialize crypt device")
|
|
|
|
defer freeDevice()
|
|
|
|
require.NoError(ccrypt.LoadLUKS2(), "failed to load LUKS2")
|
|
|
|
|
|
|
|
tokenJSON, err := ccrypt.TokenJSONGet(ccryptsetup.ConstellationStateDiskTokenID)
|
|
|
|
require.NoError(err, "token should have been set")
|
|
|
|
var token struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Keyslots []string `json:"keyslots"`
|
|
|
|
DiskIsInitialized bool `json:"diskIsInitialized"`
|
|
|
|
}
|
|
|
|
require.NoError(json.Unmarshal([]byte(tokenJSON), &token))
|
|
|
|
assert.False(token.DiskIsInitialized, "disk should be marked as not initialized")
|
|
|
|
assert.False(ccrypt.ConstellationStateDiskTokenIsInitialized(), "disk should be marked as not initialized")
|
|
|
|
|
|
|
|
// Disk should still be marked as not initialized because token is set to false.
|
|
|
|
assert.False(mapper.IsInitialized())
|
2022-04-12 08:24:36 -04:00
|
|
|
|
|
|
|
// Try to map disk with incorrect passphrase
|
2022-04-05 09:12:20 -04:00
|
|
|
assert.Error(mapper.MapDisk(mappedDevice, "invalid-passphrase"), "was able to map disk with incorrect passphrase")
|
2022-04-11 08:25:19 -04:00
|
|
|
|
2023-07-18 10:20:03 -04:00
|
|
|
// Disk can be reformatted without manually re-initializing a mapper
|
|
|
|
passphrase2 := passphrase + "2"
|
|
|
|
require.NoError(mapper.FormatDisk(passphrase2), "failed to format disk")
|
|
|
|
require.NoError(mapper.MapDisk(mappedDevice, passphrase2), "failed to map disk")
|
|
|
|
require.NoError(mapper.UnmapDisk(mappedDevice), "failed to remove disk mapping")
|
2022-06-29 10:17:23 -04:00
|
|
|
}
|