Documentation
¶
Overview ¶
Goexpose is lightweight json server that can map url paths to various tasks. Main idea is to have possibility to call shell scripts by http request. These shell scripts can consume mux vars from url(gorilla mux is used). You should be very careful how you construct your regular expressions, so you don't open doors to shell injection. Goexpose supports authorization system (right now only basic auth is supported). Goexpose can be run on https so if you combine https with strong password, you should be not vulnerable.
Various http helpers and utilities
Index ¶
- Constants
- Variables
- func AddFormat(format, id string) (result string)
- func AuthorizerExists(id string) (ok bool)
- func Format(body string, f string) (result interface{}, format string, err error)
- func FormatJSON(body string) (result interface{}, err error)
- func FormatJSONLines(body string) (result interface{}, err error)
- func FormatLines(body string) (result interface{}, err error)
- func FormatText(body string) (result interface{}, err error)
- func HasFormat(format, id string) bool
- func Interpolate(strTemplate string, data map[string]interface{}) (result string, err error)
- func MethodAllowed(method string, avail []string) bool
- func RegisterAuthorizer(id string, factory AuthorizerFactory)
- func RegisterFormat(id string, fn FormatFunc)
- func RegisterTaskFactory(id string, factory TaskFactory)
- func RenderTemplate(tpl *template.Template, data map[string]interface{}) (result string, err error)
- func VerifyFormat(format string) (result string, err error)
- type Authorizer
- type AuthorizerConfig
- type AuthorizerFactory
- type Authorizers
- type BasicAuthorizer
- type BasicAuthorizerConfig
- type CassandraTask
- type CassandraTaskConfig
- type CassandraTaskConfigQuery
- type Config
- type EndpointConfig
- type FilesystemConfig
- type FilesystemTask
- type FormatFunc
- type HttpAuthorizer
- type HttpAuthorizerConfig
- type HttpTask
- type HttpTaskConfig
- type HttpTaskConfigURL
- type InfoTask
- type LDAPAuthorizer
- type LDAPAuthorizerConfig
- type MultiTask
- type MultiTaskConfig
- type MySQLTask
- type MySQLTaskConfig
- type MySQLTaskConfigQuery
- type PostgresTask
- type PostgresTaskConfig
- type PostgresTaskConfigQuery
- type QueryParams
- type QueryParamsConfigParam
- type RedisTask
- type RedisTaskConfig
- type RedisTaskConfigQuery
- type Requester
- type RequesterSetFunc
- type Response
- func (r *Response) AddValue(key string, value interface{}) *Response
- func (r *Response) DelValue(key string) *Response
- func (r *Response) Error(err interface{}) *Response
- func (r *Response) GetStatus() int
- func (r *Response) HasValue(key string) bool
- func (r *Response) MarshalJSON() (result []byte, err error)
- func (r *Response) Pretty(pretty bool) *Response
- func (r *Response) Raw(raw interface{}) *Response
- func (r *Response) Result(result interface{}) *Response
- func (r *Response) Status(status int) *Response
- func (r *Response) StripStatusData() *Response
- func (r *Response) UpdateStatusData() *Response
- func (r *Response) Write(w http.ResponseWriter, req *http.Request, start ...time.Time) (err error)
- type SSLConfig
- type Server
- func (s *Server) GetEnv() map[string]interface{}
- func (s *Server) GetQueryParams(r *http.Request, ec *EndpointConfig) (result map[string]string)
- func (s *Server) Handle(task Tasker, authorizers Authorizers, ec *EndpointConfig, tc *TaskConfig) http.HandlerFunc
- func (s *Server) NotFoundHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) Run() (err error)
- type ShellTask
- type ShellTaskConfig
- type ShellTaskConfigCommand
- type Task
- type TaskConfig
- type TaskFactory
- type Tasker
- func CassandraTaskFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
- func FilesystemFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
- func HttpTaskFactory(server *Server, tc *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
- func InfoTaskFactory(server *Server, taskconfig *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
- func MultiTaskFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
- func MySQLTaskFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
- func PostgresTaskFactory(server *Server, tc *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
- func RedisTaskFactory(server *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
- func ShellTaskFactory(server *Server, taskconfig *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
Constants ¶
const ( LDAP_DEFAULT_HOST = "localhost" LDAP_DEFAULT_PORT = 389 LDAP_DEFAULT_NETWORK = "tcp" )
const (
DEFAULT_TIMEOUT = 10 * time.Second
)
Requester makes http requests
const (
VERSION = "1.0.0"
)
Variables ¶
var ( ErrBlacklisted = errors.New("user is blacklisted") ErrNotWhitelisted = errors.New("user is not whitelisted") ErrBlacklistWhitelistProvided = errors.New("blacklist and whitelist set, that doesn't make sense.") ErrUnknownNetwork = errors.New("unknown network") ErrURLInvalidTemplate = errors.New("url is invalid template") )
var (
ErrInvalidAuthorizationHeader = errors.New("invalid authorization header")
)
Functions ¶
func AuthorizerExists ¶
AuthorizerExists returns if exists authorizer by given id
func FormatJSON ¶
Formats body as json (map[string]interface{})
func FormatJSONLines ¶
Formats body as json lines
func FormatLines ¶
Formats body as lines of text (delimited by \n)
func FormatText ¶
Text format just returns body
func RegisterAuthorizer ¶
func RegisterAuthorizer(id string, factory AuthorizerFactory)
Register authorizer
func RegisterTaskFactory ¶
func RegisterTaskFactory(id string, factory TaskFactory)
Registers task factory to server
func VerifyFormat ¶
Verify given format
format can be multiple formats separated by "|". if text is not found in format it is automatically added.
Types ¶
type Authorizer ¶
Authorizer implements authorization
func BasicAuthorizerFactory ¶
func BasicAuthorizerFactory(ac *AuthorizerConfig) (result Authorizer, err error)
func HttpAuthorizerFactory ¶
func HttpAuthorizerFactory(ac *AuthorizerConfig) (result Authorizer, err error)
func LDAPAuthorizerFactory ¶
func LDAPAuthorizerFactory(ac *AuthorizerConfig) (result Authorizer, err error)
type AuthorizerConfig ¶
type AuthorizerConfig struct { Type string `json:"type"` Config json.RawMessage `json:"config"` }
Configuration for authorizer
type AuthorizerFactory ¶
type AuthorizerFactory func(config *AuthorizerConfig) (Authorizer, error)
AuthFactory returns new authorizer
type Authorizers ¶
type Authorizers map[string]Authorizer
Authorizers will have method that will check all authorizers
func GetAuthorizers ¶
func GetAuthorizers(config *Config) (result Authorizers, err error)
Returns authorizers for given config First step is that it validates authorizers
func (Authorizers) Authorize ¶
func (a Authorizers) Authorize(r *http.Request, config *EndpointConfig) (err error)
Try all authorizers, first that will fail with error, that error will be returned
type BasicAuthorizer ¶
type BasicAuthorizer struct {
// contains filtered or unexported fields
}
Basic auth provides method GetBasicAuth from request headers
func (*BasicAuthorizer) Authorize ¶
func (b *BasicAuthorizer) Authorize(r *http.Request) (err error)
Check username and password
func (*BasicAuthorizer) GetBasicAuth ¶
func (a *BasicAuthorizer) GetBasicAuth(r *http.Request) (username, password string, err error)
Return username and password
type BasicAuthorizerConfig ¶
type CassandraTask ¶
type CassandraTask struct { Task // contains filtered or unexported fields }
Cassandra task to run queries on cassandra
type CassandraTaskConfig ¶
type CassandraTaskConfig struct { Queries []CassandraTaskConfigQuery `json:"queries"` ReturnQueries bool `json:"return_queries"` SingleResult *int `json:"single_result"` // contains filtered or unexported fields }
func (*CassandraTaskConfig) Validate ¶
func (c *CassandraTaskConfig) Validate() (err error)
Validate config
type CassandraTaskConfigQuery ¶
type CassandraTaskConfigQuery struct { Cluster []string `json:"cluster"` Keyspace string `json:"keyspace"` Query string `json:"query"` Args []string `json:"args"` }
Config for Query
func (*CassandraTaskConfigQuery) Validate ¶
func (c *CassandraTaskConfigQuery) Validate() (err error)
Validate query config
type Config ¶
type Config struct { Host string `json:"host"` Port int `json:"port"` SSL *SSLConfig `json:"ssl"` PrettyJson bool `json:"pretty_json"` Authorizers map[string]*AuthorizerConfig `json:"authorizers"` Endpoints []*EndpointConfig `json:"endpoints"` ReloadEnv bool `json:"reload_env"` Directory string `json:"-"` }
Main config
func NewConfigFromFilename ¶
Returns filename from file
type EndpointConfig ¶
type EndpointConfig struct { Authorizers []string `json:"authorizers"` Path string `json:"path"` Methods map[string]TaskConfig `json:"methods"` Type string `json:"type"` QueryParams *QueryParams `json:"query_params"` RawResponse bool `json:"raw_response"` }
func (*EndpointConfig) RouteName ¶
func (e *EndpointConfig) RouteName() string
func (*EndpointConfig) Validate ¶
func (e *EndpointConfig) Validate() (err error)
type FilesystemConfig ¶
type FilesystemConfig struct { File string `json:"file"` Output string `json:"output"` Directory string `json:"directory"` Index bool `json:"index"` }
func NewFilesystemConfig ¶
func NewFilesystemConfig() *FilesystemConfig
func (*FilesystemConfig) Validate ¶
func (f *FilesystemConfig) Validate() (err error)
type FilesystemTask ¶
type FilesystemTask struct { Task // contains filtered or unexported fields }
FilesystemTask
serve single file
type HttpAuthorizer ¶
type HttpAuthorizer struct {
// contains filtered or unexported fields
}
HttpAuthorizer implementation
type HttpAuthorizerConfig ¶
type HttpAuthorizerConfig struct { URL string `json:"url"` Data string `json:"data"` Method string `json:"method"` }
HttpAuthorizerConfig implementation
configuration for HttpAuthorizer
func NewHttpAuthorizerConfig ¶
func NewHttpAuthorizerConfig(ac *AuthorizerConfig) (hac *HttpAuthorizerConfig, err error)
func (*HttpAuthorizerConfig) RenderData ¶
func (h *HttpAuthorizerConfig) RenderData(data map[string]interface{}) (result string, err error)
func (*HttpAuthorizerConfig) RenderMethod ¶
func (h *HttpAuthorizerConfig) RenderMethod(data map[string]interface{}) (result string, err error)
type HttpTask ¶
type HttpTask struct { Task // contains filtered or unexported fields }
HttpTask
task that can make requests to given urls
type HttpTaskConfig ¶
type HttpTaskConfig struct { URLs []*HttpTaskConfigURL `json:"urls"` SingleResult *int `json:"single_result"` // contains filtered or unexported fields }
type HttpTaskConfigURL ¶
type InfoTask ¶
type InfoTask struct { Task // contains filtered or unexported fields }
InfoTask - information about goexpose server
type LDAPAuthorizer ¶
type LDAPAuthorizer struct {
// contains filtered or unexported fields
}
LDAPAuthorizer Main ldap authorizer implementation
type LDAPAuthorizerConfig ¶
type LDAPAuthorizerConfig struct { Host string `json:"host"` Port int `json:"port"` Network string `json:"network"` Whitelist []string `json:"whitelist"` Blacklist []string `json:"blacklist"` }
func (*LDAPAuthorizerConfig) Validate ¶
func (l *LDAPAuthorizerConfig) Validate() (err error)
Validate configuration
type MultiTask ¶
type MultiTask struct { Task // contains filtered or unexported fields }
Multi task imlpementation
type MultiTaskConfig ¶
type MultiTaskConfig struct { Tasks []*TaskConfig `json:"tasks"` SingleResult *int `json:"single_result"` // contains filtered or unexported fields }
func (*MultiTaskConfig) Validate ¶
func (m *MultiTaskConfig) Validate() (err error)
type MySQLTask ¶
type MySQLTask struct { Task // contains filtered or unexported fields }
MySQL task imlpementation
type MySQLTaskConfig ¶
type MySQLTaskConfig struct { ReturnQueries bool `json:"return_queries"` Queries []*MySQLTaskConfigQuery `json:"queries"` SingleResult *int `json:"single_result"` // contains filtered or unexported fields }
func (*MySQLTaskConfig) Validate ¶
func (m *MySQLTaskConfig) Validate() (err error)
Validate mysql config
type MySQLTaskConfigQuery ¶
type MySQLTaskConfigQuery struct { URL string `json:"url"` Query string `json:"query"` Args []string `json:"args"` }
Configuration for single query
func (*MySQLTaskConfigQuery) Validate ¶
func (m *MySQLTaskConfigQuery) Validate() (err error)
type PostgresTask ¶
type PostgresTask struct { Task // contains filtered or unexported fields }
Postgres task
type PostgresTaskConfig ¶
type PostgresTaskConfig struct { Queries []*PostgresTaskConfigQuery `json:"queries"` ReturnQueries bool `json:"return_queries"` SingleResult *int `json:"single_result"` // contains filtered or unexported fields }
func (*PostgresTaskConfig) Validate ¶
func (p *PostgresTaskConfig) Validate() (err error)
type PostgresTaskConfigQuery ¶
type QueryParams ¶
type QueryParams struct { ReturnParams bool `json:"return_params"` Params []*QueryParamsConfigParam `json:"params"` }
func (*QueryParams) GetParams ¶
func (q *QueryParams) GetParams(r *http.Request) (result map[string]string)
Returns params from request
func (*QueryParams) Validate ¶
func (q *QueryParams) Validate() (err error)
type QueryParamsConfigParam ¶
type QueryParamsConfigParam struct { Name string `json:"name"` Regexp string `json:"regexp"` Default string `json:"default"` // contains filtered or unexported fields }
Param config
type RedisTask ¶
type RedisTask struct { Task // contains filtered or unexported fields }
func (*RedisTask) GetReply ¶
func (r *RedisTask) GetReply(reply interface{}, query RedisTaskConfigQuery) (interface{}, error)
type RedisTaskConfig ¶
type RedisTaskConfig struct { Address string `json:"address"` Database int `json:"database"` Network string `json:"network"` Queries []RedisTaskConfigQuery `json:"queries"` ReturnQueries bool `json:"return_queries"` SingleResult *int `json:"single_result"` // contains filtered or unexported fields }
func (*RedisTaskConfig) Validate ¶
func (r *RedisTaskConfig) Validate() (err error)
type RedisTaskConfigQuery ¶
type RedisTaskConfigQuery struct { Command string `json:"command"` Args []string `json:"args"` Type string `json:"type"` }
func (*RedisTaskConfigQuery) Validate ¶
func (r *RedisTaskConfigQuery) Validate() (err error)
type Requester ¶
type Requester struct {
// contains filtered or unexported fields
}
Making requests
func NewRequester ¶
func NewRequester(funcs ...RequesterSetFunc) (result *Requester)
NewRequester returns new requester instance
func (*Requester) DoNew ¶
func (r *Requester) DoNew(method string, url string, body io.Reader) (req *http.Request, resp *http.Response, err error)
DoNew creates new request and sends it
func (*Requester) Set ¶
func (r *Requester) Set(funcs ...RequesterSetFunc) *Requester
With is used to change values directly from constructors
type RequesterSetFunc ¶
type RequesterSetFunc func(r *Requester)
RequesterSetFunc is callback function to be called in Set method.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response
func (*Response) Error ¶
Error method adds error, it's just a shorthand to AddValue("error", err)
@TODO: store just string from error
func (*Response) MarshalJSON ¶
Marshaler interface support json marshalling
func (*Response) Raw ¶
Set raw response param can be following:
nil => clear raw raw is fmt.Stringer or string => convert to []byte []byte leave as it is otherwise try to marshal to json []byte
func (*Response) Result ¶
Result method adds result, it's just a shorthand to AddValue("result", result)
func (*Response) StripStatusData ¶
Strips status/message from data
func (*Response) UpdateStatusData ¶
Updates stripped status data
type Server ¶
type Server struct { // config instance Config *Config // Version Version string // Router Router *mux.Router }
Goexpose server
func (*Server) GetQueryParams ¶
Returns
func (*Server) Handle ¶
func (s *Server) Handle(task Tasker, authorizers Authorizers, ec *EndpointConfig, tc *TaskConfig) http.HandlerFunc
Handle func
func (*Server) NotFoundHandler ¶
func (s *Server) NotFoundHandler(w http.ResponseWriter, r *http.Request)
Handler for not found
type ShellTask ¶
type ShellTask struct { Task // config Config *ShellTaskConfig }
ShellTask runs shell commands
type ShellTaskConfig ¶
type ShellTaskConfig struct { // Custom environment variables Env map[string]string `json:"env"` Shell string `json:"shell"` Commands []*ShellTaskConfigCommand `json:"commands"` SingleResult *int `json:"single_result"` // contains filtered or unexported fields }
Config for shell task
func NewShellTaskConfig ¶
func NewShellTaskConfig() *ShellTaskConfig
func (*ShellTaskConfig) Validate ¶
func (s *ShellTaskConfig) Validate() (err error)
Validate validates config
type ShellTaskConfigCommand ¶
type ShellTaskConfigCommand struct { Command string `json:"command"` Chdir string `json:"chdir"` Format string `json:"format"` ReturnCommand bool `json:"return_command"` }
func (*ShellTaskConfigCommand) Validate ¶
func (s *ShellTaskConfigCommand) Validate() (err error)
type TaskConfig ¶
type TaskConfig struct { Type string `json:"type"` Authorizers []string `json:"authorizers"` Config json.RawMessage `json:"config"` QueryParams *QueryParams `json:"query_params"` Description string `json:"description"` }
Task config
func (*TaskConfig) Validate ¶
func (t *TaskConfig) Validate() (err error)
Validate method validates task config
type TaskFactory ¶
type TaskFactory func(server *Server, config *TaskConfig, ec *EndpointConfig) ([]Tasker, error)
TaskFactory returns instance of task by server and config
type Tasker ¶
type Tasker interface { // Returns path for task Path() string // Run method is called on http request Run(r *http.Request, vars map[string]interface{}) *Response }
Tasker interface Main task
func CassandraTaskFactory ¶
func CassandraTaskFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
func FilesystemFactory ¶
func FilesystemFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
Factory to create filesystem tasks
func HttpTaskFactory ¶
func HttpTaskFactory(server *Server, tc *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
HttpTaskFactory - factory to create HttpTasks
func InfoTaskFactory ¶
func InfoTaskFactory(server *Server, taskconfig *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
Factory for InfoTask task
func MultiTaskFactory ¶
func MultiTaskFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
Factory to create task
func MySQLTaskFactory ¶
func MySQLTaskFactory(s *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
Factory to create task
func PostgresTaskFactory ¶
func PostgresTaskFactory(server *Server, tc *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
func RedisTaskFactory ¶
func RedisTaskFactory(server *Server, tc *TaskConfig, ec *EndpointConfig) (result []Tasker, err error)
Factory to create task instances
func ShellTaskFactory ¶
func ShellTaskFactory(server *Server, taskconfig *TaskConfig, ec *EndpointConfig) (tasks []Tasker, err error)
Factory for ShellTask