Skip to content

Commit

Permalink
Merge pull request #64 from underdog-tech/fix/reporttime-time-objects
Browse files Browse the repository at this point in the history
fix: Use time.Time structs for the reportTime passed to reporters
  • Loading branch information
tarkatronic authored Jun 13, 2023
2 parents 1dfce26 + ed78a24 commit af5ceee
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion internal/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Scan(cmd *cobra.Command, args []string) {
}
reporters = append(reporters, &reporting.ConsoleReporter{Config: userConfig})

reportTime := time.Now().UTC().Unix()
reportTime := time.Now().UTC()
ghOrgName, allRepos := api.QueryGithubOrgVulnerabilities(ghOrgLogin, *ghClient)
repositoryOwners := api.QueryGithubOrgRepositoryOwners(ghOrgLogin, *ghClient)
// Count our vulnerabilities
Expand Down
10 changes: 6 additions & 4 deletions reporting/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sort"
"sync"
"time"

"github.com/gookit/color"
"github.com/underdog-tech/vulnbot/config"
Expand Down Expand Up @@ -41,22 +42,23 @@ func (c *ConsoleReporter) SendSummaryReport(
header string,
numRepos int,
report VulnerabilityReport,
reportTime int64,
reportTime time.Time,
wg *sync.WaitGroup,
) error {
defer wg.Done()
summaryReport := color.Bold.Sprint(header) + "\n"
summaryReport += color.Style{color.OpItalic}.Sprint(reportTime.Format(time.RFC1123)) + "\n\n"
summaryReport += fmt.Sprintf("Total repositories: %d\n", numRepos)
summaryReport += fmt.Sprintf("Total vulnerabilities: %d\n", report.TotalCount)
summaryReport += fmt.Sprintf("Affected repositories: %d\n", report.AffectedRepos)
summaryReport += color.Bold.Sprint("Breakdown by Severity") + "\n"
summaryReport += "\n" + color.Bold.Sprint("Breakdown by Severity") + "\n"
severities := getSeverityReportOrder()
severityColors := getConsoleSeverityColors()
for _, severity := range severities {
title := color.HEX(severityColors[severity]).Sprint(severity)
summaryReport += fmt.Sprintf("%s: %d\n", title, report.VulnsBySeverity[severity])
}
summaryReport += color.Bold.Sprint("Breakdown by Ecosystem") + "\n"
summaryReport += "\n" + color.Bold.Sprint("Breakdown by Ecosystem") + "\n"
ecosystems := maps.Keys(report.VulnsByEcosystem)
sort.Strings(ecosystems)
ecosystemIcons := getConsoleEcosystemIcons()
Expand All @@ -72,7 +74,7 @@ func (c *ConsoleReporter) SendSummaryReport(
// of this could be quite overwhelming.
func (c *ConsoleReporter) SendTeamReports(
teamReports map[string]map[string]VulnerabilityReport,
reportTime int64,
reportTime time.Time,
wg *sync.WaitGroup,
) error {
defer wg.Done()
Expand Down
9 changes: 7 additions & 2 deletions reporting/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,24 @@ func TestSendConsoleSummaryReport(t *testing.T) {
severityColors := getConsoleSeverityColors()
ecosystemIcons := getConsoleEcosystemIcons()
expected := fmt.Sprintf(`%s
%s
Total repositories: 13
Total vulnerabilities: 42
Affected repositories: 2
%s
%s: 10
%s: 10
%s: 10
%s: 12
%s
%s Npm: 40
%s Pip: 2
`,
color.Bold.Sprint("OrgName Dependabot Report for now"),
color.Bold.Sprint("OrgName Dependabot Report"),
color.Style{color.OpItalic}.Sprint(TEST_REPORT_TIME_FORMATTED),
color.Bold.Sprint("Breakdown by Severity"),
color.HEX(severityColors["Critical"]).Sprint("Critical"),
color.HEX(severityColors["High"]).Sprint("High"),
Expand All @@ -55,7 +60,7 @@ Affected repositories: 2

wg := new(sync.WaitGroup)
wg.Add(1)
reporter.SendSummaryReport("OrgName Dependabot Report for now", 13, report, UNIX_TIME, wg)
reporter.SendSummaryReport("OrgName Dependabot Report", 13, report, TEST_REPORT_TIME, wg)
writer.Close()
written, _ := ioutil.ReadAll(reader)
os.Stdout = origStdout
Expand Down
9 changes: 6 additions & 3 deletions reporting/reporter.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package reporting

import "sync"
import (
"sync"
"time"
)

type Reporter interface {
SendSummaryReport(
header string,
numRepos int,
report VulnerabilityReport,
reportTime int64,
reportTime time.Time,
wg *sync.WaitGroup,
) error
SendTeamReports(
teamReports map[string]map[string]VulnerabilityReport,
reportTime int64,
reportTime time.Time,
wg *sync.WaitGroup,
) error
}
17 changes: 8 additions & 9 deletions reporting/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/underdog-tech/vulnbot/config"
"github.com/underdog-tech/vulnbot/logger"
"golang.org/x/exp/maps"

"strconv"

"github.com/slack-go/slack"
)

Expand All @@ -34,15 +33,15 @@ func (s *SlackReporter) buildSummaryReport(
header string,
numRepos int,
report VulnerabilityReport,
reportTime int64,
reportTime time.Time,
) slack.Message {
reportBlocks := []slack.Block{
slack.NewHeaderBlock(
slack.NewTextBlockObject(slack.PlainTextType, header, false, false),
),
slack.NewDividerBlock(),
slack.NewContextBlock("", slack.NewTextBlockObject(
slack.PlainTextType, strconv.Itoa(int(reportTime)), false, false,
slack.PlainTextType, reportTime.Format(time.RFC1123), false, false,
)),
slack.NewSectionBlock(
slack.NewTextBlockObject(
Expand Down Expand Up @@ -108,7 +107,7 @@ func (s *SlackReporter) SendSummaryReport(
header string,
numRepos int,
report VulnerabilityReport,
reportTime int64,
reportTime time.Time,
wg *sync.WaitGroup,
) error {
defer wg.Done()
Expand Down Expand Up @@ -151,7 +150,7 @@ func (s *SlackReporter) buildTeamRepositoryReport(
func (s *SlackReporter) buildTeamReport(
teamID string,
repos map[string]VulnerabilityReport,
reportTime int64,
reportTime time.Time,
) *SlackReport {
log := logger.Get()
teamInfo, err := config.GetTeamConfigBySlug(teamID, s.Config.Team)
Expand All @@ -169,7 +168,7 @@ func (s *SlackReporter) buildTeamReport(
),
slack.NewDividerBlock(),
slack.NewContextBlock("", slack.NewTextBlockObject(
slack.PlainTextType, strconv.Itoa(int(reportTime)), false, false,
slack.PlainTextType, reportTime.Format(time.RFC1123), false, false,
)),
slack.NewSectionBlock(
nil,
Expand Down Expand Up @@ -197,7 +196,7 @@ func (s *SlackReporter) buildTeamReport(

func (s *SlackReporter) buildAllTeamReports(
teamReports map[string]map[string]VulnerabilityReport,
reportTime int64,
reportTime time.Time,
) []*SlackReport {
slackMessages := []*SlackReport{}

Expand All @@ -212,7 +211,7 @@ func (s *SlackReporter) buildAllTeamReports(

func (s *SlackReporter) SendTeamReports(
teamReports map[string]map[string]VulnerabilityReport,
reportTime int64,
reportTime time.Time,
wg *sync.WaitGroup,
) error {
defer wg.Done()
Expand Down
19 changes: 9 additions & 10 deletions reporting/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package reporting
import (
"encoding/json"
"fmt"
"strconv"
"sync"
"testing"
"time"

"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/underdog-tech/vulnbot/config"
)

const (
UNIX_TIME = 1672534800
)
var TEST_REPORT_TIME time.Time = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
var TEST_REPORT_TIME_FORMATTED string = "Thu, 01 Jan 1970 00:00:00 UTC"

type MockSlackClient struct {
mock.Mock
Expand Down Expand Up @@ -112,7 +111,7 @@ func TestBuildSlackSummaryReport(t *testing.T) {
"elements": []map[string]interface{}{
{
"type": "plain_text",
"text": strconv.Itoa(int(UNIX_TIME)),
"text": TEST_REPORT_TIME_FORMATTED,
},
},
},
Expand Down Expand Up @@ -182,7 +181,7 @@ func TestBuildSlackSummaryReport(t *testing.T) {
},
}
expected, _ := json.Marshal(expected_data)
summary := reporter.buildSummaryReport("OrgName Vulnbot Report", 13, report, UNIX_TIME)
summary := reporter.buildSummaryReport("OrgName Vulnbot Report", 13, report, TEST_REPORT_TIME)
actual, _ := json.Marshal(summary)
assert.JSONEq(t, string(expected), string(actual))
}
Expand All @@ -197,7 +196,7 @@ func TestSendSlackSummaryReportSendsSingleMessage(t *testing.T) {

wg := new(sync.WaitGroup)
wg.Add(1)
reporter.SendSummaryReport("Foo", 1, report, UNIX_TIME, wg)
reporter.SendSummaryReport("Foo", 1, report, TEST_REPORT_TIME, wg)
wg.Wait()

mockClient.AssertExpectations(t)
Expand Down Expand Up @@ -294,7 +293,7 @@ func TestBuildSlackTeamReport(t *testing.T) {
"elements": []map[string]interface{}{
{
"type": "plain_text",
"text": strconv.Itoa(int(UNIX_TIME)),
"text": TEST_REPORT_TIME_FORMATTED,
},
},
},
Expand All @@ -315,7 +314,7 @@ func TestBuildSlackTeamReport(t *testing.T) {
},
}
expected, _ := json.Marshal(expectedData)
teamReport := reporter.buildTeamReport("TeamName", repoReports, UNIX_TIME)
teamReport := reporter.buildTeamReport("TeamName", repoReports, TEST_REPORT_TIME)
actual, _ := json.Marshal(teamReport.Message)
// Ensure the Slack Blocks match up
assert.JSONEq(t, string(expected), string(actual))
Expand Down Expand Up @@ -351,7 +350,7 @@ func TestSendSlackTeamReportsSendsMessagePerTeam(t *testing.T) {

wg := new(sync.WaitGroup)
wg.Add(1)
reporter.SendTeamReports(teamReports, UNIX_TIME, wg)
reporter.SendTeamReports(teamReports, TEST_REPORT_TIME, wg)
wg.Wait()

mockClient.AssertExpectations(t)
Expand Down

0 comments on commit af5ceee

Please sign in to comment.