Skip to content

Commit

Permalink
Transition Xsc to Xray (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Nov 24, 2024
1 parent 5032a9b commit 50bd3e5
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 207 deletions.
4 changes: 3 additions & 1 deletion artifactory/services/utils/tests/xray/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,9 @@ const BuildScanResultsResponse = `
]
}
`
const xscVersionResponse = `{"xsc_version": "1.0.0"}`
const xscVersionResponse = `{"xsc_version": "%s","xray_version":"3.107.8"}`

const xrayVersionResponse = `{"xray_version":"%s","xray_revision":"5735964"}`

const scanIdResponse = `{"scan_id": "3472b4e2-bddc-11ee-a9c9-acde48001122"}`

Expand Down
63 changes: 33 additions & 30 deletions artifactory/services/utils/tests/xray/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package xray

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -186,17 +185,30 @@ func buildScanHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid reports request", http.StatusBadRequest)
}

func xscGetVersionHandlerFunc(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
func xscGetVersionHandlerFunc(t *testing.T, version string) func(w http.ResponseWriter, r *http.Request) {
expectedResponse := fmt.Sprintf(xscVersionResponse, version)
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
_, err := fmt.Fprint(w, xscVersionResponse)
_, err := fmt.Fprint(w, expectedResponse)
assert.NoError(t, err)
return
}
http.Error(w, "Invalid xsc request", http.StatusBadRequest)
}
}

func xrayGetVersionHandlerFunc(t *testing.T, version string) func(w http.ResponseWriter, r *http.Request) {
expectedResponse := fmt.Sprintf(xrayVersionResponse, version)
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
_, err := fmt.Fprint(w, expectedResponse)
assert.NoError(t, err)
return
}
http.Error(w, "Invalid xray request", http.StatusBadRequest)
}
}

func enrichGetScanId(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
Expand Down Expand Up @@ -230,43 +242,34 @@ func enrichGetResults(t *testing.T) func(w http.ResponseWriter, r *http.Request)
}
}

func xscGitInfoHandlerFunc(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
req, err := io.ReadAll(r.Body)
assert.NoError(t, err)
if r.Method == http.MethodPost {
var reqBody services.XscGitInfoContext
err = json.Unmarshal(req, &reqBody)
assert.NoError(t, err)
if reqBody.GitRepoUrl == "" || reqBody.BranchName == "" || reqBody.CommitHash == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := fmt.Fprint(w, XscGitInfoBadResponse)
assert.NoError(t, err)
return
}
w.WriteHeader(http.StatusCreated)
_, err = fmt.Fprint(w, XscGitInfoResponse)
assert.NoError(t, err)
return
}
http.Error(w, "Invalid xsc request", http.StatusBadRequest)
}
type MockServerParams struct {
MSI string
XrayVersion string
XscVersion string
}

func StartXrayMockServer(t *testing.T) int {
params := MockServerParams{MSI: TestMultiScanId, XrayVersion: "3.0.0", XscVersion: "1.0.0"}
return StartXrayMockServerWithParams(t, params)
}

