Documentation ¶
Overview ¶
Package rest implements the HTTP server through which plugins can expose their REST APIs to the outside world.
Index ¶
- Constants
- Variables
- func DeclareHTTPPortFlag(pluginName infra.PluginName, defaultPortOpts ...uint)
- func FixConfig(cfg *Config)
- func ListenAndServe(config Config, handler http.Handler) (srv *http.Server, err error)
- func PluginConfig(pluginCfg config.PluginConfig, cfg *Config, pluginName infra.PluginName) error
- type BasicHTTPAuthenticator
- type Config
- type Deps
- type HTTPHandlers
- type HandlerProvider
- type Option
- type Plugin
- func (p *Plugin) AfterInit() (err error)
- func (p *Plugin) Close() error
- func (p *Plugin) GetPort() int
- func (p *Plugin) Init() (err error)
- func (p *Plugin) RegisterHTTPHandler(path string, provider HandlerProvider, methods ...string) *mux.Route
- func (p *Plugin) RegisterPermissionGroup(group ...*access.PermissionGroup)
Constants ¶
const ( // DefaultHost is a host used by default DefaultHost = "0.0.0.0" // DefaultHTTPPort is a port used by default DefaultHTTPPort = "9191" // DefaultEndpoint 0.0.0.0:9191 DefaultEndpoint = DefaultHost + ":" + DefaultHTTPPort )
Variables ¶
var DefaultPlugin = *NewPlugin()
DefaultPlugin is a default instance of Plugin.
Functions ¶
func DeclareHTTPPortFlag ¶ added in v1.0.4
func DeclareHTTPPortFlag(pluginName infra.PluginName, defaultPortOpts ...uint)
DeclareHTTPPortFlag declares http port (with usage & default value) a flag for a particular plugin name
func FixConfig ¶ added in v1.0.4
func FixConfig(cfg *Config)
FixConfig fill default values for empty fields
func ListenAndServe ¶
ListenAndServe starts a http server.
func PluginConfig ¶ added in v1.0.4
func PluginConfig(pluginCfg config.PluginConfig, cfg *Config, pluginName infra.PluginName) error
PluginConfig tries : - to load flag <plugin-name>-port and then FixConfig() just in case - alternatively <plugin-name>-config and then FixConfig() just in case - alternatively DefaultConfig()
Types ¶
type BasicHTTPAuthenticator ¶ added in v1.4.0
type BasicHTTPAuthenticator interface { // Authenticate returns true if user is authenticated successfully, false otherwise. Authenticate(user string, pass string) bool }
BasicHTTPAuthenticator is a delegate that implements basic HTTP authentication
type Config ¶
type Config struct { // Endpoint is an address of HTTP server Endpoint string // ReadTimeout is the maximum duration for reading the entire // request, including the body. // // Because ReadTimeout does not let Handlers make per-request // decisions on each request body's acceptable deadline or // upload rate, most users will prefer to use // ReadHeaderTimeout. It is valid to use them both. ReadTimeout time.Duration // ReadHeaderTimeout is the amount of time allowed to read // request headers. The connection's read deadline is reset // after reading the headers and the Handler can decide what // is considered too slow for the body. ReadHeaderTimeout time.Duration // WriteTimeout is the maximum duration before timing out // writes of the response. It is reset whenever a new // request's header is read. Like ReadTimeout, it does not // let Handlers make decisions on a per-request basis. WriteTimeout time.Duration // IdleTimeout is the maximum amount of time to wait for the // next request when keep-alives are enabled. If IdleTimeout // is zero, the value of ReadTimeout is used. If both are // zero, there is no timeout. IdleTimeout time.Duration // MaxHeaderBytes controls the maximum number of bytes the // server will read parsing the request header's keys and // values, including the request line. It does not limit the // size of the request body. // If zero, DefaultMaxHeaderBytes is used. MaxHeaderBytes int // ServerCertfile is path to the server certificate. If the certificate and corresponding // key (see config item below) is defined server uses HTTPS instead of HTTP. ServerCertfile string `json:"server-cert-file"` // ServerKeyfile is path to the server key file. ServerKeyfile string `json:"server-key-file"` // ClientBasicAuth is a slice of credentials in form "username:password" // used for basic HTTP authentication. If defined only authenticated users are allowed // to access the server. ClientBasicAuth []string `json:"client-basic-auth"` // ClientCerts is a slice of the root certificate authorities // that servers uses to verify a client certificate ClientCerts []string `json:"client-cert-files"` // EnableTokenAuth enables token authorization for HTTP requests EnableTokenAuth bool `json:"enable-token-auth"` // TokenExpiration set globaly for all user tokens TokenExpiration time.Duration `json:"token-expiration"` // Users laoded from config file Users []access.User `json:"users"` // Hash cost for password. High values take a lot of time to process. PasswordHashCost int `json:"password-hash-cost"` // TokenSignature is used to sign a token. Default value is used if not set. TokenSignature string `json:"token-signature"` }
Config is a configuration for HTTP server It is meant to be extended with security (TLS...)
func DefaultConfig ¶ added in v1.0.4
func DefaultConfig() *Config
DefaultConfig returns new instance of config with default endpoint
func (*Config) GetPort ¶ added in v1.0.4
GetPort parses suffix from endpoint & returns integer after last ":" (otherwise it returns 0)
type Deps ¶
type Deps struct { infra.PluginDeps // Authenticator can be injected in a flavor inject method. // If there is no authenticator injected and config contains // user password, the default staticAuthenticator is instantiated. // By default the authenticator is disabled. Authenticator BasicHTTPAuthenticator }
Deps lists the dependencies of the Rest plugin.
type HTTPHandlers ¶
type HTTPHandlers interface { // RegisterHTTPHandler propagates to Gorilla mux RegisterHTTPHandler(path string, provider HandlerProvider, methods ...string) *mux.Route // RegisterPermissionGroup registers new permission groups for users RegisterPermissionGroup(group ...*access.PermissionGroup) // GetPort returns configured port number (for debugging purposes) GetPort() int }
HTTPHandlers defines the API exposed by the REST plugin. Use this interface to declare dependency on the REST functionality, i.e.:
type Deps struct { HTTP rest.HTTPHandlers // inject plugin implementing RegisterHTTPHandler // other dependencies ... }
type HandlerProvider ¶ added in v1.5.0
type HandlerProvider func(formatter *render.Render) http.HandlerFunc
HandlerProvider is a function used for registering handlers via HTTPHandlers
type Option ¶ added in v1.5.0
type Option func(*Plugin)
Option is a function that can be used in NewPlugin to customize Plugin.
func UseAuthenticator ¶ added in v1.5.0
func UseAuthenticator(a BasicHTTPAuthenticator) Option
UseAuthenticator returns an Option which sets HTTP Authenticator.
func UseConf ¶ added in v1.5.0
UseConf returns Option which injects a particular configuration.
type Plugin ¶
Plugin struct holds all plugin-related data.
func NewPlugin ¶ added in v1.5.0
NewPlugin creates a new Plugin with the provided Options.
func (*Plugin) AfterInit ¶
AfterInit starts the HTTP server.
func (*Plugin) GetPort ¶ added in v1.0.4
GetPort returns plugin configuration port
func (*Plugin) Init ¶
Init is the plugin entry point called by Agent Core - It prepares Gorilla MUX HTTP Router
func (*Plugin) RegisterHTTPHandler ¶
func (p *Plugin) RegisterHTTPHandler(path string, provider HandlerProvider, methods ...string) *mux.Route
RegisterHTTPHandler registers HTTP <handler> at the given <path>. Every request is validated if enabled.
func (*Plugin) RegisterPermissionGroup ¶ added in v1.6.0
func (p *Plugin) RegisterPermissionGroup(group ...*access.PermissionGroup)
RegisterPermissionGroup adds new permission group if token authentication is enabled
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package mock implements a mock HTTP server.
|
Package mock implements a mock HTTP server. |
password-hasher
package vpp-agent-ctl implements the vpp-agent-ctl test tool for testing VPP Agent plugins.
|
package vpp-agent-ctl implements the vpp-agent-ctl test tool for testing VPP Agent plugins. |