sbctl/enroll-keys: Refactor immutable checking

Signed-off-by: Morten Linderud <morten@linderud.pw>
This commit is contained in:
Morten Linderud 2021-05-31 22:46:09 +02:00
parent 28eee4464d
commit 13bbb90c03
No known key found for this signature in database
GPG Key ID: E742683BA08CB2FF
1 changed files with 21 additions and 14 deletions

View File

@ -9,24 +9,31 @@ import (
"github.com/spf13/cobra"
)
func CheckImmutable() error {
var isImmutable bool
for _, file := range sbctl.EfivarFSFiles {
err := sbctl.IsImmutable(file)
if errors.Is(err, sbctl.ErrImmutable) {
isImmutable = true
logging.Warn("File is immutable: %s", file)
} else if errors.Is(err, sbctl.ErrNotImmutable) {
continue
} else if err != nil {
return fmt.Errorf("couldn't read file: %s", file)
}
}
if isImmutable {
return sbctl.ErrImmutable
}
return nil
}
var enrollKeysCmd = &cobra.Command{
Use: "enroll-keys",
Short: "Enroll the current keys to EFI",
RunE: func(cmd *cobra.Command, args []string) error {
var isImmutable bool
for _, file := range sbctl.EfivarFSFiles {
err := sbctl.IsImmutable(file)
if errors.Is(err, sbctl.ErrImmutable) {
isImmutable = true
logging.Warn("File is immutable: %s", file)
} else if errors.Is(err, sbctl.ErrNotImmutable) {
continue
} else if err != nil {
return fmt.Errorf("couldn't read file: %s", file)
}
}
if isImmutable {
return sbctl.ErrImmutable
if err := CheckImmutable(); err != nil {
return err
}
logging.Print("Syncing keys to EFI variables...")
synced := sbctl.SBKeySync(sbctl.KeysPath)