-
Notifications
You must be signed in to change notification settings - Fork 3
/
magefile.go
244 lines (195 loc) · 4.95 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2023 The OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
//go:build mage
// +build mage
package main
import (
"archive/zip"
"bufio"
"bytes"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/sh"
)
const (
rulesDir = "rules"
testsDir = "tests"
)
// DownloadDeps downloads the OWASP CRS and the recommended OWASP Coraza configuration file
func DownloadDeps() error {
if err := downloadCRS(); err != nil {
return err
}
if err := downloadCorazaConfig(); err != nil {
return err
}
return nil
}
// downloadCorazaConfig downloads the recommended Coraza configuration file from the OWASP Coraza repository
func downloadCorazaConfig() error {
uri := fmt.Sprintf("https://raw.githubusercontent.com/corazawaf/coraza/%s/coraza.conf-recommended", corazaVersion)
corazaConfig, err := getDataFromURL(uri)
if err != nil {
return err
}
out, err := os.Create(filepath.Join(rulesDir, "@coraza.conf-recommended"))
if err != nil {
return err
}
defer out.Close()
_, err = out.Write(corazaConfig)
if err != nil {
return err
}
return nil
}
// downloadCRS downloads the OWASP CRS from the CRS repository
func downloadCRS() error {
rulesDstDir := rulesDir + "/@owasp_crs"
// Before downloading, we need to remove:
// - old rules under rules/@owasp_crs
// - all the related tests
if err := cleanupOldCRS(rulesDstDir, testsDir); err != nil {
return err
}
if err := os.MkdirAll(rulesDstDir, os.ModePerm); err != nil {
return err
}
uri := fmt.Sprintf("https://github.com/coreruleset/coreruleset/archive/%s.zip", crsVersion)
crsZip, err := getDataFromURL(uri)
if err != nil {
return err
}
r, err := zip.NewReader(bytes.NewReader(crsZip), int64(len(crsZip)))
if err != nil {
return err
}
crsVersionStripped := strings.TrimPrefix(crsVersion, "v")
const licenseNumberOfLines = 9
for _, f := range r.File {
if f.Name == fmt.Sprintf("coreruleset-%s/LICENSE", crsVersionStripped) {
if err := copyFile(f, filepath.Join(rulesDir, "LICENSE")); err != nil {
return err
}
continue
}
if f.Name == fmt.Sprintf("coreruleset-%s/crs-setup.conf.example", crsVersionStripped) {
if err := copyFile(f, filepath.Join(rulesDir, "@crs-setup.conf.example")); err != nil {
return err
}
continue
}
if f.FileInfo().IsDir() {
continue
}
testPrefix := fmt.Sprintf("coreruleset-%s/tests/regression/tests", crsVersionStripped)
if strings.HasPrefix(f.Name, testPrefix) {
if !strings.HasSuffix(f.Name, ".yaml") {
continue
}
subdir := strings.TrimPrefix(filepath.Dir(f.Name), testPrefix)
dir := filepath.Join(testsDir, subdir)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
copyFile(f, filepath.Join(dir, filepath.Base(f.Name)))
}
prefix := fmt.Sprintf("coreruleset-%s/rules/", crsVersionStripped)
if !strings.HasPrefix(f.Name, prefix) {
continue
}
filename := f.Name[len(prefix):]
if strings.HasSuffix(filename, ".example") {
continue
}
fPath := filepath.Join(rulesDstDir, filename)
target, err := os.Create(fPath)
if err != nil {
return err
}
source, err := f.Open()
if err != nil {
os.Remove(fPath)
return err
}
fileScanner := bufio.NewScanner(source)
fileScanner.Split(bufio.ScanLines)
lineNumber := 0
for fileScanner.Scan() {
lineNumber++
if lineNumber <= licenseNumberOfLines {
target.Write(fileScanner.Bytes())
target.WriteString("\n")
continue
}
text := strings.TrimSpace(fileScanner.Text())
if len(text) == 0 || text[0] == '#' {
continue
}
target.Write(fileScanner.Bytes())
target.WriteString("\n")
}
source.Close()
target.Close()
}
return nil
}
func getDataFromURL(uri string) ([]byte, error) {
resp, err := http.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return bodyBytes, nil
}
func copyFile(f *zip.File, dstPath string) error {
source, err := f.Open()
if err != nil {
return err
}
l, err := io.ReadAll(source)
if err != nil {
return err
}
source.Close()
if err := os.WriteFile(dstPath, l, 0666); err != nil {
return err
}
return nil
}
func cleanupOldCRS(rulesDstDir, testsDir string) error {
if err := os.RemoveAll(rulesDstDir); err != nil {
return err
}
if err := filepath.WalkDir(testsDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
// tests folder contains go files related to the coraza-coreruleset repo that we don't want to remove
if d.Name() != "tests.go" && d.Name() != "tests_test.go" && path != testsDir {
if err := os.RemoveAll(path); err != nil {
return err
}
return filepath.SkipDir
}
return nil
}); err != nil {
return err
}
return nil
}
// Test runs the tests
func Test() error {
return sh.RunV("go", "test", "./...")
}