func StartXrayMockServerWithParams(t *testing.T, params MockServerParams) int {
handlers := clienttests.HttpServerHandlers{}

handlers["/"] = http.NotFound
// Xray handlers
handlers["/xray/api/v1/system/version"] = xrayGetVersionHandlerFunc(t, params.XrayVersion)
handlers["/api/xray/scanBuild"] = scanBuildHandler
handlers["/api/v2/summary/artifact"] = artifactSummaryHandler
handlers["/api/v1/entitlements/feature/"] = entitlementsHandler
handlers["/xsc/api/v1/system/version"] = xscGetVersionHandlerFunc(t)
handlers["/xsc/api/v1/gitinfo"] = xscGitInfoHandlerFunc(t)
handlers["/xray/api/v1/scan/import_xml"] = enrichGetScanId(t)
handlers[fmt.Sprintf("/xray/api/v1/scan/graph/%s", params.MSI)] = enrichGetResults(t)
handlers["/xray/api/v1/configuration/jas"] = getJasConfig(t)
getEnrichResults := fmt.Sprintf("/xray/api/v1/scan/graph/%s", TestMultiScanId)
handlers[getEnrichResults] = enrichGetResults(t)
handlers[fmt.Sprintf("/%s/", services.ReportsAPI)] = reportHandler
handlers[fmt.Sprintf("/%s/", services.BuildScanAPI)] = buildScanHandler
handlers["/"] = http.NotFound
handlers[fmt.Sprintf("/%s/", services.ReportsAPI)] = reportHandler
// Xsc handlers
handlers["/xsc/api/v1/system/version"] = xscGetVersionHandlerFunc(t, params.XscVersion)
handlers["/xray/api/v1/xsc/system/version"] = xscGetVersionHandlerFunc(t, params.XscVersion)

port, err := clienttests.StartHttpServer(handlers)
if err != nil {
Expand Down
60 changes: 0 additions & 60 deletions tests/xrayscan_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package tests

import (
"github.com/jfrog/jfrog-client-go/auth"
"github.com/stretchr/testify/assert"
"strconv"
"strings"
"testing"

"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils/tests/xray"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
xrayServices "github.com/jfrog/jfrog-client-go/xray/services"
)

var testsXrayScanService *services.XrayScanService
var testsScanService *xrayServices.ScanService

func TestNewXrayScanService(t *testing.T) {
initXrayTest(t)
Expand Down Expand Up @@ -64,59 +60,3 @@ func scanBuild(t *testing.T, buildName, buildNumber, expected string) {
t.Error("Expected:", expected, "Got: ", string(result))
}
}

func TestIsXscEnabled(t *testing.T) {
xrayServerPort, xrayDetails, client := initXrayScanTest(t)
testsScanService = xrayServices.NewScanService(client)
testsScanService.XrayDetails = xrayDetails
testsScanService.XrayDetails.SetUrl("http://localhost:" + strconv.Itoa(xrayServerPort) + "/xray/")

result, err := testsScanService.IsXscEnabled()
assert.NoError(t, err)
assert.Equal(t, xray.TestXscVersion, result)
}

func TestSendScanGitInfoContext(t *testing.T) {
xrayServerPort, xrayDetails, client := initXrayScanTest(t)
testsScanService = xrayServices.NewScanService(client)
testsScanService.XrayDetails = xrayDetails
testsScanService.XrayDetails.SetUrl("http://localhost:" + strconv.Itoa(xrayServerPort) + "/xray/")

// Run tests
tests := []struct {
name string
gitInfoContext *xrayServices.XscGitInfoContext
expected string
}{
{name: "ValidGitInfoContext", gitInfoContext: &xray.GitInfoContextWithMinimalRequiredFields, expected: xray.TestMultiScanId},
{name: "InvalidGitInfoContext", gitInfoContext: &xray.GitInfoContextWithMissingFields, expected: xray.XscGitInfoBadResponse},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
sendGitInfoContext(t, test.gitInfoContext, test.expected)
})
}
}

func sendGitInfoContext(t *testing.T, gitInfoContext *xrayServices.XscGitInfoContext, expected string) {
result, err := testsScanService.SendScanGitInfoContext(gitInfoContext)
if err != nil {
assert.ErrorContains(t, err, expected)
return
}
assert.Equal(t, expected, result)
}

func initXrayScanTest(t *testing.T) (xrayServerPort int, xrayDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) {
var err error
initXrayTest(t)
xrayServerPort = xray.StartXrayMockServer(t)
xrayDetails = GetXrayDetails()
client, err = jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xrayDetails.GetClientCertPath()).
SetClientCertKeyPath(xrayDetails.GetClientCertKeyPath()).
AppendPreRequestInterceptor(xrayDetails.RunPreRequestFunctions).
Build()
assert.NoError(t, err)
return
}
30 changes: 19 additions & 11 deletions xray/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/xray/services"
"github.com/jfrog/jfrog-client-go/xray/services/utils"
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
"github.com/jfrog/jfrog-client-go/xray/services/xsc"
)

// XrayServicesManager defines the http client and general configuration
Expand Down Expand Up @@ -50,23 +51,23 @@ func (sm *XrayServicesManager) GetVersion() (string, error) {
}

