api: Cleanup websocket protocol
This commit cleans up the websocket protocol to be in line with the documentation.
This commit is contained in:
parent
16fa721048
commit
1c68893a02
31 changed files with 3549 additions and 120 deletions
|
|
@ -84,14 +84,6 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http
|
|||
|
||||
router := httprouter.New()
|
||||
|
||||
{
|
||||
handler = &httpx.WebSocket{
|
||||
Context: ctx,
|
||||
Fallback: router,
|
||||
Handler: admin.Dependencies.Sockets.Serve,
|
||||
}
|
||||
}
|
||||
|
||||
// add a handler for the index page
|
||||
{
|
||||
index := admin.index(ctx)
|
||||
|
|
@ -161,7 +153,7 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http
|
|||
// add a router for the login page
|
||||
router.Handler(http.MethodPost, route+"login", admin.loginHandler(ctx))
|
||||
|
||||
return
|
||||
return router, nil
|
||||
}
|
||||
|
||||
func (admin *Admin) loginHandler(ctx context.Context) http.Handler {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/provision"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
|
|
@ -15,7 +16,7 @@ import (
|
|||
func (sockets *Sockets) Actions() ActionMap {
|
||||
return map[string]Action{
|
||||
// generic actions
|
||||
"backup": sockets.Generic(0, func(ctx context.Context, sockets *Sockets, in io.Reader, out io.Writer, params ...string) error {
|
||||
"backup": sockets.Generic(scopes.ScopeUserAdmin, "", 0, func(ctx context.Context, sockets *Sockets, in io.Reader, out io.Writer, params ...string) error {
|
||||
return sockets.Dependencies.Exporter.MakeExport(
|
||||
ctx,
|
||||
out,
|
||||
|
|
@ -27,7 +28,7 @@ func (sockets *Sockets) Actions() ActionMap {
|
|||
},
|
||||
)
|
||||
}),
|
||||
"provision": sockets.Generic(1, func(ctx context.Context, sockets *Sockets, in io.Reader, out io.Writer, params ...string) error {
|
||||
"provision": sockets.Generic(scopes.ScopeUserAdmin, "", 1, func(ctx context.Context, sockets *Sockets, in io.Reader, out io.Writer, params ...string) error {
|
||||
// read the flags of the instance to be provisioned
|
||||
var flags provision.Flags
|
||||
if err := json.Unmarshal([]byte(params[0]), &flags); err != nil {
|
||||
|
|
@ -52,7 +53,7 @@ func (sockets *Sockets) Actions() ActionMap {
|
|||
|
||||
// instance-specific actions!
|
||||
|
||||
"snapshot": sockets.Instance(0, func(ctx context.Context, socket *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
"snapshot": sockets.Instance(scopes.ScopeUserAdmin, "", 0, func(ctx context.Context, socket *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
return socket.Dependencies.Exporter.MakeExport(
|
||||
ctx,
|
||||
out,
|
||||
|
|
@ -64,7 +65,7 @@ func (sockets *Sockets) Actions() ActionMap {
|
|||
},
|
||||
)
|
||||
}),
|
||||
"rebuild": sockets.Instance(1, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
"rebuild": sockets.Instance(scopes.ScopeUserAdmin, "", 1, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
// read the flags of the instance to be provisioned
|
||||
var system models.System
|
||||
if err := json.Unmarshal([]byte(params[0]), &system); err != nil {
|
||||
|
|
@ -72,20 +73,23 @@ func (sockets *Sockets) Actions() ActionMap {
|
|||
}
|
||||
return instance.SystemManager().Apply(ctx, out, system, true)
|
||||
}),
|
||||
"update": sockets.Instance(0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
"update": sockets.Instance(scopes.ScopeUserAdmin, "", 0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
return instance.Drush().Update(ctx, out)
|
||||
}),
|
||||
"cron": sockets.Instance(0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, str io.Writer, params ...string) error {
|
||||
"cron": sockets.Instance(scopes.ScopeUserAdmin, "", 0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, str io.Writer, params ...string) error {
|
||||
return instance.Drush().Cron(ctx, str)
|
||||
}),
|
||||
"start": sockets.Instance(0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
"start": sockets.Instance(scopes.ScopeUserAdmin, "", 0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
return instance.Barrel().Stack().Up(ctx, out)
|
||||
}),
|
||||
"stop": sockets.Instance(0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
"stop": sockets.Instance(scopes.ScopeUserAdmin, "", 0, func(ctx context.Context, _ *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
return instance.Barrel().Stack().Down(ctx, out)
|
||||
}),
|
||||
"purge": sockets.Instance(0, func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
"purge": sockets.Instance(scopes.ScopeUserAdmin, "", 0, func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
|
||||
return sockets.Dependencies.Purger.Purge(ctx, out, instance.Slug)
|
||||
}),
|
||||
"never": sockets.Generic(scopes.ScopeNever, "", 0, func(ctx context.Context, sockets *Sockets, in io.Reader, out io.Writer, params ...string) error {
|
||||
panic("never called")
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,11 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/tkw1536/pkglib/httpx"
|
||||
"github.com/tkw1536/pkglib/status"
|
||||
)
|
||||
|
||||
// ActionMap handles a set of WebSocket actions
|
||||
|
|
@ -27,7 +29,7 @@ func (err errPanic) Error() string {
|
|||
return fmt.Sprintf("fatal error: %v", err.value)
|
||||
}
|
||||
|
||||
// Handle handles a new incoming websocket connection.
|
||||
// Handle handles a new incoming websocket connection using the given authentication.
|
||||
//
|
||||
// There are two kinds of messages:
|
||||
//
|
||||
|
|
@ -40,7 +42,7 @@ func (err errPanic) Error() string {
|
|||
// Finally it will send a ResultMessage once handling is complete.
|
||||
//
|
||||
// A corresponding client implementation of this can be found in ..../remote/proto.ts
|
||||
func (am ActionMap) Handle(conn httpx.WebSocketConnection) (name string, err error) {
|
||||
func (am ActionMap) Handle(auth *auth.Auth, conn httpx.WebSocketConnection) (name string, err error) {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// once we have finished executing send a binary message (indicating success) to the client.
|
||||
|
|
@ -123,12 +125,17 @@ func (am ActionMap) Handle(conn httpx.WebSocketConnection) (name string, err err
|
|||
|
||||
// check that the given action exists!
|
||||
// and has the right number of parameters!
|
||||
action, ok := am[call.Name]
|
||||
action, ok := am[call.Call]
|
||||
if !ok || action.Handle == nil {
|
||||
return call.Name, errUnknownAction
|
||||
return call.Call, errUnknownAction
|
||||
}
|
||||
if action.NumParams != len(call.Params) {
|
||||
return call.Name, errIncorrectParams
|
||||
return call.Call, errIncorrectParams
|
||||
}
|
||||
|
||||
// check that we have the given permission
|
||||
if err := auth.CheckScope(action.ScopeParam, action.scope(), conn.Request()); err != nil {
|
||||
return call.Call, err
|
||||
}
|
||||
|
||||
// create a context to be canceled once done
|
||||
|
|
@ -170,17 +177,22 @@ func (am ActionMap) Handle(conn httpx.WebSocketConnection) (name string, err err
|
|||
}
|
||||
}()
|
||||
|
||||
// create a linebuffer to write the output line by line
|
||||
output := &status.LineBuffer{
|
||||
Line: func(line string) {
|
||||
<-conn.WriteText(line)
|
||||
},
|
||||
FlushLineOnClose: true,
|
||||
}
|
||||
defer output.Close()
|
||||
// write the output to the client as it comes in!
|
||||
// NOTE(twiesing): We may eventually need buffering here ...
|
||||
output := WriteFunc(func(b []byte) (int, error) {
|
||||
<-conn.WriteText(string(b))
|
||||
return len(b), nil
|
||||
})
|
||||
|
||||
// handle the actual
|
||||
return call.Name, action.Handle(ctx, inputR, output, call.Params...)
|
||||
return call.Call, action.Handle(ctx, inputR, output, call.Params...)
|
||||
}
|
||||
|
||||
// WriteFunc implements io.Writer using a function.
|
||||
type WriteFunc func([]byte) (int, error)
|
||||
|
||||
func (wf WriteFunc) Write(b []byte) (int, error) {
|
||||
return wf(b)
|
||||
}
|
||||
|
||||
// Action is something that can be handled via a WebSocket connection.
|
||||
|
|
@ -188,6 +200,11 @@ type Action struct {
|
|||
// NumPara
|
||||
NumParams int
|
||||
|
||||
// Scope and ScopeParam indicate the scope required by the caller.
|
||||
// TODO(twiesing): Once we actually include scopes, make them dynamic
|
||||
Scope component.Scope
|
||||
ScopeParam string
|
||||
|
||||
// Handle handles this action.
|
||||
//
|
||||
// ctx is closed once the underlying connection is closed.
|
||||
|
|
@ -196,9 +213,18 @@ type Action struct {
|
|||
Handle func(ctx context.Context, in io.Reader, out io.Writer, params ...string) error
|
||||
}
|
||||
|
||||
// scope returns the actual scope required by this action.
|
||||
// If the caller did not provide an actual scope, uses ScopeNever
|
||||
func (action Action) scope() component.Scope {
|
||||
if action.Scope == "" {
|
||||
return scopes.ScopeNever
|
||||
}
|
||||
return action.Scope
|
||||
}
|
||||
|
||||
// CallMessage is sent by the client to the server to invoke a remote procedure
|
||||
type CallMessage struct {
|
||||
Name string `json:"name"`
|
||||
Call string `json:"call"`
|
||||
Params []string `json:"params,omitempty"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,11 @@ package socket
|
|||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances/purger"
|
||||
|
|
@ -25,22 +28,44 @@ type Sockets struct {
|
|||
Instances *instances.Instances
|
||||
Exporter *exporter.Exporter
|
||||
Purger *purger.Purger
|
||||
Auth *auth.Auth
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ component.Routeable = (*Sockets)(nil)
|
||||
)
|
||||
|
||||
func (socket *Sockets) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Prefix: "/api/v1/ws",
|
||||
Exact: true,
|
||||
Decorator: socket.Dependencies.Auth.Require(true, scopes.ScopeUserValid, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func (sockets *Sockets) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
|
||||
return &httpx.WebSocket{
|
||||
Context: ctx,
|
||||
Handler: sockets.Serve,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Serve handles a connection to the websocket api
|
||||
func (socket *Sockets) Serve(conn httpx.WebSocketConnection) {
|
||||
// handle the websocket connection!
|
||||
name, err := socket.actions.Get(socket.Actions).Handle(conn)
|
||||
name, err := socket.actions.Get(socket.Actions).Handle(socket.Dependencies.Auth, conn)
|
||||
if err != nil {
|
||||
zerolog.Ctx(conn.Context()).Err(err).Str("name", name).Msg("Error handling websocket")
|
||||
}
|
||||
}
|
||||
|
||||
// Generic returns a new action that calls handler with the provided number of parameters
|
||||
func (sockets *Sockets) Generic(numParams int, handler func(ctx context.Context, socket *Sockets, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
func (sockets *Sockets) Generic(scope component.Scope, scopeParam string, numParams int, handler func(ctx context.Context, socket *Sockets, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
return Action{
|
||||
NumParams: numParams,
|
||||
Scope: scope,
|
||||
ScopeParam: scopeParam,
|
||||
NumParams: numParams,
|
||||
Handle: func(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
|
||||
return handler(ctx, sockets, in, out, params...)
|
||||
},
|
||||
|
|
@ -48,8 +73,11 @@ func (sockets *Sockets) Generic(numParams int, handler func(ctx context.Context,
|
|||
}
|
||||
|
||||
// Insstance returns a new action that calls handler with a specific WissKI instance
|
||||
func (sockets *Sockets) Instance(numParams int, handler func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
func (sockets *Sockets) Instance(scope component.Scope, scopeParam string, numParams int, handler func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
return Action{
|
||||
Scope: scope,
|
||||
ScopeParam: scopeParam,
|
||||
|
||||
NumParams: numParams + 1,
|
||||
Handle: func(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
|
||||
instance, err := sockets.Dependencies.Instances.WissKI(ctx, params[0])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue