Skip to content

Commit

Permalink
feat: add logrus as the default logger
Browse files Browse the repository at this point in the history
This commit replaces the fmt.Printf/Println functions with logrus.

Signed-off-by: Christian Walter <[email protected]>
  • Loading branch information
walterchris committed Mar 22, 2024
1 parent 8e176a0 commit db7fc9a
Show file tree
Hide file tree
Showing 16 changed files with 316 additions and 304 deletions.
33 changes: 17 additions & 16 deletions cmd/amd-suite/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/linuxboot/fiano/pkg/amd/psb"

amd_manifest "github.com/linuxboot/fiano/pkg/amd/manifest"
log "github.com/sirupsen/logrus"
)

// Context for kong command line parser
Expand Down Expand Up @@ -129,7 +130,7 @@ func (s *showKeysCmd) Run(ctx *context) error {
return fmt.Errorf("could not extract keys from the firmware image: %w", err)
}

fmt.Println(keySet.String())
log.Infof("%s", keySet.String())
return nil
}

Expand Down Expand Up @@ -164,7 +165,7 @@ func (v *validatePSPEntriesCmd) Run(ctx *context) error {
}

for _, validation := range signatureValidations {
fmt.Println(validation.String())
log.Infof("%s", validation.String())
}
return nil
}
Expand All @@ -179,7 +180,7 @@ func (v *validateRTMCmd) Run(ctx *context) error {
if err != nil {
return err
}
fmt.Println(signatureValidation.String())
log.Infof("%s", signatureValidation.String())
return nil
}

Expand All @@ -203,15 +204,15 @@ func dumpHelper(fwPath string, entry string, resultFile string,
defer func() {
err := f.Close()
if err != nil {
fmt.Printf("could not close file %s after dumping entry %x", resultFile, id)
log.Errorf("could not close file %s after dumping entry %x", resultFile, id)
}
}()

n, err := dump(amdFw, uint32(id), f)
if err != nil {
return err
}
fmt.Println("Entry size / Number of written bytes ", n)
log.Infof("Entry size / Number of written bytes %d", n)
return nil
}

Expand Down Expand Up @@ -246,7 +247,7 @@ func patchHelper(fwPath string, entry string, entryFile string, resultFile strin
}
defer func() {
if err := inFile.Close(); err != nil {
fmt.Printf("could not close modified entry file %s: %v", entryFile, err)
log.Errorf("could not close modified entry file %s: %v", entryFile, err)
}
}()

Expand All @@ -257,7 +258,7 @@ func patchHelper(fwPath string, entry string, entryFile string, resultFile strin
defer func() {
err := outFile.Close()
if err != nil {
fmt.Printf("could not close file %s after dumping entry %x", resultFile, id)
log.Errorf("could not close file %s after dumping entry %x", resultFile, id)
}
}()

Expand All @@ -266,7 +267,7 @@ func patchHelper(fwPath string, entry string, entryFile string, resultFile strin
return err
}

fmt.Println("Firmware size / Number of written bytes ", n)
log.Infof("Firmware size / Number of written bytes %d", n)
return nil
}

Expand Down Expand Up @@ -321,12 +322,12 @@ func (v *outputAPCBSecurityTokensCmd) Run(ctx *context) error {
tokenID = fmt.Sprintf("0x%X", token.ID)
}

fmt.Println("============")
fmt.Printf("Token ID: %s\n", tokenID)
fmt.Printf("Priority Mask: %s\n", token.PriorityMask)
fmt.Printf("Board Mask: 0x%X\n", token.BoardMask)
fmt.Printf("Value: 0x%X\n", token.NumValue())
fmt.Println("============")
log.Info("============")
log.Infof("Token ID: %s", tokenID)
log.Infof("Priority Mask: %s", token.PriorityMask)
log.Infof("Board Mask: 0x%X", token.BoardMask)
log.Infof("Value: 0x%X", token.NumValue())
log.Info("============")
}
}
return nil
Expand Down Expand Up @@ -355,10 +356,10 @@ func (v *setAPCBSecurityTokenCmd) Run(ctx *context) error {
if err != nil {
return fmt.Errorf("unable to UpsertToken: %w", err)
}
fmt.Printf("successfully UPSERT-ed to %#+v\n", entry)
log.Infof("successfully UPSERT-ed to %#+v", entry)
}

