-
Notifications
You must be signed in to change notification settings - Fork 532
/
webpage.go
131 lines (105 loc) · 2.69 KB
/
webpage.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/aquasecurity/defsec/pkg/providers"
)
type templateObject struct {
ID string
ShortCode string
LegacyID string
Severity string
Summary string
Service string
Provider string
Explanation string
Impact string
Resolution string
BadExample string
GoodExample string
Links []string
}
func generateWebPages(fileContents []*FileContent) error {
for _, contents := range fileContents {
for _, check := range contents.Checks {
webProviderPath := filepath.Join(webPath, strings.ToLower(check.Provider), strings.ToLower(check.Service))
if err := generateWebPage(webProviderPath, check); err != nil {
return err
}
}
}
return nil
}
var funcMap = template.FuncMap{
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
"FormatProviderName": formatProviderName,
"Join": join,
}
func join(s []string) string {
if s == nil {
return ""
}
return strings.Join(s[1:], s[0])
}
func formatProviderName(providerName string) string {
if providerName == "digitalocean" {
providerName = "digital ocean"
}
return providers.Provider(providerName).DisplayName()
}
func generateWebPage(webProviderPath string, r templateObject) error {
if err := os.MkdirAll(webProviderPath, os.ModePerm); err != nil {
return err
}
filePath := filepath.Join(webProviderPath, r.ShortCode, "index.md")
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
return err
}
fmt.Printf("Generating page for %s at %s\n", r.ID, filePath)
webTmpl := template.Must(template.New("web").Funcs(funcMap).Parse(baseWebPageTemplate))
return writeTemplate(r, filePath, webTmpl)
}
func writeTemplate(contents interface{}, path string, tmpl *template.Template) error {
outputFile, err := os.Create(path)
if err != nil {
return err
}
defer func() { _ = outputFile.Close() }()
return tmpl.Execute(outputFile, contents)
}
const baseWebPageTemplate = `---
title: {{$.Summary}}
---
# {{$.Summary}}
### Default Severity: <span class="severity {{$.Severity | ToLower }}">{{$.Severity }}</span>
### Explanation
{{$.Explanation}}
### Possible Impact
{{$.Impact}}
### Suggested Resolution
{{$.Resolution}}
{{if $.BadExample }}
### Insecure Example
The following example will fail the {{$.ID}} check.
` + "```terraform" + `
{{ $.BadExample }}
` + "```" + `
{{end}}
{{if $.GoodExample }}
### Secure Example
The following example will pass the {{$.ID}} check.
` + "```terraform" + `
{{ $.GoodExample }}
` + "```" + `
{{end}}
{{if $.Links}}
### Links
{{range $link := $.Links}}
- [{{.}}]({{.}}){:target="_blank" rel="nofollow noreferrer noopener"}
{{end}}
{{end}}
`