Skip to content

Commit

Permalink
obsservice: ui handler serves db console assets
Browse files Browse the repository at this point in the history
This change enables the Observabilty Service to serve the DB Console UI
itself. Endpoints that CRDB can handle are proxied (`/_admin/`,
`/_status/`, `/ts/`) but the base URL will return a blank HTML page with
JS assets that load the DB Console.

In order to build with the ui code, the `--config=with_ui` flag must be
passed to bazel.

This commit also adds a shortcut to the `dev` command to build
`obsservice` which includes the `--config=with_ui` flag just as we do by
default in `cockroach` builds.

Release note: None
  • Loading branch information
dhartunian committed Jul 2, 2022
1 parent 75cf2bb commit ff97794
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -euo pipefail

# Bump this counter to force rebuilding `dev` on all machines.
DEV_VERSION=37
DEV_VERSION=38

THIS_DIR=$(cd "$(dirname "$0")" && pwd)
BINARY_DIR=$THIS_DIR/bin/dev-versions
Expand Down
7 changes: 3 additions & 4 deletions pkg/ccl/oidcccl/authentication_oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ type oidcAuthenticationConf struct {
autoLogin bool
}

// GetUIConf is used to extract certain parts of the OIDC
// configuration at run-time for embedding into the
// Admin UI HTML in order to manage the login experience
// the UI provides.
// GetOIDCConf is used to extract certain parts of the OIDC
// configuration at run-time for embedding into the DB Console in order
// to manage the login experience the UI provides.
func (s *oidcAuthenticationServer) GetOIDCConf() ui.OIDCUIConf {
return ui.OIDCUIConf{
ButtonText: s.conf.buttonText,
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/dev/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var buildTargetMapping = map[string]string{
"label-merged-pr": "//pkg/cmd/label-merged-pr:label-merged-pr",
"geos": geosTarget,
"libgeos": geosTarget,
"obsservice": "//pkg/obsservice/cmd/obsservice",
"optgen": "//pkg/sql/opt/optgen/cmd/optgen:optgen",
"optfmt": "//pkg/sql/opt/optgen/cmd/optfmt:optfmt",
"oss": "//pkg/cmd/cockroach-oss:cockroach-oss",
Expand Down Expand Up @@ -412,7 +413,9 @@ func (d *dev) getBasicBuildArgs(

// Add --config=with_ui iff we're building a target that needs it.
for _, target := range buildTargets {
if target.fullName == buildTargetMapping["cockroach"] || target.fullName == buildTargetMapping["cockroach-oss"] {
if target.fullName == buildTargetMapping["cockroach"] ||
target.fullName == buildTargetMapping["cockroach-oss"] ||
target.fullName == buildTargetMapping["obsservice"] {
args = append(args, "--config=with_ui")
break
}
Expand Down
1 change: 1 addition & 0 deletions pkg/obsservice/cmd/obsservice/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"//pkg/cli/exit",
"//pkg/obsservice/obslib",
"//pkg/ui/distoss",
"@com_github_spf13_cobra//:cobra",
],
)
Expand Down
1 change: 1 addition & 0 deletions pkg/obsservice/cmd/obsservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/cli/exit"
"github.com/cockroachdb/cockroach/pkg/obsservice/obslib"
_ "github.com/cockroachdb/cockroach/pkg/ui/distoss" // web UI init hooks
"github.com/spf13/cobra"
)

Expand Down
2 changes: 2 additions & 0 deletions pkg/obsservice/obslib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/obsservice/obslib",
visibility = ["//visibility:public"],
deps = [
"//pkg/base",
"//pkg/ui",
"//pkg/util/log",
"//pkg/util/syncutil",
"@com_github_cockroachdb_cmux//:cmux",
Expand Down
36 changes: 35 additions & 1 deletion pkg/obsservice/obslib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/cockroachdb/cmux"
"github.com/cockroachdb/cockroach/pkg/ui"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
Expand Down Expand Up @@ -93,6 +94,25 @@ to trust the certificate presented by CockroachDB.`)
}
}

// We define our own copy of this struct that also exists in
// pkg/server/authentication.go because we don't want to import that
// package. This struct is used to define a null OIDC configuration for
// the UI Config. DB Console uses information present here to show
// different login UI when OIDC is present.
type noOIDCConfigured struct{}

var _ ui.OIDCUI = &noOIDCConfigured{}

// GetOIDCConf implements the `ui.OIDCUI` interface with a configuration
// that disables OIDC login options for the UI server.
func (c *noOIDCConfigured) GetOIDCConf() ui.OIDCUIConf {
return ui.OIDCUIConf{
Enabled: false,
}
}

var CRDBProxyPaths = []string{"/_admin/", "/_status/", "/ts/", "/api/v2/"}

// RunAsync runs an HTTP proxy server in a goroutine. The returned channel is
// closed when the server terminates.
//
Expand All @@ -117,7 +137,21 @@ func (p *ReverseHTTPProxy) RunAsync(ctx context.Context) <-chan struct{} {
// Create the HTTP mux. Requests will generally be forwarded to p.proxy,
// except the /debug/pprof ones which will be served locally.
mux := http.NewServeMux()
mux.Handle("/", p.proxy)
// TODO(davidh): Ideally, the UI handler should probably be
// configured in `obsservice` and not hardcoded into `obslib`. This
// gives lib users a chance to do whatever they want with the UI.
mux.Handle("/", ui.Handler(ui.Config{
ExperimentalUseLogin: false,
LoginEnabled: false,
GetUser: func(ctx context.Context) *string {
u := "Observability Service"
return &u
},
OIDC: &noOIDCConfigured{},
}))
for _, path := range CRDBProxyPaths {
mux.Handle(path, p.proxy)
}
// This seems to be the minimal set of handlers that we need to register in
// order to get all the pprof functionality. The pprof.Index handler handles
// some types of profiles itself.
Expand Down
2 changes: 2 additions & 0 deletions pkg/server/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const (

type noOIDCConfigured struct{}

var _ ui.OIDCUI = &noOIDCConfigured{}

func (c *noOIDCConfigured) GetOIDCConf() ui.OIDCUIConf {
return ui.OIDCUIConf{
Enabled: false,
Expand Down
4 changes: 3 additions & 1 deletion pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ func Handler(cfg Config) http.Handler {
LoggedInUser: cfg.GetUser(r.Context()),
Tag: buildInfo.Tag,
Version: build.BinaryVersionPrefix(),
NodeID: cfg.NodeID.String(),
OIDCAutoLogin: oidcConf.AutoLogin,
OIDCLoginEnabled: oidcConf.Enabled,
OIDCButtonText: oidcConf.ButtonText,
}
if cfg.NodeID != nil {
args.NodeID = cfg.NodeID.String()
}
if uiConfigPath.MatchString(r.URL.Path) {
argBytes, err := json.Marshal(args)
if err != nil {
Expand Down

0 comments on commit ff97794

Please sign in to comment.