opencode-tethergate 0.0.0-stage → 0.4.1

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,313 @@
1
+ const ADMIN_VERBS = new Set([
2
+ "session",
3
+ "access",
4
+ "node",
5
+ "fleet",
6
+ "auto",
7
+ "backup",
8
+ "restore",
9
+ ])
10
+ const WRAPPERS = new Set([
11
+ "command",
12
+ "env",
13
+ "exec",
14
+ "flock",
15
+ "ionice",
16
+ "logsave",
17
+ "nice",
18
+ "nohup",
19
+ "parallel",
20
+ "script",
21
+ "setsid",
22
+ "stdbuf",
23
+ "sudo",
24
+ "doas",
25
+ "taskset",
26
+ "timeout",
27
+ "watch",
28
+ "xargs",
29
+ ])
30
+ const WRAPPER_OPERANDS = new Map([
31
+ ["flock", 1],
32
+ ["timeout", 1],
33
+ ])
34
+ const SHELLS = new Set(["sh", "bash", "dash", "zsh", "ksh"])
35
+
36
+ function splitSegments(command) {
37
+ const segments = []
38
+ let current = ""
39
+ let quote = null
40
+ let escaped = false
41
+ for (let index = 0; index < command.length; index += 1) {
42
+ const char = command[index]
43
+ if (escaped) {
44
+ current += char
45
+ escaped = false
46
+ continue
47
+ }
48
+ if (char === "\\" && quote !== "'") {
49
+ current += char
50
+ escaped = true
51
+ continue
52
+ }
53
+ if (quote) {
54
+ current += char
55
+ if (char === quote) quote = null
56
+ continue
57
+ }
58
+ if (char === "'" || char === '"') {
59
+ quote = char
60
+ current += char
61
+ continue
62
+ }
63
+ if (command.startsWith("&&", index) || command.startsWith("||", index)) {
64
+ segments.push(current)
65
+ current = ""
66
+ index += 1
67
+ continue
68
+ }
69
+ if (";|\n()".includes(char)) {
70
+ segments.push(current)
71
+ current = ""
72
+ continue
73
+ }
74
+ current += char
75
+ }
76
+ segments.push(current)
77
+ return segments
78
+ }
79
+
80
+ function tokenize(segment) {
81
+ const tokens = []
82
+ let current = ""
83
+ let quote = null
84
+ let escaped = false
85
+ for (const char of segment) {
86
+ if (escaped) {
87
+ current += char
88
+ escaped = false
89
+ continue
90
+ }
91
+ if (char === "\\" && quote !== "'") {
92
+ escaped = true
93
+ continue
94
+ }
95
+ if (quote) {
96
+ if (char === quote) quote = null
97
+ else current += char
98
+ continue
99
+ }
100
+ if (char === "'" || char === '"') {
101
+ quote = char
102
+ continue
103
+ }
104
+ if (/\s/.test(char)) {
105
+ if (current) {
106
+ tokens.push(current)
107
+ current = ""
108
+ }
109
+ continue
110
+ }
111
+ current += char
112
+ }
113
+ if (current) tokens.push(current)
114
+ return tokens
115
+ }
116
+
117
+ function commandIndex(tokens) {
118
+ let index = 0
119
+ while (index < tokens.length) {
120
+ const token = tokens[index]
121
+ if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(token)) {
122
+ index += 1
123
+ continue
124
+ }
125
+ const name = token.split("/").pop().toLowerCase()
126
+ if (WRAPPERS.has(name)) {
127
+ index += 1
128
+ while (index < tokens.length && tokens[index].startsWith("-")) {
129
+ if (["-c", "-n", "-u", "-g", "-p", "-s", "-t"].includes(tokens[index])) {
130
+ index += 2
131
+ } else {
132
+ index += 1
133
+ }
134
+ }
135
+ const operands = WRAPPER_OPERANDS.get(name) || 0
136
+ for (let skipped = 0; skipped < operands && index < tokens.length; skipped += 1) {
137
+ index += 1
138
+ }
139
+ continue
140
+ }
141
+ break
142
+ }
143
+ return index
144
+ }
145
+
146
+ function isAdminInvocation(tokens) {
147
+ // Wrapper options can move the real command anywhere in the vector. Detect
148
+ // a Tethergate executable followed by an admin verb independent of position.
149
+ for (let i = 0; i < tokens.length; i += 1) {
150
+ const words = tokens[i].trim().split(/\s+/)
151
+ const name = words[0].split("/").pop().toLowerCase()
152
+ if (name === "tethergate" || name === "tethergate.cli") {
153
+ const rest = [...words.slice(1), ...tokens.slice(i + 1)]
154
+ if (rest.some((token) => ADMIN_VERBS.has(token.toLowerCase()))) return true
155
+ }
156
+ }
157
+ const index = commandIndex(tokens)
158
+ if (index >= tokens.length) return false
159
+ const command = tokens[index].split("/").pop().toLowerCase()
160
+ const rest = tokens.slice(index + 1)
161
+ if (SHELLS.has(command)) {
162
+ const flag = rest.findIndex((token) => token === "-c" || /^-[a-z]*c[a-z]*$/i.test(token))
163
+ if (flag >= 0 && rest[flag + 1] !== undefined) {
164
+ return segmentsHaveAdmin([rest.slice(flag + 1).join(" ")])
165
+ }
166
+ return false
167
+ }
168
+ if (command === "tethergate" || command === "tethergate.cli") {
169
+ return rest.some((token) => ADMIN_VERBS.has(token.toLowerCase()))
170
+ }
171
+ if (/^python[0-9.]*$/.test(command)) {
172
+ for (let i = 0; i < rest.length; i += 1) {
173
+ if (
174
+ rest[i] === "-m" &&
175
+ rest[i + 1] !== undefined &&
176
+ rest[i + 1].startsWith("tethergate")
177
+ ) {
178
+ return true
179
+ }
180
+ if (rest[i].startsWith("-m") && rest[i].slice(2).startsWith("tethergate")) return true
181
+ }
182
+ }
183
+ if (["docker", "podman", "nerdctl"].includes(command)) {
184
+ let index = 0
185
+ while (index < rest.length) {
186
+ const token = rest[index]
187
+ if (token === "--") {
188
+ index += 1
189
+ break
190
+ }
191
+ if (!token.startsWith("-")) break
192
+ if (
193
+ ["-H", "--host", "--context", "-c", "--config", "-l", "--log-level"].includes(
194
+ token,
195
+ ) &&
196
+ index + 1 < rest.length
197
+ ) {
198
+ index += 2
199
+ continue
200
+ }
201
+ index += 1
202
+ }
203
+ let sub = rest[index]
204
+ if (sub === "container") {
205
+ index += 1
206
+ sub = rest[index]
207
+ } else if (sub === "compose") {
208
+ index += 1
209
+ while (index < rest.length && rest[index].startsWith("-")) {
210
+ if (
211
+ ["-f", "--file", "-p", "--project-name", "--project-directory", "--env-file"].includes(
212
+ rest[index],
213
+ ) &&
214
+ index + 1 < rest.length
215
+ ) {
216
+ index += 2
217
+ } else {
218
+ index += 1
219
+ }
220
+ }
221
+ sub = rest[index]
222
+ }
223
+ if (sub === "exec" || sub === "run" || sub === "create") return true
224
+ }
225
+ // Unresolved shell expansion of the binary name is treated as admin use.
226
+ if (command.startsWith("$") && rest.some((token) => ADMIN_VERBS.has(token.toLowerCase()))) {
227
+ return true
228
+ }
229
+ return false
230
+ }
231
+
232
+ function segmentsHaveAdmin(segments) {
233
+ for (const segment of segments) {
234
+ const normalized = segment
235
+ .replace(/\\\n/g, " ")
236
+ .replace(/\$\{?IFS\}?/g, " ")
237
+ if (isAdminInvocation(tokenize(normalized))) return true
238
+ }
239
+ return false
240
+ }
241
+
242
+ function collectStrings(value, out, depth = 0) {
243
+ if (depth > 8) return
244
+ if (typeof value === "string") {
245
+ out.push(value)
246
+ return
247
+ }
248
+ if (Array.isArray(value)) {
249
+ for (const item of value) collectStrings(item, out, depth + 1)
250
+ return
251
+ }
252
+ if (value && typeof value === "object") {
253
+ for (const item of Object.values(value)) collectStrings(item, out, depth + 1)
254
+ }
255
+ }
256
+
257
+ export function commandFromInput(input) {
258
+ if (typeof input === "string") return input
259
+ if (!input || typeof input !== "object") return ""
260
+ const out = []
261
+ collectStrings(input, out)
262
+ return out.join("\n")
263
+ }
264
+
265
+ export function assertNoAdminShell(command) {
266
+ const text = String(command || "")
267
+ // Heredoc bodies are data for tools such as python3/cat in normal use. A
268
+ // heredoc can still execute code through a chosen interpreter, which is the
269
+ // documented same-user limitation; do not turn documentation into a false
270
+ // positive.
271
+ const heredoc = text.indexOf("<<")
272
+ const visible = heredoc >= 0 ? text.slice(0, heredoc) : text
273
+ if (segmentsHaveAdmin(splitSegments(visible))) {
274
+ throw new Error(
275
+ "Tethergate access changes must be made through the /tethergate menu or command, not a shell",
276
+ )
277
+ }
278
+ // A heredoc piped into a shell executes its body. Inspect that case; keep
279
+ // documentation heredocs aimed at cat/python as plain data.
280
+ if (heredoc >= 0) {
281
+ const firstLine = visible.split("\n")[0].trim()
282
+ const firstTokens = tokenize(firstLine)
283
+ const firstCommand = firstTokens[0]?.split("/").pop().toLowerCase()
284
+ if (SHELLS.has(firstCommand) || firstCommand === "script" || firstCommand === "eval") {
285
+ const body = text.slice(text.indexOf("\n", heredoc) + 1)
286
+ if (segmentsHaveAdmin(splitSegments(body))) {
287
+ throw new Error(
288
+ "Tethergate access changes must be made through the /tethergate menu or command, not a shell",
289
+ )
290
+ }
291
+ }
292
+ }
293
+ }
294
+
295
+ export function approvalFor(sessionID, kind, resources, message) {
296
+ if (kind !== "grant" && kind !== "revoke" && kind !== "default" && kind !== "node") {
297
+ throw new Error(`unknown approval kind ${kind}`)
298
+ }
299
+ const action =
300
+ kind === "grant"
301
+ ? "tethergate.session.grant"
302
+ : kind === "revoke"
303
+ ? "tethergate.session.revoke"
304
+ : kind === "default"
305
+ ? "tethergate.default.set"
306
+ : "tethergate.node.change"
307
+ return {
308
+ sessionID,
309
+ action,
310
+ resources: [String(sessionID), ...resources.map(String)],
311
+ metadata: { plugin: "tethergate", message },
312
+ }
313
+ }
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.1",
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
+ }