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

Adding error logs report for audit scans #49

Merged
merged 47 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
12dbe00
Add Xsc General Event
gailazar300 Mar 24, 2024
cbc4fcd
Add Xsc General Event
gailazar300 Mar 26, 2024
1951727
Before review fixes.
gailazar300 Mar 26, 2024
c834ad1
Add UpdateGeneralEvent.
gailazar300 Mar 27, 2024
75a161e
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Mar 27, 2024
f9c9c0e
Set environments variables for analyzer manager.
gailazar300 Mar 27, 2024
3c616d1
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Mar 28, 2024
a4f0534
Add tests.
gailazar300 Mar 28, 2024
abc6553
Remove error handling for analytics.
gailazar300 Mar 28, 2024
e79cb1e
Change AnalyticsMetricsService creation.
gailazar300 Mar 28, 2024
b45d003
Merge remote-tracking branch 'upstream/dev' into feature/XRAY-36905
gailazar300 Mar 31, 2024
b3452da
go.mod+sum
gailazar300 Mar 31, 2024
fdb5051
Minor fixes.
gailazar300 Mar 31, 2024
1abd3d1
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Mar 31, 2024
4c68943
Review fixes.
gailazar300 Apr 1, 2024
18cb1bc
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 1, 2024
2c9fcba
Add integration test.
gailazar300 Apr 1, 2024
fc0f2df
Minor fixes.
gailazar300 Apr 1, 2024
610af6d
report error to coralogix initial push
eranturgeman Apr 1, 2024
ca2c92f
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 2, 2024
b9560da
Change add general event flow for Frogbot.
gailazar300 Apr 2, 2024
3c2043a
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 2, 2024
040ce9b
Second review fixes.
gailazar300 Apr 2, 2024
ad4855a
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 2, 2024
40884cb
Remove Frogbot's MSI validation - there is no common flow for cli and…
gailazar300 Apr 2, 2024
51de09a
Minor fix.
gailazar300 Apr 2, 2024
15dc089
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 2, 2024
df4ef8f
Minor fix.
gailazar300 Apr 3, 2024
08b1764
Minor fix.
gailazar300 Apr 3, 2024
59891b4
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 3, 2024
afc8843
Change UpdateGeneralEvent & moving relevant code from core
gailazar300 Apr 3, 2024
b133a9a
go.mod+sum
gailazar300 Apr 3, 2024
1a2c652
minor fix
gailazar300 Apr 3, 2024
abea15c
minor fix
gailazar300 Apr 3, 2024
aa40d8a
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 3, 2024
f0fdb3a
Fix sending scan to xsc if available.
gailazar300 Apr 3, 2024
e30c3d2
Add CountScanResultsFindings.
gailazar300 Apr 3, 2024
4080b7a
Merge branch 'feature/XRAY-36905' of https://github.com/gailazar300/j…
eranturgeman Apr 3, 2024
b9ae5c6
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-security int…
eranturgeman Apr 8, 2024
2b2b765
pull dev and fix conflicts
eranturgeman Apr 8, 2024
2554105
fix conflicts after pull
eranturgeman Apr 8, 2024
991c3db
minor fixes before CR
eranturgeman Apr 8, 2024
d019cd6
minor fixes before CR
eranturgeman Apr 8, 2024
e4ffc2c
fix CR comments
eranturgeman Apr 9, 2024
4fd91af
fix CR comments
eranturgeman Apr 9, 2024
e1c3a33
updating test location
eranturgeman Apr 9, 2024
d304405
fix go.mod
eranturgeman Apr 10, 2024
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
29 changes: 27 additions & 2 deletions cli/scancommands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/utils/usage"
"os"
"strings"

Expand Down Expand Up @@ -328,7 +330,26 @@ func AuditCmd(c *components.Context) error {
}
}
auditCmd.SetTechnologies(technologies)
return progressbar.ExecWithProgress(auditCmd)
err = progressbar.ExecWithProgress(auditCmd)

// Reporting error if Xsc service is enabled
reportErrorIfExists(err, auditCmd)
return err
}

func reportErrorIfExists(err error, auditCmd *audit.AuditCommand) {
if err == nil || !usage.ShouldReportUsage() {
return
}
var serverDetails *coreConfig.ServerDetails
serverDetails, innerError := auditCmd.ServerDetails()
if innerError != nil {
log.Debug(fmt.Sprintf("failed to get server details for error report: %q", innerError))
return
}
if reportError := utils.ReportError(serverDetails, err, "cli"); reportError != nil {
log.Debug("failed to report error log:" + reportError.Error())
}
}

func createAuditCmd(c *components.Context) (*audit.AuditCommand, error) {
Expand Down Expand Up @@ -398,7 +419,11 @@ func AuditSpecificCmd(c *components.Context, technology coreutils.Technology) er
}
technologies := []string{string(technology)}
auditCmd.SetTechnologies(technologies)
return progressbar.ExecWithProgress(auditCmd)
err = progressbar.ExecWithProgress(auditCmd)

// Reporting error if Xsc service is enabled
reportErrorIfExists(err, auditCmd)
return err
}

func CurationCmd(c *components.Context) error {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
)

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-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240408074156-13680c04f22e

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

// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go dev
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ 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/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240408074156-13680c04f22e h1:PjCzGWHyJqK4j1MP3osPDDAW6KBXMJlBypOxKtp/ZKo=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240408074156-13680c04f22e/go.mod h1:qXAP68g+DlyX2wk5znNbQdK2CcEHfOLOfYXPzdlnkxI=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240409191434-4e96d77edd64 h1:q0GV0IdhYdTqEkNykRwNZP0qNEE8j9dWfY9uKovDPzM=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240409191434-4e96d77edd64/go.mod h1:tUyEmxznphh0nwAGo6xz9Sps7RRW/TBMxIJZteo+j2k=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
25 changes: 25 additions & 0 deletions utils/errorreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/jfrog/jfrog-client-go/xsc/services"
)

