Skip to content

Commit 3e0a6a7

Browse files
authored
Validate xray url (#160)
1 parent c989616 commit 3e0a6a7

File tree

7 files changed

+93
-20
lines changed

7 files changed

+93
-20
lines changed

audit_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/jfrog/jfrog-cli-security/utils/formats"
1818
"github.com/jfrog/jfrog-cli-security/utils/validations"
1919

20+
testsUtils "github.com/jfrog/jfrog-cli-security/tests/utils"
2021
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
2122

2223
"github.com/stretchr/testify/assert"
@@ -566,7 +567,7 @@ func TestXrayAuditWithoutSastCppFlagSimpleJson(t *testing.T) {
566567
}
567568

568569
func TestXrayAuditNotEntitledForJas(t *testing.T) {
569-
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, getNoJasAuditMockCommand)
570+
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, false, getNoJasAuditMockCommand)
570571
defer cleanUp()
571572
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false, false)
572573
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 8})
@@ -739,3 +740,31 @@ func TestAuditOnEmptyProject(t *testing.T) {
739740
// No issues should be found in an empty project
740741
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{})
741742
}
743+
744+
// xray-url only - the following tests check the case of adding "xray-url", instead of "url", which is the more common one
745+
746+
func TestXrayAuditNotEntitledForJasWithXrayUrl(t *testing.T) {
747+
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, true, getNoJasAuditMockCommand)
748+
defer cleanUp()
749+
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false, false)
750+
// Verify that scan results are printed
751+
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 8})
752+
// Verify that JAS results are not printed
753+
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{})
754+
}
755+
756+
func TestXrayAuditJasSimpleJsonWithXrayUrl(t *testing.T) {
757+
cliToRun := testsUtils.GetTestCli(cli.GetJfrogCliSecurityApp(), true)
758+
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false, false)
759+
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
760+
Sast: 1,
761+
Iac: 9,
762+
Secrets: 6,
763+
764+
Vulnerabilities: 8,
765+
Applicable: 3,
766+
Undetermined: 1,
767+
NotCovered: 1,
768+
NotApplicable: 2,
769+
})
770+
}

