Documentation ¶
Overview ¶
Package apiutil contains utility functions for the REST API.
Index ¶
- Constants
- func DecodeAndVerifyJWT(secret []byte, authToken string) (*types.JWTClaims, error)
- func FilterUserRolesByNames(roles []*rbacv1.VDIRole, names []string) []*types.VDIUserRole
- func GenerateJWT(secret []byte, authResult *types.AuthResult, authorized bool, ...) (types.JWTClaims, string, error)
- func GetContainerFromRequest(r *http.Request) string
- func GetGorillaPath(r *http.Request) string
- func GetNameFromRequest(r *http.Request) string
- func GetNamespaceFromRequest(r *http.Request) string
- func GetNamespacedNameFromRequest(r *http.Request) ktypes.NamespacedName
- func GetRequestObject(r *http.Request) interface{}
- func GetRequestUserSession(r *http.Request) *types.JWTClaims
- func GetRoleFromRequest(r *http.Request) string
- func GetTemplateFromRequest(r *http.Request) string
- func GetUserFromRequest(r *http.Request) string
- func RenderTemplate(tmpl *desktopvsv1.Template, data interface{}) error
- func ReturnAPIError(err error, w http.ResponseWriter)
- func ReturnAPIErrors(errs []error, w http.ResponseWriter)
- func ReturnAPIForbidden(err error, msg string, w http.ResponseWriter)
- func ReturnAPINotFound(err error, w http.ResponseWriter)
- func ReturnAPIUnauthorized(err error, msg string, w http.ResponseWriter)
- func SetRequestObject(r *http.Request, obj interface{})
- func SetRequestUserSession(r *http.Request, sess *types.JWTClaims)
- func UnmarshalRequest(r *http.Request, in interface{}) error
- func WriteJSON(i interface{}, w http.ResponseWriter)
- func WriteOK(w http.ResponseWriter)
- func WriteOrLogError(out []byte, w http.ResponseWriter, statusCode int)
- type GorillaReadWriter
- type WebsocketWatcher
- func (w *WebsocketWatcher) BytesRecvdCount() int
- func (w *WebsocketWatcher) BytesSentCount() int
- func (w *WebsocketWatcher) Hijack(writer http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error)
- func (w *WebsocketWatcher) Read(b []byte) (int, error)
- func (w *WebsocketWatcher) WithLabels(labels map[string]string) *WebsocketWatcher
- func (w *WebsocketWatcher) WithMetrics(sendCounter, recvCounter *prometheus.CounterVec) *WebsocketWatcher
- func (w *WebsocketWatcher) Write(b []byte) (int, error)
Constants ¶
const ContextRequestObjectKey = 1
ContextRequestObjectKey is the key where decoded request objects are stored in the request context
const ContextUserKey = 0
ContextUserKey is the key where user sessions are stored in the request context
Variables ¶
This section is empty.
Functions ¶
func DecodeAndVerifyJWT ¶
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 ¶
GetContainerFromRequest returns the container inside the Desktop instance for the given request.
func GetGorillaPath ¶
GetGorillaPath will retrieve the URL path as it was configured in mux.
func GetNameFromRequest ¶
GetNameFromRequest returns the name of the Desktop instance for the given request.
func GetNamespaceFromRequest ¶
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 ¶
GetRequestObject retrieves the decoded request from the request context.
func GetRequestUserSession ¶
GetRequestUserSession retrieves the user session from the request context.
func GetRoleFromRequest ¶
GetRoleFromRequest will retrieve the role variable from a request path.
func GetTemplateFromRequest ¶
GetTemplateFromRequest will retrieve the template variable from a request path.
func GetUserFromRequest ¶
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 ¶
SetRequestObject sets the given interface to the decoded request object in the context.
func SetRequestUserSession ¶
SetRequestUserSession writes the user session to the request context
func UnmarshalRequest ¶
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 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 ¶
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.
type WebsocketWatcher ¶
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 ¶
func (w *WebsocketWatcher) Hijack(writer http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error)
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.