2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
package uiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-31 19:33:56 +00:00
|
|
|
"embed"
|
2020-10-09 13:32:39 +00:00
|
|
|
"encoding/json"
|
2020-09-23 11:37:33 +00:00
|
|
|
"fmt"
|
2022-05-31 19:33:56 +00:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2020-09-23 11:37:33 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-10-08 17:03:13 +00:00
|
|
|
"path"
|
2020-09-23 11:37:33 +00:00
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"text/template"
|
|
|
|
|
2021-03-24 18:43:42 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
)
|
|
|
|
|
2020-10-08 17:03:13 +00:00
|
|
|
const (
|
|
|
|
compiledProviderJSPath = "assets/compiled-metrics-providers.js"
|
|
|
|
)
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
//go:embed dist
|
|
|
|
var dist embed.FS
|
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
// Handler is the http.Handler that serves the Consul UI. It may serve from the
|
2022-05-31 19:33:56 +00:00
|
|
|
// embedded fs.FS or from an external directory. It provides a few important
|
2020-09-23 11:37:33 +00:00
|
|
|
// transformations on the index.html file and includes a proxy for metrics
|
|
|
|
// backends.
|
|
|
|
type Handler struct {
|
2021-03-24 18:43:42 +00:00
|
|
|
// runtimeConfig is a struct accessed through an atomic value to make
|
2020-09-23 11:37:33 +00:00
|
|
|
// it safe to reload at run time. Each call to ServeHTTP will see the latest
|
|
|
|
// version of the state without internal locking needed.
|
2021-03-24 18:43:42 +00:00
|
|
|
runtimeConfig atomic.Value
|
|
|
|
logger hclog.Logger
|
|
|
|
transform UIDataTransform
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 13:32:39 +00:00
|
|
|
// UIDataTransform is an optional dependency that allows the agent to add
|
|
|
|
// additional data into the UI index as needed. For example we use this to
|
|
|
|
// inject enterprise-only feature flags into the template without making this
|
|
|
|
// package inherently dependent on Enterprise-only code.
|
|
|
|
//
|
|
|
|
// It is passed the current RuntimeConfig being applied and a map containing the
|
|
|
|
// current data that will be passed to the template. It should be modified
|
|
|
|
// directly to inject additional context.
|
2021-03-24 18:43:42 +00:00
|
|
|
type UIDataTransform func(data map[string]interface{}) error
|
2020-10-09 13:32:39 +00:00
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
// NewHandler returns a Handler that can be used to serve UI http requests. It
|
|
|
|
// accepts a full agent config since properties like ACLs being enabled affect
|
|
|
|
// the UI so we need more than just UIConfig parts.
|
2021-03-24 18:43:42 +00:00
|
|
|
func NewHandler(runtimeCfg *config.RuntimeConfig, logger hclog.Logger, transform UIDataTransform) *Handler {
|
2020-09-23 11:37:33 +00:00
|
|
|
h := &Handler{
|
2020-10-09 13:32:39 +00:00
|
|
|
logger: logger.Named(logging.UIServer),
|
|
|
|
transform: transform,
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
2021-03-24 18:43:42 +00:00
|
|
|
h.runtimeConfig.Store(runtimeCfg)
|
2020-09-23 11:37:33 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements http.Handler and serves UI HTTP requests
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2020-10-08 17:03:13 +00:00
|
|
|
// We need to support the path being trimmed by http.StripTags just like the
|
|
|
|
// file servers do since http.StripPrefix will remove the leading slash in our
|
|
|
|
// current config. Everything else works fine that way so we should to.
|
|
|
|
pathTrimmed := strings.TrimLeft(r.URL.Path, "/")
|
|
|
|
if pathTrimmed == compiledProviderJSPath {
|
2021-03-24 18:43:42 +00:00
|
|
|
h.serveUIMetricsProviders(w)
|
2020-10-08 17:03:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:43:42 +00:00
|
|
|
srv, err := h.handleIndex()
|
|
|
|
if err != nil {
|
2020-09-23 11:37:33 +00:00
|
|
|
http.Error(w, "UI server is misconfigured.", http.StatusInternalServerError)
|
2021-03-24 18:43:42 +00:00
|
|
|
h.logger.Error("Failed to configure UI server: %s", err)
|
2020-09-23 11:37:33 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-24 18:43:42 +00:00
|
|
|
srv.ServeHTTP(w, r)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReloadConfig is called by the agent when the configuration is reloaded and
|
|
|
|
// updates the UIConfig values the handler uses to serve requests.
|
|
|
|
func (h *Handler) ReloadConfig(newCfg *config.RuntimeConfig) error {
|
2021-03-24 18:43:42 +00:00
|
|
|
h.runtimeConfig.Store(newCfg)
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2021-03-24 18:43:42 +00:00
|
|
|
func (h *Handler) handleIndex() (http.Handler, error) {
|
|
|
|
cfg := h.getRuntimeConfig()
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
var fsys fs.FS
|
2021-03-24 18:43:42 +00:00
|
|
|
if cfg.UIConfig.Dir == "" {
|
2022-05-31 19:33:56 +00:00
|
|
|
// strip the dist/ prefix
|
|
|
|
sub, err := fs.Sub(dist, "dist")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fsys = sub
|
2020-09-23 11:37:33 +00:00
|
|
|
} else {
|
2022-05-31 19:33:56 +00:00
|
|
|
fsys = os.DirFS(cfg.UIConfig.Dir)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render a new index.html with the new config values ready to serve.
|
2022-05-31 19:33:56 +00:00
|
|
|
buf, err := h.renderIndexFile(cfg, fsys)
|
2021-03-24 18:43:42 +00:00
|
|
|
if _, ok := err.(*os.PathError); ok && cfg.UIConfig.Dir != "" {
|
2020-10-01 11:26:19 +00:00
|
|
|
// A Path error indicates that there is no index.html. This could happen if
|
|
|
|
// the user configured their own UI dir and is serving something that is not
|
|
|
|
// our usual UI. This won't work perfectly because our uiserver will still
|
|
|
|
// redirect everything to the UI but we shouldn't fail the entire UI server
|
|
|
|
// with a 500 in this case. Partly that's just bad UX and partly it's a
|
|
|
|
// breaking change although quite an edge case. Instead, continue but just
|
|
|
|
// return a 404 response for the index.html and log a warning.
|
|
|
|
h.logger.Warn("ui_config.dir does not contain an index.html. Index templating and redirects to index.html are disabled.")
|
2022-05-31 19:33:56 +00:00
|
|
|
return http.FileServer(http.FS(fsys)), nil
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
2021-03-24 18:43:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
// Create a new fsys that serves the rendered index file or falls back to the
|
2021-03-24 18:43:42 +00:00
|
|
|
// underlying FS.
|
2022-05-31 19:33:56 +00:00
|
|
|
fsys = &bufIndexFS{
|
|
|
|
fs: fsys,
|
|
|
|
bufIndex: buf,
|
2021-03-24 18:43:42 +00:00
|
|
|
}
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2021-03-24 18:43:42 +00:00
|
|
|
// Wrap the buffering FS our redirect FS. This needs to happen later so that
|
|
|
|
// redirected requests for /index.html get served the rendered version not the
|
|
|
|
// original.
|
2022-05-31 19:33:56 +00:00
|
|
|
return http.FileServer(http.FS(&redirectFS{fs: fsys})), nil
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:43:42 +00:00
|
|
|
// getRuntimeConfig is a helper to atomically access the runtime config.
|
|
|
|
func (h *Handler) getRuntimeConfig() *config.RuntimeConfig {
|
|
|
|
if cfg, ok := h.runtimeConfig.Load().(*config.RuntimeConfig); ok {
|
|
|
|
return cfg
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:43:42 +00:00
|
|
|
func (h *Handler) serveUIMetricsProviders(resp http.ResponseWriter) {
|
2020-10-08 17:03:13 +00:00
|
|
|
// Reload config in case it's changed
|
2021-03-24 18:43:42 +00:00
|
|
|
cfg := h.getRuntimeConfig()
|
2020-10-08 17:03:13 +00:00
|
|
|
|
2021-03-24 18:43:42 +00:00
|
|
|
if len(cfg.UIConfig.MetricsProviderFiles) < 1 {
|
2020-10-08 17:03:13 +00:00
|
|
|
http.Error(resp, "No provider JS files configured", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// Open each one and concatenate them
|
2021-03-24 18:43:42 +00:00
|
|
|
for _, file := range cfg.UIConfig.MetricsProviderFiles {
|
2020-10-08 17:03:13 +00:00
|
|
|
if err := concatFile(&buf, file); err != nil {
|
|
|
|
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
h.logger.Error("failed serving metrics provider js file", "file", file, "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Done!
|
|
|
|
resp.Header()["Content-Type"] = []string{"application/javascript"}
|
|
|
|
_, err := buf.WriteTo(resp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
h.logger.Error("failed writing ui metrics provider files: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func concatFile(buf *bytes.Buffer, file string) error {
|
|
|
|
base := path.Base(file)
|
|
|
|
_, err := buf.WriteString("// " + base + "\n\n")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed writing provider JS files: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to open the file
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed opening ui metrics provider JS file: %w", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = buf.ReadFrom(f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed reading ui metrics provider JS file: %w", err)
|
|
|
|
}
|
|
|
|
_, err = buf.WriteString("\n\n")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed writing provider JS files: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
func (h *Handler) renderIndexFile(cfg *config.RuntimeConfig, fsys fs.FS) (fs.File, error) {
|
2020-09-23 11:37:33 +00:00
|
|
|
// Open the original index.html
|
2022-05-31 19:33:56 +00:00
|
|
|
f, err := fsys.Open("index.html")
|
2020-09-23 11:37:33 +00:00
|
|
|
if err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
return nil, err
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
content, err := io.ReadAll(f)
|
2020-09-23 11:37:33 +00:00
|
|
|
if err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
return nil, fmt.Errorf("failed reading index.html: %w", err)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
2022-05-31 19:33:56 +00:00
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
info, err := f.Stat()
|
|
|
|
if err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
return nil, fmt.Errorf("failed reading metadata for index.html: %w", err)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create template data from the current config.
|
|
|
|
tplData, err := uiTemplateDataFromConfig(cfg)
|
|
|
|
if err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
return nil, fmt.Errorf("failed loading UI config for template: %w", err)
|
2020-10-09 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow caller to apply additional data transformations if needed.
|
|
|
|
if h.transform != nil {
|
2021-03-24 18:43:42 +00:00
|
|
|
if err := h.transform(tplData); err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
return nil, fmt.Errorf("failed running transform: %w", err)
|
2020-10-09 13:32:39 +00:00
|
|
|
}
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 13:32:39 +00:00
|
|
|
tpl, err := template.New("index").Funcs(template.FuncMap{
|
2021-01-20 18:40:46 +00:00
|
|
|
"jsonEncode": func(data map[string]interface{}) (string, error) {
|
|
|
|
bs, err := json.MarshalIndent(data, "", " ")
|
2020-10-09 13:32:39 +00:00
|
|
|
if err != nil {
|
2021-01-20 18:40:46 +00:00
|
|
|
return "", fmt.Errorf("failed jsonEncode: %w", err)
|
2020-10-09 13:32:39 +00:00
|
|
|
}
|
2021-01-20 18:40:46 +00:00
|
|
|
return string(bs), nil
|
2020-10-09 13:32:39 +00:00
|
|
|
},
|
|
|
|
}).Parse(string(content))
|
2020-09-23 11:37:33 +00:00
|
|
|
if err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
return nil, fmt.Errorf("failed parsing index.html template: %w", err)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
err = tpl.Execute(&buf, tplData)
|
|
|
|
if err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
return nil, fmt.Errorf("failed to render index.html: %w", err)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
file := newBufferedFile(&buf, info)
|
|
|
|
return file, nil
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|