err = ioutil.WriteFile(v.FwPath, b, 0)
err = os.WriteFile(v.FwPath, b, 0)
if err != nil {
return fmt.Errorf("unable to write to file '%s': %w", v.FwPath, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/bg-prov/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntkey"
"github.com/linuxboot/fiano/pkg/intel/metadata/common/bgheader"
"github.com/linuxboot/fiano/pkg/intel/metadata/fit"
log "github.com/sirupsen/logrus"

"github.com/linuxboot/fiano/pkg/uefi"

Expand Down Expand Up @@ -357,7 +358,7 @@ func (biosp *biosPrintCmd) Run(ctx *context) error {
if err != nil {
return err
}
fmt.Printf("%s", table.String())
log.Infof("%s", table.String())
err = bootguard.PrintStructures(data)
if err != nil {
return err
Expand Down Expand Up @@ -1189,7 +1190,7 @@ func (p printFITCmd) Run(ctx *context) error {
if err != nil {
return err
}
fmt.Printf("%s", table.String())
log.Infof("%s", table.String())
return nil
}

Expand Down
11 changes: 9 additions & 2 deletions cmd/bg-prov/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"github.com/9elements/converged-security-suite/v2/pkg/log"
"github.com/alecthomas/kong"
"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt"
fianoLog "github.com/linuxboot/fiano/pkg/log"
log "github.com/sirupsen/logrus"
)

const (
Expand All @@ -27,7 +27,14 @@ func main() {
Summary: true,
}))
cbnt.StrictOrderCheck = cli.ManifestStrictOrderCheck
fianoLog.DefaultLogger = log.FianoLogger{}

if cli.Debug {
log.SetLevel(log.DebugLevel)
}

fianologger := log.StandardLogger()

fianoLog.DefaultLogger = fianologger
err := ctx.Run(&context{Debug: cli.Debug})
ctx.FatalIfErrorf(err)
}
42 changes: 19 additions & 23 deletions cmd/bg-suite/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"os"
Expand All @@ -12,6 +11,7 @@ import (

"github.com/9elements/converged-security-suite/v2/pkg/test"
"github.com/9elements/converged-security-suite/v2/pkg/tools"
log "github.com/sirupsen/logrus"

"github.com/9elements/go-linux-lowlevel-hw/pkg/hwapi"

Expand Down Expand Up @@ -60,7 +60,7 @@ func (e *execTestsCmd) Run(ctx *context) error {
}
switch e.Set {
case "all":
fmt.Println("For more information about the documents and chapters, run: bg-suite -m")
log.Info("For more information about the documents and chapters, run: bg-suite -m")
ret = run("All", getTests(), &preset, e.Interactive)
case "static":
ret = run("Static", getStaticTest(), &preset, e.Interactive)
Expand Down Expand Up @@ -98,7 +98,7 @@ func (e *execTestsCmd) Run(ctx *context) error {
func (l *listCmd) Run(ctx *context) error {
tests := getTests()
for i := range tests {
fmt.Printf("Test No: %v, %v\n", i, tests[i].Name)
log.Infof("Test No: %v, %v", i, tests[i].Name)
}
return nil
}
Expand All @@ -107,8 +107,9 @@ func (m *markdownCmd) Run(ctx *context) error {
var teststate string
tests := getTests()

fmt.Println("Id | Test | Implemented | Document | Chapter")
fmt.Println("------------|------------|------------|------------|------------")
log.Info("Id | Test | Implemented | Document | Chapter")
log.Info("------------|------------|------------|------------|------------")

for i := range tests {
if tests[i].Status == test.Implemented {
teststate = ":white_check_mark:"
Expand All @@ -121,7 +122,7 @@ func (m *markdownCmd) Run(ctx *context) error {
if docID != "" {
docID = "Document " + docID
}
fmt.Printf("%02d | %-48s | %-22s | %-28s | %-56s\n", i, tests[i].Name, teststate, docID, tests[i].SpecificationChapter)
log.Infof("%02d | %-48s | %-22s | %-28s | %-56s", i, tests[i].Name, teststate, docID, tests[i].SpecificationChapter)
}
return nil
}
Expand Down Expand Up @@ -161,16 +162,11 @@ func getRuntimeTest() []*test.Test {

func run(testGroup string, tests []*test.Test, preset *test.PreSet, interactive bool) bool {
result := false
f := bufio.NewWriter(os.Stdout)

hwAPI := hwapi.GetAPI()

fmt.Printf("\n%s tests\n", a.Bold(a.Gray(20-1, testGroup).BgGray(4-1)))
var i int
for i = 0; i < len(testGroup)+6; i++ {
fmt.Print("_")
}
fmt.Println()
log.Infof("%s tests", a.Bold(a.Gray(20-1, testGroup).BgGray(4-1)))
log.Info("--------------------------------------------------")
for idx := range tests {
if len(testnos) > 0 {
// SearchInt returns an index where to "insert" idx
Expand Down Expand Up @@ -202,38 +198,38 @@ func run(testGroup string, tests []*test.Test, preset *test.PreSet, interactive
data, _ := json.MarshalIndent(t, "", "")
err := os.WriteFile(logfile, data, 0o664)
if err != nil {
fmt.Println("Error writing log file")
log.Errorf("Error writing log file: %v", err)
}

// If not interactive, we just print the results and return
result = true
}

for index := range tests {
var s string

if tests[index].Status == test.NotImplemented {
continue
}
if tests[index].Result == test.ResultNotRun {
continue
}
fmt.Printf("%02d - ", index)
fmt.Printf("%-40s: ", a.Bold(tests[index].Name))
f.Flush()
s += fmt.Sprintf("%02d - ", index)
s += fmt.Sprintf("%-40s: ", a.Bold(tests[index].Name))

if tests[index].Result == test.ResultPass {
fmt.Printf("%-20s", a.Bold(a.Green(tests[index].Result)))
s += fmt.Sprintf("%-20s", a.Bold(a.Green(tests[index].Result)))
} else {
fmt.Printf("%-20s", a.Bold(a.Red(tests[index].Result)))
s += fmt.Sprintf("%-20s", a.Bold(a.Red(tests[index].Result)))
result = false
}
if tests[index].ErrorText != "" {
fmt.Printf(" (%s)", tests[index].ErrorText)
s += fmt.Sprintf(" (%s)", tests[index].ErrorText)
} else if len(tests[index].ErrorText) == 0 && tests[index].Result == test.ResultFail {
fmt.Print(" (No error text given)")
s += fmt.Sprintf(" (No error text given)")
}
fmt.Printf("\n")
log.Infof("%s", s)

f.Flush()
}

return result
Expand Down
14 changes: 6 additions & 8 deletions cmd/txt-prov/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ import (
"github.com/9elements/go-linux-lowlevel-hw/pkg/hwapi"
"github.com/google/go-tpm/tpm"
"github.com/google/go-tpm/tpm2"
"golang.org/x/crypto/ssh/terminal"
log "github.com/sirupsen/logrus"
"golang.org/x/term"
)

var (
tpm2LockedResult = "error code 0x22"
)
var tpm2LockedResult = "error code 0x22"

func readPassphraseHashTPM20() ([]byte, error) {
fmt.Printf("Now, please type in the password (mandatory): ")
password, err := terminal.ReadPassword(0)
log.Info("Now, please type in the password (mandatory): ")
password, err := term.ReadPassword(0)
if err != nil {
return []byte{}, err
}
fmt.Println()
hash := sha256.Sum256([]byte(password))
return hash[:], nil
}
Expand All @@ -37,7 +35,7 @@ func writePSPolicy2file(policy *tools.LCPPolicy2, filename string) error {
if err != nil {
return err
}
if err = os.WriteFile(filename, buf.Bytes(), 0600); err != nil {
if err = os.WriteFile(filename, buf.Bytes(), 0o600); err != nil {
return err
}
return nil
Expand Down
Loading

0 comments on commit db7fc9a

Please sign in to comment.