Skip to content

Commit 71395ca

Browse files
committed
WIP:blackbox_tests: add clevis binding validation
1 parent 0069a26 commit 71395ca

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

tests/blackbox_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
318318
}
319319

320320
for _, disk := range test.Out {
321+
err = validateClevisBinding(t, disk.Partitions)
322+
if err != nil {
323+
return err
324+
}
321325
err = validateDisk(t, disk)
322326
if err != nil {
323327
return err

tests/positive/luks/creation.go

+4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ func LuksWithTPM() types.Test {
118118
}`
119119
configMinVersion := "3.2.0"
120120
in[0].Partitions.GetPartition("OEM").FilesystemType = "ext4"
121+
in[0].Partitions.GetPartition("OEM").ClevisBinding = ""
122+
in[0].Partitions.GetPartition("OEM").LuksDeviceName = ""
121123
out[0].Partitions.GetPartition("OEM").FilesystemType = "crypto_LUKS"
124+
out[0].Partitions.GetPartition("OEM").ClevisBinding = "tpm2"
125+
out[0].Partitions.GetPartition("OEM").LuksDeviceName = "luks-device-b"
122126

123127
return types.Test{
124128
Name: name,

tests/types/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ type Partition struct {
8686
Directories []Directory
8787
Links []Link
8888
RemovedNodes []Node
89+
ClevisBinding string
90+
LuksDeviceName string
8991
}
9092

9193
type MntDevice struct {

tests/validator.go

+45
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package blackbox
1616

1717
import (
1818
"context"
19+
"encoding/json"
1920
"fmt"
2021
"os"
2122
"os/exec"
@@ -25,6 +26,7 @@ import (
2526
"strings"
2627
"testing"
2728

29+
"github.com/coreos/ignition/v2/internal/distro"
2830
"github.com/coreos/ignition/v2/internal/exec/util"
2931
"github.com/coreos/ignition/v2/tests/types"
3032

@@ -139,6 +141,49 @@ func formatUUID(s string) string {
139141
return strings.ToUpper(strings.Replace(s, "-", "", -1))
140142
}
141143

144+
func validateClevisBinding(t *testing.T, expected []*types.Partition) error {
145+
for _, e := range expected {
146+
if e.ClevisBinding != "" && e.LuksDeviceName == "" {
147+
return fmt.Errorf("Expected LuksDeviceName for ClevisBinding %s", e.ClevisBinding)
148+
}
149+
150+
switch e.ClevisBinding {
151+
case "":
152+
continue
153+
case "tpm2":
154+
output, err := getLuksDump(e.LuksDeviceName)
155+
if err != nil {
156+
return fmt.Errorf("Error getting luks metadata: %v", err)
157+
}
158+
if len(output.Config.Flags) > 0 && output.Config.Flags[0] != "tpm2" {
159+
return fmt.Errorf("Expected tpm2 binding, got %s", output.Config.Flags[0])
160+
}
161+
continue
162+
default:
163+
return fmt.Errorf("Unknown clevis binding: %s", e.ClevisBinding)
164+
}
165+
}
166+
return nil
167+
}
168+
169+
type LuksDump struct {
170+
Config struct {
171+
Flags []string `json:"flags"`
172+
} `json:"config"`
173+
}
174+
175+
func getLuksDump(devAlias string) (LuksDump, error) {
176+
dump, err := exec.Command("sudo", distro.CryptsetupCmd(), "luksDump", "--dump-json-metadata", devAlias).CombinedOutput()
177+
if err != nil {
178+
return LuksDump{}, err
179+
}
180+
var ret LuksDump
181+
if err := json.Unmarshal(dump, &ret); err != nil {
182+
return LuksDump{}, fmt.Errorf("parsing luks metadata: %w", err)
183+
}
184+
return ret, nil
185+
}
186+
142187
func validateFilesystems(t *testing.T, expected []*types.Partition) error {
143188
for _, e := range expected {
144189
if e.FilesystemType == "" &&

0 commit comments

Comments
 (0)