Skip to content

Commit

Permalink
Add method to run analyzerManager directly
Browse files Browse the repository at this point in the history
  • Loading branch information
guyshe-jfrog committed Oct 28, 2024
1 parent 1b16215 commit b8f1cb8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
80 changes: 78 additions & 2 deletions cli/scancommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package cli
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

buildInfoUtils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/datastructures"
"github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
Expand All @@ -17,13 +23,13 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/utils/usage"
enrichDocs "github.com/jfrog/jfrog-cli-security/cli/docs/enrich"
"github.com/jfrog/jfrog-cli-security/commands/enrich"
"github.com/jfrog/jfrog-cli-security/jas"
"github.com/jfrog/jfrog-cli-security/jas/external_files"
"github.com/jfrog/jfrog-cli-security/utils/xray"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/urfave/cli"
"os"
"strings"

flags "github.com/jfrog/jfrog-cli-security/cli/docs"
auditSpecificDocs "github.com/jfrog/jfrog-cli-security/cli/docs/auditspecific"
Expand Down Expand Up @@ -95,6 +101,14 @@ func getAuditAndScansCommands() []components.Command {
Category: securityCategory,
Action: AuditCmd,
},
{
Name: "run-am",
Aliases: []string{"aud"},
Flags: flags.GetCommandFlags(flags.Audit),
Description: auditDocs.GetDescription(),
Category: securityCategory,
Action: runAnalyzerManager,
},
{
Name: "curation-audit",
Aliases: []string{"ca"},
Expand Down Expand Up @@ -428,6 +442,68 @@ func shouldAddSubScan(subScan utils.SubScanType, c *components.Context) bool {
(subScan == utils.ContextualAnalysisScan && c.GetBoolFlagValue(flags.Sca) && !c.GetBoolFlagValue(flags.WithoutCA)) || (subScan == utils.SecretTokenValidationScan && c.GetBoolFlagValue(flags.Secrets) && c.GetBoolFlagValue(flags.SecretValidation))
}

func runCommand(cmd string, args ...string) ([]string, error) {
parts := append([]string{cmd}, args...)
cmdStr := strings.Join(parts, " ")
outBytes, err := []byte{}, error(nil)

Check failure on line 448 in cli/scancommands.go

View workflow job for this annotation

GitHub Actions / Static-Check

ineffectual assignment to outBytes (ineffassign)

Check failure on line 448 in cli/scancommands.go

View workflow job for this annotation

GitHub Actions / Static-Check

ineffectual assignment to outBytes (ineffassign)
if runtime.GOOS == "windows" {
outBytes, err = exec.Command("cmd", "/C", cmdStr).CombinedOutput()
} else {
outBytes, err = exec.Command("sh", "-c", cmdStr).CombinedOutput()
}
if err != nil {
return nil, err
}
outStr := string(outBytes)
return strings.Split(outStr, "\n"), nil
}
func runAnalyzerManager(c *components.Context) error {
config_path := c.Arguments[0]
log.Info("Make sure to set CI=true JFROG_CLI_LOG_LEVEL=DEBUG")
log.Info(fmt.Sprintf("Using following config file: %s", config_path))
external_files.SwapScanners("ca_scanner", "applicability_scanner")
external_files.SwapScanners("secrets_scanner", "secrets_scanner")
external_files.SwapScanners("jas_scanner", "jas_scanner")
serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return err
}
if err = jas.SetAnalyzerManagerEnvVariables(serverDetails); err != nil {
return err
}
// TODO: equivelent of
// os.Setenv("CI", "true")
// os.Setenv("JFROG_CLI_LOG_LEVEL", "DEBUG")
// cmd := "~/.jfrog/dependencies/analyzerManager/analyzerManager"
analyzerManagerDir, err := jas.GetAnalyzerManagerDirAbsolutePath()
if err != nil {
panic(err)
}
// Define the relative path to the analyzerManager
relativePath := "analyzerManager"
if runtime.GOOS == "windows" {
relativePath = "analyzerManager.exe"
}
if err != nil {
print("Error: can't get deps folder\n")
}
// Combine home directory with the relative path
cmd := filepath.Join(analyzerManagerDir, relativePath)
args := []string{"ca", config_path}
_, err = os.Stat(cmd)
if err != nil {
return err
}
fmt.Println("Running command:", cmd, args)
cmdOut, err := runCommand(cmd, args...)
if err != nil {
fmt.Fprintln(os.Stderr, "Error running command:", err)
return err
}
fmt.Println(strings.Join(cmdOut, "\n"))
return nil
}

func reportErrorIfExists(err error, auditCmd *audit.AuditCommand) {
if err == nil || !usage.ShouldReportUsage() {
return
Expand Down
28 changes: 28 additions & 0 deletions jas/analyzermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ func isCI() bool {
return strings.ToLower(os.Getenv(coreutils.CI)) == "true"
}

func SetAnalyzerManagerEnvVariables(serverDetails *config.ServerDetails) error {
if serverDetails == nil {
return errors.New("cant get xray server details")
}
if err := os.Setenv(jfUserEnvVariable, serverDetails.User); errorutils.CheckError(err) != nil {
return err
}
if err := os.Setenv(jfPasswordEnvVariable, serverDetails.Password); errorutils.CheckError(err) != nil {
return err
}
if err := os.Setenv(jfPlatformUrlEnvVariable, serverDetails.Url); errorutils.CheckError(err) != nil {
return err
}
if err := os.Setenv(jfTokenEnvVariable, serverDetails.AccessToken); errorutils.CheckError(err) != nil {
return err
}
if isCI() {
analyzerManagerLogFolder, err := coreutils.CreateDirInJfrogHome(filepath.Join(coreutils.JfrogLogsDirName, analyzerManagerLogDirName))
if err != nil {
return err
}
if err = os.Setenv(logDirEnvVariable, analyzerManagerLogFolder); errorutils.CheckError(err) != nil {
return err
}
}
return nil
}

func GetAnalyzerManagerEnvVariables(serverDetails *config.ServerDetails) (envVars map[string]string, err error) {
envVars = map[string]string{
jfUserEnvVariable: serverDetails.User,
Expand Down

0 comments on commit b8f1cb8

Please sign in to comment.