1
0
Fork 0

Replace go-bindata with Go embed

Co-authored-by: nrwiersma <nick@wiersma.co.za>
This commit is contained in:
Antoine 2021-09-15 10:36:14 +02:00 committed by GitHub
parent 7ff13c3e3e
commit 70359e5d27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 142 additions and 205 deletions

View file

@ -1,59 +0,0 @@
package api
import (
"net/http"
"net/url"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/mux"
"github.com/traefik/traefik/v2/pkg/log"
)
// DashboardHandler expose dashboard routes.
type DashboardHandler struct {
Assets *assetfs.AssetFS
}
// Append add dashboard routes on a router.
func (g DashboardHandler) Append(router *mux.Router) {
if g.Assets == nil {
log.WithoutContext().Error("No assets for dashboard")
return
}
// Expose dashboard
router.Methods(http.MethodGet).
Path("/").
HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
http.Redirect(resp, req, safePrefix(req)+"/dashboard/", http.StatusFound)
})
router.Methods(http.MethodGet).
PathPrefix("/dashboard/").
Handler(http.StripPrefix("/dashboard/", http.FileServer(g.Assets)))
}
func (g DashboardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// allow iframes from our domains only
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
http.FileServer(g.Assets).ServeHTTP(w, r)
}
func safePrefix(req *http.Request) string {
prefix := req.Header.Get("X-Forwarded-Prefix")
if prefix == "" {
return ""
}
parse, err := url.Parse(prefix)
if err != nil {
return ""
}
if parse.Host != "" {
return ""
}
return parse.Path
}

View file

@ -0,0 +1,68 @@
package dashboard
import (
"io/fs"
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/traefik/traefik/v2/webui"
)
// Handler expose dashboard routes.
type Handler struct {
assets fs.FS // optional assets, to override the webui.FS default
}
// Append adds dashboard routes on the given router, optionally using the given
// assets (or webui.FS otherwise).
func Append(router *mux.Router, customAssets fs.FS) {
assets := customAssets
if assets == nil {
assets = webui.FS
}
// Expose dashboard
router.Methods(http.MethodGet).
Path("/").
HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
http.Redirect(resp, req, safePrefix(req)+"/dashboard/", http.StatusFound)
})
router.Methods(http.MethodGet).
PathPrefix("/dashboard/").
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// allow iframes from our domains only
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
http.StripPrefix("/dashboard/", http.FileServer(http.FS(assets))).ServeHTTP(w, r)
})
}
func (g Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
assets := g.assets
if assets == nil {
assets = webui.FS
}
// allow iframes from our domains only
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
http.FileServer(http.FS(assets)).ServeHTTP(w, r)
}
func safePrefix(req *http.Request) string {
prefix := req.Header.Get("X-Forwarded-Prefix")
if prefix == "" {
return ""
}
parse, err := url.Parse(prefix)
if err != nil {
return ""
}
if parse.Host != "" {
return ""
}
return parse.Path
}

View file

@ -1,12 +1,14 @@
package api
package dashboard
import (
"fmt"
"errors"
"io/fs"
"net/http"
"net/http/httptest"
"testing"
"testing/fstest"
"time"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -59,48 +61,30 @@ func Test_safePrefix(t *testing.T) {
func Test_ContentSecurityPolicy(t *testing.T) {
testCases := []struct {
desc string
handler DashboardHandler
handler Handler
expected int
}{
{
desc: "OK",
handler: DashboardHandler{
Assets: &assetfs.AssetFS{
Asset: func(path string) ([]byte, error) {
return []byte{}, nil
},
AssetDir: func(path string) ([]string, error) {
return []string{}, nil
},
},
handler: Handler{
assets: fstest.MapFS{"foobar.html": &fstest.MapFile{
Mode: 0755,
ModTime: time.Now(),
}},
},
expected: http.StatusOK,
},
{
desc: "Not found",
handler: DashboardHandler{
Assets: &assetfs.AssetFS{
Asset: func(path string) ([]byte, error) {
return []byte{}, fmt.Errorf("not found")
},
AssetDir: func(path string) ([]string, error) {
return []string{}, fmt.Errorf("not found")
},
},
handler: Handler{
assets: fstest.MapFS{},
},
expected: http.StatusNotFound,
},
{
desc: "Internal server error",
handler: DashboardHandler{
Assets: &assetfs.AssetFS{
Asset: func(path string) ([]byte, error) {
return []byte{}, fmt.Errorf("oops")
},
AssetDir: func(path string) ([]string, error) {
return []string{}, fmt.Errorf("oops")
},
},
handler: Handler{
assets: errorFS{},
},
expected: http.StatusInternalServerError,
},
@ -122,3 +106,9 @@ func Test_ContentSecurityPolicy(t *testing.T) {
})
}
}
type errorFS struct{}
func (e errorFS) Open(name string) (fs.File, error) {
return nil, errors.New("oops")
}

View file

@ -6,7 +6,6 @@ import (
"reflect"
"strings"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/mux"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/config/runtime"
@ -48,10 +47,7 @@ type RunTimeRepresentation struct {
// Handler serves the configuration and status of Traefik on API endpoints.
type Handler struct {
dashboard bool
debug bool
staticConfig static.Configuration
dashboardAssets *assetfs.AssetFS
staticConfig static.Configuration
// runtimeConfiguration is the data set used to create all the data representations exposed by the API.
runtimeConfiguration *runtime.Configuration
@ -73,11 +69,8 @@ func New(staticConfig static.Configuration, runtimeConfig *runtime.Configuration
}
return &Handler{
dashboard: staticConfig.API.Dashboard,
dashboardAssets: staticConfig.API.DashboardAssets,
runtimeConfiguration: rConfig,
staticConfig: staticConfig,
debug: staticConfig.API.Debug,
}
}
@ -85,7 +78,7 @@ func New(staticConfig static.Configuration, runtimeConfig *runtime.Configuration
func (h Handler) createRouter() *mux.Router {
router := mux.NewRouter()
if h.debug {
if h.staticConfig.API.Debug {
DebugHandler{}.Append(router)
}
@ -118,10 +111,6 @@ func (h Handler) createRouter() *mux.Router {
version.Handler{}.Append(router)
if h.dashboard {
DashboardHandler{Assets: h.dashboardAssets}.Append(router)
}
return router
}