From 9ca1d0a10e7414ea22c23b7b77153a6028c51a8a Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Wed, 17 Aug 2022 13:08:46 +0200 Subject: [PATCH] use go:embed --- cli/serve/serve.go | 48 ++++++++++++++++------------------------- cli/serve/serve_test.go | 15 ------------- go.mod | 2 +- 3 files changed, 19 insertions(+), 46 deletions(-) diff --git a/cli/serve/serve.go b/cli/serve/serve.go index 716751904..909789398 100644 --- a/cli/serve/serve.go +++ b/cli/serve/serve.go @@ -3,8 +3,10 @@ package serve import ( "crypto/tls" + "embed" "errors" "fmt" + "io/fs" "net" "net/http" "net/url" @@ -13,7 +15,6 @@ import ( "strconv" "strings" - rice "github.com/GeertJohan/go.rice" "github.com/cloudflare/cfssl/api" "github.com/cloudflare/cfssl/api/bundle" "github.com/cloudflare/cfssl/api/certadd" @@ -81,38 +82,28 @@ func v1APIPath(path string) string { return (&url.URL{Path: path}).String() } -// httpBox implements http.FileSystem which allows the use of Box with a http.FileServer. -// Attempting to Open an API endpoint will result in an error. -type httpBox struct { - *rice.Box - redirects map[string]string +//go:embed static +var staticContent embed.FS + +var staticRedirections = map[string]string{ + "bundle": "index.html", + "scan": "index.html", + "packages": "index.html", } -func (hb *httpBox) findStaticBox() (err error) { - hb.Box, err = rice.FindBox("static") - return +type staticFS struct { + fs fs.FS + redirections map[string]string } -// Open returns a File for non-API enpoints using the http.File interface. -func (hb *httpBox) Open(name string) (http.File, error) { +func (s *staticFS) Open(name string) (fs.File, error) { if strings.HasPrefix(name, V1APIPrefix) { return nil, os.ErrNotExist } - - if location, ok := hb.redirects[name]; ok { - return hb.Box.Open(location) + if location, ok := s.redirections[name]; ok { + return s.fs.Open(location) } - - return hb.Box.Open(name) -} - -// staticBox is the box containing all static assets. -var staticBox = &httpBox{ - redirects: map[string]string{ - "/scan": "/index.html", - "/bundle": "/index.html", - "/packages": "/index.html", - }, + return s.fs.Open(name) } var errBadSigner = errors.New("signer not initialized") @@ -242,11 +233,8 @@ var endpoints = map[string]func() (http.Handler, error){ }, "/": func() (http.Handler, error) { - if err := staticBox.findStaticBox(); err != nil { - return nil, err - } - - return http.FileServer(staticBox), nil + subFS, _ := fs.Sub(staticContent, "static") + return http.FileServer(http.FS(&staticFS{fs: subFS, redirections: staticRedirections})), nil }, "health": func() (http.Handler, error) { diff --git a/cli/serve/serve_test.go b/cli/serve/serve_test.go index bdaeca9bc..356d69f06 100644 --- a/cli/serve/serve_test.go +++ b/cli/serve/serve_test.go @@ -3,7 +3,6 @@ package serve import ( "net/http" "net/http/httptest" - "os" "testing" "github.com/cloudflare/cfssl/cli" @@ -18,20 +17,6 @@ func TestServe(t *testing.T) { expected[v1APIPath(endpoint)] = http.StatusOK } - err := staticBox.Walk("", func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if !info.IsDir() { - expected["/"+path] = http.StatusOK - } - return nil - }) - if err != nil { - t.Error(err) - } - // Disabled endpoints should return '404 Not Found' expected[v1APIPath("sign")] = http.StatusNotFound expected[v1APIPath("authsign")] = http.StatusNotFound diff --git a/go.mod b/go.mod index 2c7c6f736..64f238ecb 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/cloudflare/cfssl -go 1.14 +go 1.16 require ( bitbucket.org/liamstask/goose v0.0.0-20150115234039-8488cc47d90c