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
- func UserName(r *http.Request) string
- 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 )
const ( HeaderKeyAuthUsername = "X-Ligato-Auth-Username" HeaderKeyRateLimitLimit = "X-Ligato-RateLimiter-Limit" HeaderKeyRateLimitBurst = "X-Ligato-RateLimiter-MaxBurst" HeaderKeyRateLimitDelay = "X-Ligato-RateLimiter-WaitDelay" )
Variables ¶
var DefaultPlugin = *NewPlugin()
DefaultPlugin is a default instance of Plugin.
Functions ¶
func DeclareHTTPPortFlag ¶
func DeclareHTTPPortFlag(pluginName infra.PluginName, defaultPortOpts ...uint)
DeclareHTTPPortFlag declares http port (with usage & default value) a flag for a particular plugin name
func ListenAndServe ¶
ListenAndServe starts a http server.
func PluginConfig ¶
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 ¶
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 {
// Disabled disables HTTP server.
Disabled bool
// 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"`
// SignKey is used to sign a token. Default value is used if not set.
SignKey string `json:"sign-key"`
RateLimiter *struct {
// Limit defines rate limit for number of requests per second.
Limit float64 `json:"limit"`
// MaxBurst defines max number of requests in single burst.
MaxBurst int `json:"burst"`
} `json:"rate-limiter"`
}
Config is a configuration for HTTP server It is meant to be extended with security (TLS...)
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns new instance of config with default endpoint
type Deps ¶
type Deps struct {
infra.PluginDeps
// Authenticator is used for authenticating requests.
// 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 ¶
type HandlerProvider func(formatter *render.Render) http.HandlerFunc
HandlerProvider is a function used for registering handlers via HTTPHandlers
type Option ¶
type Option func(*Plugin)
Option is a function that can be used in NewPlugin to customize Plugin.
func UseAuthenticator ¶
func UseAuthenticator(a BasicHTTPAuthenticator) Option
UseAuthenticator returns an Option which sets HTTP Authenticator.
type Plugin ¶
Plugin struct holds all plugin-related data.
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 ¶
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
command
|
