Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework of CLI + Cobra #69

Merged
merged 39 commits into from
Jun 5, 2021
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1b7f188
Added new print module
Foxboron May 16, 2021
955c547
Added more fidelity to the logging methods
Foxboron May 16, 2021
0d249b2
Added bundle cli format
Foxboron May 16, 2021
7a4defc
Added deps
Foxboron May 16, 2021
431363f
Added list-files new WIP for commands
Foxboron May 16, 2021
62d653d
Added status new format
Foxboron May 16, 2021
2a53d52
Added list-bundles setup
Foxboron May 16, 2021
a05e6c8
Fixed commands with colors off
Foxboron May 16, 2021
bb78cf9
Remove previous logging, improve error bubling
Foxboron May 16, 2021
b82e17e
Return errors when generating bundles
Foxboron May 16, 2021
3568e9d
sbctl: Buble up errors from the "library"
Foxboron May 16, 2021
1508b29
Moved json out function
Foxboron May 17, 2021
23381e0
Added NotOK instead of "Error". Makes more sense semantically
Foxboron May 17, 2021
3d7f094
Added an iter function
Foxboron May 17, 2021
fb9b3c7
🤷
Foxboron May 17, 2021
30e16f5
Catch for unknown command
Foxboron May 17, 2021
70b00f3
Added new error
Foxboron May 18, 2021
d0022cb
Added BundleIter
Foxboron May 18, 2021
877ab49
Implement GetGUID
Foxboron May 18, 2021
adadb52
Give status the ability to display owner GUID
Foxboron May 18, 2021
3505f1b
New structure
Foxboron May 18, 2021
a5e0551
GUID package
Foxboron May 18, 2021
342ba34
Fixup
Foxboron May 18, 2021
235238c
Fixed lint issues
Foxboron May 18, 2021
f01453a
Change immutable error a little bit
Foxboron May 18, 2021
97435cc
More internal restructuring
Foxboron May 18, 2021
3454841
Moved create-keys top-level
Foxboron May 18, 2021
a318695
Moved generate-bundles top-level
Foxboron May 18, 2021
3f05d1d
Propegate errors better
Foxboron May 18, 2021
6dfc186
enroll changes
Foxboron May 19, 2021
b49ebbb
Added CanVerifyFiles
Foxboron May 19, 2021
8b4fc40
Added internal functions for checked paths, and CheckMSDos
Foxboron May 19, 2021
0d12167
Move verify to top-level
Foxboron May 19, 2021
fe514e1
Added errors to WriteFileDatabase
Foxboron May 19, 2021
6b0242c
Added print layout for key syncing
Foxboron May 19, 2021
57a1c93
Remove last of the log.* stuff
Foxboron May 19, 2021
ba0cee8
Make lint happy
Foxboron May 19, 2021
550b4e6
Move global flags to persistent
Foxboron May 22, 2021
ae1aec1
sbctl: Ensure all commands inherit stdout turning off
Foxboron May 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
sbctl: Buble up errors from the "library"
Signed-off-by: Morten Linderud <morten@linderud.pw>
  • Loading branch information
