Skip to content

Commit

Permalink
Bugfixes vSphere and added stats to vulnmgmt
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker committed Dec 5, 2023
1 parent 41292c2 commit c6a7a55
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 74 deletions.
3 changes: 0 additions & 3 deletions providers/core/resources/core.lr
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ asset.eol @defaults("date") {
date time
}

// Vulnerability Information
vulnmgmt {}

// Date and time functions
time {
// The current time on the local system
Expand Down
47 changes: 0 additions & 47 deletions providers/core/resources/core.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion providers/core/resources/core.resources.json

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions providers/os/resources/asset_vuln.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ package resources

import (
"context"
"errors"
"time"

"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v9/llx"
"go.mondoo.com/cnquery/v9/logger"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/resources"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream/gql"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream/mvd"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream/mvd/cvss"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/util/convert"
Expand Down Expand Up @@ -115,31 +116,30 @@ func (p *mqlAsset) vulnerabilityReport() (interface{}, error) {
}

func getAdvisoryReport(runtime *plugin.Runtime) (*mvd.VulnReport, error) {
obj, err := CreateResource(runtime, "asset", map[string]*llx.RawData{})
if err != nil {
return nil, err
mcc := runtime.Upstream
if mcc == nil || mcc.ApiEndpoint == "" {
return nil, resources.MissingUpstreamError{}
}
asset := obj.(*mqlAsset)

r := asset.GetVulnerabilityReport()
if r.Error != nil {
return nil, r.Error
// get new gql client
mondooClient, err := gql.NewClient(mcc.UpstreamConfig, mcc.HttpClient)
if err != nil {
return nil, err
}
rawReport := r.Data

var vulnReport mvd.VulnReport
cfg := &mapstructure.DecoderConfig{
Metadata: nil,
Result: &vulnReport,
TagName: "json",
}
decoder, _ := mapstructure.NewDecoder(cfg)
err = decoder.Decode(rawReport)
gqlVulnReport, err := mondooClient.GetVulnCompactReport(runtime.Upstream.AssetMrn)
if err != nil {
return nil, err
}

return &vulnReport, nil
log.Debug().Interface("gqlReport", gqlVulnReport).Msg("search for asset vuln report")
if gqlVulnReport == nil {
return nil, errors.New("no vulnerability report available")
}

vulnReport := gql.ConvertToMvdVulnReport(gqlVulnReport)

return vulnReport, nil
}

func (a *mqlPlatformAdvisories) id() (string, error) {
Expand Down
4 changes: 3 additions & 1 deletion providers/os/resources/os.lr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ platform {
}

// Vulnerability Information
extend vulnmgmt {
vulnmgmt {
// List of all CVEs affecting the asset
cves() []vuln.cve
// List of all Advisories affecting the asset
Expand All @@ -72,6 +72,8 @@ extend vulnmgmt {
packages() []vuln.package
// Last time the vulnerability information was updated
lastAssessment() time
// Statistics about the vulnerabilities
stats() audit.cvss
}

// CVE information
Expand Down
24 changes: 24 additions & 0 deletions providers/os/resources/os.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions providers/os/resources/os.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ resources:
cves: {}
lastAssessment: {}
packages: {}
stats: {}
min_mondoo_version: latest
windows:
fields:
Expand Down
14 changes: 14 additions & 0 deletions providers/os/resources/vulnmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (v *mqlVulnmgmt) packages() ([]interface{}, error) {
return nil, v.populateData()
}

func (v *mqlVulnmgmt) stats() (*mqlAuditCvss, error) {
return nil, v.populateData()
}

func (v *mqlVulnmgmt) populateData() error {
vulnReport, err := v.getReport()
if err != nil {
Expand Down Expand Up @@ -147,9 +151,19 @@ func (v *mqlVulnmgmt) populateData() error {
mqlVulnPackages[i] = mqlVulnPackage
}

res, err := CreateResource(v.MqlRuntime, "audit.cvss", map[string]*llx.RawData{
"score": llx.FloatData(float64(vulnReport.Stats.Score.Value) / 10),
"vector": llx.StringData(vulnReport.Stats.Score.Vector),
})
if err != nil {
return err
}
statsCvssScore := res.(*mqlAuditCvss)

v.Advisories = plugin.TValue[[]interface{}]{Data: mqlVulAdvisories, State: plugin.StateIsSet}
v.Cves = plugin.TValue[[]interface{}]{Data: mqlVulnCves, State: plugin.StateIsSet}
v.Packages = plugin.TValue[[]interface{}]{Data: mqlVulnPackages, State: plugin.StateIsSet}
v.Stats = plugin.TValue[*mqlAuditCvss]{Data: statsCvssScore, State: plugin.StateIsSet}

return nil
}
Expand Down
4 changes: 3 additions & 1 deletion providers/vsphere/resources/vsphere.lr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ platform {
}

// Vulnerability Information
extend vulnmgmt {
vulnmgmt {
// List of all CVEs affecting the asset
cves() []vuln.cve
// List of all Advisories affecting the asset
Expand All @@ -30,6 +30,8 @@ extend vulnmgmt {
packages() []vuln.package
// Last time the vulnerability information was updated
lastAssessment() time
// Statistics about the vulnerabilities
stats() audit.cvss
}

// CVE information
Expand Down
24 changes: 24 additions & 0 deletions providers/vsphere/resources/vsphere.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions providers/vsphere/resources/vsphere.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,5 @@ resources:
cves: {}
lastAssessment: {}
packages: {}
stats: {}
min_mondoo_version: latest
19 changes: 16 additions & 3 deletions providers/vsphere/resources/vulnmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.mondoo.com/cnquery/v9/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/resources"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream/gql"
"go.mondoo.com/cnquery/v9/providers/os/connection/shared"
"go.mondoo.com/cnquery/v9/providers/vsphere/connection"
mondoogql "go.mondoo.com/mondoo-go"
)

Expand Down Expand Up @@ -67,6 +67,10 @@ func (v *mqlVulnmgmt) packages() ([]interface{}, error) {
return nil, v.populateData()
}

func (v *mqlVulnmgmt) stats() (*mqlAuditCvss, error) {
return nil, v.populateData()
}

func (v *mqlVulnmgmt) populateData() error {
vulnReport, err := v.getReport()
if err != nil {
Expand Down Expand Up @@ -133,8 +137,18 @@ func (v *mqlVulnmgmt) populateData() error {
mqlVulnCves[i] = mqlVulnCve
}

res, err := CreateResource(v.MqlRuntime, "audit.cvss", map[string]*llx.RawData{
"score": llx.FloatData(float64(vulnReport.Stats.Score.Value) / 10),
"vector": llx.StringData(vulnReport.Stats.Score.Vector),
})
if err != nil {
return err
}
statsCvssScore := res.(*mqlAuditCvss)

v.Advisories = plugin.TValue[[]interface{}]{Data: mqlVulAdvisories, State: plugin.StateIsSet}
v.Cves = plugin.TValue[[]interface{}]{Data: mqlVulnCves, State: plugin.StateIsSet}
v.Stats = plugin.TValue[*mqlAuditCvss]{Data: statsCvssScore, State: plugin.StateIsSet}

return nil
}
Expand Down Expand Up @@ -169,8 +183,7 @@ func (v *mqlVulnmgmt) getReport() (*gql.VulnReport, error) {
}

func (v *mqlVulnmgmt) getIncognitoReport(mondooClient *gql.MondooClient) (*gql.VulnReport, error) {
// FIXME: wrong connection
conn := v.MqlRuntime.Connection.(shared.Connection)
conn := v.MqlRuntime.Connection.(*connection.VsphereConnection)
platform := conn.Asset().Platform

gqlVulnReport, err := mondooClient.GetIncognitoVulnReport(mondoogql.PlatformInput{
Expand Down

0 comments on commit c6a7a55

Please sign in to comment.