fix an error produced by analyze-local-images remotely

This commit is contained in:
ruokai.lai 2017-03-07 11:52:02 +11:00
parent 84f74fa1ee
commit 28e04a653f

View File

@ -23,7 +23,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -173,7 +172,7 @@ func AnalyzeLocalImage(imageName string, minSeverity types.Priority, endpoint, m
log.Printf("Setting up HTTP server (allowing: %s)\n", allowedHost) log.Printf("Setting up HTTP server (allowing: %s)\n", allowedHost)
ch := make(chan error) ch := make(chan error)
go listenHTTP(tmpPath, allowedHost, ch) go listenHTTP(tmpPath, ch)
select { select {
case err := <-ch: case err := <-ch:
return fmt.Errorf("An error occured when starting HTTP server: %s", err) return fmt.Errorf("An error occured when starting HTTP server: %s", err)
@ -362,20 +361,14 @@ func historyFromCommand(imageName string) ([]string, error) {
return layers, nil return layers, nil
} }
func listenHTTP(path, allowedHost string, ch chan error) { func listenHTTP(path string, ch chan error) {
restrictedFileServer := func(path, allowedHost string) http.Handler { restrictedFileServer := func(path string) http.Handler {
fc := func(w http.ResponseWriter, r *http.Request) { fc := func(w http.ResponseWriter, r *http.Request) {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil && strings.EqualFold(host, allowedHost) {
http.FileServer(http.Dir(path)).ServeHTTP(w, r) http.FileServer(http.Dir(path)).ServeHTTP(w, r)
return
}
w.WriteHeader(403)
} }
return http.HandlerFunc(fc) return http.HandlerFunc(fc)
} }
ch <- http.ListenAndServe(":"+strconv.Itoa(httpPort), restrictedFileServer(path))
ch <- http.ListenAndServe(":"+strconv.Itoa(httpPort), restrictedFileServer(path, allowedHost))
} }
func analyzeLayer(endpoint, path, layerName, parentLayerName string) error { func analyzeLayer(endpoint, path, layerName, parentLayerName string) error {