mirror of
https://github.com/genuinetools/reg.git
synced 2024-06-30 11:04:16 -04:00
cleanup
Signed-off-by: Jess Frazelle <acidburn@google.com>
This commit is contained in:
parent
0c96eab540
commit
f504f1306f
|
@ -29,6 +29,7 @@ const (
|
||||||
var (
|
var (
|
||||||
updating = false
|
updating = false
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
tmpl *template.Template
|
||||||
)
|
)
|
||||||
|
|
||||||
// preload initializes any global options and configuration
|
// preload initializes any global options and configuration
|
||||||
|
@ -92,25 +93,44 @@ func main() {
|
||||||
app.Action = func(c *cli.Context) error {
|
app.Action = func(c *cli.Context) error {
|
||||||
auth, err := utils.GetAuthConfig(c)
|
auth, err := utils.GetAuthConfig(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create the registry client
|
// create the registry client
|
||||||
r, err := registry.New(auth, c.GlobalBool("debug"))
|
r, err := registry.New(auth, c.GlobalBool("debug"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the path to the static directory
|
// get the path to the static directory
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
staticDir := filepath.Join(wd, "static")
|
staticDir := filepath.Join(wd, "static")
|
||||||
|
|
||||||
|
// create the template
|
||||||
|
templateDir := filepath.Join(staticDir, "../templates")
|
||||||
|
funcMap := template.FuncMap{
|
||||||
|
"trim": func(s string) string {
|
||||||
|
a := []rune(s)
|
||||||
|
var b []rune
|
||||||
|
for i, r := range a {
|
||||||
|
// add new line every 80 chars
|
||||||
|
b = append(b, r)
|
||||||
|
if i > 0 && (i+1)%80 == 0 {
|
||||||
|
b = append(b, '\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tmpl = template.Must(template.New("").Funcs(funcMap).ParseFiles(filepath.Join(templateDir, "vulns.txt"), filepath.Join(templateDir, "layout.html")))
|
||||||
|
|
||||||
// create the initial index
|
// create the initial index
|
||||||
|
logrus.Info("creating initial static index")
|
||||||
if err := createStaticIndex(r, staticDir, c.GlobalString("clair")); err != nil {
|
if err := createStaticIndex(r, staticDir, c.GlobalString("clair")); err != nil {
|
||||||
return cli.NewExitError(fmt.Sprintf("Error creating index: %s", err.Error()), 1)
|
logrus.Fatalf("Error creating index: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse the duration
|
// parse the duration
|
||||||
|
@ -124,6 +144,7 @@ func main() {
|
||||||
// create more indexes every X minutes based off interval
|
// create more indexes every X minutes based off interval
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
if !updating {
|
if !updating {
|
||||||
|
logrus.Info("creating timer based static index")
|
||||||
if err := createStaticIndex(r, staticDir, c.GlobalString("clair")); err != nil {
|
if err := createStaticIndex(r, staticDir, c.GlobalString("clair")); err != nil {
|
||||||
logrus.Warnf("creating static index failed: %v", err)
|
logrus.Warnf("creating static index failed: %v", err)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
@ -131,6 +152,8 @@ func main() {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
logrus.Info("finished waiting for vulns wait group")
|
logrus.Info("finished waiting for vulns wait group")
|
||||||
|
} else {
|
||||||
|
logrus.Warnf("skipping timer based static index update for %s", c.String("interval"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -255,7 +278,7 @@ func createStaticIndex(r *registry.Registry, staticDir, clairURI string) error {
|
||||||
LastUpdated: time.Now().Local().Format(time.RFC1123),
|
LastUpdated: time.Now().Local().Format(time.RFC1123),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := renderTemplate(staticDir, "layout.html", "index.html", d); err != nil {
|
if err := renderTemplate(staticDir, "index", "index.html", d); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
updating = false
|
updating = false
|
||||||
|
@ -273,8 +296,10 @@ type vulnsReport struct {
|
||||||
|
|
||||||
func createVulnStaticPage(r *registry.Registry, staticDir, clairURI, repo, tag string, m schema1.SignedManifest) error {
|
func createVulnStaticPage(r *registry.Registry, staticDir, clairURI, repo, tag string, m schema1.SignedManifest) error {
|
||||||
report := vulnsReport{
|
report := vulnsReport{
|
||||||
Repo: repo,
|
Repo: repo,
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
|
Date: time.Now().Local().Format(time.RFC1123),
|
||||||
|
VulnsBySeverity: make(map[string][]clair.Vulnerability),
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter out the empty layers
|
// filter out the empty layers
|
||||||
|
@ -337,33 +362,30 @@ func createVulnStaticPage(r *registry.Registry, staticDir, clairURI, repo, tag s
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Join(repo, tag, "vulns.txt")
|
path := filepath.Join(repo, tag, "vulns.txt")
|
||||||
if err := renderTemplate(staticDir, "vulns.txt", path, report); err != nil {
|
if err := renderTemplate(staticDir, "vulns", path, report); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTemplate(staticDir, src, dest string, data interface{}) error {
|
func renderTemplate(staticDir, templateName, dest string, data interface{}) error {
|
||||||
// parse & execute the template
|
// parse & execute the template
|
||||||
logrus.Info("parsing and executing the template %s", src)
|
logrus.Infof("executing the template %s", templateName)
|
||||||
templateDir := filepath.Join(staticDir, "../templates")
|
|
||||||
lp := filepath.Join(templateDir, src)
|
|
||||||
|
|
||||||
path := filepath.Join(staticDir, dest)
|
path := filepath.Join(staticDir, dest)
|
||||||
if err := os.MkdirAll(filepath.Dir(path), 0644); err != nil {
|
if err := os.MkdirAll(filepath.Dir(path), 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logrus.Info("creating/opening file %s", path)
|
logrus.Debugf("creating/opening file %s", path)
|
||||||
f, err := os.Create(path)
|
f, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").ParseFiles(lp))
|
if err := tmpl.ExecuteTemplate(f, templateName, data); err != nil {
|
||||||
if err := tmpl.ExecuteTemplate(f, "layout", data); err != nil {
|
|
||||||
f.Close()
|
f.Close()
|
||||||
return fmt.Errorf("execute template %s failed: %v", src, err)
|
return fmt.Errorf("execute template %s failed: %v", templateName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
{{define "layout"}}
|
{{define "index"}}
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||||||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
CVE Report for {{.Repo}}:{{.Tag}}
|
{{define "vulns"}}CVE Report for {{.Repo}}:{{.Tag}}
|
||||||
Generated on: {{.Date}}
|
Generated on: {{.Date}}
|
||||||
|
|
||||||
Vulnerabilities Found: {{len .Vulns}}
|
Vulnerabilities Found: {{len .Vulns}}
|
||||||
|
@ -8,13 +8,12 @@ Vulnerabilities Found: {{len .Vulns}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{if gt .BadVulns 10}}
|
{{if gt .BadVulns 10}}
|
||||||
----------------- ALERT -----------------
|
------------------------------------ ALERT ------------------------------------
|
||||||
{{.BadVulns}} High, Critical, and/or Defcon1 vulnerabilities found
|
{{.BadVulns}} High, Critical, and/or Defcon1 vulnerabilities found
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{range $key, $value := .VulnsBySeverity}}
|
{{range $vulns := .VulnsBySeverity}}{{range $value := $vulns}}{{$value.Name}}: [{{$value.Severity}}]
|
||||||
{{$value.Name}}: [{{$value.Severity}}]
|
{{trim $value.Description}}
|
||||||
{{$value.Description}}
|
|
||||||
{{$value.Link}}
|
{{$value.Link}}
|
||||||
-----------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
{{end}}
|
{{end}}{{end}}{{end}}
|
||||||
|
|
Loading…
Reference in a new issue