Documentation
¶
Overview ¶
Package endpoint is the standard form of a loopback data endpoint that a plugin result declares and a host admits or refuses. JSON lines cannot carry continuous data such as media or capture frames, so a plugin points at a local listener instead; this package validates the pointer, ties its lifetime to the plugin session and keeps its secret parts out of errors and logs.
An endpoint value is a credential: whoever can read it and reach the port can use the listener. Every error, String and %v/%+v/%#v rendering from this package omits the address, path and token. JSON encoding carries the real values because the protocol needs them.
Presenting the token. A client presents Endpoint.Token to the listener the same way for every consumer: for ws, wss, http and https, as the header "Authorization: Bearer <token>" on the handshake or request; for tcp, as the first line of the stream, the token followed by one "\n", before any other data. A listener that gets no token, or a wrong one, closes the connection. When Token is empty the listener needs none. The token is never put in a URL. Grant.AuthorizationHeader and Grant.DialTCP present it this way.
Rotation. A grant is bounded by its session and by ExpiresAt. For a listener that outlives one expiry the plugin announces a replacement endpoint and the host Admits it and then Revokes the old grant. Announcing is the consumer's contract (a reverse operation, conventionally named "endpoint.rotate", whose payload is {"endpoint": <Endpoint>}); the library only validates and admits.
Index ¶
- Constants
- Variables
- type Endpoint
- type Grant
- func (g *Grant) Active() bool
- func (g *Grant) AuthorizationHeader() (string, error)
- func (g *Grant) DialTCP(ctx context.Context) (net.Conn, error)
- func (g *Grant) Format(f fmt.State, _ rune)
- func (g *Grant) GoString() string
- func (g *Grant) Revoke()
- func (g *Grant) Scheme() string
- func (g *Grant) String() string
- func (g *Grant) Target() (scheme, address, path string, err error)
- func (g *Grant) Token() (string, error)
- type Lifetime
- type Policy
Constants ¶
const ( MaxAddressBytes = 255 MaxPathBytes = 1024 MaxTokenBytes = 512 // DefaultMaxLifetime applies when a Policy sets none. DefaultMaxLifetime = time.Hour )
Bounds on a declared endpoint.
const Key = "endpoint"
Key is the conventional JSON key under which a result carries an Endpoint: {"endpoint": {...}}. A contract may nest it elsewhere; Decode reads the object.
Variables ¶
var ( // ErrUsed is returned when a one-time token is read twice. ErrUsed = errors.New("plugin endpoint token already used") // ErrExpired is returned for a grant that was revoked, whose session closed // or whose time ran out. ErrExpired = errors.New("plugin endpoint expired") )
var ErrDial = errors.New("plugin endpoint connection failed")
ErrDial is returned when DialTCP cannot connect or present the token. It carries no detail: the system's own dial errors name the address, which is as secret as the token.
Functions ¶
This section is empty.
Types ¶
type Endpoint ¶
type Endpoint struct {
// Scheme is one of ws, wss, http, https or tcp, and must be allowed by the
// host's Policy.
Scheme string `json:"scheme"`
// Address is a loopback "host:port": "localhost", a loopback IP literal, or
// a bracketed IPv6 loopback literal.
Address string `json:"address"`
// Path is optional and begins with "/". It may hold a secret identifier.
Path string `json:"path,omitempty"`
// Token is an optional secret the host presents to the listener. It is
// reusable while the grant is active unless OneTime is set.
Token string `json:"token,omitempty"`
// OneTime makes the token single-use: the first Grant.Token succeeds and
// later calls return ErrUsed. It limits how often the host may read the
// token, not how many connections the listener accepts: a listener that wants
// one attachment enforces that itself.
OneTime bool `json:"oneTime,omitempty"`
// ExpiresAt bounds the endpoint's life. It is required.
ExpiresAt time.Time `json:"expiresAt"`
}
Endpoint is a loopback listener declared by a plugin.
func Decode ¶
func Decode(raw json.RawMessage) (Endpoint, error)
Decode reads one Endpoint from JSON, rejecting unknown fields, duplicate keys and anything Check refuses. It applies no host policy: call Admit (or Validate) for that. An error never includes a value from the input.
func (Endpoint) Check ¶
Check validates the endpoint's shape, which no policy changes: a supported scheme, a loopback host with a port, a well-formed path and token, and an expiry. Its errors never include a value from the endpoint.
type Grant ¶
type Grant struct {
// contains filtered or unexported fields
}
Grant is an admitted endpoint. It stays usable until the session ends or the endpoint expires, whichever is first.
func Admit ¶
Admit validates the endpoint and returns a Grant tied to the session. A host that wants to refuse an endpoint returns an error before calling Admit.
func (*Grant) AuthorizationHeader ¶
AuthorizationHeader returns the value for the Authorization header that a client of a ws, wss, http or https endpoint sends ("Bearer <token>"), per the convention in the package documentation. It returns an empty string when the endpoint has no token, and ErrInvalid for a tcp endpoint, which presents its token as the first line instead (see DialTCP). It reads the token, so a one-time endpoint allows one call; an ended grant returns ErrExpired.
func (*Grant) DialTCP ¶
DialTCP connects to a tcp endpoint and presents its token as the first line, the token followed by one "\n", before any other data, per the convention in the package documentation. An endpoint without a token sends nothing. The caller owns the returned connection and the grant's lifetime still applies: an ended grant returns ErrExpired, a one-time token is read only once, and a cancelled or expired ctx returns ctx.Err().
func (*Grant) Revoke ¶
func (g *Grant) Revoke()
Revoke ends the grant now; it is how a host retires an endpoint it replaced. It is safe to call more than once.
func (*Grant) Target ¶
Target returns the scheme, address and path without the token, or ErrExpired. The result is secret: do not log it.
type Lifetime ¶
type Lifetime interface{ Done() <-chan struct{} }
Lifetime is anything that ends: a plugin session, normally.
type Policy ¶
type Policy struct {
// Schemes lists the admitted schemes. It is required: an empty list admits
// nothing.
Schemes []string
// MaxLifetime bounds ExpiresAt from now; zero uses DefaultMaxLifetime.
MaxLifetime time.Duration
// Now overrides the clock for tests.
Now func() time.Time
}
Policy is the host's decision about what it will admit.