2020-09-23 11:37:33 +00:00
|
|
|
package uiserver
|
|
|
|
|
|
|
|
import (
|
2021-01-20 18:40:46 +00:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2020-10-01 11:26:19 +00:00
|
|
|
"io/ioutil"
|
2020-09-23 11:37:33 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-10-01 11:26:19 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-01-20 18:40:46 +00:00
|
|
|
"strings"
|
2020-09-23 11:37:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-03-24 19:33:52 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2021-03-24 18:43:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-24 19:33:52 +00:00
|
|
|
"golang.org/x/net/html"
|
2021-03-24 18:43:42 +00:00
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
|
|
|
|
2020-10-08 17:03:13 +00:00
|
|
|
func TestUIServerIndex(t *testing.T) {
|
2020-09-23 11:37:33 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
cfg *config.RuntimeConfig
|
|
|
|
path string
|
2020-10-09 13:32:39 +00:00
|
|
|
tx UIDataTransform
|
2020-09-23 11:37:33 +00:00
|
|
|
wantStatus int
|
|
|
|
wantContains []string
|
|
|
|
wantNotContains []string
|
|
|
|
wantEnv map[string]interface{}
|
|
|
|
wantUICfgJSON string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic UI serving",
|
|
|
|
cfg: basicUIEnabledConfig(),
|
|
|
|
path: "/", // Note /index.html redirects to /
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContains: []string{"<!-- CONSUL_VERSION:"},
|
2021-01-20 18:40:46 +00:00
|
|
|
wantUICfgJSON: `{
|
|
|
|
"ACLsEnabled": false,
|
|
|
|
"LocalDatacenter": "dc1",
|
|
|
|
"ContentPath": "/ui/",
|
|
|
|
"UIConfig": {
|
|
|
|
"metrics_provider": "",
|
|
|
|
"metrics_proxy_enabled": false,
|
|
|
|
"dashboard_url_templates": null
|
|
|
|
}
|
|
|
|
}`,
|
2020-09-23 11:37:33 +00:00
|
|
|
},
|
|
|
|
{
|
2020-10-26 19:48:23 +00:00
|
|
|
// We do this redirect just for UI dir since the app is a single page app
|
|
|
|
// and any URL under the path should just load the index and let Ember do
|
|
|
|
// it's thing unless it's a specific asset URL in the filesystem.
|
2020-09-23 11:37:33 +00:00
|
|
|
name: "unknown paths to serve index",
|
|
|
|
cfg: basicUIEnabledConfig(),
|
|
|
|
path: "/foo-bar-bazz-qux",
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContains: []string{"<!-- CONSUL_VERSION:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "injecting metrics vars",
|
|
|
|
cfg: basicUIEnabledConfig(
|
|
|
|
withMetricsProvider("foo"),
|
2020-10-09 13:32:39 +00:00
|
|
|
withMetricsProviderOptions(`{"a-very-unlikely-string":1}`),
|
2020-09-23 11:37:33 +00:00
|
|
|
),
|
|
|
|
path: "/",
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContains: []string{
|
|
|
|
"<!-- CONSUL_VERSION:",
|
|
|
|
},
|
|
|
|
wantUICfgJSON: `{
|
2021-01-20 18:40:46 +00:00
|
|
|
"ACLsEnabled": false,
|
|
|
|
"LocalDatacenter": "dc1",
|
|
|
|
"ContentPath": "/ui/",
|
|
|
|
"UIConfig": {
|
|
|
|
"metrics_provider": "foo",
|
|
|
|
"metrics_provider_options": {
|
|
|
|
"a-very-unlikely-string":1
|
|
|
|
},
|
|
|
|
"metrics_proxy_enabled": false,
|
|
|
|
"dashboard_url_templates": null
|
|
|
|
}
|
2020-09-23 11:37:33 +00:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "acls enabled",
|
|
|
|
cfg: basicUIEnabledConfig(withACLs()),
|
|
|
|
path: "/",
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContains: []string{"<!-- CONSUL_VERSION:"},
|
2021-01-20 18:40:46 +00:00
|
|
|
wantUICfgJSON: `{
|
|
|
|
"ACLsEnabled": true,
|
|
|
|
"LocalDatacenter": "dc1",
|
|
|
|
"ContentPath": "/ui/",
|
|
|
|
"UIConfig": {
|
|
|
|
"metrics_provider": "",
|
|
|
|
"metrics_proxy_enabled": false,
|
|
|
|
"dashboard_url_templates": null
|
|
|
|
}
|
|
|
|
}`,
|
2020-09-23 11:37:33 +00:00
|
|
|
},
|
2020-10-09 13:32:39 +00:00
|
|
|
{
|
|
|
|
name: "external transformation",
|
|
|
|
cfg: basicUIEnabledConfig(
|
|
|
|
withMetricsProvider("foo"),
|
|
|
|
),
|
|
|
|
path: "/",
|
2021-03-24 18:43:42 +00:00
|
|
|
tx: func(data map[string]interface{}) error {
|
2020-10-09 13:32:39 +00:00
|
|
|
data["SSOEnabled"] = true
|
|
|
|
o := data["UIConfig"].(map[string]interface{})
|
|
|
|
o["metrics_provider"] = "bar"
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContains: []string{
|
|
|
|
"<!-- CONSUL_VERSION:",
|
|
|
|
},
|
|
|
|
wantUICfgJSON: `{
|
2021-01-20 18:40:46 +00:00
|
|
|
"ACLsEnabled": false,
|
|
|
|
"SSOEnabled": true,
|
|
|
|
"LocalDatacenter": "dc1",
|
|
|
|
"ContentPath": "/ui/",
|
|
|
|
"UIConfig": {
|
|
|
|
"metrics_provider": "bar",
|
|
|
|
"metrics_proxy_enabled": false,
|
|
|
|
"dashboard_url_templates": null
|
|
|
|
}
|
2020-10-09 13:32:39 +00:00
|
|
|
}`,
|
|
|
|
},
|
2020-10-08 17:03:13 +00:00
|
|
|
{
|
|
|
|
name: "serving metrics provider js",
|
|
|
|
cfg: basicUIEnabledConfig(
|
|
|
|
withMetricsProvider("foo"),
|
|
|
|
withMetricsProviderFiles("testdata/foo.js", "testdata/bar.js"),
|
|
|
|
),
|
|
|
|
path: "/",
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContains: []string{
|
|
|
|
"<!-- CONSUL_VERSION:",
|
|
|
|
`<script src="/ui/assets/compiled-metrics-providers.js">`,
|
|
|
|
},
|
|
|
|
},
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-10-09 13:32:39 +00:00
|
|
|
h := NewHandler(tc.cfg, testutil.Logger(t), tc.tx)
|
2020-09-23 11:37:33 +00:00
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", tc.path, nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, tc.wantStatus, rec.Code)
|
|
|
|
for _, want := range tc.wantContains {
|
|
|
|
require.Contains(t, rec.Body.String(), want)
|
|
|
|
}
|
|
|
|
if tc.wantUICfgJSON != "" {
|
|
|
|
require.JSONEq(t, tc.wantUICfgJSON, extractUIConfig(t, rec.Body.String()))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 18:40:46 +00:00
|
|
|
func extractApplicationJSON(t *testing.T, attrName, content string) string {
|
2020-09-23 11:37:33 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2021-01-20 18:40:46 +00:00
|
|
|
var scriptContent *html.Node
|
|
|
|
var find func(node *html.Node)
|
|
|
|
|
|
|
|
// Recurse down the tree and pick out <script attrName=ourAttrName>
|
|
|
|
find = func(node *html.Node) {
|
|
|
|
if node.Type == html.ElementNode && node.Data == "script" {
|
|
|
|
for i := 0; i < len(node.Attr); i++ {
|
|
|
|
attr := node.Attr[i]
|
|
|
|
if attr.Key == attrName {
|
|
|
|
// find the script and save off the content, which in this case is
|
|
|
|
// the JSON we are looking for, once we have it finish up
|
|
|
|
scriptContent = node.FirstChild
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
|
|
|
find(child)
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2021-01-20 18:40:46 +00:00
|
|
|
doc, err := html.Parse(strings.NewReader(content))
|
2020-09-23 11:37:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-20 18:40:46 +00:00
|
|
|
find(doc)
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2021-01-20 18:40:46 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
w := io.Writer(&buf)
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2021-01-20 18:40:46 +00:00
|
|
|
renderErr := html.Render(w, scriptContent)
|
|
|
|
require.NoError(t, renderErr)
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2021-01-20 18:40:46 +00:00
|
|
|
jsonStr := html.UnescapeString(buf.String())
|
|
|
|
return jsonStr
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractUIConfig(t *testing.T, content string) string {
|
|
|
|
t.Helper()
|
2021-01-20 18:40:46 +00:00
|
|
|
return extractApplicationJSON(t, "data-consul-ui-config", content)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cfgFunc func(cfg *config.RuntimeConfig)
|
|
|
|
|
|
|
|
func basicUIEnabledConfig(opts ...cfgFunc) *config.RuntimeConfig {
|
|
|
|
cfg := &config.RuntimeConfig{
|
|
|
|
UIConfig: config.UIConfig{
|
2020-10-08 17:03:13 +00:00
|
|
|
Enabled: true,
|
|
|
|
ContentPath: "/ui/",
|
2020-09-23 11:37:33 +00:00
|
|
|
},
|
2020-10-26 19:48:23 +00:00
|
|
|
Datacenter: "dc1",
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
for _, f := range opts {
|
|
|
|
f(cfg)
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func withACLs() cfgFunc {
|
|
|
|
return func(cfg *config.RuntimeConfig) {
|
2021-08-06 22:00:58 +00:00
|
|
|
cfg.PrimaryDatacenter = "dc1"
|
2021-08-06 22:59:05 +00:00
|
|
|
cfg.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2020-09-23 11:37:33 +00:00
|
|
|
cfg.ACLsEnabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withMetricsProvider(name string) cfgFunc {
|
|
|
|
return func(cfg *config.RuntimeConfig) {
|
|
|
|
cfg.UIConfig.MetricsProvider = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 17:03:13 +00:00
|
|
|
func withMetricsProviderFiles(names ...string) cfgFunc {
|
|
|
|
return func(cfg *config.RuntimeConfig) {
|
|
|
|
cfg.UIConfig.MetricsProviderFiles = names
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
func withMetricsProviderOptions(jsonStr string) cfgFunc {
|
|
|
|
return func(cfg *config.RuntimeConfig) {
|
|
|
|
cfg.UIConfig.MetricsProviderOptionsJSON = jsonStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMultipleIndexRequests validates that the buffered file mechanism works
|
|
|
|
// beyond the first request. The initial implementation did not as it shared an
|
|
|
|
// bytes.Reader between callers.
|
|
|
|
func TestMultipleIndexRequests(t *testing.T) {
|
2020-10-09 13:32:39 +00:00
|
|
|
h := NewHandler(basicUIEnabledConfig(), testutil.Logger(t), nil)
|
2020-09-23 11:37:33 +00:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
require.Contains(t, rec.Body.String(), "<!-- CONSUL_VERSION:",
|
|
|
|
"request %d didn't return expected content", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReload(t *testing.T) {
|
2020-10-09 13:32:39 +00:00
|
|
|
h := NewHandler(basicUIEnabledConfig(), testutil.Logger(t), nil)
|
2020-09-23 11:37:33 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
require.Contains(t, rec.Body.String(), "<!-- CONSUL_VERSION:")
|
|
|
|
require.NotContains(t, rec.Body.String(), "exotic-metrics-provider-name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload the config with the changed metrics provider name
|
|
|
|
newCfg := basicUIEnabledConfig(
|
|
|
|
withMetricsProvider("exotic-metrics-provider-name"),
|
|
|
|
)
|
|
|
|
h.ReloadConfig(newCfg)
|
|
|
|
|
|
|
|
// Now we should see the new provider name in the output of index
|
|
|
|
{
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
require.Contains(t, rec.Body.String(), "<!-- CONSUL_VERSION:")
|
|
|
|
require.Contains(t, rec.Body.String(), "exotic-metrics-provider-name")
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 11:26:19 +00:00
|
|
|
|
|
|
|
func TestCustomDir(t *testing.T) {
|
|
|
|
uiDir := testutil.TempDir(t, "consul-uiserver")
|
|
|
|
defer os.RemoveAll(uiDir)
|
|
|
|
|
|
|
|
path := filepath.Join(uiDir, "test-file")
|
|
|
|
require.NoError(t, ioutil.WriteFile(path, []byte("test"), 0644))
|
|
|
|
|
|
|
|
cfg := basicUIEnabledConfig()
|
|
|
|
cfg.UIConfig.Dir = uiDir
|
2020-10-09 13:32:39 +00:00
|
|
|
h := NewHandler(cfg, testutil.Logger(t), nil)
|
2020-10-01 11:26:19 +00:00
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/test-file", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
require.Contains(t, rec.Body.String(), "test")
|
|
|
|
}
|
2020-10-08 17:03:13 +00:00
|
|
|
|
|
|
|
func TestCompiledJS(t *testing.T) {
|
|
|
|
cfg := basicUIEnabledConfig(
|
|
|
|
withMetricsProvider("foo"),
|
|
|
|
withMetricsProviderFiles("testdata/foo.js", "testdata/bar.js"),
|
|
|
|
)
|
2020-10-09 13:32:39 +00:00
|
|
|
h := NewHandler(cfg, testutil.Logger(t), nil)
|
2020-10-08 17:03:13 +00:00
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"/" + compiledProviderJSPath,
|
|
|
|
// We need to work even without the initial slash because the agent uses
|
|
|
|
// http.StripPrefix with the entire ContentPath which includes a trailing
|
|
|
|
// slash. This apparently works fine for the assetFS etc. so we need to
|
|
|
|
// also tolerate it when the URL doesn't have a slash at the start of the
|
|
|
|
// path.
|
|
|
|
compiledProviderJSPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
t.Run(path, func(t *testing.T) {
|
|
|
|
// NewRequest doesn't like paths with no leading slash but we need to test
|
|
|
|
// a request with a URL that has that so just create with root path and
|
|
|
|
// then manually modify the URL path so it emulates one that has been
|
|
|
|
// doctored by http.StripPath.
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
req.URL.Path = path
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
require.Equal(t, rec.Result().Header["Content-Type"][0], "application/javascript")
|
|
|
|
wantCompiled, err := ioutil.ReadFile("testdata/compiled-metrics-providers-golden.js")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, rec.Body.String(), string(wantCompiled))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-03-24 19:33:52 +00:00
|
|
|
|
|
|
|
func TestHandler_ServeHTTP_TransformIsEvaluatedOnEachRequest(t *testing.T) {
|
|
|
|
cfg := basicUIEnabledConfig()
|
|
|
|
|
|
|
|
value := "seeds"
|
|
|
|
transform := func(data map[string]interface{}) error {
|
|
|
|
data["apple"] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
h := NewHandler(cfg, hclog.New(nil), transform)
|
|
|
|
|
|
|
|
t.Run("initial request", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
expected := `{
|
|
|
|
"ACLsEnabled": false,
|
|
|
|
"LocalDatacenter": "dc1",
|
|
|
|
"ContentPath": "/ui/",
|
|
|
|
"UIConfig": {
|
|
|
|
"metrics_provider": "",
|
|
|
|
"metrics_proxy_enabled": false,
|
|
|
|
"dashboard_url_templates": null
|
|
|
|
},
|
|
|
|
"apple": "seeds"
|
|
|
|
}`
|
|
|
|
require.JSONEq(t, expected, extractUIConfig(t, rec.Body.String()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("transform value has changed", func(t *testing.T) {
|
|
|
|
|
|
|
|
value = "plant"
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
expected := `{
|
|
|
|
"ACLsEnabled": false,
|
|
|
|
"LocalDatacenter": "dc1",
|
|
|
|
"ContentPath": "/ui/",
|
|
|
|
"UIConfig": {
|
|
|
|
"metrics_provider": "",
|
|
|
|
"metrics_proxy_enabled": false,
|
|
|
|
"dashboard_url_templates": null
|
|
|
|
},
|
|
|
|
"apple": "plant"
|
|
|
|
}`
|
|
|
|
require.JSONEq(t, expected, extractUIConfig(t, rec.Body.String()))
|
|
|
|
})
|
|
|
|
}
|