-
Notifications
You must be signed in to change notification settings - Fork 151
/
generate.go
215 lines (194 loc) · 5.47 KB
/
generate.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
package main
import (
"encoding/base64"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"gopkg.in/yaml.v2"
)
const (
PARSER_TYPE = "parsers"
SCENARIO_TYPE = "scenarios"
POSTOVERFLOW_TYPE = "postoverflows"
CONTEXT_TYPE = "contexts"
APPSEC_RULES_TYPE = "appsec-rules"
APPSEC_CONFIGS_TYPE = "appsec-configs"
COLLECTIONS_TYPE = "collections"
)
func (ti *typeInfo) generate(filepath string, configType string) (string, error) {
pathSplit := strings.Split(filepath, "/")
//generate doc path ?
pdocpath := strings.Replace(filepath, ".yaml", ".md", 1)
pdocpath = strings.Replace(pdocpath, ".yml", ".md", 1)
if pathSplit[0] != configType {
return "", fmt.Errorf("invalid filepath (doesn't start with scenarios) : %s", filepath)
}
// Remove the first item (we don't need it)
pathSplit = pathSplit[1:]
// set user, stage and config name
var user string
var configName string
switch configType {
case PARSER_TYPE, POSTOVERFLOW_TYPE:
if len(pathSplit) != 3 {
return "", fmt.Errorf("invalid filepath '%s', should be : './%s/<user>/<scenario.yaml>'", configType, filepath)
}
ti.Stage = pathSplit[0]
user = pathSplit[1]
configName = pathSplit[2]
configName = strings.Split(configName, ".")[0]
case SCENARIO_TYPE, APPSEC_RULES_TYPE, APPSEC_CONFIGS_TYPE, COLLECTIONS_TYPE, CONTEXT_TYPE:
if len(pathSplit) != 2 {
return "", fmt.Errorf("invalid filepath '%s', should be : './%s/<user>/<scenario.yaml>'", configType, filepath)
}
user = pathSplit[0]
configName = pathSplit[1]
configName = strings.Split(configName, ".")[0]
default:
return "", fmt.Errorf("invalid config type '%s'", configType)
}
// set the filepath
ti.Path = filepath
// set the author from the user
ti.Author = user
// set file information : autor, references, description
/* Get description, author and references from the file */
var fInfo fileInfo
yamlFile, err := os.ReadFile(filepath)
if err != nil {
return "", err
}
err = yaml.Unmarshal(yamlFile, &fInfo)
if err != nil {
return "", err
}
if fInfo.Author != "" {
ti.Author = fInfo.Author
}
if len(fInfo.References) > 0 {
ti.References = fInfo.References
}
if fInfo.Description != "" {
ti.Description = fInfo.Description
}
if fInfo.Labels != nil {
ti.Labels = fInfo.Labels
// var tags_to_keep = []string{"service", "type"}
// for _, v := range tags_to_keep {
// if x, ok := fInfo.Labels[v]; ok {
// ti.Tags = append(ti.Tags, x)
// }
// }
}
if configType == "collections" {
if len(fInfo.Parsers) > 0 {
ti.Parsers = fInfo.Parsers
} else {
ti.Parsers = nil
}
if len(fInfo.PostOverflows) > 0 {
ti.PostOverflows = fInfo.PostOverflows
} else {
ti.PostOverflows = nil
}
if len(fInfo.Scenarios) > 0 {
ti.Scenarios = fInfo.Scenarios
} else {
ti.Scenarios = nil
}
if len(fInfo.Collections) > 0 {
ti.Collections = fInfo.Collections
} else {
ti.Collections = nil
}
if len(fInfo.AppsecRules) > 0 {
ti.AppsecRules = fInfo.AppsecRules
} else {
ti.AppsecRules = nil
}
if len(fInfo.AppsecConfigs) > 0 {
ti.AppsecConfigs = fInfo.AppsecConfigs
} else {
ti.AppsecConfigs = nil
}
if len(fInfo.Contexts) > 0 {
ti.Contexts = fInfo.Contexts
} else {
ti.Contexts = nil
}
}
// versions informations (digest and deprecated for each version)
if len(ti.Versions) == 0 {
ti.Versions = make(map[string]versionInfo)
h, err := getSHA256(filepath)
if err != nil {
return "", fmt.Errorf("unable to get sha256 of '%s' : %v", filepath, err)
}
var vInfo versionInfo
vInfo.Digest = h
vInfo.Deprecated = false
ti.Versions["0.1"] = vInfo
ti.Version = "0.1"
} else {
lastVersion := ti.Version
lastDigest := ti.Versions[lastVersion].Digest
currentDigest, err := getSHA256(filepath)
if err != nil {
return "", fmt.Errorf("unable to get sha256 of '%s' : %v", filepath, err)
}
if currentDigest != lastDigest {
floatVersion, err := strconv.ParseFloat(ti.Version, 32)
if err != nil {
return "", fmt.Errorf("unable to convert version '%s' to float : %s", ti.Version, err.Error())
}
newVersion := fmt.Sprintf("%0.1f", floatVersion+0.1)
ti.Version = newVersion
log.Printf("%s new version : %s (sha:%s)", ti.Path, newVersion, currentDigest)
var vInfo versionInfo
vInfo.Digest = currentDigest
vInfo.Deprecated = false
ti.Versions[newVersion] = vInfo
}
}
hubName := fmt.Sprintf("%s/%s", user, configName)
/*if we're all good, check if markdown documentation exists and join it*/
//pdocpath
mdFile, err := os.ReadFile(pdocpath)
if err == nil {
ti.LongDescription = base64.StdEncoding.EncodeToString([]byte(string(mdFile)))
}
ti.FileContent = base64.StdEncoding.EncodeToString([]byte(string(yamlFile)))
return hubName, nil
}
func generateIndex(configType string) (map[string]typeInfo, error) {
var files []string
tInfo := make(map[string]typeInfo)
folder := path.Join("./", configType)
err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
files = append(files, path)
}
return nil
})
if err != nil {
panic(err)
}
for _, filepath := range files {
if strings.HasPrefix(filepath, folder) {
var info typeInfo
var hubName string
var err error
hubName, err = info.generate(filepath, configType)
if err != nil {
fmt.Printf("skipping '%s' for index generation because : %s\n", filepath, err.Error())
} else {
tInfo[hubName] = info
}
}
}
return tInfo, nil
}