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

✨ Use structured errors #439

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions apps/cnspec/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ var serveCmd = &cobra.Command{
// log errors
if result != nil && result.GetErrors() != nil && len(result.GetErrors()) > 0 {
assetErrors := result.GetErrors()
for a, err := range assetErrors {
log.Error().Err(errors.New(err)).Str("asset", a).Msg("could not connect to asset")
for a, errStatus := range assetErrors {
log.Error().Err(errors.New(errStatus.Message)).Str("asset", a).Msg("could not connect to asset")
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/reporter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func ReportCollectionToJSON(data *policy.ReportCollection, out shared.OutputHelp
"\"errors\":" +
"{")
pre = ""
for id, err := range data.Errors {
out.WriteString(pre + llx.PrettyPrintString(id) + ":" + llx.PrettyPrintString(err))
for id, errStatus := range data.Errors {
out.WriteString(pre + llx.PrettyPrintString(id) + ":" + llx.PrettyPrintString(errStatus.Message))
pre = ","
}
out.WriteString("}}")
Expand Down
4 changes: 2 additions & 2 deletions cli/reporter/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ReportCollectionToJunit(r *policy.ReportCollection, out shared.OutputHelper
suites := junit.Testsuites{}

// render asset errors
for assetMrn, errMsg := range r.Errors {
for assetMrn, errStatus := range r.Errors {
a := r.Assets[assetMrn]

properties := []junit.Property{}
Expand All @@ -36,7 +36,7 @@ func ReportCollectionToJunit(r *policy.ReportCollection, out shared.OutputHelper
Name: "Scan " + a.Name,
Failure: &junit.Result{
Type: "error",
Message: errMsg,
Message: errStatus.Message,
},
},
},
Expand Down
31 changes: 29 additions & 2 deletions cli/reporter/print_compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"go.mondoo.com/cnquery/upstream/mvd"
cnspecComponents "go.mondoo.com/cnspec/cli/components"
"go.mondoo.com/cnspec/policy"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)

type assetMrnName struct {
Expand Down Expand Up @@ -242,6 +245,23 @@ func (r *defaultReporter) printAssetsByPlatform(assetsByPlatform map[string][]*p
} else {
assetScoreRating = policy.ScoreRating_error
assetScore = "X"
if r.data.Errors[assetsByPlatform[platform][i].Mrn] != nil {
es := r.data.Errors[assetsByPlatform[platform][i].Mrn]
ei := errdetails.ErrorInfo{}
pberr := anypb.UnmarshalTo(es.Details[0], &ei, proto.UnmarshalOptions{})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explorer ErrorStatusCode is buried in the details, so I have to unpack it here.

if pberr != nil {
log.Error().Err(pberr).Msg("failed to unmarshal error info")
} else {
if _, ok := ei.Metadata["errorCode"]; ok {
statusCode := ei.Metadata["errorCode"]
esc := explorer.NewErrorStatusCodeFromString(statusCode)
if esc.Category() == explorer.ErrorCategoryWarning || esc.Category() == explorer.ErrorCategoryInformational {
assetScoreRating = policy.ScoreRating_unrated
assetScore = "U"
}
}
}
}
}
scoreColor := cnspecComponents.DefaultRatingColors.Color(assetScoreRating)
output := fmt.Sprintf(" %s %s", termenv.String(assetScore).Foreground(scoreColor), assetsByPlatform[platform][i].Name)
Expand Down Expand Up @@ -315,9 +335,16 @@ func (r *defaultReporter) printAssetSections(orderedAssets []assetMrnName) {

r.out.Write([]byte(r.Printer.H2("Asset: " + target)))

errorMsg, ok := r.data.Errors[assetMrn]
errStatus, ok := r.data.Errors[assetMrn]
if ok {
r.out.Write([]byte(r.Printer.Error(errorMsg)))
switch errStatus.ErrorCode().Category() {
case explorer.ErrorCategoryInformational:
r.out.Write([]byte(errStatus.Message))
case explorer.ErrorCategoryWarning:
r.out.Write([]byte(r.Printer.Warn(errStatus.Message)))
case explorer.ErrorCategoryError:
r.out.Write([]byte(r.Printer.Error(errStatus.Message)))
}
r.out.Write([]byte(NewLineCharacter + NewLineCharacter))
continue
}
Expand Down
2 changes: 1 addition & 1 deletion cli/reporter/print_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *reportRenderer) print() error {
assetLine := termenv.String(fmt.Sprintf("■ Asset: %s%s", name, NewLineCharacter)).
Foreground(colors.DefaultColorTheme.Critical).String()
res.WriteString(assetLine)
errLine := termenv.String(stringx.Indent(2, fmt.Sprintf("Error: %s%s", errMsg, NewLineCharacter))).
errLine := termenv.String(stringx.Indent(2, fmt.Sprintf("Error: %s%s", errMsg.Message, NewLineCharacter))).
Foreground(colors.DefaultColorTheme.Critical).String()
errLine = strings.ReplaceAll(errLine, "\n", NewLineCharacter)
res.WriteString(errLine)
Expand Down
4 changes: 2 additions & 2 deletions cli/reporter/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func (s *summaryPrinter) GenerateStats(report *policy.ReportCollection) summaryS
stats.assetNames[assetMrn] = report.Assets[assetMrn].Name
assetReport, ok := report.Reports[assetMrn]
if !ok {
if errMsg := report.Errors[assetMrn]; errMsg != "" {
if errStatus := report.Errors[assetMrn]; errStatus != nil {
stats.assetScores[assetMrn] = &policy.Score{
QrId: assetMrn,
Type: policy.ScoreType_Error,
Message: errMsg,
Message: errStatus.Message,
}
} else {
stats.assetScores[assetMrn] = &policy.Score{
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
go.mondoo.com/cnquery v0.0.0-20230309071919-a3f56da5a8af
go.mondoo.com/cnquery v0.0.0-20230313121739-52d6a28db951
go.mondoo.com/ranger-rpc v0.5.1-0.20220923135836-9e7732899d34
go.opentelemetry.io/otel v1.14.0
golang.org/x/sync v0.1.0
Expand Down Expand Up @@ -379,7 +379,7 @@ require (
github.com/nishanths/exhaustive v0.9.5 // indirect
github.com/nishanths/predeclared v0.2.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/okta/okta-sdk-golang/v2 v2.14.0 // indirect
github.com/okta/okta-sdk-golang/v2 v2.17.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/packethost/packngo v0.25.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,8 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/okta/okta-sdk-golang/v2 v2.14.0 h1:Z2Gs6lnKALHkCw4AM6+h/nLkXKzmiR5bGnfXEsylicg=
github.com/okta/okta-sdk-golang/v2 v2.14.0/go.mod h1:dz30v3ctAiMb7jpsCngGfQUAEGm1/NsWT92uTbNDQIs=
github.com/okta/okta-sdk-golang/v2 v2.17.0 h1:awY6FyunU4bA90KsQ4ygCJm2qunZTkjZKAq4DbphwSQ=
github.com/okta/okta-sdk-golang/v2 v2.17.0/go.mod h1:dz30v3ctAiMb7jpsCngGfQUAEGm1/NsWT92uTbNDQIs=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down Expand Up @@ -1342,6 +1344,8 @@ gitlab.com/bosi/decorder v0.2.3 h1:gX4/RgK16ijY8V+BRQHAySfQAb354T7/xQpDB2n10P0=
gitlab.com/bosi/decorder v0.2.3/go.mod h1:9K1RB5+VPNQYtXtTDAzd2OEftsZb1oV0IrJrzChSdGE=
go.mondoo.com/cnquery v0.0.0-20230309071919-a3f56da5a8af h1:HZpScT8IrCwRa/o7iJosucitrhp/EjSAhcUxvjCo46U=
go.mondoo.com/cnquery v0.0.0-20230309071919-a3f56da5a8af/go.mod h1:iI/TeZcXvyQioi2LPjx5PJ6kUUn/ILZA1fi9YB8J5hw=
go.mondoo.com/cnquery v0.0.0-20230313121739-52d6a28db951 h1:znzsoi1UBd1kUVehoLikANRIxfchNeukCqGAdekR1v0=
go.mondoo.com/cnquery v0.0.0-20230313121739-52d6a28db951/go.mod h1:YDPb/M+uohxkbkSGyk0xKfLQRPYQ6ssH2cfgoI0hkmY=
go.mondoo.com/ranger-rpc v0.5.1-0.20220923135836-9e7732899d34 h1:mtPZ1J+nRI/ivV+n41bjIwY6Rfxb2Jf49svZSQMGHIA=
go.mondoo.com/ranger-rpc v0.5.1-0.20220923135836-9e7732899d34/go.mod h1:3YKcqFrlPgaB4FZ4EoLgdmRtwMQdO7RoAkZYFn+F1eY=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
Loading