// CreateWatch will create a new Xray watch
func (sm *XrayServicesManager) CreateWatch(params utils.WatchParams) error {
func (sm *XrayServicesManager) CreateWatch(params xrayUtils.WatchParams) error {
watchService := services.NewWatchService(sm.client)
watchService.XrayDetails = sm.config.GetServiceDetails()
return watchService.Create(params)
}

// GetWatch retrieves the details about an Xray watch by name
// It will error if no watch can be found by that name.
func (sm *XrayServicesManager) GetWatch(watchName string) (*utils.WatchParams, error) {
func (sm *XrayServicesManager) GetWatch(watchName string) (*xrayUtils.WatchParams, error) {
watchService := services.NewWatchService(sm.client)
watchService.XrayDetails = sm.config.GetServiceDetails()
return watchService.Get(watchName)
}

// UpdateWatch will update an existing Xray watch by name
// It will error if no watch can be found by that name.
func (sm *XrayServicesManager) UpdateWatch(params utils.WatchParams) error {
func (sm *XrayServicesManager) UpdateWatch(params xrayUtils.WatchParams) error {
watchService := services.NewWatchService(sm.client)
watchService.XrayDetails = sm.config.GetServiceDetails()
return watchService.Update(params)
Expand All @@ -81,23 +82,23 @@ func (sm *XrayServicesManager) DeleteWatch(watchName string) error {
}

// CreatePolicy will create a new Xray policy
func (sm *XrayServicesManager) CreatePolicy(params utils.PolicyParams) error {
func (sm *XrayServicesManager) CreatePolicy(params xrayUtils.PolicyParams) error {
policyService := services.NewPolicyService(sm.client)
policyService.XrayDetails = sm.config.GetServiceDetails()
return policyService.Create(params)
}

// GetPolicy retrieves the details about an Xray policy by name
// It will error if no policy can be found by that name.
func (sm *XrayServicesManager) GetPolicy(policyName string) (*utils.PolicyParams, error) {
func (sm *XrayServicesManager) GetPolicy(policyName string) (*xrayUtils.PolicyParams, error) {
policyService := services.NewPolicyService(sm.client)
policyService.XrayDetails = sm.config.GetServiceDetails()
return policyService.Get(policyName)
}

// UpdatePolicy will update an existing Xray policy by name
// It will error if no policy can be found by that name.
func (sm *XrayServicesManager) UpdatePolicy(params utils.PolicyParams) error {
func (sm *XrayServicesManager) UpdatePolicy(params xrayUtils.PolicyParams) error {
policyService := services.NewPolicyService(sm.client)
policyService.XrayDetails = sm.config.GetServiceDetails()
return policyService.Update(params)
Expand All @@ -113,15 +114,15 @@ func (sm *XrayServicesManager) DeletePolicy(policyName string) error {

// CreatePolicy will create a new Xray ignore rule
// The function returns the ignore rule id if succeeded or empty string and error message if fails
func (sm *XrayServicesManager) CreateIgnoreRule(params utils.IgnoreRuleParams) (string, error) {
func (sm *XrayServicesManager) CreateIgnoreRule(params xrayUtils.IgnoreRuleParams) (string, error) {
ignoreRuleService := services.NewIgnoreRuleService(sm.client)
ignoreRuleService.XrayDetails = sm.config.GetServiceDetails()
return ignoreRuleService.Create(params)
}

// CreatePolicy will create a new Xray ignore rule
// The function returns the ignore rule id if succeeded or empty string and error message if fails
func (sm *XrayServicesManager) GetIgnoreRule(ignoreRuleId string) (*utils.IgnoreRuleParams, error) {
func (sm *XrayServicesManager) GetIgnoreRule(ignoreRuleId string) (*xrayUtils.IgnoreRuleParams, error) {
ignoreRuleService := services.NewIgnoreRuleService(sm.client)
ignoreRuleService.XrayDetails = sm.config.GetServiceDetails()
return ignoreRuleService.Get(ignoreRuleId)
Expand Down Expand Up @@ -158,10 +159,10 @@ func (sm *XrayServicesManager) ScanGraph(params services.XrayGraphScanParams) (s

// GetScanGraphResults returns an Xray scan output of the requested graph scan.
// The scanId input should be received from ScanGraph request.
func (sm *XrayServicesManager) GetScanGraphResults(scanID string, includeVulnerabilities, includeLicenses, xscEnabled bool) (*services.ScanResponse, error) {
func (sm *XrayServicesManager) GetScanGraphResults(scanID, xrayVersion string, includeVulnerabilities, includeLicenses, xscEnabled bool) (*services.ScanResponse, error) {
scanService := services.NewScanService(sm.client)
scanService.XrayDetails = sm.config.GetServiceDetails()
return scanService.GetScanGraphResults(scanID, includeVulnerabilities, includeLicenses, xscEnabled)
return scanService.GetScanGraphResults(scanID, xrayVersion, includeVulnerabilities, includeLicenses, xscEnabled)
}

func (sm *XrayServicesManager) ImportGraph(params services.XrayGraphImportParams) (scanId string, err error) {
Expand Down Expand Up @@ -242,3 +243,10 @@ func (sm *XrayServicesManager) IsEntitled(featureId string) (bool, error) {
entitlementsService.XrayDetails = sm.config.GetServiceDetails()
return entitlementsService.IsEntitled(featureId)
}

// Xsc returns the Xsc service inside Xray
func (sm *XrayServicesManager) Xsc() *xsc.XscInnerService {
xscService := xsc.NewXscService(sm.client)
xscService.XrayDetails = sm.config.GetServiceDetails()
return xscService
}
Loading

0 comments on commit 50bd3e5

Please sign in to comment.