Skip to content

Commit

Permalink
WIP:blackbox_tests: add clevis binding validation
Browse files Browse the repository at this point in the history
add fields to test.types.partition [ClevisBinding,LuksDeviceName] to
support validating the results of luks.
  • Loading branch information
prestist committed Apr 30, 2024
1 parent b55d5f2 commit e84adda
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
}

for _, disk := range test.Out {
err = validateClevisBinding(t, disk.Partitions)
if err != nil {
return err
}
err = validateDisk(t, disk)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions tests/positive/luks/creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ func LuksWithTPM() types.Test {
}`
configMinVersion := "3.2.0"
in[0].Partitions.GetPartition("OEM").FilesystemType = "ext4"
in[0].Partitions.GetPartition("OEM").ClevisBinding = ""
in[0].Partitions.GetPartition("OEM").LuksDeviceName = ""
out[0].Partitions.GetPartition("OEM").FilesystemType = "crypto_LUKS"
out[0].Partitions.GetPartition("OEM").ClevisBinding = "tpm2"
out[0].Partitions.GetPartition("OEM").LuksDeviceName = "luks-device-b"

return types.Test{
Name: name,
Expand Down
2 changes: 2 additions & 0 deletions tests/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type Partition struct {
Directories []Directory
Links []Link
RemovedNodes []Node
ClevisBinding string
LuksDeviceName string
}

type MntDevice struct {
Expand Down
45 changes: 45 additions & 0 deletions tests/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package blackbox

import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -25,6 +26,7 @@ import (
"strings"
"testing"

"github.com/coreos/ignition/v2/internal/distro"
"github.com/coreos/ignition/v2/internal/exec/util"
"github.com/coreos/ignition/v2/tests/types"

Expand Down Expand Up @@ -139,6 +141,49 @@ func formatUUID(s string) string {
return strings.ToUpper(strings.Replace(s, "-", "", -1))
}

func validateClevisBinding(t *testing.T, expected []*types.Partition) error {
for _, e := range expected {
if e.ClevisBinding != "" && e.LuksDeviceName == "" {
return fmt.Errorf("Expected LuksDeviceName for ClevisBinding %s", e.ClevisBinding)
}

switch e.ClevisBinding {
case "":
continue
case "tpm2":
output, err := getLuksDump(e.LuksDeviceName)
if err != nil {
return fmt.Errorf("Error getting luks metadata: %v", err)
}
if len(output.Config.Flags) > 0 && output.Config.Flags[0] != "tpm2" {
return fmt.Errorf("Expected tpm2 binding, got %s", output.Config.Flags[0])
}
continue
default:
return fmt.Errorf("Unknown clevis binding: %s", e.ClevisBinding)
}
}
return nil
}

type LuksDump struct {
Config struct {
Flags []string `json:"flags"`
} `json:"config"`
}

func getLuksDump(devAlias string) (LuksDump, error) {
dump, err := exec.Command("sudo", distro.CryptsetupCmd(), "luksDump", "--dump-json-metadata", devAlias).CombinedOutput()
if err != nil {
return LuksDump{}, err
}
var ret LuksDump
if err := json.Unmarshal(dump, &ret); err != nil {
return LuksDump{}, fmt.Errorf("parsing luks metadata: %w", err)
}
return ret, nil
}

func validateFilesystems(t *testing.T, expected []*types.Partition) error {
for _, e := range expected {
if e.FilesystemType == "" &&
Expand Down

0 comments on commit e84adda

Please sign in to comment.