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

Validate xray url #160

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 35 additions & 1 deletion audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func addDummyPackageDescriptor(t *testing.T, hasPackageJson bool) {
// JAS

func TestXrayAuditNotEntitledForJas(t *testing.T) {
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, getNoJasAuditMockCommand)
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, false, getNoJasAuditMockCommand)
defer cleanUp()
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false)
// Verify that scan results are printed
Expand Down Expand Up @@ -620,3 +620,37 @@ func TestAuditOnEmptyProject(t *testing.T) {
output := securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+string(format.SimpleJson))
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 0, 0, 0)
}

// xray-url only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the comment more informative?


func TestXrayAuditNotEntitledForJasWithXrayUrl(t *testing.T) {
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, true, getNoJasAuditMockCommandWithXrayUrl)
defer cleanUp()
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false)
// Verify that scan results are printed
securityTestUtils.VerifySimpleJsonScanResults(t, output, 0, 8, 0)
// Verify that JAS results are not printed
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 0, 0, 0)
}

func getNoJasAuditMockCommandWithXrayUrl() components.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use the function getNoJasAuditMockCommand (I don't see any difference)

return components.Command{
Name: docs.Audit,
Flags: docs.GetCommandFlags(docs.Audit),
Action: func(c *components.Context) error {
auditCmd, err := cli.CreateAuditCmd(c)
if err != nil {
return err
}
// Disable Jas for this test
auditCmd.SetUseJas(false)
return progressbar.ExecWithProgress(auditCmd)
},
}
}

