apiutil

package
v0.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2021 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package apiutil contains utility functions for the REST API.

Index

Constants

View Source
const ContextRequestObjectKey = 1

ContextRequestObjectKey is the key where decoded request objects are stored in the request context

View Source
const ContextUserKey = 0

ContextUserKey is the key where user sessions are stored in the request context

Variables

This section is empty.

Functions

func DecodeAndVerifyJWT

func DecodeAndVerifyJWT(secret []byte, authToken string) (*types.JWTClaims, error)

DecodeAndVerifyJWT will decode the provided JWT and verify the validity of its claims. If the claims are valid, they are returned, otherwise an error with the reason why they are invalid.

func FilterUserRolesByNames

func FilterUserRolesByNames(roles []*rbacv1.VDIRole, names []string) []*types.VDIUserRole

FilterUserRolesByNames returns a list of UserRoles matching the provided names and clusterw

func GenerateJWT

func GenerateJWT(secret []byte, authResult *types.AuthResult, authorized bool, sessionLength time.Duration) (types.JWTClaims, string, error)

GenerateJWT will create a new JWT with the given user object's fields embedded in the claims.

func GetContainerFromRequest

func GetContainerFromRequest(r *http.Request) string

GetContainerFromRequest returns the container inside the Desktop instance for the given request.

func GetGorillaPath

func GetGorillaPath(r *http.Request) string

GetGorillaPath will retrieve the URL path as it was configured in mux.

func GetNameFromRequest

func GetNameFromRequest(r *http.Request) string

GetNameFromRequest returns the name of the Desktop instance for the given request.

func GetNamespaceFromRequest

func GetNamespaceFromRequest(r *http.Request) string

GetNamespaceFromRequest returns the namespace of the Desktop instance for the given request.

func GetNamespacedNameFromRequest

func GetNamespacedNameFromRequest(r *http.Request) ktypes.NamespacedName

GetNamespacedNameFromRequest returns the namespaced name of the Desktop instance for the given request.

func GetRequestObject

func GetRequestObject(r *http.Request) interface{}

GetRequestObject retrieves the decoded request from the request context.

func GetRequestUserSession

func GetRequestUserSession(r *http.Request) *types.JWTClaims

GetRequestUserSession retrieves the user session from the request context.

func GetRoleFromRequest

func GetRoleFromRequest(r *http.Request) string

GetRoleFromRequest will retrieve the role variable from a request path.

func GetTemplateFromRequest

func GetTemplateFromRequest(r *http.Request) string

GetTemplateFromRequest will retrieve the template variable from a request path.

func GetUserFromRequest

func GetUserFromRequest(r *http.Request) string

GetUserFromRequest will retrieve the user variable from a request path.

func RenderTemplate

func RenderTemplate(tmpl *desktopvsv1.Template, data interface{}) error

RenderTemplate renders the given template with the provided data.

func ReturnAPIError

func ReturnAPIError(err error, w http.ResponseWriter)

ReturnAPIError returns a BadRequest status code with a json encoded error message.

func ReturnAPIErrors

func ReturnAPIErrors(errs []error, w http.ResponseWriter)

ReturnAPIErrors returns a BadRequest status code with a json encoded list of errors.

func ReturnAPIForbidden

func ReturnAPIForbidden(err error, msg string, w http.ResponseWriter)

ReturnAPIForbidden returns a Forbidden status code with a json encoded error message. If the denial happened due to an error, it logs the error server side.

func ReturnAPINotFound

func ReturnAPINotFound(err error, w http.ResponseWriter)

ReturnAPINotFound returns a NotFound status code with a json encoded error message.

func ReturnAPIUnauthorized

func ReturnAPIUnauthorized(err error, msg string, w http.ResponseWriter)

ReturnAPIUnauthorized returns an Unauthorized status with a json encoded error message. If the denial happened due to an error, it is logged server side.

func SetRequestObject

func SetRequestObject(r *http.Request, obj interface{})

SetRequestObject sets the given interface to the decoded request object in the context.

func SetRequestUserSession

func SetRequestUserSession(r *http.Request, sess *types.JWTClaims)

SetRequestUserSession writes the user session to the request context

func UnmarshalRequest

func UnmarshalRequest(r *http.Request, in interface{}) error

UnmarshalRequest will read the body of the given request and decode it into the given interface.

func WriteJSON

func WriteJSON(i interface{}, w http.ResponseWriter)

WriteJSON encodes the provided interface to JSON and writes it to the response stream.

func WriteOK

func WriteOK(w http.ResponseWriter)

WriteOK write a simple boolean okay response.

func WriteOrLogError

func WriteOrLogError(out []byte, w http.ResponseWriter, statusCode int)

WriteOrLogError will write the provided content to the response writer, or log any error. It assumes the content is valid JSON.

Types

type GorillaReadWriter

type GorillaReadWriter struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

GorillaReadWriter implements a wrapper around gorilla websocket connections. It implements a ReadWriter and is used by the kvdi API for copying display/audio connections.

func NewGorillaReadWriter

func NewGorillaReadWriter(conn *websocket.Conn) *GorillaReadWriter

NewGorillaReadWriter returns a new gorilla websocket readwriter.

func (*GorillaReadWriter) Read

func (w *GorillaReadWriter) Read(b []byte) (int, error)

Read implements a Reader.

func (*GorillaReadWriter) Write

func (w *GorillaReadWriter) Write(b []byte) (int, error)

Write implements a Writer.

type WebsocketWatcher

type WebsocketWatcher struct {
	net.Conn
	// contains filtered or unexported fields
}

WebsocketWatcher implements a wrapper around websocket connections, primarily for tracking metrics.

func NewWebsocketWatcher

func NewWebsocketWatcher(c net.Conn) *WebsocketWatcher

NewWebsocketWatcher returns a new websocket watcher.

func (*WebsocketWatcher) BytesRecvdCount

func (w *WebsocketWatcher) BytesRecvdCount() int

BytesRecvdCount returns the total number of bytes read on the connection so far.

func (*WebsocketWatcher) BytesSentCount

func (w *WebsocketWatcher) BytesSentCount() int

BytesSentCount returns the total number of bytes written to the connection so far.

func (*WebsocketWatcher) Hijack

Hijack will hijack the given ResponseWriter. Use `nil` for NewWebsocketWatcher when intending to call this method.

func (*WebsocketWatcher) Read

func (w *WebsocketWatcher) Read(b []byte) (int, error)

Read implements read on the net.Conn interface.

func (*WebsocketWatcher) WithLabels

func (w *WebsocketWatcher) WithLabels(labels map[string]string) *WebsocketWatcher

WithLabels adds the given labels to the prometheus metrics.

func (*WebsocketWatcher) WithMetrics

func (w *WebsocketWatcher) WithMetrics(sendCounter, recvCounter *prometheus.CounterVec) *WebsocketWatcher

WithMetrics applies prometheus counters to the read/write events on the websocket.

func (*WebsocketWatcher) Write

func (w *WebsocketWatcher) Write(b []byte) (int, error)

Write implements write on the net.Conn interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL