- Notifications
You must be signed in to change notification settings - Fork928
feat: add etag to slim binaries endpoint#5750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -16,6 +16,7 @@ import ( | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"text/template" // html/template escapes some nonces | ||
"time" | ||
@@ -24,6 +25,7 @@ import ( | ||
"github.com/unrolled/secure" | ||
"golang.org/x/exp/slices" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/sync/singleflight" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/coderd/httpapi" | ||
@@ -48,7 +50,7 @@ func init() { | ||
} | ||
// Handler returns an HTTP handler for serving the static site. | ||
func Handler(siteFS fs.FS, binFS http.FileSystem, binHashes map[string]string) http.Handler { | ||
// html files are handled by a text/template. Non-html files | ||
// are served by the default file server. | ||
// | ||
@@ -59,13 +61,43 @@ func Handler(siteFS fs.FS, binFS http.FileSystem) http.Handler { | ||
panic(xerrors.Errorf("Failed to return handler for static files. Html files failed to load: %w", err)) | ||
} | ||
binHashCache := newBinHashCache(binFS, binHashes) | ||
mux := http.NewServeMux() | ||
mux.Handle("/bin/", http.StripPrefix("/bin", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
// Convert underscores in the filename to hyphens. We eventually want to | ||
// change our hyphen-based filenames to underscores, but we need to | ||
// support both for now. | ||
r.URL.Path = strings.ReplaceAll(r.URL.Path, "_", "-") | ||
// Set ETag header to the SHA1 hash of the file contents. | ||
name := filePath(r.URL.Path) | ||
if name == "" || name == "/" { | ||
// Serve the directory listing. | ||
http.FileServer(binFS).ServeHTTP(rw, r) | ||
return | ||
} | ||
if strings.Contains(name, "/") { | ||
// We only serve files from the root of this directory, so avoid any | ||
// shenanigans by blocking slashes in the URL path. | ||
http.NotFound(rw, r) | ||
return | ||
} | ||
deansheather marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
hash, err := binHashCache.getHash(name) | ||
if xerrors.Is(err, os.ErrNotExist) { | ||
http.NotFound(rw, r) | ||
return | ||
} | ||
if err != nil { | ||
http.Error(rw, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
// ETag header needs to be quoted. | ||
rw.Header().Set("ETag", fmt.Sprintf(`%q`, hash)) | ||
// http.FileServer will see the ETag header and automatically handle | ||
// If-Match and If-None-Match headers on the request properly. | ||
http.FileServer(binFS).ServeHTTP(rw, r) | ||
}))) | ||
mux.Handle("/", http.FileServer(http.FS(siteFS))) // All other non-html static files. | ||
@@ -409,20 +441,23 @@ func htmlFiles(files fs.FS) (*htmlTemplates, error) { | ||
}, nil | ||
} | ||
// ExtractOrReadBinFS checks the provided fs for compressed coder binaries and | ||
// extracts them into dest/bin if found. As a fallback, the provided FS is | ||
// checked for a /bin directory, if it is non-empty it is returned. Finally | ||
// dest/bin is returned as a fallback allowing binaries to be manually placed in | ||
// dest (usually ${CODER_CACHE_DIRECTORY}/site/bin). | ||
// | ||
// Returns a http.FileSystem that serves unpacked binaries, and a map of binary | ||
// name to SHA1 hash. The returned hash map may be incomplete or contain hashes | ||
// for missing files. | ||
func ExtractOrReadBinFS(dest string, siteFS fs.FS) (http.FileSystem, map[string]string, error) { | ||
if dest == "" { | ||
// No destination on fs, embedded fs is the only option. | ||
binFS, err := fs.Sub(siteFS, "bin") | ||
if err != nil { | ||
return nil,nil,xerrors.Errorf("cache path is empty and embedded fs does not have /bin: %w", err) | ||
} | ||
return http.FS(binFS), nil, nil | ||
} | ||
dest = filepath.Join(dest, "bin") | ||
@@ -440,51 +475,63 @@ func ExtractOrReadBinFS(dest string, siteFS fs.FS) (http.FileSystem, error) { | ||
files, err := fs.ReadDir(siteFS, "bin") | ||
if err != nil { | ||
if xerrors.Is(err, fs.ErrNotExist) { | ||
// Given fs does not have a bin directory, serve from cache | ||
// directory without extracting anything. | ||
binFS, err := mkdest() | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("mkdest failed: %w", err) | ||
} | ||
return binFS, map[string]string{}, nil | ||
} | ||
return nil,nil,xerrors.Errorf("site fs read dir failed: %w", err) | ||
} | ||
if len(filterFiles(files, "GITKEEP")) > 0 { | ||
// If there are other files than bin/GITKEEP, serve the files. | ||
binFS, err := fs.Sub(siteFS, "bin") | ||
if err != nil { | ||
return nil,nil,xerrors.Errorf("site fs sub dir failed: %w", err) | ||
} | ||
return http.FS(binFS), nil, nil | ||
} | ||
// Nothing we can do, serve the cache directory, thus allowing | ||
// binaries to be placed there. | ||
binFS, err := mkdest() | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("mkdest failed: %w", err) | ||
} | ||
return binFS, map[string]string{}, nil | ||
} | ||
return nil,nil,xerrors.Errorf("open coder binary archive failed: %w", err) | ||
} | ||
defer archive.Close() | ||
binFS, err := mkdest() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
shaFiles, err := parseSHA1(siteFS) | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("parse sha1 file failed: %w", err) | ||
} | ||
ok, err := verifyBinSha1IsCurrent(dest, siteFS, shaFiles) | ||
if err != nil { | ||
return nil,nil,xerrors.Errorf("verify coder binaries sha1 failed: %w", err) | ||
} | ||
if !ok { | ||
n, err := extractBin(dest, archive) | ||
if err != nil { | ||
return nil,nil,xerrors.Errorf("extract coder binaries failed: %w", err) | ||
} | ||
if n == 0 { | ||
return nil,nil,xerrors.New("no files were extracted from coder binaries archive") | ||
} | ||
} | ||
returnbinFS, shaFiles, nil | ||
} | ||
func filterFiles(files []fs.DirEntry, names ...string) []fs.DirEntry { | ||
@@ -501,24 +548,32 @@ func filterFiles(files []fs.DirEntry, names ...string) []fs.DirEntry { | ||
// errHashMismatch is a sentinel error used in verifyBinSha1IsCurrent. | ||
var errHashMismatch = xerrors.New("hash mismatch") | ||
funcparseSHA1(siteFS fs.FS) (map[string]string, error) { | ||
b, err := fs.ReadFile(siteFS, "bin/coder.sha1") | ||
if err != nil { | ||
returnnil, xerrors.Errorf("read coder sha1 from embedded fs failed: %w", err) | ||
} | ||
shaFiles := make(map[string]string) | ||
for _, line := range bytes.Split(bytes.TrimSpace(b), []byte{'\n'}) { | ||
parts := bytes.Split(line, []byte{' ', '*'}) | ||
if len(parts) != 2 { | ||
returnnil, xerrors.Errorf("malformed sha1 file: %w", err) | ||
} | ||
shaFiles[string(parts[1])] =strings.ToLower(string(parts[0])) | ||
} | ||
if len(shaFiles) == 0 { | ||
returnnil, xerrors.Errorf("empty sha1 file: %w", err) | ||
} | ||
return shaFiles, nil | ||
} | ||
func verifyBinSha1IsCurrent(dest string, siteFS fs.FS, shaFiles map[string]string) (ok bool, err error) { | ||
b1, err := fs.ReadFile(siteFS, "bin/coder.sha1") | ||
if err != nil { | ||
return false, xerrors.Errorf("read coder sha1 from embedded fs failed: %w", err) | ||
} | ||
b2, err := os.ReadFile(filepath.Join(dest, "coder.sha1")) | ||
if err != nil { | ||
if xerrors.Is(err, fs.ErrNotExist) { | ||
@@ -551,7 +606,7 @@ func verifyBinSha1IsCurrent(dest string, siteFS fs.FS) (ok bool, err error) { | ||
} | ||
return xerrors.Errorf("hash file failed: %w", err) | ||
} | ||
if !strings.EqualFold(hash1, hash2) { | ||
return errHashMismatch | ||
} | ||
return nil | ||
@@ -570,24 +625,24 @@ func verifyBinSha1IsCurrent(dest string, siteFS fs.FS) (ok bool, err error) { | ||
// sha1HashFile computes a SHA1 hash of the file, returning the hex | ||
// representation. | ||
func sha1HashFile(name string) (string, error) { | ||
//#nosec // Not used for cryptography. | ||
hash := sha1.New() | ||
f, err := os.Open(name) | ||
if err != nil { | ||
return"", err | ||
} | ||
defer f.Close() | ||
_, err = io.Copy(hash, f) | ||
if err != nil { | ||
return"", err | ||
} | ||
b := make([]byte, hash.Size()) | ||
hash.Sum(b[:0]) | ||
return hex.EncodeToString(b), nil | ||
} | ||
func extractBin(dest string, r io.Reader) (numExtracted int, err error) { | ||
@@ -672,3 +727,67 @@ func RenderStaticErrorPage(rw http.ResponseWriter, r *http.Request, data ErrorPa | ||
return | ||
} | ||
} | ||
type binHashCache struct { | ||
binFS http.FileSystem | ||
hashes map[string]string | ||
mut sync.RWMutex | ||
sf singleflight.Group | ||
sem chan struct{} | ||
} | ||
func newBinHashCache(binFS http.FileSystem, binHashes map[string]string) *binHashCache { | ||
b := &binHashCache{ | ||
binFS: binFS, | ||
hashes: make(map[string]string, len(binHashes)), | ||
mut: sync.RWMutex{}, | ||
sf: singleflight.Group{}, | ||
sem: make(chan struct{}, 4), | ||
} | ||
// Make a copy since we're gonna be mutating it. | ||
for k, v := range binHashes { | ||
b.hashes[k] = v | ||
} | ||
return b | ||
} | ||
func (b *binHashCache) getHash(name string) (string, error) { | ||
b.mut.RLock() | ||
hash, ok := b.hashes[name] | ||
b.mut.RUnlock() | ||
if ok { | ||
return hash, nil | ||
} | ||
// Avoid DOS by using a pool, and only doing work once per file. | ||
v, err, _ := b.sf.Do(name, func() (interface{}, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Did I understand the logic correctly that the purpose of this is to be able to compute hashes of served binaries that were not in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yep, basically. This also serves the case where we're serving directly from the site FS because the binaries weren't compressed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Ok, perfect. Thanks for confirming! | ||
b.sem <- struct{}{} | ||
defer func() { <-b.sem }() | ||
f, err := b.binFS.Open(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
h := sha1.New() //#nosec // Not used for cryptography. | ||
_, err = io.Copy(h, f) | ||
if err != nil { | ||
return "", err | ||
} | ||
hash := hex.EncodeToString(h.Sum(nil)) | ||
b.mut.Lock() | ||
b.hashes[name] = hash | ||
b.mut.Unlock() | ||
return hash, nil | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
//nolint:forcetypeassert | ||
return strings.ToLower(v.(string)), nil | ||
} |
Uh oh!
There was an error while loading.Please reload this page.