|
1 | | -// Scripts to check if all the rules that exist in the latest version of "gitleaks" are included in our list of rules (in secret.go file) |
2 | | -package main |
3 | | - |
4 | | -import ( |
5 | | - "encoding/json" |
6 | | - "fmt" |
7 | | - "io" |
8 | | - "net/http" |
9 | | - "os" |
10 | | - "regexp" |
11 | | -) |
12 | | - |
13 | | -var ( |
14 | | - regexGitleaksRules = regexp.MustCompile(`(?m)^[^/\n\r]\s*rules\.([a-zA-Z0-9_]+)\(`) |
15 | | - regex2msRules = regexp.MustCompile(`(?m)^[^/\n\r]\s*(?:// )?{Rule:\s*\*(?:rules\.)?([a-zA-Z0-9_]+)\(\),`) |
16 | | -) |
17 | | - |
18 | | -func main() { |
19 | | - |
20 | | - latestGitleaksRelease, err := fetchGitleaksLatestRelease() |
21 | | - if err != nil { |
22 | | - fmt.Printf("%s\n", err) |
23 | | - os.Exit(1) |
24 | | - } |
25 | | - fmt.Printf("Latest Gitleaks release: %s\n", latestGitleaksRelease) |
26 | | - |
27 | | - gitleaksRules, err := fetchGitleaksRules(latestGitleaksRelease) |
28 | | - if err != nil { |
29 | | - fmt.Printf("%s\n", err) |
30 | | - os.Exit(1) |
31 | | - } |
32 | | - |
33 | | - matchesGitleaksRules := regexGitleaksRules.FindAllStringSubmatch(string(gitleaksRules), -1) |
34 | | - if len(matchesGitleaksRules) == 0 { |
35 | | - fmt.Println("No rules found in the latest version of Gitleaks.") |
36 | | - os.Exit(1) |
37 | | - } |
38 | | - fmt.Printf("Total rules in the latest version of Gitleaks: %d\n", len(matchesGitleaksRules)) |
39 | | - |
40 | | - ourRules, err := fetchOurRules() |
41 | | - if err != nil { |
42 | | - fmt.Printf("%s\n", err) |
43 | | - os.Exit(1) |
44 | | - } |
45 | | - match2msRules := regex2msRules.FindAllStringSubmatch(string(ourRules), -1) |
46 | | - if len(match2msRules) == 0 { |
47 | | - fmt.Println("No rules found in 2ms.") |
48 | | - os.Exit(1) |
49 | | - } |
50 | | - fmt.Printf("Total rules in 2ms: %d\n", len(match2msRules)) |
51 | | - |
52 | | - map2msRules := make(map[string]bool) |
53 | | - for _, match := range match2msRules { |
54 | | - map2msRules[match[1]] = true |
55 | | - } |
56 | | - |
57 | | - missingRulesIn2ms := []string{} |
58 | | - for _, rule := range matchesGitleaksRules { |
59 | | - if _, found := map2msRules[rule[1]]; !found { |
60 | | - missingRulesIn2ms = append(missingRulesIn2ms, rule[1]) |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - if len(missingRulesIn2ms) > 0 { |
65 | | - fmt.Printf("%d rules exist in the latest version of Gitleaks but missing on 2ms: \n\n", len(missingRulesIn2ms)) |
66 | | - for _, rule := range missingRulesIn2ms { |
67 | | - fmt.Printf("%s \n", rule) |
68 | | - } |
69 | | - |
70 | | - fmt.Printf("\nLink to Gitleaks main.go file of version: %s:\n", latestGitleaksRelease) |
71 | | - fmt.Println(getGitleaksRulesRawURL(latestGitleaksRelease)) |
72 | | - |
73 | | - os.Exit(1) |
74 | | - } else { |
75 | | - fmt.Println("No differences found.") |
76 | | - os.Exit(0) |
77 | | - } |
78 | | -} |
79 | | - |
80 | | -type Release struct { |
81 | | - TagName string `json:"tag_name"` |
82 | | -} |
83 | | - |
84 | | -func fetchGitleaksLatestRelease() (string, error) { |
85 | | - var release Release |
86 | | - |
87 | | - response, err := http.Get("https://api.github.com/repos/zricethezav/gitleaks/releases/latest") |
88 | | - if err != nil { |
89 | | - return "", fmt.Errorf("failed to get latest release: %w", err) |
90 | | - } |
91 | | - defer response.Body.Close() |
92 | | - |
93 | | - decoder := json.NewDecoder(response.Body) |
94 | | - if err := decoder.Decode(&release); err != nil { |
95 | | - return "", fmt.Errorf("failed to decode latest release JSON: %w", err) |
96 | | - } |
97 | | - |
98 | | - return release.TagName, nil |
99 | | -} |
100 | | - |
101 | | -func fetchGitleaksRules(version string) ([]byte, error) { |
102 | | - rawURLGitleaksRules := getGitleaksRulesRawURL(version) |
103 | | - response, err := http.Get(rawURLGitleaksRules) |
104 | | - if err != nil { |
105 | | - return nil, fmt.Errorf("failed to fetch remote file: %w", err) |
106 | | - } |
107 | | - defer response.Body.Close() |
108 | | - |
109 | | - content, err := io.ReadAll(response.Body) |
110 | | - if err != nil { |
111 | | - return nil, fmt.Errorf("failed to read remote file content: %w", err) |
112 | | - } |
113 | | - |
114 | | - return content, nil |
115 | | -} |
116 | | - |
117 | | -func getGitleaksRulesRawURL(version string) string { |
118 | | - return fmt.Sprintf("https://raw.githubusercontent.com/zricethezav/gitleaks/%s/cmd/generate/config/main.go", version) |
119 | | -} |
120 | | - |
121 | | -func fetchOurRules() ([]byte, error) { |
122 | | - content, err := os.ReadFile("engine/rules/rules.go") |
123 | | - if err != nil { |
124 | | - return nil, fmt.Errorf("failed to read our file content: %w", err) |
125 | | - } |
126 | | - return content, nil |
127 | | -} |
| 1 | +// Scripts to check if all the rules that exist in the latest version of "gitleaks" are included in our list of rules (in secret.go file) |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "regexp" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + regexGitleaksRules = regexp.MustCompile(`(?m)^[^/\n\r]\s*rules\.([a-zA-Z0-9_]+)\(`) |
| 15 | + regex2msRules = regexp.MustCompile(`(?m)^[^/\n\r]\s*(?:// )?{Rule:\s*\*(?:rules\.)?([a-zA-Z0-9_]+)\(\),`) |
| 16 | +) |
| 17 | + |
| 18 | +func main() { |
| 19 | + |
| 20 | + latestGitleaksRelease, err := fetchGitleaksLatestRelease() |
| 21 | + if err != nil { |
| 22 | + fmt.Printf("%s\n", err) |
| 23 | + os.Exit(1) |
| 24 | + } |
| 25 | + fmt.Printf("Latest Gitleaks release: %s\n", latestGitleaksRelease) |
| 26 | + |
| 27 | + gitleaksRules, err := fetchGitleaksRules(latestGitleaksRelease) |
| 28 | + if err != nil { |
| 29 | + fmt.Printf("%s\n", err) |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | + |
| 33 | + matchesGitleaksRules := regexGitleaksRules.FindAllStringSubmatch(string(gitleaksRules), -1) |
| 34 | + if len(matchesGitleaksRules) == 0 { |
| 35 | + fmt.Println("No rules found in the latest version of Gitleaks.") |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + fmt.Printf("Total rules in the latest version of Gitleaks: %d\n", len(matchesGitleaksRules)) |
| 39 | + |
| 40 | + ourRules, err := fetchOurRules() |
| 41 | + if err != nil { |
| 42 | + fmt.Printf("%s\n", err) |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | + match2msRules := regex2msRules.FindAllStringSubmatch(string(ourRules), -1) |
| 46 | + if len(match2msRules) == 0 { |
| 47 | + fmt.Println("No rules found in 2ms.") |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + fmt.Printf("Total rules in 2ms: %d\n", len(match2msRules)) |
| 51 | + |
| 52 | + map2msRules := make(map[string]bool) |
| 53 | + for _, match := range match2msRules { |
| 54 | + map2msRules[match[1]] = true |
| 55 | + } |
| 56 | + |
| 57 | + missingRulesIn2ms := []string{} |
| 58 | + for _, rule := range matchesGitleaksRules { |
| 59 | + if _, found := map2msRules[rule[1]]; !found { |
| 60 | + missingRulesIn2ms = append(missingRulesIn2ms, rule[1]) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if len(missingRulesIn2ms) > 0 { |
| 65 | + fmt.Printf("%d rules exist in the latest version of Gitleaks but missing on 2ms: \n\n", len(missingRulesIn2ms)) |
| 66 | + for _, rule := range missingRulesIn2ms { |
| 67 | + fmt.Printf("%s \n", rule) |
| 68 | + } |
| 69 | + |
| 70 | + fmt.Printf("\nLink to Gitleaks main.go file of version: %s:\n", latestGitleaksRelease) |
| 71 | + fmt.Println(getGitleaksRulesRawURL(latestGitleaksRelease)) |
| 72 | + |
| 73 | + os.Exit(1) |
| 74 | + } else { |
| 75 | + fmt.Println("No differences found.") |
| 76 | + os.Exit(0) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +type Release struct { |
| 81 | + TagName string `json:"tag_name"` |
| 82 | +} |
| 83 | + |
| 84 | +func fetchGitleaksLatestRelease() (string, error) { |
| 85 | + var release Release |
| 86 | + |
| 87 | + response, err := http.Get("https://api.github.com/repos/zricethezav/gitleaks/releases/latest") |
| 88 | + if err != nil { |
| 89 | + return "", fmt.Errorf("failed to get latest release: %w", err) |
| 90 | + } |
| 91 | + defer response.Body.Close() |
| 92 | + |
| 93 | + decoder := json.NewDecoder(response.Body) |
| 94 | + if err := decoder.Decode(&release); err != nil { |
| 95 | + return "", fmt.Errorf("failed to decode latest release JSON: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + return release.TagName, nil |
| 99 | +} |
| 100 | + |
| 101 | +func fetchGitleaksRules(version string) ([]byte, error) { |
| 102 | + rawURLGitleaksRules := getGitleaksRulesRawURL(version) |
| 103 | + response, err := http.Get(rawURLGitleaksRules) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("failed to fetch remote file: %w", err) |
| 106 | + } |
| 107 | + defer response.Body.Close() |
| 108 | + |
| 109 | + content, err := io.ReadAll(response.Body) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("failed to read remote file content: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + return content, nil |
| 115 | +} |
| 116 | + |
| 117 | +func getGitleaksRulesRawURL(version string) string { |
| 118 | + return fmt.Sprintf("https://raw.githubusercontent.com/zricethezav/gitleaks/%s/cmd/generate/config/main.go", version) |
| 119 | +} |
| 120 | + |
| 121 | +func fetchOurRules() ([]byte, error) { |
| 122 | + content, err := os.ReadFile("engine/rules/rules.go") |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("failed to read our file content: %w", err) |
| 125 | + } |
| 126 | + return content, nil |
| 127 | +} |
0 commit comments