Skip to content

Commit

Permalink
feat(cli): cache host vuln assessments for 1 hour
Browse files Browse the repository at this point in the history
This is the first feature we want implement where we cache an asset, in
this case, a host vulnerability assessment for one hour.

One main benefit is the ability to run further commands with additional
flags to manipulate the output of the assessment without having to fetch
the asset from the APIs again.

Why one hour!? Because our agent runs a scan and returns results (the
assessment) every day (24 hours)

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Nov 23, 2021
1 parent ad920ca commit 7f50bd2
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions cli/cmd/vuln_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -304,12 +305,22 @@ Grab a CVE id and feed it to the command:
return err
}

response, err := cli.LwApi.Vulnerabilities.Host.GetHostAssessment(args[0])
if err != nil {
return errors.Wrap(err, "unable to get host assessment with id "+args[0])
var (
assessment api.HostVulnHostAssessment
cacheKey = fmt.Sprintf("host/assessment/%s", args[0])
)
expired := cli.ReadCachedAsset(cacheKey, &assessment)
if expired {
response, err := cli.LwApi.Vulnerabilities.Host.GetHostAssessment(args[0])
if err != nil {
return errors.Wrap(err, "unable to get host assessment with id "+args[0])
}
assessment = response.Assessment

cli.WriteAssetToCache(cacheKey, time.Now().Add(time.Hour*1), assessment)
}

if err = buildVulnHostReports(response.Assessment); err != nil {
if err := buildVulnHostReports(assessment); err != nil {
return err
}

Expand All @@ -318,7 +329,7 @@ Grab a CVE id and feed it to the command:
"fail_on_severity", vulCmdState.FailOnSeverity,
"fail_on_fixable", vulCmdState.FailOnFixable,
)
assessmentCounts := response.Assessment.VulnerabilityCounts()
assessmentCounts := assessment.VulnerabilityCounts()
vulnPolicy := NewVulnerabilityPolicyError(
&assessmentCounts,
vulCmdState.FailOnSeverity,
Expand Down

0 comments on commit 7f50bd2

Please sign in to comment.