Skip to content

Commit

Permalink
Review fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
gailazar300 committed Apr 7, 2024
1 parent e30c3d2 commit 07387e3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
12 changes: 8 additions & 4 deletions commands/audit/jas/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ func SetAnalyticsMetricsDataForAnalyzerManager(msi string, technologies []coreut
if len(technologies) != 1 {
// Only report analytics for one technology at a time.
return func() {
if resetAnalyzerManageJfMsiVar() != nil {
err = resetAnalyzerManageJfMsiVar()
if err != nil {
log.Debug(fmt.Sprintf(errMsg, "restoring", utils.JfMsiEnvVariable, err.Error()))
}
}
Expand All @@ -300,13 +301,16 @@ func SetAnalyticsMetricsDataForAnalyzerManager(msi string, technologies []coreut
log.Debug(fmt.Sprintf(errMsg, "setting", utils.JfLanguageEnvVariable, err.Error()))
}
return func() {
if resetAnalyzerManageJfMsiVar() != nil {
err = resetAnalyzerManageJfMsiVar()
if err != nil {
log.Debug(fmt.Sprintf(errMsg, "restoring", utils.JfMsiEnvVariable, err.Error()))
}
if resetAnalyzerManagerPackageManagerVar() != nil {
err = resetAnalyzerManagerPackageManagerVar()
if err != nil {
log.Debug(fmt.Sprintf(errMsg, "restoring", utils.JfPackageManagerEnvVariable, err.Error()))
}
if resetAnalyzerManagerLanguageVar() != nil {
err = resetAnalyzerManagerLanguageVar()
if err != nil {
log.Debug(fmt.Sprintf(errMsg, "restoring", utils.JfLanguageEnvVariable, err.Error()))
}
}
Expand Down
5 changes: 5 additions & 0 deletions commands/audit/jas/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func TestSetAnalyticsMetricsDataForAnalyzerManager(t *testing.T) {
assert.Equal(t, "", os.Getenv(utils.JfLanguageEnvVariable))
assert.Equal(t, "msi", os.Getenv(utils.JfMsiEnvVariable))
}},
{name: "Zero technologies", args: args{msi: "msi", technologies: []coreutils.Technology{}}, want: func() {
assert.Equal(t, "", os.Getenv(utils.JfPackageManagerEnvVariable))
assert.Equal(t, "", os.Getenv(utils.JfLanguageEnvVariable))
assert.Equal(t, "msi", os.Getenv(utils.JfMsiEnvVariable))
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
)

replace github.com/jfrog/jfrog-cli-core/v2 => github.com/gailazar300/jfrog-cli-core/v2 v2.0.0-20240403103644-317f145c1f66

//replace github.com/jfrog/jfrog-cli-core/v2 => ../jfrog-cli-core
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240404075604-3df49e9a9d64

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240403100335-8292671b7cc4

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gailazar300/jfrog-cli-core/v2 v2.0.0-20240403103644-317f145c1f66 h1:1hLXnLn/iA4S6Yg5g8SgvnQDkqNdEC4PMG6+lekehOI=
github.com/gailazar300/jfrog-cli-core/v2 v2.0.0-20240403103644-317f145c1f66/go.mod h1:iQoYSsjLWF8x//rtQCwNPE2ycle2X2x6VFQM0LQE2n0=
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
Expand Down Expand Up @@ -104,6 +102,8 @@ github.com/jfrog/gofrog v1.6.3 h1:F7He0+75HcgCe6SGTSHLFCBDxiE2Ja0tekvvcktW6wc=
github.com/jfrog/gofrog v1.6.3/go.mod h1:SZ1EPJUruxrVGndOzHd+LTiwWYKMlHqhKD+eu+v5Hqg=
github.com/jfrog/jfrog-apps-config v1.0.1 h1:mtv6k7g8A8BVhlHGlSveapqf4mJfonwvXYLipdsOFMY=
github.com/jfrog/jfrog-apps-config v1.0.1/go.mod h1:8AIIr1oY9JuH5dylz2S6f8Ym2MaadPLR6noCBO4C22w=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240404075604-3df49e9a9d64 h1:eCAqJ8hqJ6bqgmjswjpqhInJMG80MT5D2r465s/fXzg=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240404075604-3df49e9a9d64/go.mod h1:iQoYSsjLWF8x//rtQCwNPE2ycle2X2x6VFQM0LQE2n0=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240403100335-8292671b7cc4 h1:A67yoFRYjRzg+xhLYhH0QN7b4/wggRa/lSQKSjzOwNQ=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240403100335-8292671b7cc4/go.mod h1:tUyEmxznphh0nwAGo6xz9Sps7RRW/TBMxIJZteo+j2k=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down
43 changes: 6 additions & 37 deletions utils/analyticsmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (ams *AnalyticsMetricsService) CreateGeneralEvent(product xscservices.Produ

func (ams *AnalyticsMetricsService) AddGeneralEvent(event *xscservices.XscAnalyticsGeneralEvent) {
if !ams.ShouldReportEvents() {
log.Debug("A general event request was not sent to XSC - analytics metrics are disabled.")
log.Debug("Analytics metrics are disabled, skipping sending event request to XSC")
return
}
msi, err := ams.xscManager.AddAnalyticsGeneralEvent(*event)
Expand All @@ -127,16 +127,18 @@ func (ams *AnalyticsMetricsService) AddGeneralEvent(event *xscservices.XscAnalyt

func (ams *AnalyticsMetricsService) UpdateGeneralEvent(event *xscservices.XscAnalyticsGeneralEventFinalize) {
if !ams.ShouldReportEvents() {
log.Debug("A general event update request was not sent to XSC - analytics metrics are disabled.")
log.Debug("Analytics metrics are disabled, skipping sending update event request to XSC")
return
}
if ams.msi == "" {
log.Debug("A general event update request was not sent to XSC - failed to add the starting event, and there is no valid multi-scan identifier provided.")
log.Debug("MultiScanId is empty, skipping update general event.")
return
}
err := ams.xscManager.UpdateAnalyticsGeneralEvent(*event)
if err != nil {
log.Debug(fmt.Sprintf("failed updading general event request in XSC service for multi_scan_id %s, error: %s \"", ams.GetMsi(), err.Error()))
} else {
log.Debug(fmt.Sprintf("General event updated\n%v", *event))
}
}

Expand All @@ -161,44 +163,11 @@ func (ams *AnalyticsMetricsService) CreateXscAnalyticsGeneralEventFinalizeFromAu

basicEvent := xscservices.XscAnalyticsBasicGeneralEvent{
EventStatus: eventStatus,
TotalFindings: ams.CountScanResultsFindings(auditResults),
TotalFindings: auditResults.CountScanResultsFindings(),
TotalScanDuration: totalDuration.String(),
}
return &xscservices.XscAnalyticsGeneralEventFinalize{
MultiScanId: ams.msi,
XscAnalyticsBasicGeneralEvent: basicEvent,
}
}

// Counts the total amount of findings in the provided results and updates the AnalyticsMetricsService with the amount of the new added findings
func (ams *AnalyticsMetricsService) CountScanResultsFindings(scanResults *Results) int {
findingsCountMap := make(map[string]int)
var totalFindings int

// Counting ScaResults
for _, scaResult := range scanResults.ScaResults {
for _, xrayResult := range scaResult.XrayResults {
// XrayResults may contain Vulnerabilities OR Violations, but not both. Therefore, only one of them will be counted
for _, vulnerability := range xrayResult.Vulnerabilities {
findingsCountMap[vulnerability.IssueId] += len(vulnerability.Components)
}

for _, violation := range xrayResult.Violations {
findingsCountMap[violation.IssueId] += len(violation.Components)
}
}
}

for _, issueIdCount := range findingsCountMap {
totalFindings += issueIdCount
}

// Counting ExtendedScanResults
if scanResults.ExtendedScanResults != nil {
totalFindings += len(scanResults.ExtendedScanResults.SastScanResults)
totalFindings += len(scanResults.ExtendedScanResults.IacScanResults)
totalFindings += len(scanResults.ExtendedScanResults.SecretsScanResults)
}

return totalFindings
}
33 changes: 33 additions & 0 deletions utils/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ func (r *Results) IsIssuesFound() bool {
return false
}

// Counts the total amount of findings in the provided results and updates the AnalyticsMetricsService with the amount of the new added findings
func (r *Results) CountScanResultsFindings() int {
findingsCountMap := make(map[string]int)
var totalFindings int

// Counting ScaResults
for _, scaResult := range r.ScaResults {
for _, xrayResult := range scaResult.XrayResults {
// XrayResults may contain Vulnerabilities OR Violations, but not both. Therefore, only one of them will be counted
for _, vulnerability := range xrayResult.Vulnerabilities {
findingsCountMap[vulnerability.IssueId] += len(vulnerability.Components)
}

for _, violation := range xrayResult.Violations {
findingsCountMap[violation.IssueId] += len(violation.Components)
}
}
}

for _, issueIdCount := range findingsCountMap {
totalFindings += issueIdCount
}

// Counting ExtendedScanResults
if r.ExtendedScanResults != nil {
totalFindings += len(r.ExtendedScanResults.SastScanResults)
totalFindings += len(r.ExtendedScanResults.IacScanResults)
totalFindings += len(r.ExtendedScanResults.SecretsScanResults)
}

return totalFindings
}

type ScaScanResult struct {
Technology coreutils.Technology `json:"Technology"`
WorkingDirectory string `json:"WorkingDirectory"`
Expand Down

0 comments on commit 07387e3

Please sign in to comment.