func TestXrayAuditJasSimpleJsonWithXrayUrl(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("jas", "jas"), "3", false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should create here a 'cliToRun' object, same in TestXrayAuditNotEntitledForJasWithXrayUrl. Otherwise the test will use the default CLI configured to the integration test

securityTestUtils.VerifySimpleJsonScanResults(t, output, 0, 8, 0)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 6, 3, 1, 1, 2, 0, 0)
}
10 changes: 6 additions & 4 deletions jas/analyzermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
jfPasswordEnvVariable = "JF_PASS"
jfTokenEnvVariable = "JF_TOKEN"
jfPlatformUrlEnvVariable = "JF_PLATFORM_URL"
jfPlatformXrayUrlEnvVariable = "JF_PLATFORM_XRAY_URL"
logDirEnvVariable = "AM_LOG_DIRECTORY"
notEntitledExitCode = 31
unsupportedCommandExitCode = 13
Expand Down Expand Up @@ -143,10 +144,11 @@ func isCI() bool {

func GetAnalyzerManagerEnvVariables(serverDetails *config.ServerDetails) (envVars map[string]string, err error) {
envVars = map[string]string{
jfUserEnvVariable: serverDetails.User,
jfPasswordEnvVariable: serverDetails.Password,
jfPlatformUrlEnvVariable: serverDetails.Url,
jfTokenEnvVariable: serverDetails.AccessToken,
jfUserEnvVariable: serverDetails.User,
jfPasswordEnvVariable: serverDetails.Password,
jfPlatformUrlEnvVariable: serverDetails.Url,
jfPlatformXrayUrlEnvVariable: serverDetails.XrayUrl,
jfTokenEnvVariable: serverDetails.AccessToken,
}
if !isCI() {
analyzerManagerLogFolder, err := coreutils.CreateDirInJfrogHome(filepath.Join(coreutils.JfrogLogsDirName, analyzerManagerLogDirName))
Expand Down
1 change: 1 addition & 0 deletions jas/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func CreateJasScanner(jfrogAppsConfig *jfrogappsconfig.JFrogAppsConfig, serverDe

func getJasEnvVars(serverDetails *config.ServerDetails, vars map[string]string) (map[string]string, error) {
amBasicVars, err := GetAnalyzerManagerEnvVariables(serverDetails)
log.Debug("Adding the following environment variables to the analyzer manager", amBasicVars)
if err != nil {
return nil, err
}
Expand Down
34 changes: 34 additions & 0 deletions jas/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,40 @@ func TestGetAnalyzerManagerEnvVariables(t *testing.T) {
jfTokenEnvVariable: "token",
},
},
{
name: "Valid server details xray only",
serverDetails: &config.ServerDetails{
Url: "",
XrayUrl: "url/xray",
User: "user",
Password: "password",
AccessToken: "token",
},
expectedOutput: map[string]string{
jfPlatformUrlEnvVariable: "",
jfPlatformXrayUrlEnvVariable: "url/xray",
jfUserEnvVariable: "user",
jfPasswordEnvVariable: "password",
jfTokenEnvVariable: "token",
},
},
{
name: "Valid server details both url and xray",
serverDetails: &config.ServerDetails{
Url: "url",
XrayUrl: "url/xray",
User: "user",
Password: "password",
AccessToken: "token",
},
expectedOutput: map[string]string{
jfPlatformUrlEnvVariable: "url",
jfPlatformXrayUrlEnvVariable: "url/xray",
jfUserEnvVariable: "user",
jfPasswordEnvVariable: "password",
jfTokenEnvVariable: "token",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion scans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func initNativeDockerWithXrayTest(t *testing.T) (mockCli *coreTests.JfrogCli, cl
if !*securityTests.TestDockerScan || !*securityTests.TestSecurity {
t.Skip("Skipping Docker scan test. To run Xray Docker test add the '-test.dockerScan=true' and '-test.security=true' options.")
}
return securityTestUtils.InitTestWithMockCommandOrParams(t, cli.DockerScanMockCommand)
return securityTestUtils.InitTestWithMockCommandOrParams(t, false, cli.DockerScanMockCommand)
}

func runDockerScan(t *testing.T, testCli *coreTests.JfrogCli, imageName, watchName string, minViolations, minVulnerabilities, minLicenses int, minInactives int, validateSecrets bool) {
Expand Down
14 changes: 9 additions & 5 deletions tests/utils/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ func InitTestCliDetails() {

configTests.TestApplication = &testApplication
if configTests.PlatformCli == nil {
configTests.PlatformCli = GetTestCli(testApplication)
configTests.PlatformCli = GetTestCli(testApplication, false)
}
}

func GetTestCli(testApplication components.App) (testCli *coreTests.JfrogCli) {
creds := authenticateXray()
func GetTestCli(testApplication components.App, xrayUrlOnly bool) (testCli *coreTests.JfrogCli) {
creds := authenticateXray(xrayUrlOnly)
return coreTests.NewJfrogCli(func() error { return plugins.RunCliWithPlugin(testApplication)() }, "", creds)
}

func authenticateXray() string {
func authenticateXray(xrayUrlOnly bool) string {
*configTests.JfrogUrl = clientUtils.AddTrailingSlashIfNeeded(*configTests.JfrogUrl)
configTests.XrDetails = &config.ServerDetails{Url: *configTests.JfrogUrl, ArtifactoryUrl: *configTests.JfrogUrl + configTests.ArtifactoryEndpoint, XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint}
if xrayUrlOnly {
configTests.XrDetails = &config.ServerDetails{XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint}
} else {
configTests.XrDetails = &config.ServerDetails{Url: *configTests.JfrogUrl, ArtifactoryUrl: *configTests.JfrogUrl + configTests.ArtifactoryEndpoint, XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint}
}
cred := fmt.Sprintf("--url=%s", configTests.XrDetails.XrayUrl)
if *configTests.JfrogAccessToken != "" {
configTests.XrDetails.AccessToken = *configTests.JfrogAccessToken
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func InitSecurityTest(t *testing.T, xrayMinVersion string) {
ValidateXrayVersion(t, xrayMinVersion)
}

func InitTestWithMockCommandOrParams(t *testing.T, mockCommands ...func() components.Command) (mockCli *coreTests.JfrogCli, cleanUp func()) {
func InitTestWithMockCommandOrParams(t *testing.T, xrayUrlOnly bool, mockCommands ...func() components.Command) (mockCli *coreTests.JfrogCli, cleanUp func()) {
oldHomeDir := os.Getenv(coreutils.HomeDir)
// Create server config to use with the command.
CreateJfrogHomeConfig(t, true)
Expand All @@ -59,7 +59,7 @@ func InitTestWithMockCommandOrParams(t *testing.T, mockCommands ...func() compon
for _, mockCommand := range mockCommands {
commands = append(commands, mockCommand())
}
return GetTestCli(components.CreateEmbeddedApp("security", commands)), func() {
return GetTestCli(components.CreateEmbeddedApp("security", commands), xrayUrlOnly), func() {
clientTests.SetEnvAndAssert(t, coreutils.HomeDir, oldHomeDir)
}
}
Expand Down
Loading