Repository navigation
Scan files of all languages in a single opengrep run TAR-155 #40
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -232,135 +232,28 @@ func formatParameterName(name string) string { | |
| return result | ||
| } | ||
|
|
||
| var filesByLanguage = make(map[string][]string) | ||
|
|
||
| // Semgrep: supported language tags are: apex, bash, c, c#, c++, cairo, clojure, cpp, csharp, dart, docker, dockerfile, elixir, ex, generic, go, golang, hack, hcl, html, java, javascript, js, json, jsonnet, julia, kotlin, kt, lisp, lua, none, ocaml, php, promql, proto, proto3, protobuf, py, python, python2, python3, r, regex, ruby, rust, scala, scheme, sh, sol, solidity, swift, terraform, tf, ts, typescript, vue, xml, yaml | ||
| // Semgrep: https://github.com/semgrep/semgrep/blob/0ec2b95ec8c3afb8e31fc0295d3604e540c982b0/src/parsing/Unit_parsing.ml#L61 | ||
| // Codacy: taken from https://github.com/codacy/ragnaros/blob/05d1374b7ca4a0aa3be44972484938b4785c046f/components/language/src/main/scala/codacy/foundation/api/Language.scala#L6 | ||
| var extensionToLanguageMap = map[string]string{ | ||
| ".js": "javascript", | ||
| ".jsx": "javascript", | ||
| ".jsm": "javascript", // missing from tests | ||
| ".vue": "vue", | ||
| ".mjs": "javascript", // missing from tests | ||
| ".scala": "scala", | ||
| // ".css" | ||
| ".php": "php", | ||
| ".py": "python", | ||
| ".rb": "ruby", | ||
| ".gemspec": "ruby", // missing from tests | ||
| ".podspec": "ruby", // missing from tests | ||
| ".jbuilder": "ruby", // missing from tests | ||
| ".rake": "ruby", // missing from tests | ||
| ".opal": "ruby", // missing from tests | ||
| ".java": "java", | ||
| // ".coffee" | ||
| ".swift": "swift", | ||
| ".cpp": "cpp", | ||
| ".hpp": "cpp", // missing from tests | ||
| ".cc": "cpp", // missing from tests | ||
| ".cxx": "cpp", // missing from tests | ||
| ".ino": "cpp", // missing from tests | ||
| ".c": "c", | ||
| ".h": "c", // missing | ||
| ".sh": "sh", // missing from tests | ||
| ".bash": "bash", | ||
| ".ts": "typescript", | ||
| ".tsx": "typescript", | ||
| ".dockerfile": "dockerfile", | ||
| "Dockerfile": "dockerfile", | ||
| ".sql": "generic", | ||
| ".pls": "generic", | ||
| ".trg": "generic", | ||
| ".prc": "generic", | ||
| ".fnc": "generic", | ||
| ".pld": "generic", | ||
| ".plh": "generic", | ||
| ".plb": "generic", | ||
| ".pck": "generic", | ||
| ".pks": "generic", | ||
| ".pkh": "generic", | ||
| ".pkb": "generic", | ||
| ".typ": "generic", | ||
| ".tyb": "generic", | ||
| ".tps": "generic", | ||
| ".tpb": "generic", | ||
| // ".tsql" | ||
| // ".trg", ".prc", ".fnc", ".pld", ".pls", ".plh", ".plb", ".pck", ".pks", ".pkh", ".pkb", ".typ", ".tyb", ".tps", ".tpb" | ||
| ".json": "json", | ||
| // ".scss" | ||
| // ".less" | ||
| ".go": "go", | ||
| // ".jsp" | ||
| // ".vm" | ||
| ".xml": "xml", | ||
| ".xsl": "xml", // missing from tests | ||
| ".wsdl": "xml", // missing from tests | ||
| ".pom": "xml", // missing from tests | ||
| ".cls": "apex", // missing from tests | ||
| ".trigger": "apex", // missing from testss | ||
| // ".component", ".page" | ||
| ".cs": "csharp", | ||
| ".kt": "kotlin", | ||
| ".kts": "kotlin", // missing from tests | ||
| ".ex": "elixir", // missing from tests | ||
| ".exs": "elixir", | ||
| // ".md", ".markdown", ".mdown", ".mkdn", ".mkd", ".mdwn", ".mkdown", ".ron" | ||
| // ".ps1", ".psc1", ".psd1", ".psm1", ".ps1xml", ".pssc", ".cdxml", ".clixml" | ||
| // ".cr" | ||
| // ".cbl", ".cob" | ||
| // ".groovy" | ||
| // ".abap" | ||
| // ".vb" | ||
| // ".m" | ||
| ".yaml": "yaml", // should these be Terraform? | ||
| ".yml": "yaml", | ||
| ".dart": "dart", // missing from tests | ||
| ".rs": "rust", | ||
| ".rlib": "rust", // missing from tests | ||
| ".clj": "clojure", | ||
| ".cljs": "clojure", // missing from tests | ||
| ".cljc": "clojure", // missing from tests | ||
| ".edn": "clojure", // missing from tests | ||
| // ".hs", ".lhs" | ||
| // ".erl" | ||
| // ".elm" | ||
| ".html": "html", // missing from tests | ||
| // ".pl" | ||
| // ".fs" | ||
| // ".f90", ".f95", ".f03" | ||
| ".r": "r", // missing from tests | ||
| // ".scratch", ".sb", ".sprite", ".sb2", ".sprite2" | ||
| ".lua": "lua", // missing from tests | ||
| ".asd": "lisp", // missing from tests | ||
| ".el": "lisp", // missing from tests | ||
| ".lsp": "lisp", // missing from tests | ||
| ".lisp": "lisp", // missing from tests | ||
| // ".P", ".swipl" | ||
| ".jl": "julia", // missing from tests | ||
| // ".ml", ".mli", ".mly", ".mll" | ||
| ".sol": "solidity", | ||
| ".tf": "terraform", | ||
| } | ||
| // filesToAnalyse holds every file to scan, regardless of language: opengrep | ||
| // picks the rules that apply to each file, so a single run covers all languages. | ||
| var filesToAnalyse []string | ||
|
|
||
| func populateFilesByLanguage(toolExecutionFiles *[]string, toolExecutionSourceDir string) error { | ||
| func populateFilesToAnalyse(toolExecutionFiles *[]string, toolExecutionSourceDir string) error { | ||
| // If there are files to analyse, analyse only those files | ||
| if toolExecutionFiles != nil && len(*toolExecutionFiles) > 0 { | ||
| return populateFilesByLanguageFromFiles(*toolExecutionFiles) | ||
| return populateFilesToAnalyseFromFiles(*toolExecutionFiles) | ||
| } | ||
| // If there are no files to analyse, analyse all files from source dir | ||
| return populateFilesByLanguageFromSourceDir(toolExecutionSourceDir) | ||
| return populateFilesToAnalyseFromSourceDir(toolExecutionSourceDir) | ||
| } | ||
|
|
||
| func populateFilesByLanguageFromFiles(toolExecutionFiles []string) error { | ||
| func populateFilesToAnalyseFromFiles(toolExecutionFiles []string) error { | ||
| for _, file := range toolExecutionFiles { | ||
| addFileToFilesByLanguage(file) | ||
| addFileToAnalyse(file) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Reset the shared file list before each execution, or make it local to the execution, so repeated tool runs remain isolated.
|
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func populateFilesByLanguageFromSourceDir(toolExecutionSourceDir string) error { | ||
| func populateFilesToAnalyseFromSourceDir(toolExecutionSourceDir string) error { | ||
| // Semgrep can analyse full directories and its subdirectories | ||
| // but we will have to analyse every extension from every file | ||
| // so we will have to do this walk somewhere else if we dont do it here | ||
|
|
@@ -382,26 +275,12 @@ func processFile(path string, info fs.DirEntry, err error) error { | |
| } | ||
| // if it is a file and it is not a hidden file | ||
| if !pathInfo.IsDir() && !strings.HasPrefix(pathInfo.Name(), ".") { | ||
| addFileToFilesByLanguage(path) | ||
| addFileToAnalyse(path) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func addFileToFilesByLanguage(fileName string) { | ||
| language := detectLanguage(fileName) | ||
| filesByLanguage[language] = append(filesByLanguage[language], fileName) | ||
| } | ||
|
|
||
| func detectLanguage(fileName string) string { | ||
| extension := strings.ToLower(filepath.Ext(fileName)) | ||
| extensionOrFilename := extension | ||
| if extension == "" { | ||
| extensionOrFilename = fileName | ||
| } | ||
|
|
||
| if language, ok := extensionToLanguageMap[extensionOrFilename]; ok { | ||
| return language | ||
| } | ||
| return "none" | ||
| func addFileToAnalyse(fileName string) { | ||
| filesToAnalyse = append(filesToAnalyse, fileName) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package tool | ||
|
|
||
| import ( | ||
| "math" | ||
| "os" | ||
| "runtime" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // jobsEnvVar lets the deployment set opengrep's job count explicitly, | ||
| // e.g. from the pod's CPU request. | ||
| const jobsEnvVar = "OPENGREP_JOBS" | ||
|
|
||
| const cgroupV2CPUMax = "/sys/fs/cgroup/cpu.max" | ||
|
|
||
| // opengrepJobs returns the number of parallel jobs opengrep should use: | ||
| // the env var if set, else the cgroup CPU quota, else the number of CPUs. | ||
| func opengrepJobs() int { | ||
| if jobs, err := strconv.Atoi(os.Getenv(jobsEnvVar)); err == nil && jobs > 0 { | ||
| return jobs | ||
| } | ||
| if jobs, ok := cgroupJobs(cgroupV2CPUMax); ok { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Resolve the process's cgroup from
|
||
| return jobs | ||
| } | ||
| return runtime.NumCPU() | ||
| } | ||
|
|
||
| // cgroupJobs parses a cgroup v2 cpu.max file ("<quota> <period>" or "max <period>"). | ||
| func cgroupJobs(path string) (int, bool) { | ||
| content, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
| fields := strings.Fields(string(content)) | ||
| if len(fields) != 2 || fields[0] == "max" { | ||
| return 0, false | ||
| } | ||
| quota, err := strconv.ParseFloat(fields[0], 64) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
| period, err := strconv.ParseFloat(fields[1], 64) | ||
| if err != nil || period <= 0 || quota <= 0 { | ||
| return 0, false | ||
| } | ||
| return max(1, int(math.Ceil(quota/period))), true | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have ideia why was commented? Maybe something that existed on semgrep that was not available yet?