Skip to content

Commit

Permalink
feat(pcr0tool): Add support of a logger
Browse files Browse the repository at this point in the history
  • Loading branch information
xaionaro committed Mar 2, 2023
1 parent 237c035 commit 77e09bc
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/bg-prov/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
Summary: true,
}))
cbnt.StrictOrderCheck = cli.ManifestStrictOrderCheck
fianoLog.DefaultLogger = log.DummyLogger{}
fianoLog.DefaultLogger = log.FianoLogger{}
err := ctx.Run(&context{Debug: cli.Debug})
ctx.FatalIfErrorf(err)
}
3 changes: 2 additions & 1 deletion cmd/pcr0tool/commands/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"flag"
)

Expand All @@ -21,5 +22,5 @@ type Command interface {
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
Execute(args []string)
Execute(ctx context.Context, args []string)
}
5 changes: 3 additions & 2 deletions cmd/pcr0tool/commands/diff/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package diff

import (
"context"
"encoding/hex"
"encoding/json"
"flag"
Expand Down Expand Up @@ -122,7 +123,7 @@ but ignore the overridden bytes. The value is represented in hex characters sepa
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
func (cmd Command) Execute(args []string) {
func (cmd Command) Execute(ctx context.Context, args []string) {
if len(args) != 2 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "expected amount of arguments is two, but received: %d\n", len(args))
usageAndExit()
Expand Down Expand Up @@ -182,7 +183,7 @@ func (cmd Command) Execute(args []string) {
assertNoError(err)
}

measurements, _, debugInfo, err := pcr.GetMeasurements(firmwareGood, 0, measureOpts...)
measurements, _, debugInfo, err := pcr.GetMeasurements(ctx, firmwareGood, 0, measureOpts...)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "GetPCRMeasurements error: %v\n", err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/pcr0tool/commands/displayeventlog/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package displayeventlog

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (cmd *Command) SetupFlagSet(flag *flag.FlagSet) {
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
func (cmd Command) Execute(args []string) {
func (cmd Command) Execute(ctx context.Context, args []string) {
if len(args) > 0 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: too many parameters\n")
usageAndExit()
Expand Down
3 changes: 2 additions & 1 deletion cmd/pcr0tool/commands/displayfwinfo/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package displayfwinfo

import (
"context"
"encoding/json"
"errors"
"flag"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (cmd *Command) SetupFlagSet(flag *flag.FlagSet) {
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
func (cmd Command) Execute(args []string) {
func (cmd Command) Execute(ctx context.Context, args []string) {
if len(args) < 1 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: no path to the firmware was specified\n")
usageAndExit()
Expand Down
3 changes: 2 additions & 1 deletion cmd/pcr0tool/commands/dumpfit/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dumpfit

import (
"context"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -44,7 +45,7 @@ func (cmd *Command) SetupFlagSet(flag *flag.FlagSet) {}
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
func (cmd Command) Execute(args []string) {
func (cmd Command) Execute(ctx context.Context, args []string) {
if len(args) < 1 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: no path to the firmare was specified\n")
usageAndExit()
Expand Down
3 changes: 2 additions & 1 deletion cmd/pcr0tool/commands/dumpregisters/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dumpregisters

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -42,7 +43,7 @@ func (cmd *Command) SetupFlagSet(flag *flag.FlagSet) {
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
func (cmd Command) Execute(args []string) {
func (cmd Command) Execute(ctx context.Context, args []string) {
if *cmd.txtPublicDump != "" && cmd.registers != nil {
panic(fmt.Errorf("cannot use flags -txt-public-dump and -registers together"))
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/pcr0tool/commands/printnodes/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package printnodes

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (v *visitor) Visit(f fianoUEFI.Firmware) error {
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
func (cmd Command) Execute(args []string) {
func (cmd Command) Execute(ctx context.Context, args []string) {
if len(args) < 1 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: no path to the firmare was specified\n")
usageAndExit()
Expand Down
7 changes: 5 additions & 2 deletions cmd/pcr0tool/commands/sum/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sum

import (
"context"
"crypto/sha1"
"crypto/sha256"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -72,7 +75,7 @@ func (cmd *Command) SetupFlagSet(flag *flag.FlagSet) {
// start the execution of the command.
//
// `args` are the arguments left unused by verb itself and options.
func (cmd Command) Execute(args []string) {
func (cmd Command) Execute(ctx context.Context, args []string) {
if len(args) < 1 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: no path to the firmare was specified\n")
usageAndExit()
Expand Down Expand Up @@ -133,7 +136,7 @@ func (cmd Command) Execute(args []string) {
firmware, err := uefi.ParseUEFIFirmwareFile(imagePath)
assertNoError(err)

measurements, flow, debugInfo, err := pcr.GetMeasurements(firmware, 0, measureOpts...)
measurements, flow, debugInfo, err := pcr.GetMeasurements(ctx, firmware, 0, measureOpts...)
var pcrLogger pcr.Printfer
if !*cmd.isQuiet {
debugInfoBytes, err := json.MarshalIndent(debugInfo, "", " ")
Expand Down
15 changes: 11 additions & 4 deletions cmd/pcr0tool/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"os"
Expand All @@ -14,6 +15,8 @@ import (
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/printnodes"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/sum"
"github.com/9elements/converged-security-suite/v2/pkg/log"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/facebookincubator/go-belt/tool/logger/implementation/logrus"
"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt"
fianoLog "github.com/linuxboot/fiano/pkg/log"
)
Expand All @@ -33,6 +36,8 @@ func usageAndExit() {
os.Exit(2) // the standard Go's exit-code on invalid flags
}

var logLevel = logger.LevelWarning

func setupFlag() {
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "syntax: pcr0tool <command> [options] {arguments}\n")
Expand All @@ -44,6 +49,7 @@ func setupFlag() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "\n")
}

flag.Var(&logLevel, "log-level", "")
flag.Parse()
if flag.NArg() < 1 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: no command specified\n\n")
Expand All @@ -53,10 +59,11 @@ func setupFlag() {

func main() {
cbnt.StrictOrderCheck = false // some firmwares have incorrect elements order, should parse them anyway
fianoLog.DefaultLogger = log.DummyLogger{}

setupFlag()

ctx := logger.CtxWithLogger(context.Background(), logrus.Default().WithLevel(logLevel))
fianoLog.DefaultLogger = log.NewFianoLogger(logger.FromCtx(ctx), logger.LevelTrace)

commandName := flag.Arg(0)
command := knownCommands[commandName]
if command == nil {
Expand All @@ -75,6 +82,6 @@ func main() {
flag.Usage = flagSet.Usage // so a the "command" could just call `flag.Usage()` to print it's usage

command.SetupFlagSet(flagSet)
_ = flagSet.Parse(os.Args[2:])
command.Execute(flagSet.Args())
_ = flagSet.Parse(os.Args[len(os.Args)-flag.NArg()+1:])
command.Execute(ctx, flagSet.Args())
}
2 changes: 1 addition & 1 deletion cmd/txt-prov/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
Summary: true,
}))
cbnt.StrictOrderCheck = cli.ManifestStrictOrderCheck
fianoLog.DefaultLogger = log.DummyLogger{}
fianoLog.DefaultLogger = log.FianoLogger{}

// Run commands
err := ctx.Run(&context{
Expand Down
2 changes: 1 addition & 1 deletion cmd/txt-suite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
Summary: true,
}))
cbnt.StrictOrderCheck = cli.ManifestStrictOrderCheck
fianoLog.DefaultLogger = log.DummyLogger{}
fianoLog.DefaultLogger = log.FianoLogger{}
err := ctx.Run(&context{})
ctx.FatalIfErrorf(err)
}
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e
github.com/edsrzf/mmap-go v1.1.0
github.com/facebookincubator/go-belt v0.0.0-20221130102226-a78fd808461b
github.com/facebookincubator/go-belt v0.0.0-20230119144706-62080641a285
github.com/fearful-symmetry/gomsr v0.0.1
github.com/golang-collections/go-datastructures v0.0.0-20150211160725-59788d5eb259
github.com/google/go-attestation v0.4.3
Expand All @@ -30,7 +30,6 @@ require (
)

require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/go-ng/slices v0.0.0-20220616195238-b8d239c57d65 // indirect
github.com/go-ng/sort v0.0.0-20220617173827-2cc7cd04f7c7 // indirect
github.com/go-ng/xsort v0.0.0-20220617174223-1d146907bccc // indirect
Expand All @@ -42,7 +41,12 @@ require (
github.com/intel-go/cpuid v0.0.0-20220614022739-219e067757cb // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d // indirect
)

require (
github.com/dustin/go-humanize v1.0.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/term v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.0.14/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/facebookincubator/go-belt v0.0.0-20221130102226-a78fd808461b h1:rijRsnMo8rlM0w+oDR0iAruOQyqq62G0NDiqjFHbYPk=
github.com/facebookincubator/go-belt v0.0.0-20221130102226-a78fd808461b/go.mod h1:IVtbJ8FYmTzOyJTBL6Mgkls2Rx/REnfuHAD1g4THRv4=
github.com/facebookincubator/go-belt v0.0.0-20230119144706-62080641a285 h1:/sKz12ioZnp1VZ6sUHQyfyn7AKbekJhCXFhhAzphnuU=
github.com/facebookincubator/go-belt v0.0.0-20230119144706-62080641a285/go.mod h1:IVtbJ8FYmTzOyJTBL6Mgkls2Rx/REnfuHAD1g4THRv4=
github.com/fanliao/go-promise v0.0.0-20141029170127-1890db352a72/go.mod h1:PjfxuH4FZdUyfMdtBio2lsRr1AKEaVPwelzuHuh8Lqc=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down Expand Up @@ -435,6 +435,8 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand Down Expand Up @@ -701,6 +703,7 @@ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
14 changes: 0 additions & 14 deletions pkg/log/dummy_logger.go

This file was deleted.

47 changes: 47 additions & 0 deletions pkg/log/fiano_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package log

import (
"github.com/facebookincubator/go-belt/tool/logger"
fianoLog "github.com/linuxboot/fiano/pkg/log"
)

var _ fianoLog.Logger = FianoLogger{}

// FianoLogger is an implementation of a fiano/pkg/log.Logger based
// on go-belt's Logger. Fiano is pretty noisy on low-important problems,
// so we enforce another logging level.
type FianoLogger struct {
Backend logger.Logger
LogAsLevel logger.Level
}

// NewFianoLogger returns a new instance of FianoLogger.
//
// Argument "log" is the actual logger used to log, and all non-fatal the logs
// are logged with the specified logging level "logAsLevel".
func NewFianoLogger(log logger.Logger, logAsLevel logger.Level) *FianoLogger {
return &FianoLogger{
Backend: log,
LogAsLevel: logAsLevel,
}
}

func (l FianoLogger) Warnf(format string, args ...interface{}) {
if l.Backend == nil {
return
}
l.Backend.Logf(l.LogAsLevel, "[warn] "+format, args...)
}
func (l FianoLogger) Errorf(format string, args ...interface{}) {
if l.Backend == nil {
return
}
l.Backend.Logf(l.LogAsLevel, "[error] "+format, args...)
}
func (l FianoLogger) Fatalf(format string, args ...interface{}) {
if l.Backend == nil {
logger.Default().Fatalf(format, args...)
return
}
l.Backend.Fatalf(format, args...)
}
Loading

0 comments on commit 77e09bc

Please sign in to comment.