opencode-tethergate 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 peva3
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,160 @@
1
- # Temporary Holding Version
1
+ # opencode-tethergate
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ OpenCode plugin for [Tethergate](https://github.com/peva3/tethergate): secure remote
4
+ node access over mutual TLS, with per-session grants and a permission-gated
5
+ `/tethergate` menu.
6
+
7
+ The plugin never connects to a node directly. Its server component calls the local
8
+ Tethergate daemon through the admin control socket, and the daemon remains the
9
+ enforcement point. You do not need to run the Tethergate CLI or `docker exec` to
10
+ authorize a session.
11
+
12
+ Every authorization RPC first calls `ctx.permission.assert`. The plugin registers a
13
+ permission hook that forces **Ask** for `tethergate.*` actions even when the agent's
14
+ normal rules would auto-allow them. The model cannot answer the prompt.
15
+
16
+ ## Requirements
17
+
18
+ - A running Tethergate server (normally in Docker) with the MCP bridge configured.
19
+ - OpenCode 2.4.x or newer.
20
+ - Node.js 20 or newer.
21
+
22
+ ## Install
23
+
24
+ ```sh
25
+ opencode plugin add opencode-tethergate
26
+ ```
27
+
28
+ Or add it manually to the global or project `opencode.json(c)`:
29
+
30
+ ```jsonc
31
+ {
32
+ "$schema": "https://opencode.ai/config.json",
33
+ "plugins": ["opencode-tethergate"]
34
+ }
35
+ ```
36
+
37
+ Configure the Tethergate MCP bridge in the same config:
38
+
39
+ ```jsonc
40
+ {
41
+ "mcp": {
42
+ "servers": {
43
+ "tethergate": {
44
+ "type": "local",
45
+ "command": ["docker", "exec", "-i", "tethergate", "tethergate", "mcp"],
46
+ "enabled": true
47
+ }
48
+ }
49
+ }
50
+ }
51
+ ```
52
+
53
+ Use `-i`, not `-it`; a TTY corrupts MCP's JSON stream. If your container or CLI path
54
+ differs, pass it as a plugin option:
55
+
56
+ ```jsonc
57
+ {
58
+ "plugins": [
59
+ {
60
+ "package": "opencode-tethergate",
61
+ "options": {
62
+ "command": ["docker", "exec", "-i", "my-tethergate", "tethergate"]
63
+ }
64
+ }
65
+ ]
66
+ }
67
+ ```
68
+
69
+ The command can also be a direct CLI, for example `["/usr/local/bin/tethergate"]` when
70
+ the daemon and CLI run on the same host.
71
+
72
+ ## TUI
73
+
74
+ Run `/tethergate`:
75
+
76
+ - **Choose nodes…** — toggle a subset; offline nodes are disabled.
77
+ - **Revoke this session** — remove this session's grant.
78
+ - **Enroll a node…** — create a one-time installer and display the command.
79
+ - **Create fleet installer…** — create one reusable one-liner for all machines.
80
+ - **Manage fleet installers…** — review use counts and revoke a reusable one-liner.
81
+ - **Revoke a node…** — disable a node certificate immediately.
82
+ - **Re-enable a quarantined node…** — appears when a tripwire has isolated a node.
83
+ Review the audit entry before clearing the quarantine.
84
+ - **Default for new sessions** — toggle deny/all. Switching to all requires a
85
+ confirmation dialog and a permission prompt.
86
+ - **Grant all nodes** — last in the Advanced section; this session may use every
87
+ enrolled node.
88
+
89
+ Each change triggers a native OpenCode permission prompt. The menu uses the current
90
+ session from `ctx.ui.router.current()` and native `ctx.ui.dialog.select` /
91
+ `ctx.ui.dialog.confirm` dialogs.
92
+
93
+ The plugin also installs `tool.execute.before` and shell guards. A model that tries
94
+ `tethergate session …`, `tethergate access …`, `tethergate node …`, or
95
+ `docker exec … tethergate …` through a shell is blocked with a message pointing at
96
+ `/tethergate`.
97
+
98
+ ## Slash commands and web/desktop
99
+
100
+ The server plugin registers a `/tethergate` command. It runs on the server, not through
101
+ the model, so it works wherever server commands are exposed:
102
+
103
+ ```text
104
+ /tethergate
105
+ /tethergate status
106
+ /tethergate grant all
107
+ /tethergate grant node-a,node-b
108
+ /tethergate revoke
109
+ /tethergate default deny
110
+ /tethergate default all --yes
111
+ /tethergate node list
112
+ /tethergate node add node-a
113
+ /tethergate node revoke node-a
114
+ /tethergate node reset node-a
115
+ /tethergate node rename node-a node-b
116
+ /tethergate node retire node-a
117
+ /tethergate node update node-a
118
+ /tethergate node rollback node-a
119
+ /tethergate node uninstall node-a
120
+ /tethergate fleet add tailnet
121
+ /tethergate fleet list
122
+ /tethergate fleet revoke ID
123
+ ```
124
+
125
+ Third-party GUI panels in the browser web UI are still experimental in OpenCode
126
+ 2.4.x. The slash command is the supported web/desktop path until upstream GUI
127
+ plugin loading is stable. The same RPC contract can back a `gui-experimental` panel.
128
+
129
+ ## Development
130
+
131
+ ```sh
132
+ cd opencode-plugin
133
+ node --check index.js
134
+ node --check tui.js
135
+ node --check rpc.js
136
+ node --test
137
+ ```
138
+
139
+ The service tests run the real CLI orchestration against a fake runner and cover
140
+ grant/revoke/default-all, argv safety, and validation. The TUI dialogs require a
141
+ live OpenCode TUI session to exercise interactively.
142
+
143
+ ## Security notes
144
+
145
+ - Grant/revoke calls are argv vectors, never shell strings. Session ids and node
146
+ names are validated before spawning.
147
+ - Grant, revoke, node enrollment, node revocation, and default-all all call
148
+ `ctx.permission.assert`. The `permission.hook("evaluate")` forces **Ask** for
149
+ `tethergate.*`, so agent auto-allow rules do not silently authorize access.
150
+ - `default all` is refused without explicit `--yes` / a confirmation dialog and is
151
+ audited by the daemon.
152
+ - Model shell attempts to invoke the admin CLI or `docker exec` are rejected by
153
+ `tool.execute.before` and `shell.hook("create.before")`.
154
+ - The session id is supplied by OpenCode as `_meta["ai.opencode/sessionID"]`. It is a
155
+ correlation value, not an authentication token. The node hard floor remains the
156
+ security boundary; see the Tethergate security documentation.
157
+
158
+ ## License
159
+
160
+ MIT
package/guard.js ADDED
@@ -0,0 +1,263 @@
1
+ const ADMIN_VERBS = new Set(["session", "access", "node", "fleet"])
2
+ const WRAPPERS = new Set([
3
+ "command",
4
+ "env",
5
+ "exec",
6
+ "flock",
7
+ "ionice",
8
+ "logsave",
9
+ "nice",
10
+ "nohup",
11
+ "parallel",
12
+ "script",
13
+ "setsid",
14
+ "stdbuf",
15
+ "sudo",
16
+ "doas",
17
+ "taskset",
18
+ "timeout",
19
+ "watch",
20
+ "xargs",
21
+ ])
22
+ const WRAPPER_OPERANDS = new Map([
23
+ ["flock", 1],
24
+ ["timeout", 1],
25
+ ])
26
+ const SHELLS = new Set(["sh", "bash", "dash", "zsh", "ksh"])
27
+
28
+ function splitSegments(command) {
29
+ const segments = []
30
+ let current = ""
31
+ let quote = null
32
+ let escaped = false
33
+ for (let index = 0; index < command.length; index += 1) {
34
+ const char = command[index]
35
+ if (escaped) {
36
+ current += char
37
+ escaped = false
38
+ continue
39
+ }
40
+ if (char === "\\" && quote !== "'") {
41
+ current += char
42
+ escaped = true
43
+ continue
44
+ }
45
+ if (quote) {
46
+ current += char
47
+ if (char === quote) quote = null
48
+ continue
49
+ }
50
+ if (char === "'" || char === '"') {
51
+ quote = char
52
+ current += char
53
+ continue
54
+ }
55
+ if (command.startsWith("&&", index) || command.startsWith("||", index)) {
56
+ segments.push(current)
57
+ current = ""
58
+ index += 1
59
+ continue
60
+ }
61
+ if (";|\n()".includes(char)) {
62
+ segments.push(current)
63
+ current = ""
64
+ continue
65
+ }
66
+ current += char
67
+ }
68
+ segments.push(current)
69
+ return segments
70
+ }
71
+
72
+ function tokenize(segment) {
73
+ const tokens = []
74
+ let current = ""
75
+ let quote = null
76
+ let escaped = false
77
+ for (const char of segment) {
78
+ if (escaped) {
79
+ current += char
80
+ escaped = false
81
+ continue
82
+ }
83
+ if (char === "\\" && quote !== "'") {
84
+ escaped = true
85
+ continue
86
+ }
87
+ if (quote) {
88
+ if (char === quote) quote = null
89
+ else current += char
90
+ continue
91
+ }
92
+ if (char === "'" || char === '"') {
93
+ quote = char
94
+ continue
95
+ }
96
+ if (/\s/.test(char)) {
97
+ if (current) {
98
+ tokens.push(current)
99
+ current = ""
100
+ }
101
+ continue
102
+ }
103
+ current += char
104
+ }
105
+ if (current) tokens.push(current)
106
+ return tokens
107
+ }
108
+
109
+ function commandIndex(tokens) {
110
+ let index = 0
111
+ while (index < tokens.length) {
112
+ const token = tokens[index]
113
+ if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(token)) {
114
+ index += 1
115
+ continue
116
+ }
117
+ const name = token.split("/").pop().toLowerCase()
118
+ if (WRAPPERS.has(name)) {
119
+ index += 1
120
+ while (index < tokens.length && tokens[index].startsWith("-")) {
121
+ if (["-c", "-n", "-u", "-g", "-p", "-s", "-t"].includes(tokens[index])) {
122
+ index += 2
123
+ } else {
124
+ index += 1
125
+ }
126
+ }
127
+ const operands = WRAPPER_OPERANDS.get(name) || 0
128
+ for (let skipped = 0; skipped < operands && index < tokens.length; skipped += 1) {
129
+ index += 1
130
+ }
131
+ continue
132
+ }
133
+ break
134
+ }
135
+ return index
136
+ }
137
+
138
+ function isAdminInvocation(tokens) {
139
+ const index = commandIndex(tokens)
140
+ if (index >= tokens.length) return false
141
+ const command = tokens[index].split("/").pop().toLowerCase()
142
+ const rest = tokens.slice(index + 1)
143
+ if (SHELLS.has(command)) {
144
+ const flag = rest.findIndex((token) => token === "-c" || /^-[a-z]*c[a-z]*$/i.test(token))
145
+ if (flag >= 0 && rest[flag + 1] !== undefined) {
146
+ return segmentsHaveAdmin([rest.slice(flag + 1).join(" ")])
147
+ }
148
+ return false
149
+ }
150
+ if (command === "tethergate" || command === "tethergate.cli") {
151
+ return rest.some((token) => ADMIN_VERBS.has(token.toLowerCase()))
152
+ }
153
+ if (/^python[0-9.]*$/.test(command)) {
154
+ for (let i = 0; i < rest.length; i += 1) {
155
+ if (
156
+ rest[i] === "-m" &&
157
+ rest[i + 1] !== undefined &&
158
+ rest[i + 1].startsWith("tethergate")
159
+ ) {
160
+ return true
161
+ }
162
+ if (rest[i].startsWith("-m") && rest[i].slice(2).startsWith("tethergate")) return true
163
+ }
164
+ }
165
+ if (["docker", "podman", "nerdctl"].includes(command)) {
166
+ const args = rest.filter((token) => !token.startsWith("-"))
167
+ const sub =
168
+ args[0] === "container"
169
+ ? args[1]
170
+ : args[0] === "compose"
171
+ ? args[1]
172
+ : args[0]
173
+ if (sub === "exec" || sub === "run" || sub === "create") return true
174
+ }
175
+ // Unresolved shell expansion of the binary name is treated as admin use.
176
+ if (command.startsWith("$") && rest.some((token) => ADMIN_VERBS.has(token.toLowerCase()))) {
177
+ return true
178
+ }
179
+ return false
180
+ }
181
+
182
+ function segmentsHaveAdmin(segments) {
183
+ for (const segment of segments) {
184
+ const normalized = segment
185
+ .replace(/\\\n/g, " ")
186
+ .replace(/\$\{?IFS\}?/g, " ")
187
+ if (isAdminInvocation(tokenize(normalized))) return true
188
+ }
189
+ return false
190
+ }
191
+
192
+ function collectStrings(value, out, depth = 0) {
193
+ if (depth > 8) return
194
+ if (typeof value === "string") {
195
+ out.push(value)
196
+ return
197
+ }
198
+ if (Array.isArray(value)) {
199
+ for (const item of value) collectStrings(item, out, depth + 1)
200
+ return
201
+ }
202
+ if (value && typeof value === "object") {
203
+ for (const item of Object.values(value)) collectStrings(item, out, depth + 1)
204
+ }
205
+ }
206
+
207
+ export function commandFromInput(input) {
208
+ if (typeof input === "string") return input
209
+ if (!input || typeof input !== "object") return ""
210
+ const out = []
211
+ collectStrings(input, out)
212
+ return out.join("\n")
213
+ }
214
+
215
+ export function assertNoAdminShell(command) {
216
+ const text = String(command || "")
217
+ // Heredoc bodies are data for tools such as python3/cat in normal use. A
218
+ // heredoc can still execute code through a chosen interpreter, which is the
219
+ // documented same-user limitation; do not turn documentation into a false
220
+ // positive.
221
+ const heredoc = text.indexOf("<<")
222
+ const visible = heredoc >= 0 ? text.slice(0, heredoc) : text
223
+ if (segmentsHaveAdmin(splitSegments(visible))) {
224
+ throw new Error(
225
+ "Tethergate access changes must be made through the /tethergate menu or command, not a shell",
226
+ )
227
+ }
228
+ // A heredoc piped into a shell executes its body. Inspect that case; keep
229
+ // documentation heredocs aimed at cat/python as plain data.
230
+ if (heredoc >= 0) {
231
+ const firstLine = visible.split("\n")[0].trim()
232
+ const firstTokens = tokenize(firstLine)
233
+ const firstCommand = firstTokens[0]?.split("/").pop().toLowerCase()
234
+ if (SHELLS.has(firstCommand) || firstCommand === "script" || firstCommand === "eval") {
235
+ const body = text.slice(text.indexOf("\n", heredoc) + 1)
236
+ if (segmentsHaveAdmin(splitSegments(body))) {
237
+ throw new Error(
238
+ "Tethergate access changes must be made through the /tethergate menu or command, not a shell",
239
+ )
240
+ }
241
+ }
242
+ }
243
+ }
244
+
245
+ export function approvalFor(sessionID, kind, resources, message) {
246
+ if (kind !== "grant" && kind !== "revoke" && kind !== "default" && kind !== "node") {
247
+ throw new Error(`unknown approval kind ${kind}`)
248
+ }
249
+ const action =
250
+ kind === "grant"
251
+ ? "tethergate.session.grant"
252
+ : kind === "revoke"
253
+ ? "tethergate.session.revoke"
254
+ : kind === "default"
255
+ ? "tethergate.default.set"
256
+ : "tethergate.node.change"
257
+ return {
258
+ sessionID,
259
+ action,
260
+ resources: [String(sessionID), ...resources.map(String)],
261
+ metadata: { plugin: "tethergate", message },
262
+ }
263
+ }
package/index.js ADDED
@@ -0,0 +1,13 @@
1
+ import { Plugin } from "@opencode/plugin"
2
+
3
+ import { Tethergate } from "./rpc.js"
4
+ import { TethergateService } from "./service.js"
5
+ import { makeRunner, setupTethergate } from "./setup.js"
6
+
7
+ export default Plugin.define({
8
+ id: "tethergate",
9
+ async setup(ctx) {
10
+ const service = new TethergateService(makeRunner(ctx))
11
+ return setupTethergate(ctx, service, Tethergate)
12
+ },
13
+ })
package/menu.js ADDED
@@ -0,0 +1,102 @@
1
+ export function buildMenuOptions(data) {
2
+ const options = []
3
+ if (data.defaultAccess !== "all") {
4
+ options.push({
5
+ title: "Choose nodes…",
6
+ value: "choose",
7
+ description: "Select a subset for this session",
8
+ })
9
+ }
10
+
11
+ if (
12
+ data.defaultAccess !== "all" &&
13
+ (data.grantAll || data.grantNodes.length)
14
+ ) {
15
+ options.push({
16
+ title: "Revoke this session",
17
+ value: "revoke",
18
+ description: "Remove all tethergate access",
19
+ })
20
+ }
21
+
22
+ options.push(
23
+ {
24
+ title: "Enroll a node…",
25
+ value: "node-add",
26
+ description: "Create a one-time installer command",
27
+ category: "Nodes",
28
+ },
29
+ {
30
+ title: "Create fleet installer…",
31
+ value: "fleet-add",
32
+ description: "One reusable one-liner for Tailscale machines",
33
+ category: "Nodes",
34
+ },
35
+ {
36
+ title: "Manage fleet installers…",
37
+ value: "fleet-manage",
38
+ description: "Review or revoke reusable installers",
39
+ category: "Nodes",
40
+ },
41
+ {
42
+ title: "Revoke a node…",
43
+ value: "node-revoke",
44
+ description: "Disable a node certificate immediately",
45
+ category: "Nodes",
46
+ },
47
+ {
48
+ title: "Update node agents…",
49
+ value: "node-update",
50
+ description: "Push the current release or roll back",
51
+ category: "Nodes",
52
+ },
53
+ {
54
+ title: "Rename a node…",
55
+ value: "node-rename",
56
+ description: "Change the display name without re-enrolling",
57
+ category: "Nodes",
58
+ },
59
+ ...(data.nodes.some(
60
+ (node) => node.status === "revoked" || node.status === "quarantined",
61
+ )
62
+ ? [
63
+ {
64
+ title: "Retire a node…",
65
+ value: "node-retire",
66
+ description: "Keep audit history and free the name for replacement",
67
+ category: "Nodes",
68
+ },
69
+ ]
70
+ : []),
71
+ {
72
+ title: "Uninstall an agent…",
73
+ value: "node-uninstall",
74
+ description: "Remove the service and files from a node",
75
+ category: "Nodes",
76
+ },
77
+ ...(data.nodes.some((node) => node.status === "quarantined")
78
+ ? [
79
+ {
80
+ title: "Re-enable a quarantined node…",
81
+ value: "node-reset",
82
+ description: "Clear a safety-tripwire quarantine",
83
+ category: "Nodes",
84
+ },
85
+ ]
86
+ : []),
87
+ {
88
+ title: `Default for new sessions: ${data.defaultAccess}`,
89
+ value: "default",
90
+ description: "Explicit opt-in to allow every session",
91
+ category: "Advanced",
92
+ },
93
+ {
94
+ title: "Grant all nodes",
95
+ value: "all",
96
+ description: "Full access to every enrolled node",
97
+ category: "Advanced",
98
+ },
99
+ )
100
+
101
+ return options
102
+ }
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "opencode-tethergate",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.0",
4
+ "type": "module",
5
+ "description": "OpenCode plugin for Tethergate: secure remote node access over mutual TLS",
6
+ "license": "MIT",
7
+ "author": "peva3",
8
+ "homepage": "https://github.com/peva3/tethergate#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/peva3/tethergate.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/peva3/tethergate/issues"
15
+ },
16
+ "keywords": [
17
+ "opencode",
18
+ "opencode-plugin",
19
+ "mcp",
20
+ "remote",
21
+ "tailscale",
22
+ "mutual-tls",
23
+ "tethergate"
24
+ ],
25
+ "engines": {
26
+ "node": ">=20"
27
+ },
28
+ "exports": {
29
+ ".": "./index.js",
30
+ "./tui": "./tui.js",
31
+ "./rpc": "./rpc.js"
32
+ },
33
+ "files": [
34
+ "index.js",
35
+ "setup.js",
36
+ "service.js",
37
+ "rpc.js",
38
+ "tui.js",
39
+ "menu.js",
40
+ "guard.js",
41
+ "README.md",
42
+ "LICENSE"
43
+ ],
44
+ "scripts": {
45
+ "test": "node --test"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "dependencies": {
51
+ "@opencode/plugin": "^2.4.3"
52
+ }
53
+ }