-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
403 lines (355 loc) · 11.3 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"regexp"
"runtime"
"strings"
"time"
sprig "github.com/Masterminds/sprig/v3"
"github.com/dustin/go-humanize"
"github.com/google/uuid"
flags "github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/webdevops/go-common/azuresdk/prometheus/tracing"
yaml "gopkg.in/yaml.v3"
auditor "github.com/webdevops/azure-auditor/auditor"
"github.com/webdevops/azure-auditor/auditor/types"
"github.com/webdevops/azure-auditor/auditor/validator"
"github.com/webdevops/azure-auditor/config"
)
const (
Author = "webdevops.io"
UserAgent = "azure-auditor/"
)
var (
argparser *flags.Parser
Opts config.Opts
azureAuditor *auditor.AzureAuditor
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
)
func main() {
initArgparser()
defer initLogger().Sync() // nolint:errcheck
logger.Infof("starting azure-auditor v%s (%s; %s; by %v)", gitTag, gitCommit, runtime.Version(), Author)
logger.Info(string(Opts.GetJson()))
initSystem()
logger.Infof("starting audit")
azureAuditor = auditor.NewAzureAuditor()
azureAuditor.Opts = Opts
azureAuditor.Logger = logger
azureAuditor.UserAgent = UserAgent + gitTag
azureAuditor.SetConfigs(Opts.Config...)
azureAuditor.Run()
logger.Infof("Starting http server on %s", Opts.Server.Bind)
startHttpServer()
}
func initArgparser() {
argparser = flags.NewParser(&Opts, flags.Default)
_, err := argparser.Parse()
// check if there is a parse error
if err != nil {
var flagsErr *flags.Error
if ok := errors.As(err, &flagsErr); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
}
// start and handle prometheus handler
func startHttpServer() {
var err error
mux := http.NewServeMux()
if Opts.Server.PathReport == "/" {
Opts.Server.PathReport = ""
}
endpoints := map[string]string{
"healthz": "/healthz",
"readyz": "/readyz",
"metrics": "/metrics",
"frontend": Opts.Server.PathReport + "/",
"data": Opts.Server.PathReport + "/data",
"config": Opts.Server.PathReport + "/config",
}
// healthz
mux.HandleFunc(endpoints["healthz"], func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
// readyz
mux.HandleFunc(endpoints["readyz"], func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
// report
tmpl := template.New("report")
cssOptimizeRegexp := regexp.MustCompile(`\n[\s]*`)
tmpl, err = tmpl.Funcs(template.FuncMap{
"toYaml": func(obj interface{}) string {
out, _ := yaml.Marshal(obj)
return string(out)
},
"raw": func(val string) template.HTML {
return template.HTML(val) // #nosec G203 this template function is for returning unescaped html
},
"rawHtml": func(val string) template.HTML {
return template.HTML(val) // #nosec G203 this template function is for returning unescaped html
},
"rawCss": func(val string) template.CSS {
val = cssOptimizeRegexp.ReplaceAllString(val, " ")
return template.CSS(val) // #nosec G203 this template function is for returning unescaped html
},
"rawJs": func(val string) template.JS {
jsFilter := regexp.MustCompile(`(?m)^[\s]*(//.+$)?`)
if !Opts.Logger.Development {
val = jsFilter.ReplaceAllString(val, "")
val = jsFilter.ReplaceAllString(val, "")
val = strings.ReplaceAll(val, "{\n", "{")
val = strings.ReplaceAll(val, "}\n", "};")
val = strings.ReplaceAll(val, ";\n", ";")
val = strings.ReplaceAll(val, ",\n", ",")
}
return template.JS(val) // #nosec G203 this template function is for returning unescaped html
},
"reportTitle": func(val string) (reportTitle string) {
reportTitle = val
if pos := strings.Index(reportTitle, ":"); pos >= 0 {
reportTitle = strings.TrimPrefix(reportTitle, reportTitle[0:pos+1])
}
return
},
"humanizeReportCount": func(val int64) string {
return humanize.SIWithDigits(float64(val), 1, "")
},
"include": func(name string, data interface{}) string {
var buf strings.Builder
err := tmpl.ExecuteTemplate(&buf, name, data)
if err != nil {
logger.Panic(err.Error())
}
return buf.String()
},
"version": func() string {
return gitTag
},
}).Funcs(sprig.HtmlFuncMap()).ParseGlob("./templates/*")
if err != nil {
logger.Panic(err)
}
mux.HandleFunc(endpoints["frontend"], func(w http.ResponseWriter, r *http.Request) {
cspNonce := base64.StdEncoding.EncodeToString([]byte(uuid.New().String()))
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Referrer-Policy", "same-origin")
w.Header().Add("X-Frame-Options", "DENY")
w.Header().Add("X-XSS-Protection", "1; mode=block")
w.Header().Add("X-Content-Type-Options", "nosniff")
w.Header().Add("Content-Security-Policy",
fmt.Sprintf(
"default-src 'self'; script-src 'nonce-%[1]s'; style-src 'nonce-%[1]s' 'unsafe-inline'; img-src 'self' data:",
cspNonce,
),
)
selectedReport := r.URL.Query().Get("report")
templatePayload := struct {
Nonce string
Config auditor.AuditConfig
ReportTitle string
ReportConfig *validator.AuditConfigValidation
Reports map[string]*auditor.AzureAuditorReport
ServerPathReport string
RequestReport string
ReportPaginationSize int
}{
Nonce: cspNonce,
Config: azureAuditor.GetConfig(),
ReportTitle: Opts.Report.Title,
ReportConfig: nil,
Reports: azureAuditor.GetReport(),
ServerPathReport: Opts.Server.PathReport,
RequestReport: "",
ReportPaginationSize: Opts.Report.PaginationSize,
}
reportInfo := strings.SplitN(selectedReport, ":", 2)
switch reportInfo[0] {
case "RoleAssignment":
templatePayload.ReportConfig = templatePayload.Config.RoleAssignments
templatePayload.RequestReport = selectedReport
case "ResourceGroup":
templatePayload.ReportConfig = templatePayload.Config.ResourceGroups
templatePayload.RequestReport = selectedReport
case "ResourceProvider":
templatePayload.ReportConfig = templatePayload.Config.ResourceProviders
templatePayload.RequestReport = selectedReport
case "ResourceProviderFeature":
templatePayload.ReportConfig = templatePayload.Config.ResourceProviderFeatures
templatePayload.RequestReport = selectedReport
case "KeyvaultAccessPolicy":
templatePayload.ReportConfig = templatePayload.Config.KeyvaultAccessPolicies
templatePayload.RequestReport = selectedReport
case "ResourceGraph":
if len(reportInfo) == 2 && reportInfo[1] != "" {
if v, ok := templatePayload.Config.ResourceGraph.Queries[reportInfo[1]]; ok {
templatePayload.ReportConfig = v
templatePayload.RequestReport = selectedReport
}
}
case "LogAnalytics":
if len(reportInfo) == 2 && reportInfo[1] != "" {
if v, ok := templatePayload.Config.LogAnalytics.Queries[reportInfo[1]]; ok {
templatePayload.ReportConfig = v
templatePayload.RequestReport = selectedReport
}
}
}
if err := tmpl.ExecuteTemplate(w, "report.html", templatePayload); err != nil {
logger.Error(err)
}
})
mux.HandleFunc(endpoints["data"], func(w http.ResponseWriter, r *http.Request) {
var reportGroupBy *string
var reportFields *[]string
var reportStatus *types.RuleStatus
if val := r.URL.Query().Get("groupBy"); val != "" {
reportGroupBy = &val
}
if val := r.URL.Query().Get("fields"); val != "" && val != "*" {
fieldList := []string{}
for _, field := range strings.Split(val, ":") {
fieldList = append(fieldList, strings.TrimSpace(field))
}
reportFields = &fieldList
}
if val := r.URL.Query().Get("status"); val != "" {
valStatus := types.StringToRuleStatus(val)
reportStatus = &valStatus
}
if reportName := r.URL.Query().Get("report"); reportName != "" {
reportList := azureAuditor.GetReport()
if report, ok := reportList[reportName]; ok {
if report.UpdateTime != nil {
w.Header().Add("x-report-time", report.UpdateTime.Format(time.RFC1123Z))
} else {
w.Header().Add("x-report-time", "init")
}
reportData := []auditor.AzureAuditorReportLine{}
for _, row := range report.Lines {
line := auditor.AzureAuditorReportLine{} // nolint:ineffassign
line = *row
// filter: status
if reportStatus != nil {
if row.Status != (*reportStatus).String() {
continue
}
}
// group by
line.GroupBy = ""
if reportGroupBy != nil {
switch *reportGroupBy {
case "rule", "ruleid":
line.GroupBy = line.RuleID
case "status":
line.GroupBy = line.Status
default:
if val, ok := line.Resource[*reportGroupBy]; ok {
line.GroupBy = val
}
}
}
// report field filtering
if reportFields != nil {
resource := map[string]interface{}{}
for _, fieldName := range *reportFields {
if val, ok := line.Resource[fieldName]; ok {
resource[fieldName] = val
}
}
line.Resource = resource
}
reportData = append(reportData, line)
}
// unique filter
reportDataUnique := map[[20]byte]auditor.AzureAuditorReportLine{}
for _, line := range reportData {
hash := line.Hash()
if existingLine, exists := reportDataUnique[hash]; exists {
existingLine.Count++
reportDataUnique[hash] = existingLine
} else {
line.Count = 1
reportDataUnique[hash] = line
}
}
reportData = []auditor.AzureAuditorReportLine{}
for _, line := range reportDataUnique {
reportData = append(reportData, line)
}
// json encoding
data, err := json.Marshal(reportData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
/* #nosec G104 */
w.Write([]byte(err.Error())) // nolint:errcheck
}
w.Header().Add("Content-Type", "application/json")
/* #nosec G104 */
w.Write(data) // nolint:errcheck
}
} else {
w.WriteHeader(http.StatusNotFound)
}
})
// config
mux.HandleFunc(endpoints["config"], func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
content, err := yaml.Marshal(azureAuditor.GetConfig())
if err == nil {
if _, writeErr := w.Write(content); writeErr != nil {
logger.Error(writeErr)
}
} else {
w.WriteHeader(http.StatusInternalServerError)
if _, writeErr := w.Write([]byte("Unable to marshal configuration")); writeErr != nil {
logger.Error(writeErr)
}
logger.Error(err)
}
})
mux.Handle(endpoints["metrics"], http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
azureAuditor.MetricsLock().RLock()
defer azureAuditor.MetricsLock().RUnlock()
tracing.RegisterAzureMetricAutoClean(promhttp.Handler()).ServeHTTP(w, r)
},
))
srv := &http.Server{
Addr: Opts.Server.Bind,
Handler: mux,
ReadTimeout: Opts.Server.ReadTimeout,
WriteTimeout: Opts.Server.WriteTimeout,
}
go func() {
logger.Fatal(srv.ListenAndServe())
}()
// Setting up signal capturing
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
// Waiting for SIGINT (kill -2)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
logger.Fatal(srv.Shutdown(ctx))
}