jas/analyzermanager.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
ApplicabilityFeatureId = "contextual_analysis"
2626
AnalyzerManagerZipName = "analyzerManager.zip"
27-
defaultAnalyzerManagerVersion = "1.9.11"
27+
defaultAnalyzerManagerVersion = "1.10.2"
2828
analyzerManagerDownloadPath = "xsc-gen-exe-analyzer-manager-local/v1"
2929
analyzerManagerDirName = "analyzerManager"
3030
analyzerManagerExecutableName = "analyzerManager"
@@ -33,6 +33,7 @@ const (
3333
jfPasswordEnvVariable = "JF_PASS"
3434
jfTokenEnvVariable = "JF_TOKEN"
3535
jfPlatformUrlEnvVariable = "JF_PLATFORM_URL"
36+
jfPlatformXrayUrlEnvVariable = "JF_PLATFORM_XRAY_URL"
3637
logDirEnvVariable = "AM_LOG_DIRECTORY"
3738
notEntitledExitCode = 31
3839
unsupportedCommandExitCode = 13
@@ -138,10 +139,11 @@ func GetAnalyzerManagerExecutableName() string {
138139

139140
func GetAnalyzerManagerEnvVariables(serverDetails *config.ServerDetails) (envVars map[string]string, err error) {
140141
envVars = map[string]string{
141-
jfUserEnvVariable: serverDetails.User,
142-
jfPasswordEnvVariable: serverDetails.Password,
143-
jfPlatformUrlEnvVariable: serverDetails.Url,
144-
jfTokenEnvVariable: serverDetails.AccessToken,
142+
jfUserEnvVariable: serverDetails.User,
143+
jfPasswordEnvVariable: serverDetails.Password,
144+
jfPlatformUrlEnvVariable: serverDetails.Url,
145+
jfPlatformXrayUrlEnvVariable: serverDetails.XrayUrl,
146+
jfTokenEnvVariable: serverDetails.AccessToken,
145147
}
146148
if !utils.IsCI() {
147149
analyzerManagerLogFolder, err := coreutils.CreateDirInJfrogHome(filepath.Join(coreutils.JfrogLogsDirName, analyzerManagerLogDirName))

jas/common.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ func CreateJasScanner(serverDetails *config.ServerDetails, validateSecrets bool,
5454
if len(serverDetails.Url) == 0 {
5555
if len(serverDetails.XrayUrl) != 0 {
5656
log.Debug("Xray URL provided without platform URL")
57+
} else {
58+
if len(serverDetails.ArtifactoryUrl) != 0 {
59+
log.Debug("Artifactory URL provided without platform URL")
60+
}
61+
log.Warn(NoServerUrlWarn)
62+
return
5763
}
58-
if len(serverDetails.ArtifactoryUrl) != 0 {
59-
log.Debug("Artifactory URL provided without platform URL")
60-
}
61-
log.Warn(NoServerUrlWarn)
62-
return
6364
}
6465
scanner = &JasScanner{}
6566
if scanner.EnvVars, err = getJasEnvVars(serverDetails, validateSecrets, envVars); err != nil {
@@ -81,6 +82,7 @@ func CreateJasScanner(serverDetails *config.ServerDetails, validateSecrets bool,
8182

8283
func getJasEnvVars(serverDetails *config.ServerDetails, validateSecrets bool, vars map[string]string) (map[string]string, error) {
8384
amBasicVars, err := GetAnalyzerManagerEnvVariables(serverDetails)
85+
log.Debug("Adding the following environment variables to the analyzer manager", amBasicVars)
8486
if err != nil {
8587
return nil, err
8688
}

jas/common_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,40 @@ func TestGetJasEnvVars(t *testing.T) {
157157
"test": "testValue",
158158
},
159159
},
160+
{
161+
name: "Valid server details xray only",
162+
serverDetails: &config.ServerDetails{
163+
Url: "",
164+
XrayUrl: "url/xray",
165+
User: "user",
166+
Password: "password",
167+
AccessToken: "token",
168+
},
169+
expectedOutput: map[string]string{
170+
jfPlatformUrlEnvVariable: "",
171+
jfPlatformXrayUrlEnvVariable: "url/xray",
172+
jfUserEnvVariable: "user",
173+
jfPasswordEnvVariable: "password",
174+
jfTokenEnvVariable: "token",
175+
},
176+
},
177+
{
178+
name: "Valid server details both url and xray",
179+
serverDetails: &config.ServerDetails{
180+
Url: "url",
181+
XrayUrl: "url/xray",
182+
User: "user",
183+
Password: "password",
184+
AccessToken: "token",
185+
},
186+
expectedOutput: map[string]string{
187+
jfPlatformUrlEnvVariable: "url",
188+
jfPlatformXrayUrlEnvVariable: "url/xray",
189+
jfUserEnvVariable: "user",
190+
jfPasswordEnvVariable: "password",
191+
jfTokenEnvVariable: "token",
192+
},
193+
},
160194
}
161195
for _, test := range tests {
162196
t.Run(test.name, func(t *testing.T) {

scans_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func initNativeDockerWithXrayTest(t *testing.T) (mockCli *coreTests.JfrogCli, cl
162162
if !*securityTests.TestDockerScan || !*securityTests.TestSecurity {
163163
t.Skip("Skipping Docker scan test. To run Xray Docker test add the '-test.dockerScan=true' and '-test.security=true' options.")
164164
}
165-
return securityTestUtils.InitTestWithMockCommandOrParams(t, cli.DockerScanMockCommand)
165+
return securityTestUtils.InitTestWithMockCommandOrParams(t, false, cli.DockerScanMockCommand)
166166
}
167167

168168
func runDockerScan(t *testing.T, testCli *coreTests.JfrogCli, imageName, watchName string, minViolations, minVulnerabilities, minLicenses int, minInactives int, validateSecrets bool) {

tests/utils/test_config.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,25 @@ func CreateJfrogHomeConfig(t *testing.T, encryptPassword bool) {
5050
func InitTestCliDetails(testApplication components.App) {
5151
configTests.TestApplication = &testApplication
5252
if configTests.PlatformCli == nil {
53-
configTests.PlatformCli = GetTestCli(testApplication)
53+
configTests.PlatformCli = GetTestCli(testApplication, false)
5454
}
5555
}
5656

57-
func GetTestCli(testApplication components.App) (testCli *coreTests.JfrogCli) {
58-
creds := authenticateXray()
57+
func GetTestCli(testApplication components.App, xrayUrlOnly bool) (testCli *coreTests.JfrogCli) {
58+
creds := authenticateXray(xrayUrlOnly)
5959
return coreTests.NewJfrogCli(func() error { return plugins.RunCliWithPlugin(testApplication)() }, "", creds)
6060
}
6161

62-
func authenticateXray() string {
62+
func authenticateXray(xrayUrlOnly bool) string {
6363
*configTests.JfrogUrl = clientUtils.AddTrailingSlashIfNeeded(*configTests.JfrogUrl)
64-
configTests.XrDetails = &config.ServerDetails{Url: *configTests.JfrogUrl, ArtifactoryUrl: *configTests.JfrogUrl + configTests.ArtifactoryEndpoint, XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint}
65-
cred := fmt.Sprintf("--url=%s", configTests.XrDetails.XrayUrl)
64+
var cred string
65+
if xrayUrlOnly {
66+
configTests.XrDetails = &config.ServerDetails{XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint}
67+
cred = fmt.Sprintf("--xray-url=%s", configTests.XrDetails.XrayUrl)
68+
} else {
69+
configTests.XrDetails = &config.ServerDetails{Url: *configTests.JfrogUrl, ArtifactoryUrl: *configTests.JfrogUrl + configTests.ArtifactoryEndpoint, XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint}
70+
cred = fmt.Sprintf("--url=%s", configTests.XrDetails.XrayUrl)
71+
}
6672
if *configTests.JfrogAccessToken != "" {
6773
configTests.XrDetails.AccessToken = *configTests.JfrogAccessToken
6874
cred += fmt.Sprintf(" --access-token=%s", configTests.XrDetails.AccessToken)

tests/utils/test_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ValidateXscVersion(t *testing.T, minVersion string) {
7878
}
7979
}
8080

81-
func InitTestWithMockCommandOrParams(t *testing.T, mockCommands ...func() components.Command) (mockCli *coreTests.JfrogCli, cleanUp func()) {
81+
func InitTestWithMockCommandOrParams(t *testing.T, xrayUrlOnly bool, mockCommands ...func() components.Command) (mockCli *coreTests.JfrogCli, cleanUp func()) {
8282
oldHomeDir := os.Getenv(coreutils.HomeDir)
8383
// Create server config to use with the command.
8484
CreateJfrogHomeConfig(t, true)
@@ -87,7 +87,7 @@ func InitTestWithMockCommandOrParams(t *testing.T, mockCommands ...func() compon
8787
for _, mockCommand := range mockCommands {
8888
commands = append(commands, mockCommand())
8989
}
90-
return GetTestCli(components.CreateEmbeddedApp("security", commands)), func() {
90+
return GetTestCli(components.CreateEmbeddedApp("security", commands), xrayUrlOnly), func() {
9191
clientTests.SetEnvAndAssert(t, coreutils.HomeDir, oldHomeDir)
9292
}
9393
}

0 commit comments

Comments
 (0)