@@ -16,6 +16,7 @@ package blackbox
16
16
17
17
import (
18
18
"context"
19
+ "encoding/json"
19
20
"fmt"
20
21
"os"
21
22
"os/exec"
@@ -25,6 +26,7 @@ import (
25
26
"strings"
26
27
"testing"
27
28
29
+ "github.com/coreos/ignition/v2/internal/distro"
28
30
"github.com/coreos/ignition/v2/internal/exec/util"
29
31
"github.com/coreos/ignition/v2/tests/types"
30
32
@@ -139,6 +141,49 @@ func formatUUID(s string) string {
139
141
return strings .ToUpper (strings .Replace (s , "-" , "" , - 1 ))
140
142
}
141
143
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
+
142
187
func validateFilesystems (t * testing.T , expected []* types.Partition ) error {
143
188
for _ , e := range expected {
144
189
if e .FilesystemType == "" &&
0 commit comments