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

Transition Xsc to Xray #1046

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
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
53 changes: 19 additions & 34 deletions tests/xrayscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,51 +66,36 @@ func scanBuild(t *testing.T, buildName, buildNumber, expected string) {
}

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/")
testCases := []struct {
name string
params xray.MockServerParams
}{
{name: "Old XSC API", params: xray.MockServerParams{XrayVersion: "3.0.0", XscVersion: xray.TestXscVersion}},
{name: "New XSC API", params: xray.MockServerParams{XrayVersion: "3.108.0", XscVersion: xray.TestXscVersion}},
}

result, err := testsScanService.IsXscEnabled()
assert.NoError(t, err)
assert.Equal(t, xray.TestXscVersion, result)
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testIsXscEnabled(t, testCase.params)
})
}
}

func TestSendScanGitInfoContext(t *testing.T) {
xrayServerPort, xrayDetails, client := initXrayScanTest(t)
func testIsXscEnabled(t *testing.T, params xray.MockServerParams) {
xrayServerPort, xrayDetails, client := initXrayScanTest(t, params)
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)
result, err := testsScanService.IsXscEnabled()
assert.NoError(t, err)
assert.Equal(t, xray.TestXscVersion, result)
}

func initXrayScanTest(t *testing.T) (xrayServerPort int, xrayDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) {
func initXrayScanTest(t *testing.T, params xray.MockServerParams) (xrayServerPort int, xrayDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) {
var err error
initXrayTest(t)
xrayServerPort = xray.StartXrayMockServer(t)
xrayServerPort = xray.StartXrayMockServerWithParams(t, params)
xrayDetails = GetXrayDetails()
client, err = jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xrayDetails.GetClientCertPath()).
Expand Down
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
Loading