Skip to content

Commit

Permalink
ui: create server to handle ui specifics
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce committed Sep 13, 2018
1 parent 770c355 commit 371aab7
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 222 deletions.
4 changes: 3 additions & 1 deletion agent/agent.go
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/skydive-project/skydive/topology"
"github.com/skydive-project/skydive/topology/graph"
"github.com/skydive-project/skydive/topology/graph/traversal"
"github.com/skydive-project/skydive/ui"
ws "github.com/skydive-project/skydive/websocket"
)

Expand Down Expand Up @@ -183,7 +184,8 @@ func NewAgent() (*Agent, error) {
return nil, err
}

hserver.RegisterLoginRoute(apiAuthBackend)
uiServer := ui.NewServer(hserver, config.GetString("ui.extra_assets"))
uiServer.RegisterLoginRoute(apiAuthBackend)

if err := hserver.Listen(); err != nil {
return nil, err
Expand Down
23 changes: 8 additions & 15 deletions analyzer/server.go
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/skydive-project/skydive/topology/enhancers"
"github.com/skydive-project/skydive/topology/graph"
"github.com/skydive-project/skydive/topology/graph/traversal"
"github.com/skydive-project/skydive/ui"
ws "github.com/skydive-project/skydive/websocket"
)

Expand Down Expand Up @@ -76,6 +77,7 @@ type Status struct {
// Server describes an Analyzer servers mechanism like http, websocket, topology, ondemand probes, ...
type Server struct {
httpServer *shttp.Server
uiServer *ui.Server
agentWSServer *ws.StructServer
publisherWSServer *ws.StructServer
replicationWSServer *ws.StructServer
Expand Down Expand Up @@ -233,10 +235,12 @@ func NewServerFromConfig() (*Server, error) {
return nil, err
}

uiServer := ui.NewServer(hserver, config.GetString("ui.extra_assets"))

// add some global vars
hserver.AddGlobalVar("ui", config.Get("ui"))
hserver.AddGlobalVar("flow-metric-keys", (&flow.FlowMetric{}).GetFields())
hserver.AddGlobalVar("interface-metric-keys", (&topology.InterfaceMetric{}).GetFields())
uiServer.AddGlobalVar("ui", config.Get("ui"))
uiServer.AddGlobalVar("flow-metric-keys", (&flow.FlowMetric{}).GetFields())
uiServer.AddGlobalVar("interface-metric-keys", (&topology.InterfaceMetric{}).GetFields())

name := config.GetString("analyzer.topology.backend")
if len(name) == 0 {
Expand Down Expand Up @@ -272,7 +276,7 @@ func NewServerFromConfig() (*Server, error) {
return nil, err
}

hserver.RegisterLoginRoute(apiAuthBackend)
uiServer.RegisterLoginRoute(apiAuthBackend)

agentWSServer := ws.NewStructServer(ws.NewServer(hserver, "/ws/agent", clusterAuthBackend))
_, err = NewTopologyAgentEndpoint(agentWSServer, cached, g)
Expand Down Expand Up @@ -397,17 +401,6 @@ func NewServerFromConfig() (*Server, error) {
}
}

// server index for the following url as the client side will redirect
// the user to the correct page
routes := []shttp.Route{
{Path: "/topology", Method: "GET", HandlerFunc: hserver.ServeIndex},
{Path: "/conversation", Method: "GET", HandlerFunc: hserver.ServeIndex},
{Path: "/discovery", Method: "GET", HandlerFunc: hserver.ServeIndex},
{Path: "/preference", Method: "GET", HandlerFunc: hserver.ServeIndex},
{Path: "/status", Method: "GET", HandlerFunc: hserver.ServeIndex},
}
hserver.RegisterRoutes(routes, shttp.NewNoAuthenticationBackend())

return s, nil
}

Expand Down
4 changes: 2 additions & 2 deletions http/auth.go
Expand Up @@ -109,7 +109,7 @@ func authCallWrapped(w http.ResponseWriter, r *http.Request, username string, wr
context.Clear(&ar.Request)
}

func authenticate(backend AuthenticationBackend, w http.ResponseWriter, username, password string) (string, error) {
func Authenticate(backend AuthenticationBackend, w http.ResponseWriter, username, password string) (string, error) {
token, err := backend.Authenticate(username, password)
if err != nil {
return "", err
Expand Down Expand Up @@ -157,7 +157,7 @@ func authenticateWithHeaders(backend AuthenticationBackend, w http.ResponseWrite
}
username, password := pair[0], pair[1]

return authenticate(backend, w, username, password)
return Authenticate(backend, w, username, password)
}

// NewAuthenticationBackendByName creates a new auth backend based on the name
Expand Down
4 changes: 2 additions & 2 deletions http/basic.go
Expand Up @@ -74,15 +74,15 @@ func (b *BasicAuthenticationBackend) Wrap(wrapped auth.AuthenticatedHandlerFunc)
return func(w http.ResponseWriter, r *http.Request) {
token, err := authenticateWithHeaders(b, w, r)
if err != nil {
unauthorized(w, r)
Unauthorized(w, r)
return
}

// add "fake" header to let the basic auth library do the authentication
r.Header.Set("Authorization", "Basic "+token)

if username := b.CheckAuth(r); username == "" {
unauthorized(w, r)
Unauthorized(w, r)
} else {
authCallWrapped(w, r, username, wrapped)
}
Expand Down
4 changes: 2 additions & 2 deletions http/keystone.go
Expand Up @@ -165,15 +165,15 @@ func (b *KeystoneAuthenticationBackend) Wrap(wrapped auth.AuthenticatedHandlerFu
return func(w http.ResponseWriter, r *http.Request) {
token, err := authenticateWithHeaders(b, w, r)
if err != nil {
unauthorized(w, r)
Unauthorized(w, r)
return
}

if username, err := b.CheckUser(token); username == "" {
if err != nil {
logging.GetLogger().Warningf("Failed to check token: %s", err)
}
unauthorized(w, r)
Unauthorized(w, r)
} else {
authCallWrapped(w, r, username, wrapped)
}
Expand Down
1 change: 0 additions & 1 deletion http/noauth.go
Expand Up @@ -52,7 +52,6 @@ func (h *NoAuthenticationBackend) Authenticate(username string, password string)

func (h *NoAuthenticationBackend) Wrap(wrapped auth.AuthenticatedHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
setTLSHeader(w, r)
ar := &auth.AuthenticatedRequest{Request: *r, Username: "admin"}
copyRequestVars(r, &ar.Request)
wrapped(w, ar)
Expand Down

0 comments on commit 371aab7

Please sign in to comment.