// Sends an error report when the Xsc service is enabled.
// Errors returned by this function typically do not disrupt the flow, as reporting errors is optional.
func ReportError(serverDetails *config.ServerDetails, errorToReport error, source string) error {
log.Debug("Sending an error report to JFrog analytics...")
xscManager, err := CreateXscServiceManager(serverDetails)
if err != nil {
return fmt.Errorf("failed to create an HTTP client: %s.\nReporting to JFrog analytics is skipped...", err.Error())
}

errorLog := &services.ExternalErrorLog{
Log_level: "error",
Source: source,
Message: errorToReport.Error(),
}
return SendXscLogMessageIfEnabled(errorLog, xscManager)
}
69 changes: 69 additions & 0 deletions utils/errorreport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package utils

import (
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
clienttestutils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

const (
unsupportedXscVersionForErrorLogs = "1.6.0"
supportedXscVersionForErrorLogs = minXscVersionForErrorReport
)

func TestReportLogErrorEventPossible(t *testing.T) {
restoreEnvVarFunc := clienttestutils.SetEnvWithCallbackAndAssert(t, coreutils.ReportUsage, "")
defer restoreEnvVarFunc()

testCases := []struct {
serverCreationFunc func() (*httptest.Server, *config.ServerDetails)
expectedResponse bool
}{
{
serverCreationFunc: func() (*httptest.Server, *config.ServerDetails) {
serverMock, serverDetails, _ := CreateXscRestsMockServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/xsc/api/v1/system/version" {
w.WriteHeader(http.StatusNotFound)
_, innerError := w.Write([]byte("Xsc service is not enabled"))
if innerError != nil {
return
}
}
})
return serverMock, serverDetails
},
expectedResponse: false,
},
{
serverCreationFunc: func() (*httptest.Server, *config.ServerDetails) { return xscServer(t, "") },
expectedResponse: false,
},
{
serverCreationFunc: func() (*httptest.Server, *config.ServerDetails) {
return xscServer(t, unsupportedXscVersionForErrorLogs)
},
expectedResponse: false,
},
{
serverCreationFunc: func() (*httptest.Server, *config.ServerDetails) { return xscServer(t, supportedXscVersionForErrorLogs) },
expectedResponse: true,
},
}

for _, testcase := range testCases {
mockServer, serverDetails := testcase.serverCreationFunc()
xscManager, err := CreateXscServiceManager(serverDetails)
assert.NoError(t, err)
reportPossible := IsReportLogErrorEventPossible(xscManager)
if testcase.expectedResponse {
assert.True(t, reportPossible)
} else {
assert.False(t, reportPossible)
}
mockServer.Close()
}
}
34 changes: 33 additions & 1 deletion utils/xscmanager.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package utils

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
clientconfig "github.com/jfrog/jfrog-client-go/config"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/jfrog/jfrog-client-go/xsc"
"github.com/jfrog/jfrog-client-go/xsc/services"

clientconfig "github.com/jfrog/jfrog-client-go/config"
)

const minXscVersionForErrorReport = "1.7.7"

func CreateXscServiceManager(serviceDetails *config.ServerDetails) (*xsc.XscServicesManager, error) {
xscDetails, err := serviceDetails.CreateXscAuthConfig()
if err != nil {
Expand All @@ -19,3 +26,28 @@ func CreateXscServiceManager(serviceDetails *config.ServerDetails) (*xsc.XscServ
}
return xsc.New(serviceConfig)
}

func SendXscLogMessageIfEnabled(errorLog *services.ExternalErrorLog, xscManager *xsc.XscServicesManager) error {
if !IsReportLogErrorEventPossible(xscManager) {
return nil
}
return xscManager.SendXscLogErrorRequest(errorLog)
}

// Determines if reporting the error is feasible.
func IsReportLogErrorEventPossible(xscManager *xsc.XscServicesManager) bool {
xscVersion, err := xscManager.GetVersion()
if err != nil {
log.Debug(fmt.Sprintf("failed to check availability of Xsc service:%s\nReporting to JFrog analytics is skipped...", err.Error()))
return false
}
if xscVersion == "" {
log.Debug("Xsc service is not available. Reporting to JFrog analytics is skipped...")
return false
}
if err = clientutils.ValidateMinimumVersion(clientutils.Xsc, xscVersion, minXscVersionForErrorReport); err != nil {
log.Debug(err.Error())
return false
}
return true
}
31 changes: 31 additions & 0 deletions xsc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"errors"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-security/tests"
"github.com/jfrog/jfrog-cli-security/utils"
"github.com/stretchr/testify/assert"
"testing"
)

func TestReportError(t *testing.T) {
serverDetails := &config.ServerDetails{
Url: *tests.JfrogUrl,
ArtifactoryUrl: *tests.JfrogUrl + tests.ArtifactoryEndpoint,
XrayUrl: *tests.JfrogUrl + tests.XrayEndpoint,
AccessToken: *tests.JfrogAccessToken,
ServerId: tests.ServerId,
}

// Prior to initiating the test, we verify whether Xsc is enabled for the customer. If not, the test is skipped.
xscManager, err := utils.CreateXscServiceManager(serverDetails)
assert.NoError(t, err)

if !utils.IsReportLogErrorEventPossible(xscManager) {
t.Skip("Skipping test since Xsc server is not enabled or below minimal required version")
}

errorToReport := errors.New("THIS IS NOT A REAL ERROR! This Error is posted as part of TestReportError test")
assert.NoError(t, utils.ReportError(serverDetails, errorToReport, "cli"))
}
Loading