Foxboron committed May 30, 2021
commit 3568e9d34b49339568c343b730404ba1cf2b6d56
96 changes: 50 additions & 46 deletions cmd/sbctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"errors"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -46,8 +45,8 @@ func createKeysCmd() *cobra.Command {
return &cobra.Command{
Use: "create-keys",
Short: "Create a set of secure boot signing keys",
Run: func(cmd *cobra.Command, args []string) {
sbctl.CreateKeys()
RunE: func(cmd *cobra.Command, args []string) error {
return sbctl.CreateKeys()
},
}
}
Expand All @@ -56,8 +55,8 @@ func enrollKeysCmd() *cobra.Command {
return &cobra.Command{
Use: "enroll-keys",
Short: "Enroll the current keys to EFI",
Run: func(cmd *cobra.Command, args []string) {
sbctl.SyncKeys()
RunE: func(cmd *cobra.Command, args []string) error {
return sbctl.SyncKeys()
},
}
}
Expand All @@ -69,28 +68,30 @@ func signCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sign",
Short: "Sign a file with secure boot keys",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.Fatalf("Requires a file to sign...\n")
logging.Print("Requires a file to sign\n")
os.Exit(1)
}

// Ensure we have absolute paths
file, err := filepath.Abs(args[0])
if err != nil {
log.Fatal(err)
return err
}
if output == "" {
output = file
} else {
output, err = filepath.Abs(output)
if err != nil {
log.Fatal(err)
return err
}
}

if err := sbctl.Sign(file, output, save); err != nil {
log.Fatalln(err)
return err
}
return nil
},
}
f := cmd.Flags()
Expand All @@ -104,22 +105,21 @@ func signAllCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sign-all",
Short: "Sign all enrolled files with secure boot keys",
Run: func(cmd *cobra.Command, args []string) {
var outBundle error
outSign := false

RunE: func(cmd *cobra.Command, args []string) error {
if generate {
outBundle = sbctl.GenerateAllBundles(true)
if err := sbctl.GenerateAllBundles(true); err != nil {
logging.Fatal(err)
}
}

files, err := sbctl.ReadFileDatabase(sbctl.DBPath)
if err != nil {
log.Fatalln(err)
return err
}
for _, entry := range files {

if sbctl.SignFile(sbctl.DBKey, sbctl.DBCert, entry.File, entry.OutputFile, entry.Checksum) != nil {
outSign = true
if err := sbctl.SignFile(sbctl.DBKey, sbctl.DBCert, entry.File, entry.OutputFile, entry.Checksum); err != nil {
logging.Fatal(err)
continue
}

Expand All @@ -130,10 +130,7 @@ func signAllCmd() *cobra.Command {
sbctl.WriteFileDatabase(sbctl.DBPath, files)

}

if outBundle != nil || outSign {
log.Fatalln("Errors were encountered, see above")
}
return nil
},
}
f := cmd.Flags()
Expand All @@ -145,20 +142,22 @@ func removeFileCmd() *cobra.Command {
return &cobra.Command{
Use: "remove-file",
Short: "Remove file from database",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.Fatal("Need to specify file")
logging.Println("Need to specify file")
os.Exit(1)
}
files, err := sbctl.ReadFileDatabase(sbctl.DBPath)
if err != nil {
log.Fatalln(err)
return err
}
if _, ok := files[args[0]]; !ok {
log.Printf("File %s doesn't exist in database!\n", args[0])
logging.Print("File %s doesn't exist in database!\n", args[0])
os.Exit(1)
}
delete(files, args[0])
sbctl.WriteFileDatabase(sbctl.DBPath, files)
return nil
},
}
}
Expand All @@ -178,11 +177,12 @@ func verifyCmd() *cobra.Command {
return &cobra.Command{
Use: "verify",
Short: "Find and check if files in the ESP are signed or not",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if err := sbctl.VerifyESP(); err != nil {
// Really need to sort out the low level error handling
os.Exit(1)
return err
}
return nil
},
}
}
Expand Down Expand Up @@ -211,24 +211,25 @@ func bundleCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bundle",
Short: "Bundle the needed files for an EFI stub image",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.Fatalf("Requires a file to sign...\n")
logging.Print("Requires a file to sign...\n")
os.Exit(1)
}
checkFiles := []string{amducode, intelucode, splashImg, osRelease, efiStub, kernelImg, cmdline, initramfs}
for _, path := range checkFiles {
if path == "" {
continue
}
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatalf("%s does not exist!", path)
logging.Print("%s does not exist!\n", path)
os.Exit(1)
}
}
bundle := sbctl.NewBundle()
output, err := filepath.Abs(args[0])
if err != nil {
log.Fatal(err)
return err
}
// Fail early if user wants to save bundle but doesn't have permissions
var bundles sbctl.Bundles
Expand All @@ -237,7 +238,7 @@ func bundleCmd() *cobra.Command {
// to use ":=", which shadows the "bundles" variable
bundles, err = sbctl.ReadBundleDatabase(sbctl.BundleDBPath)
if err != nil {
log.Fatalln(err)
return err
}
}
bundle.Output = output
Expand All @@ -251,14 +252,14 @@ func bundleCmd() *cobra.Command {
bundle.EFIStub = efiStub
bundle.ESP = espPath
if err = sbctl.CreateBundle(*bundle); err != nil {
log.Fatalln(err)
os.Exit(1)
return err
}
if save {
bundles[bundle.Output] = bundle
sbctl.WriteBundleDatabase(sbctl.BundleDBPath, bundles)
sbctl.FormatBundle(bundle.Output, bundle)
}
return nil
},
}
esp := sbctl.GetESP()
Expand All @@ -281,8 +282,8 @@ func generateBundlesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate-bundles",
Short: "Generate all EFI stub bundles",
Run: func(cmd *cobra.Command, args []string) {
sbctl.GenerateAllBundles(sign)
RunE: func(cmd *cobra.Command, args []string) error {
return sbctl.GenerateAllBundles(sign)
},
}
f := cmd.Flags()
Expand All @@ -294,8 +295,12 @@ func listBundlesCmd() *cobra.Command {
return &cobra.Command{
Use: "list-bundles",
Short: "List stored bundles",
Run: func(cmd *cobra.Command, args []string) {
sbctl.ListBundles()
RunE: func(cmd *cobra.Command, args []string) error {
_, err := sbctl.ListBundles()
if err != nil {
return err
}
return nil
},
}
}
Expand All @@ -304,21 +309,23 @@ func removeBundleCmd() *cobra.Command {
return &cobra.Command{
Use: "remove-bundle",
Short: "Remove bundle from database",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
log.Fatal("Need to specify file")
logging.Print("Need to specify file\n")
os.Exit(1)
}
bundles, err := sbctl.ReadBundleDatabase(sbctl.BundleDBPath)
if err != nil {
log.Fatalln(err)
return err
}

if _, ok := bundles[args[0]]; !ok {
log.Printf("Bundle %s doesn't exist in database!\n", args[0])
logging.Print("Bundle %s doesn't exist in database!\n", args[0])
os.Exit(1)
}
delete(bundles, args[0])
sbctl.WriteBundleDatabase(sbctl.BundleDBPath, bundles)
return nil
},
}
}
Expand Down Expand Up @@ -371,9 +378,6 @@ func main() {
removeFileCmd(),
}
for _, c := range cmds {
c.PostRun = func(c *cobra.Command, args []string) {
sbctl.ColorsOff()
}
rootCmd.AddCommand(c)
}

Expand Down