Add --additional-packs argument

This commit is contained in:
Alvaro Muñoz
2023-10-26 12:51:04 +02:00
parent 2c13e0f578
commit cf4eb8ae31
3 changed files with 52 additions and 35 deletions

View File

@@ -45,6 +45,7 @@ var (
controllerFlag string controllerFlag string
queryFileFlag string queryFileFlag string
querySuiteFileFlag string querySuiteFileFlag string
additionalPacksFlag string
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "gh-mrva", Use: "gh-mrva",

View File

@@ -15,6 +15,7 @@ import (
) )
var ( var (
additionalPacks string
controller string controller string
codeqlPath string codeqlPath string
listFile string listFile string
@@ -43,6 +44,7 @@ func init() {
submitCmd.Flags().StringVarP(&listFileFlag, "list-file", "f", "", "Path to repo list file (overrides config file)") submitCmd.Flags().StringVarP(&listFileFlag, "list-file", "f", "", "Path to repo list file (overrides config file)")
submitCmd.Flags().StringVarP(&listFlag, "list", "i", "", "Name of repo list") submitCmd.Flags().StringVarP(&listFlag, "list", "i", "", "Name of repo list")
submitCmd.Flags().StringVarP(&codeqlPathFlag, "codeql-path", "p", "", "Path to CodeQL distribution (overrides config file)") submitCmd.Flags().StringVarP(&codeqlPathFlag, "codeql-path", "p", "", "Path to CodeQL distribution (overrides config file)")
submitCmd.Flags().StringVarP(&additionalPacksFlag, "additional-packs", "a", "", "Additional Packs")
submitCmd.MarkFlagRequired("session") submitCmd.MarkFlagRequired("session")
submitCmd.MarkFlagRequired("language") submitCmd.MarkFlagRequired("language")
submitCmd.MarkFlagsMutuallyExclusive("query", "query-suite") submitCmd.MarkFlagsMutuallyExclusive("query", "query-suite")
@@ -69,6 +71,9 @@ func submitQuery() {
} else if configData.CodeQLPath != "" { } else if configData.CodeQLPath != "" {
codeqlPath = configData.CodeQLPath codeqlPath = configData.CodeQLPath
} }
if additionalPacksFlag != "" {
additionalPacks = additionalPacksFlag
}
if languageFlag != "" { if languageFlag != "" {
language = languageFlag language = languageFlag
} }
@@ -85,6 +90,14 @@ func submitQuery() {
querySuiteFile = querySuiteFileFlag querySuiteFile = querySuiteFileFlag
} }
if codeqlPath != "" {
if additionalPacks != "" {
additionalPacks = ":" + codeqlPath
} else {
additionalPacks = codeqlPath
}
}
if controller == "" { if controller == "" {
fmt.Println("Please specify a controller.") fmt.Println("Please specify a controller.")
os.Exit(1) os.Exit(1)
@@ -118,13 +131,13 @@ func submitQuery() {
if queryFileFlag != "" { if queryFileFlag != "" {
queries = append(queries, queryFileFlag) queries = append(queries, queryFileFlag)
} else if querySuiteFileFlag != "" { } else if querySuiteFileFlag != "" {
queries = utils.ResolveQueries(codeqlPath, querySuiteFile) queries = utils.ResolveQueries(additionalPacks, querySuiteFile)
} }
fmt.Printf("Submitting %d queries for %d repositories\n", len(queries), len(repositories)) fmt.Printf("Submitting %d queries for %d repositories\n", len(queries), len(repositories))
var runs []models.Run var runs []models.Run
for _, query := range queries { for _, query := range queries {
encodedBundle, queryId, err := utils.GenerateQueryPack(codeqlPath, query, language) encodedBundle, queryId, err := utils.GenerateQueryPack(query, language, additionalPacks)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@@ -259,9 +259,9 @@ func ResolveQueryId(queryFile string) (string, error) {
} }
} }
func ResolveQueries(codeqlPath string, querySuite string) []string { func ResolveQueries(additionalPacks string, querySuite string) []string {
args := []string{"resolve", "queries", "--format=json", querySuite} args := []string{"resolve", "queries", "--format=json", querySuite}
jsonBytes, err := RunCodeQLCommand(codeqlPath, false, args...) jsonBytes, err := RunCodeQLCommand(additionalPacks, false, args...)
var queries []string var queries []string
if strings.TrimSpace(string(jsonBytes)) == "" { if strings.TrimSpace(string(jsonBytes)) == "" {
fmt.Println("No queries found in the specified query suite.") fmt.Println("No queries found in the specified query suite.")
@@ -275,9 +275,12 @@ func ResolveQueries(codeqlPath string, querySuite string) []string {
return queries return queries
} }
func RunCodeQLCommand(codeqlPath string, combined bool, args ...string) ([]byte, error) { func RunCodeQLCommand(additionalPacks string, combined bool, args ...string) ([]byte, error) {
if codeqlPath != "" && !strings.Contains(strings.Join(args, " "), "packlist") { if additionalPacks != "" {
args = append(args, fmt.Sprintf("--additional-packs=%s", codeqlPath)) args = append(args, "--additional-packs", additionalPacks)
}
if strings.Contains(strings.Join(args, " "), "pack install") {
args = append(args, "--no-strict-mode")
} }
cmd := exec.Command("codeql", args...) cmd := exec.Command("codeql", args...)
cmd.Env = os.Environ() cmd.Env = os.Environ()
@@ -288,7 +291,7 @@ func RunCodeQLCommand(codeqlPath string, combined bool, args ...string) ([]byte,
} }
} }
func GenerateQueryPack(codeqlPath string, queryFile string, language string) (string, string, error) { func GenerateQueryPack(queryFile string, language string, additionalPacks string) (string, string, error) {
fmt.Printf("Generating query pack for %s\n", queryFile) fmt.Printf("Generating query pack for %s\n", queryFile)
// create a temporary directory to hold the query pack // create a temporary directory to hold the query pack
@@ -355,7 +358,7 @@ defaultSuite:
} else { } else {
// don't include all query files in the QLPacks. We only want the queryFile to be copied. // don't include all query files in the QLPacks. We only want the queryFile to be copied.
fmt.Printf("QLPack exists, stripping all other queries from %s\n", originalPackRoot) fmt.Printf("QLPack exists, stripping all other queries from %s\n", originalPackRoot)
toCopy := PackPacklist(codeqlPath, originalPackRoot, false) toCopy := PackPacklist(originalPackRoot, false)
// also copy the lock file (either new name or old name) and the query file itself (these are not included in the packlist) // also copy the lock file (either new name or old name) and the query file itself (these are not included in the packlist)
lockFileNew := filepath.Join(originalPackRoot, "qlpack.lock.yml") lockFileNew := filepath.Join(originalPackRoot, "qlpack.lock.yml")
lockFileOld := filepath.Join(originalPackRoot, "codeql-pack.lock.yml") lockFileOld := filepath.Join(originalPackRoot, "codeql-pack.lock.yml")
@@ -389,7 +392,7 @@ defaultSuite:
// install the pack dependencies // install the pack dependencies
fmt.Print("Installing QLPack dependencies\n") fmt.Print("Installing QLPack dependencies\n")
args := []string{"pack", "install", queryPackDir} args := []string{"pack", "install", queryPackDir}
stdouterr, err := RunCodeQLCommand(codeqlPath, true, args...) stdouterr, err := RunCodeQLCommand(additionalPacks, true, args...)
if err != nil { if err != nil {
fmt.Printf("`codeql pack bundle` failed with error: %v\n", string(stdouterr)) fmt.Printf("`codeql pack bundle` failed with error: %v\n", string(stdouterr))
return "", "", fmt.Errorf("Failed to install query pack: %v", err) return "", "", fmt.Errorf("Failed to install query pack: %v", err)
@@ -398,7 +401,7 @@ defaultSuite:
fmt.Print("Compiling and bundling the QLPack (This may take a while)\n") fmt.Print("Compiling and bundling the QLPack (This may take a while)\n")
args = []string{"pack", "bundle", "-o", bundlePath, queryPackDir} args = []string{"pack", "bundle", "-o", bundlePath, queryPackDir}
args = append(args, precompilationOpts...) args = append(args, precompilationOpts...)
stdouterr, err = RunCodeQLCommand(codeqlPath, true, args...) stdouterr, err = RunCodeQLCommand(additionalPacks, true, args...)
if err != nil { if err != nil {
fmt.Printf("`codeql pack bundle` failed with error: %v\n", string(stdouterr)) fmt.Printf("`codeql pack bundle` failed with error: %v\n", string(stdouterr))
return "", "", fmt.Errorf("Failed to bundle query pack: %v\n", err) return "", "", fmt.Errorf("Failed to bundle query pack: %v\n", err)
@@ -419,14 +422,14 @@ defaultSuite:
return bundleBase64, queryId, nil return bundleBase64, queryId, nil
} }
func PackPacklist(codeqlPath string, dir string, includeQueries bool) []string { func PackPacklist(dir string, includeQueries bool) []string {
// since 2.7.1, packlist returns an object with a "paths" property that is a list of packs. // since 2.7.1, packlist returns an object with a "paths" property that is a list of packs.
args := []string{"pack", "packlist", "--format=json"} args := []string{"pack", "packlist", "--format=json"}
if !includeQueries { if !includeQueries {
args = append(args, "--no-include-queries") args = append(args, "--no-include-queries")
} }
args = append(args, dir) args = append(args, dir)
jsonBytes, err := RunCodeQLCommand(codeqlPath, false, args...) jsonBytes, err := RunCodeQLCommand("", false, args...)
var packlist map[string][]string var packlist map[string][]string
err = json.Unmarshal(jsonBytes, &packlist) err = json.Unmarshal(jsonBytes, &packlist)
if err != nil { if err != nil {