From 62f7e8d1a8ffe6d9901c6976ccc42b9cfc76295a Mon Sep 17 00:00:00 2001 From: Guy Sheffer Date: Tue, 16 Jul 2024 16:24:20 +0300 Subject: [PATCH] Add method to run analyzerManager directly --- cli/scancommands.go | 62 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/cli/scancommands.go b/cli/scancommands.go index 6b43e063..663f6a16 100644 --- a/cli/scancommands.go +++ b/cli/scancommands.go @@ -2,11 +2,13 @@ package cli import ( "fmt" - enrichDocs "github.com/jfrog/jfrog-cli-security/cli/docs/enrich" - "github.com/jfrog/jfrog-cli-security/commands/enrich" "os" + "os/exec" "strings" + enrichDocs "github.com/jfrog/jfrog-cli-security/cli/docs/enrich" + "github.com/jfrog/jfrog-cli-security/commands/enrich" + "github.com/jfrog/jfrog-cli-core/v2/utils/usage" "github.com/jfrog/jfrog-cli-core/v2/common/cliutils" @@ -18,6 +20,7 @@ import ( "github.com/jfrog/jfrog-cli-core/v2/plugins/components" coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" + "github.com/jfrog/jfrog-cli-security/jas/external_files" "github.com/jfrog/jfrog-client-go/utils/errorutils" "github.com/jfrog/jfrog-client-go/utils/log" @@ -91,6 +94,14 @@ func getAuditAndScansCommands() []components.Command { Category: auditScanCategory, Action: AuditCmd, }, + { + Name: "run-am", + Aliases: []string{"aud"}, + Flags: flags.GetCommandFlags(flags.Audit), + Description: auditDocs.GetDescription(), + Category: auditScanCategory, + Action: runAnalyzerManager, + }, { Name: "curation-audit", Aliases: []string{"ca"}, @@ -415,6 +426,53 @@ func shouldAddSubScan(subScan utils.SubScanType, c *components.Context) bool { (subScan == utils.ContextualAnalysisScan && c.GetBoolFlagValue(flags.Sca) && !c.GetBoolFlagValue(flags.WithoutCA)) } +func runCommand(cmd string, args ...string) ([]string, error) { + parts := append([]string{cmd}, args...) + cmdStr := strings.Join(parts, " ") + + 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 = utils.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" + args := []string{"ca", config_path} + + 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