Documentation
¶
Overview ¶
Package tunnelproto is the wire protocol between the on-cluster CubeCOS AI Advisor agent and the Bigstack SaaS.
It lives in this public repository, and the private SaaS imports it. The dependency direction is deliberate and one-way: nothing public depends on anything private, so the component that holds console access on a customer's cluster stays auditable by that customer.
Shape ¶
The agent dials out (wss over 443) and the SaaS multiplexes channels back over the single stream. There are no inbound rules and no VPN. Three channel kinds exist:
ChannelCtl — enrollment, heartbeat, kill switch ChannelTool — AI plane: read-only tool calls, sandboxed agent-side ChannelConsole — human plane: recorded PTY / web proxy; the AI has no path here
The property this package exists to guarantee ¶
A channel-open names a *symbolic target* — never an address. The protocol cannot express "connect to 10.0.0.5:22", so a compromised or prompt-injected SaaS cannot use the tunnel to reach arbitrary hosts on the customer's network. The agent resolves symbolic targets against its own local allowlist. Everything in Target below serves that one guarantee.
Index ¶
- Constants
- func ReadToolArgs(r io.Reader) (map[string]string, error)
- func WriteToolArgs(w io.Writer, args map[string]string) error
- func WriteToolResult(w io.Writer, r ToolResult) error
- type ChannelKind
- type ChannelOpen
- type Hello
- type HelloAck
- type InstanceProfile
- type KillSwitch
- type Target
- type TargetKind
- type ToolResult
Constants ¶
const DescribeInstanceProfile = "describe_instance_profile"
DescribeInstanceProfile is the tool the SaaS calls to learn what a create would make, so the sentence a person approves can name it.
It is not offered to the model. The model already cannot choose a flavour or an image — that is what the profile is for — so advertising a tool that reports them would widen what the model sees without widening what it can do.
const MaxTargetNameLen = 64
MaxTargetNameLen bounds a name so a peer cannot use it as a data channel.
const MaxToolArgsBytes = 8 << 10
MaxToolArgsBytes bounds the argument frame. The receiving registry rejects unknown arguments anyway; this stops a peer making the agent buffer first.
const MaxToolOutputBytes = 1 << 20
MaxToolOutputBytes bounds a result frame. Generous next to `cluster check` output, small next to memory exhaustion from a hostile or broken peer.
const MinSupportedVersion = 1
MinSupportedVersion is the oldest protocol version a peer may speak and still be served. The window is a policy choice: enterprises that forbid self-updating privileged daemons pin their agents deliberately, and the window is what makes that supportable rather than a bug.
const RefusedAtLevelReason = "refused: this cluster's action level does not serve tools that change it"
RefusedAtLevelReason is the one refusal that is allowed to say why.
A cluster's action level (ADR 0011) is policy its own operator wrote into a file on the node, not a fact about the allowlist. Saying "this cluster does not serve tools that change it" tells a caller nothing it could not read on the cluster itself, and names no tool, no argument and no value set — so the oracle argument behind RefusedReason does not reach it. Withholding it costs something real instead: an operator who raised a level on the wrong cluster, or a SaaS whose mirror is stale, sees a bare "refused" and cannot tell a policy decision from a broken tool.
This is the only exception, and it stays one by being a distinct constant rather than a format string: there is no room in it for a name or a value.
const RefusedReason = "refused"
RefusedReason is the single reason string a refused call returns.
Every refusal looks identical to the SaaS; the specific cause goes to the customer's local audit log, where it belongs. A precise refusal is a probe oracle — it would let a caller map the allowlist by reading error text.
const Version = 1
Version is the protocol version this build speaks.
Bump it only for a breaking change. Additive changes — a new channel kind, a new target kind — do not bump it, because the SaaS must keep serving the prior version while pinned clusters exist and every bump widens that window.
Variables ¶
This section is empty.
Functions ¶
func ReadToolArgs ¶
ReadToolArgs reads the argument frame: exactly one newline-terminated JSON object, `{}` when the tool takes none.
Line-framed rather than read-to-EOF, and the reason is concrete: a channel is a yamux stream, and yamux streams have no half-close. A caller cannot signal "I have finished sending arguments" without closing the stream it is about to read the result from, so reading to EOF here would deadlock every call. The framing matches the channel-open header for the same reason.
func WriteToolArgs ¶
WriteToolArgs writes the argument frame: one newline-terminated JSON object.
Arguments are strings, not arbitrary JSON. A tool argument that reaches a cluster is validated against an exact-match allowlist, and a type the allowlist cannot express is one it cannot check.
func WriteToolResult ¶
func WriteToolResult(w io.Writer, r ToolResult) error
WriteToolResult writes the result frame.
Types ¶
type ChannelKind ¶
type ChannelKind uint8
ChannelKind discriminates multiplexed streams on one tunnel.
const ( ChannelCtl ChannelKind = iota ChannelTool ChannelConsole )
func (ChannelKind) String ¶
func (k ChannelKind) String() string
func (ChannelKind) Valid ¶
func (k ChannelKind) Valid() bool
Valid reports whether k is a channel kind this build understands. An unknown kind from a newer peer is refused rather than guessed at.
type ChannelOpen ¶
type ChannelOpen struct {
ID uint32 `json:"id"`
Kind ChannelKind `json:"kind"`
Target Target `json:"target"`
// Approved is the SaaS's claim that a person agreed to this call
// (ADR 0011's consent dial, amended).
//
// A claim, not a proof: it arrives over the tunnel, so a compromised SaaS
// can set it on a call nobody saw. The action level is the control that
// survives that, because the agent answers it from its own state. This
// carries the honest failure instead — a stale mirror or a bug skipping
// the question — and makes it a refusal the agent audits rather than an
// unattended call nobody notices.
//
// Absent means false, and false is the safe reading: an older SaaS that
// never sets it has its configuring calls refused by a cluster whose own
// dial asks for a person, rather than served as though one had. No
// deployed cluster has a consent file yet, so nothing working today
// changes.
//
// Omitted when false so an unapproved open is byte-identical to what
// every SaaS sent before the field existed.
Approved bool `json:"approved,omitempty"`
// AgentDriven marks a console channel the SaaS opened to run the shell tool
// rather than one a human is typing (ADR 0017). The agent serves it only at
// action level internal, so an honest SaaS is held to the node's own level.
//
// Only a console-to-ssh channel may carry it; Validate rejects it elsewhere.
// Absent means false — a human console, byte-identical to what every SaaS
// sent before the field existed.
AgentDriven bool `json:"agentDriven,omitempty"`
}
ChannelOpen asks the agent to open a channel to a symbolic target.
func (ChannelOpen) Validate ¶
func (c ChannelOpen) Validate() error
Validate checks an open request before the agent acts on it. The SaaS is never trusted: every field is re-checked agent-side even though the SaaS validates before sending.
type Hello ¶
type Hello struct {
ClusterID string `json:"clusterId"`
Fingerprint string `json:"fingerprint"` // agent identity, shown at enrollment verify
AgentVersion string `json:"agentVersion"` // release of the agent binary
ProtocolVersion int `json:"protocolVersion"` // what this agent speaks
// ServedLevel and ServedConsent are what this node enforces, from its own
// files. Advisory to the SaaS: it narrows what it offers to them and never
// widens. Empty from agents that predate them.
ServedLevel string `json:"servedLevel,omitempty"`
ServedConsent string `json:"servedConsent,omitempty"`
// Brokers names the read brokers the sandbox can call (ADR 0019), e.g.
// ["prom", "ceph-ro"]; empty from an agent without them.
Brokers []string `json:"brokers,omitempty"`
}
Hello is the agent's opening frame, sent after the mTLS handshake.
type HelloAck ¶
type HelloAck struct {
Accepted bool `json:"accepted"`
ProtocolVersion int `json:"protocolVersion"` // what the SaaS will speak
Reason string `json:"reason,omitempty"`
// ConsoleOrigins maps a web target to the origin a browser reaches it on
// through the web console -- "scheme://host[:port]", with "*" standing in
// for the session id where the mode puts one in the hostname.
//
// Sent on every connect, not once at enrolment: a cluster that learned
// these once would go on trusting a stale set. Advisory -- the agent
// records it and does not act on it.
ConsoleOrigins map[string]string `json:"consoleOrigins,omitempty"`
// CurrentRelease is the agent version this SaaS would install today. Sent
// on every connect so an operator can be told a newer agent exists; the
// agent records it and does not act on it — updating what runs as root on
// a customer node is the customer's command to give (`advisor upgrade`).
CurrentRelease string `json:"currentRelease,omitempty"`
}
HelloAck is the SaaS's response. Accepted=false carries an operator-readable Reason: a refused agent is something a human has to fix, and "handshake failed" in a log is not enough to act on.
type InstanceProfile ¶
type InstanceProfile struct {
Configured bool `json:"configured"`
Flavor string `json:"flavor,omitempty"`
Image string `json:"image,omitempty"`
Network string `json:"network,omitempty"`
Project string `json:"project,omitempty"`
}
InstanceProfile is what a cluster creates, as the SaaS needs to state it.
It crosses the tool channel as that tool's result. Defined here because both repositories read it: the executor renders it from the file its operator wrote, the SaaS renders a sentence from it. A shape only one side could import gets restated on the other, which is how two definitions of one protocol come to disagree — cube-ai-advisor#121 and cube-advisor-agent#24 were both green while doing exactly that.
Configured separates "this cluster has no profile" from "its fields are empty". Those are different things to tell a person: the first is an operator who has not opted in, the second would be a bug. A caller reading only the fields could not tell them apart.
type KillSwitch ¶
type KillSwitch struct {
Reason string `json:"reason,omitempty"`
}
KillSwitch is sent agent→SaaS when the customer disables the agent. The SaaS must drop every open channel, including live console sessions.
type Target ¶
type Target struct {
Kind TargetKind `json:"kind"`
Name string `json:"name"`
}
Target names what a channel should connect to, symbolically.
Name is a label the agent resolves against its own allowlist — a node name, a web endpoint name, a tool name. It is deliberately not an address, and Validate rejects anything that looks like one. The agent enforces the allowlist regardless; this validation exists so the protocol itself cannot carry the request in the first place.
func (Target) Validate ¶
Validate returns nil when the target is a well-formed symbolic reference.
The rules are deliberately narrow — letters, digits, hyphen, underscore, dot, and nothing else. That excludes every shape an address can take: ports (`host:22`), IPv6 (`[::1]:22`), URLs (`http://…`), credentials (`user@host`), paths and globs. A name that survives this cannot be read as a network address by any resolver, which is the point.
Dots are allowed because node names are frequently FQDNs. That does mean a name can *look* like a hostname — which is fine: the agent still resolves it against a local allowlist of known nodes, so "looks like a hostname" and "is dialable" remain unrelated.
type TargetKind ¶
type TargetKind string
TargetKind is the class of thing a console or tool channel connects to. Adding a kind is an additive change and does not bump Version.
const ( // TargetSSH is a cluster node's own sshd, named by node. TargetSSH TargetKind = "ssh" // TargetWeb is one of the cluster's own web endpoints, named symbolically. TargetWeb TargetKind = "web" // TargetTool is a registered read-only tool on the AI plane. TargetTool TargetKind = "tool" )
func (TargetKind) Valid ¶
func (k TargetKind) Valid() bool
type ToolResult ¶
type ToolResult struct {
OK bool `json:"ok"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
}
ToolResult is the single frame an agent writes back on a tool channel.
Success is explicit rather than inferred from the output, so the SaaS never has to parse tool output to find out whether the tool ran.
func ReadToolResult ¶
func ReadToolResult(r io.Reader) (ToolResult, error)
ReadToolResult reads the result frame.
Bounded for the same reason the argument frame is: the SaaS is reading from a cluster it does not control, and an unbounded read is an unbounded allocation.
func Refused ¶
func Refused() ToolResult
Refused builds the one refusal every rejected call returns.
func RefusedAtLevel ¶
func RefusedAtLevel() ToolResult
RefusedAtLevel builds the refusal for a call the cluster's action level does not serve. Both repositories import this package, so the string the executor writes and the string the SaaS recognises are one definition rather than two that happen to agree — the failure that let cube-ai-advisor#121 and cube-advisor-agent#24 both pass while disagreeing about a frame.