-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding error logs report for audit scans (#49)
- Loading branch information
1 parent
c9b84da
commit 35f6200
Showing
7 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |