Documentation
¶
Overview ¶
Package script is the script plugin: it runs every executable file in a folder as a pipeline step, so a user can customize Cortex with a small Python or shell script instead of a Go plugin and a rebuild.
One pipeline entry, `script`, is one slot: the files directly in its folder (default ~/.cortex/plugins), in file-name order. Each file is a long-lived worker process (see internal/worker) that answers one JSON message per hook. Repeating `script` with a different `dir` puts a second slot elsewhere in the chain, which is how a Go plugin sits between two scripts.
A script declares, in its handshake, which hooks it implements and whether it enforces. It starts in shadow mode — its denies and edits are recorded and not applied — so trying a script cannot break a request. A script that errors (a crash, a timeout, a malformed reply) or does not start lets the request through unless its slot is fail_closed.
Scripts run only on a local install (plugins.Deps.LocalInstall). Anywhere else a `script` entry loads nothing, so a config that names one cannot run commands in a sidecar.
Index ¶
- Constants
- func Fingerprint(dir string) string
- func SlotDir(dir string) (string, error)
- type Script
- func (s *Script) Capabilities() pipeline.PluginCapabilities
- func (s *Script) ConfigSchema() []pipeline.FieldSchema
- func (s *Script) Configure(raw json.RawMessage) error
- func (s *Script) Init(context.Context) error
- func (s *Script) Metrics() []pipeline.Metric
- func (s *Script) Name() string
- func (s *Script) OnRequest(ctx context.Context, pctx *pipeline.Context) pipeline.Action
- func (s *Script) OnResponse(ctx context.Context, pctx *pipeline.Context) pipeline.Action
- func (s *Script) OnResponseFrame(ctx context.Context, pctx *pipeline.Context, frame []byte, last bool) pipeline.Action
- func (s *Script) SetLocalInstall(local bool)
- func (s *Script) Shutdown(context.Context) error
Constants ¶
const Name = "script"
Name is the plugin's registered name.
Variables ¶
This section is empty.
Functions ¶
func Fingerprint ¶
Fingerprint is a digest of every script directly in dir — names, order and contents — so the reloader can tell that a slot changed even when the config file did not. Empty for an empty or missing directory.
Types ¶
type Script ¶
type Script struct {
// contains filtered or unexported fields
}
Script is the plugin.
func (*Script) Capabilities ¶
func (s *Script) Capabilities() pipeline.PluginCapabilities
Capabilities is the ceiling until Configure, and after it what the slot is wherever scripts run: a request writer, empty or not. The response side is what still narrows to what the loaded scripts asked for — reading or rewriting responses — so a slot of request scripts does not cost every streamed response its streaming.
func (*Script) ConfigSchema ¶
func (s *Script) ConfigSchema() []pipeline.FieldSchema
func (*Script) Configure ¶
func (s *Script) Configure(raw json.RawMessage) error
Configure loads the slot's scripts and starts their workers. A script that will not start is skipped with a WARN, never failing the build: one broken file must not stop the proxy from starting or a config edit from applying. A fail_closed slot remembers it and refuses requests until a build in which it starts; see notStarted.
func (*Script) Init ¶
Init runs when this build's pipeline starts: from here its workers are the ones traffic uses, and anything an abandoned build left in the pool can go.
func (*Script) OnRequest ¶
OnRequest runs every loaded script's request hook, in order. Each sees the request as the scripts before it left it, except that a shadow script's edits are not applied and so are not seen.
func (*Script) OnResponse ¶
OnResponse is never called: the pipeline sends a StreamingResponder's responses to OnResponseFrame instead, buffered or streamed. It exists for the Plugin interface, and runs the hooks should a caller use it directly.
func (*Script) OnResponseFrame ¶
func (s *Script) OnResponseFrame(ctx context.Context, pctx *pipeline.Context, frame []byte, last bool) pipeline.Action
OnResponseFrame is how responses reach scripts. A buffered response arrives as one last frame with the whole body; a streamed one as each SSE event's payload, then an empty last frame — by then every byte is with the client, so the scripts read it and cannot change or block it. A script that rewrites responses narrows this plugin to WritesResponseBody, which sends streamed responses down the buffered path, where its edits apply.
func (*Script) SetLocalInstall ¶
SetLocalInstall is how plugins.BuildWithDeps tells the plugin it may run scripts.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package catalog is the script plugins Cortex ships: each folder under entries/ is one entry, which installed is one slot.
|
Package catalog is the script plugins Cortex ships: each folder under entries/ is one entry, which installed is one slot. |
|
internal
|
|
|
worker
Package worker runs one script as a long-lived child process and exchanges line-delimited JSON with it: one object per line on the child's stdin, one per line back on its stdout.
|
Package worker runs one script as a long-lived child process and exchanges line-delimited JSON with it: one object per line on the child's stdin, one per line back on its stdout. |