do not show vulns on tags page if there is no clair registry

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-07-16 05:26:50 -04:00
parent f9d8dca27d
commit 3e014d1732
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
3 changed files with 38 additions and 23 deletions

View file

@ -47,6 +47,7 @@ type AnalysisResult struct {
RegistryDomain string `json:"registryDomain"`
Name string `json:"name"`
LastUpdated string `json:"lastUpdated"`
HasVulns bool `json:"hasVulns"`
}
func (rc *registryController) repositories(staticDir string, generateTagsFiles bool) error {
@ -65,6 +66,7 @@ func (rc *registryController) repositories(staticDir string, generateTagsFiles b
return fmt.Errorf("getting catalog for %s failed: %v", rc.reg.Domain, err)
}
var wg sync.WaitGroup
for _, repo := range repoList {
repoURI := fmt.Sprintf("%s/%s", rc.reg.Domain, repo)
r := Repository{
@ -74,28 +76,36 @@ func (rc *registryController) repositories(staticDir string, generateTagsFiles b
result.Repositories = append(result.Repositories, r)
if generateTagsFiles {
// TODO(jessfraz): make this a go routine with a wait group.
if !generateTagsFiles {
// Continue early because we don't need to generate the tags pages.
continue
}
// Generate the tags pages in a go routine.
wg.Add(1)
go func(repo string) {
defer wg.Done()
logrus.Infof("generating static tags page for repo %s", repo)
// Parse and execute the tags templates.
b, err := rc.generateTagsTemplate(repo)
if err != nil {
logrus.Warnf("generating tags tamplate for repo %q failed: %v", repo, err)
logrus.Warnf("generating tags template for repo %q failed: %v", repo, err)
}
// Create the directory for the static tags files.
tagsDir := filepath.Join(staticDir, "repo", repo, "tags")
if err := os.MkdirAll(tagsDir, 0755); err != nil {
return err
logrus.Warn(err)
}
// Write the tags file.
tagsFile := filepath.Join(tagsDir, "index.html")
if err := ioutil.WriteFile(tagsFile, b, 0755); err != nil {
logrus.Warnf("writing tags tamplate for repo %s to %sfailed: %v", repo, tagsFile, err)
logrus.Warnf("writing tags template for repo %s to %sfailed: %v", repo, tagsFile, err)
}
}
}(repo)
}
wg.Wait()
// Parse & execute the template.
logrus.Info("executing the template repositories")
@ -174,6 +184,7 @@ func (rc *registryController) generateTagsTemplate(repo string) ([]byte, error)
RegistryDomain: rc.reg.Domain,
LastUpdated: time.Now().Local().Format(time.RFC1123),
Name: repo,
HasVulns: rc.cl != nil, // if we have a clair client we can return vulns
}
for _, tag := range tags {
@ -242,19 +253,15 @@ func (rc *registryController) vulnerabilitiesHandler(w http.ResponseWriter, r *h
return
}
result := clair.VulnerabilityReport{}
if rc.cl != nil {
result, err = rc.cl.Vulnerabilities(rc.reg, repo, tag)
if err != nil {
logrus.WithFields(logrus.Fields{
"func": "vulnerabilities",
"URL": r.URL,
"method": r.Method,
}).Errorf("vulnerability scanning for %s:%s failed: %v", repo, tag, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
result, err := rc.cl.Vulnerabilities(rc.reg, repo, tag)
if err != nil {
logrus.WithFields(logrus.Fields{
"func": "vulnerabilities",
"URL": r.URL,
"method": r.Method,
}).Errorf("vulnerability scanning for %s:%s failed: %v", repo, tag, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if strings.HasSuffix(r.URL.String(), ".json") {

View file

@ -156,9 +156,13 @@ func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
mux.HandleFunc("/repo/{repo}/tags/", rc.tagsHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns/", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns.json", rc.vulnerabilitiesHandler)
// Add the vulns endpoints if we have a client for a clair server.
if rc.cl != nil {
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns/", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns.json", rc.vulnerabilitiesHandler)
}
// Serve the static assets.
staticHandler := http.FileServer(http.Dir(staticDir))

View file

@ -20,7 +20,7 @@
<th>Name</th>
<th>Tag</th>
<th>Created</th>
<th>Vulnerabilities</th>
{{if .HasVulns}}<th>Vulnerabilities</th>{{end}}
</tr>
{{ range $key, $value := .Repositories }}
<tr>
@ -37,11 +37,13 @@
<td align="right" nowrap>
{{ $value.Created.Format "02 Jan, 2006 15:04:05 UTC" }}
</td>
{{if .HasVulns}}
<td align="right" nowrap>
<a href="/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns" id="{{ $value.Name }}:{{ $value.Tag }}">
<div class="signal"></div>
</a>
</td>
{{end}}
</tr>
{{ end }}
</table>
@ -52,6 +54,7 @@
<p>Last Updated: {{ .LastUpdated }}</p>
</div><!--/.footer-->
<script src="/static/js/scripts.js"></script>
{{if .HasVulns}}
<script type="text/javascript">
var ajaxCalls = [
{{ range $key, $value := .Repositories }}
@ -64,6 +67,7 @@
});
};
</script>
{{end}}
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),