-
Notifications
You must be signed in to change notification settings - Fork 150
/
html.go
241 lines (205 loc) · 5.88 KB
/
html.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
package output
import (
"bytes"
"embed"
"encoding/base64"
"fmt"
"html/template"
"math"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/r3labs/diff/v2"
"github.com/snyk/driftctl/pkg/alerter"
"github.com/snyk/driftctl/pkg/analyser"
"github.com/snyk/driftctl/pkg/resource"
)
const HTMLOutputType = "html"
const HTMLOutputExample = "html://PATH/TO/FILE.html"
// assets holds our static web content.
//go:embed assets/*
var assets embed.FS
type HTML struct {
path string
}
type HTMLTemplateParams struct {
IsSync bool
ScanDate string
Coverage int
Summary analyser.Summary
Unmanaged []*resource.Resource
Differences []analyser.Difference
Deleted []*resource.Resource
Alerts alerter.Alerts
Stylesheet template.CSS
ScanDuration string
ProviderName string
ProviderVersion string
LogoSvg template.HTML
FaviconBase64 string
}
func NewHTML(path string) *HTML {
return &HTML{path}
}
func (c *HTML) Write(analysis *analyser.Analysis) error {
file := os.Stdout
if !isStdOut(c.path) {
f, err := os.OpenFile(c.path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer f.Close()
file = f
}
tmplFile, err := assets.ReadFile("assets/index.tmpl")
if err != nil {
return err
}
styleFile, err := assets.ReadFile("assets/style.css")
if err != nil {
return err
}
logoSvgFile, err := assets.ReadFile("assets/driftctl_light.svg")
if err != nil {
return err
}
faviconFile, err := assets.ReadFile("assets/favicon.ico")
if err != nil {
return err
}
funcMap := template.FuncMap{
"getResourceTypes": func() []string {
resources := make([]*resource.Resource, 0)
resources = append(resources, analysis.Unmanaged()...)
resources = append(resources, analysis.Deleted()...)
for _, d := range analysis.Differences() {
resources = append(resources, d.Res)
}
return distinctResourceTypes(resources)
},
"getIaCSources": func() []string {
resources := make([]*resource.Resource, 0)
resources = append(resources, analysis.Deleted()...)
resources = append(resources, analysis.Managed()...)
return distinctIaCSources(resources)
},
"rate": func(count int) float64 {
if analysis.Summary().TotalResources == 0 {
return 0
}
rate := 100 * float64(count) / float64(analysis.Summary().TotalResources)
return math.Floor(rate*100) / 100
},
"jsonDiff": func(ch analyser.Changelog) template.HTML {
var buf bytes.Buffer
whiteSpace := " "
for _, change := range ch {
for i, v := range change.Path {
if _, err := strconv.Atoi(v); err == nil {
change.Path[i] = fmt.Sprintf("[%s]", v)
}
}
path := strings.Join(change.Path, ".")
switch change.Type {
case diff.CREATE:
pref := fmt.Sprintf("%s %s:", "+", path)
_, _ = fmt.Fprintf(&buf, "%s%s <span class=\"code-box-line-create\">%s</span>", whiteSpace, pref, prettify(change.To))
case diff.DELETE:
pref := fmt.Sprintf("%s %s:", "-", path)
_, _ = fmt.Fprintf(&buf, "%s%s <span class=\"code-box-line-delete\">%s</span>", whiteSpace, pref, prettify(change.From))
case diff.UPDATE:
prefix := fmt.Sprintf("%s %s:", "~", path)
if change.JsonString {
_, _ = fmt.Fprintf(&buf, "%s%s<br>%s%s<br>", whiteSpace, prefix, whiteSpace, jsonDiffHTML(change.From, change.To))
continue
}
_, _ = fmt.Fprintf(&buf, "%s%s <span class=\"code-box-line-delete\">%s</span> => <span class=\"code-box-line-create\">%s</span>", whiteSpace, prefix, htmlPrettify(change.From), htmlPrettify(change.To))
}
if change.Computed {
_, _ = fmt.Fprintf(&buf, " %s", "(computed)")
}
_, _ = fmt.Fprintf(&buf, "<br>")
}
return template.HTML(buf.String())
},
}
tmpl, err := template.New("main").Funcs(funcMap).Parse(string(tmplFile))
if err != nil {
return err
}
data := &HTMLTemplateParams{
IsSync: analysis.IsSync(),
ScanDate: analysis.Date.Format("Jan 02, 2006"),
Coverage: analysis.Coverage(),
Summary: analysis.Summary(),
Unmanaged: analysis.Unmanaged(),
Differences: analysis.Differences(),
Deleted: analysis.Deleted(),
Alerts: analysis.Alerts(),
Stylesheet: template.CSS(styleFile),
ScanDuration: analysis.Duration.Round(time.Second).String(),
ProviderName: analysis.ProviderName,
ProviderVersion: analysis.ProviderVersion,
LogoSvg: template.HTML(logoSvgFile),
FaviconBase64: base64.StdEncoding.EncodeToString(faviconFile),
}
err = tmpl.Execute(file, data)
if err != nil {
return err
}
return nil
}
func distinctResourceTypes(resources []*resource.Resource) []string {
types := make([]string, 0)
for _, res := range resources {
found := false
for _, v := range types {
if v == res.ResourceType() {
found = true
break
}
}
if !found {
types = append(types, res.ResourceType())
}
}
return types
}
func distinctIaCSources(resources []*resource.Resource) []string {
types := make([]string, 0)
for _, res := range resources {
if res.Src() == nil {
continue
}
found := false
for _, v := range types {
if v == res.Src().Source() {
found = true
break
}
}
if !found {
types = append(types, res.Src().Source())
}
}
return types
}
func htmlPrettify(resource interface{}) string {
res := reflect.ValueOf(resource)
if resource == nil || res.Kind() == reflect.Ptr && res.IsNil() {
return "null"
}
return awsutil.Prettify(resource)
}
func jsonDiffHTML(a, b interface{}) string {
diffStr := jsonDiff(a, b, false)
re := regexp.MustCompile(`(?m)^(?P<value>(\-)(.*))$`)
diffStr = re.ReplaceAllString(diffStr, `<span class="code-box-line-delete">$value</span>`)
re = regexp.MustCompile(`(?m)^(?P<value>(\+)(.*))$`)
diffStr = re.ReplaceAllString(diffStr, `<span class="code-box-line-create">$value</span>`)
return diffStr
}