shapewall 0.3.3 → 0.6.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/CHANGELOG.md +249 -0
- package/README.md +49 -3
- package/dist/agent/package-lock.json +2 -2
- package/dist/client/assets/BoardChat-DdwBcb2N.js +27 -0
- package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-DpUcnU6S.js} +1 -1
- package/dist/client/assets/ChecksPanel-CngYV4yz.js +1 -0
- package/dist/client/assets/History-CD0saQA5.js +1 -0
- package/dist/client/assets/LocalSettings-MZy2baig.js +1 -0
- package/dist/client/assets/{Management-NVFDtSZB.js → Management-CSvZ-SHO.js} +1 -1
- package/dist/client/assets/QuickConnect-foLh2QUm.js +1 -0
- package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-DPWDftoL.js} +2 -2
- package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-CtiXpTRD.js} +1 -1
- package/dist/client/assets/{circle-x-B6KfScOE.js → circle-x-CteRBtXC.js} +1 -1
- package/dist/client/assets/index-Cxz1UVcV.css +2 -0
- package/dist/client/assets/index-uUZVBYUS.js +37 -0
- package/dist/client/assets/play-BjRNIGSf.js +1 -0
- package/dist/client/assets/{refresh-cw-vkgvCCfR.js → refresh-cw-r_tZiFfB.js} +1 -1
- package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
- package/dist/client/assets/ui-zQpn-PdL.js +41 -0
- package/dist/client/build-info.json +1 -1
- package/dist/client/index.html +3 -3
- package/dist/client/sw.js +2 -2
- package/dist/console/assets/{LocalAccountsApp-VOCZg8dg.js → LocalAccountsApp-DIdy1KnN.js} +1 -1
- package/dist/console/assets/{LocalConsole-CMwAW-mF.js → LocalConsole-DfcQztF-.js} +1 -1
- package/dist/console/assets/{app-update-dialog-Cw6UDqNP.js → app-update-dialog-ttl9lF5A.js} +1 -1
- package/dist/console/assets/console-OqAjD0kV.js +55 -0
- package/dist/console/assets/console-kvjrAArQ.css +2 -0
- package/dist/console/build-info.json +1 -1
- package/dist/console/console.html +2 -2
- package/docs/agent-hardening.md +192 -0
- package/docs/agent-recipes.md +23 -0
- package/docs/agent-workflow.md +5 -0
- package/docs/agents.md +14 -6
- package/docs/hosted.md +804 -11
- package/docs/where-to-run.md +37 -2
- package/package.json +1 -1
- package/src/agent/backends/acp.js +528 -25
- package/src/agent/branch-mode.js +17 -3
- package/src/agent/card-hash.js +65 -0
- package/src/agent/context.js +41 -0
- package/src/agent/exec-paths.js +105 -0
- package/src/agent/routes.js +2 -2
- package/src/agent/run.js +11 -1
- package/src/agent/starter/cli.js +6 -1
- package/src/agent/turn-changes.js +70 -8
- package/src/cli/args.js +13 -0
- package/src/cli/commands.js +20 -5
- package/src/cli/help.js +36 -1
- package/src/hosted/admin.js +7 -0
- package/src/hosted/agent-approvals.js +28 -3
- package/src/hosted/agent-link.js +1032 -0
- package/src/hosted/agent-policy.js +10 -3
- package/src/hosted/agent-presence.js +63 -22
- package/src/hosted/agent-routes.js +135 -0
- package/src/hosted/agents.js +1199 -0
- package/src/hosted/app.js +89 -5
- package/src/hosted/board-routes.js +127 -3
- package/src/hosted/chat-frames.js +256 -0
- package/src/hosted/chat-job.js +204 -0
- package/src/hosted/chat-permissions.js +191 -0
- package/src/hosted/chat-routes.js +1009 -0
- package/src/hosted/chat-store.js +460 -0
- package/src/hosted/comment-files.js +8 -1
- package/src/hosted/comments.js +1 -1
- package/src/hosted/console-routes.js +11 -0
- package/src/hosted/db.js +165 -0
- package/src/hosted/device-routes.js +64 -8
- package/src/hosted/device.js +35 -4
- package/src/hosted/events.js +3 -3
- package/src/hosted/membership.js +9 -1
- package/src/hosted/pages.js +2 -0
- package/src/hosted/permissions.js +38 -0
- package/src/hosted/presence.js +17 -0
- package/src/hosted/projects.js +29 -2
- package/src/hosted/ratelimit.js +8 -0
- package/src/hosted/tenancy.js +18 -0
- package/src/hosted/token-api.js +118 -19
- package/src/hosted/token-events.js +5 -3
- package/src/hosted/tokens.js +21 -5
- package/src/hosted/turn-hub.js +152 -0
- package/src/remote/agent-connect.js +433 -0
- package/src/remote/agent-fix.js +611 -0
- package/src/remote/agent-job.js +216 -0
- package/src/remote/agent-link.js +904 -0
- package/src/remote/agent-policy-file.js +242 -0
- package/src/remote/credentials.js +35 -1
- package/src/remote/login.js +12 -2
- package/dist/client/assets/BoardChat-DxehB761.js +0 -27
- package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
- package/dist/client/assets/History-C9N32kCv.js +0 -1
- package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
- package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
- package/dist/client/assets/index-CsqLdqGx.css +0 -2
- package/dist/client/assets/index-DnZarJ3U.js +0 -37
- package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
- package/dist/client/assets/ui-DI-FGWWK.js +0 -41
- package/dist/console/assets/console-DgfWmUEx.js +0 -55
- package/dist/console/assets/console-Lis0vvdL.css +0 -2
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// What this machine allows a connected agent (`shapewall agent connect`),
|
|
2
|
+
// in a file of its own beside the saved agents: $XDG_CONFIG_HOME/shapewall/
|
|
3
|
+
// agent-policy.json (default ~/.config/...), the folder 0700, the file 0600.
|
|
4
|
+
// Outside every repository, read by the connector only, and never sent
|
|
5
|
+
// anywhere but as the summary the board shows. The server cannot change it:
|
|
6
|
+
// this file is the ceiling, and a setting on the server only ever narrows it.
|
|
7
|
+
//
|
|
8
|
+
// audience "server": whoever the agent's audience on the server names
|
|
9
|
+
// (set when the connection is approved, and on its Agents page)
|
|
10
|
+
// "owner": the server is told to keep it to the person who
|
|
11
|
+
// connected it, and this machine refuses a job the server
|
|
12
|
+
// labels as someone else's. The machine cannot check that
|
|
13
|
+
// label: this does not protect against a server that lies
|
|
14
|
+
// approvals "none" (the default): the agent reads and answers only. Nobody
|
|
15
|
+
// answers a permission request from the board, and no fix runs.
|
|
16
|
+
// "owner": the person who connected it may allow an edit from
|
|
17
|
+
// the board. "admins": anyone the server lets answer may.
|
|
18
|
+
// Either way the edit is made on a new branch in a folder of
|
|
19
|
+
// its own, never in this checkout.
|
|
20
|
+
// commands the exact commands the agent may ever ask to run, each still
|
|
21
|
+
// approved on the board. Empty (the default): never.
|
|
22
|
+
// auto the kinds ("edits", "commands") the server's "always allow"
|
|
23
|
+
// rule may apply to. Empty (the default): every request waits
|
|
24
|
+
// for a person. Never widens `commands` or what may be edited.
|
|
25
|
+
// allowPaths files that decide what runs later or hold settings (dotfiles,
|
|
26
|
+
// package.json, a Makefile, ...) the agent may ask to edit all
|
|
27
|
+
// the same. Empty (the default): such an edit is refused here.
|
|
28
|
+
// base the branch a fix starts from. null (the default): the commit
|
|
29
|
+
// this checkout is on, and only while it has no uncommitted
|
|
30
|
+
// changes.
|
|
31
|
+
// remote the git remote a finished fix is pushed to ("origin"), or
|
|
32
|
+
// null: never pushed.
|
|
33
|
+
//
|
|
34
|
+
// One entry per server and connection name.
|
|
35
|
+
|
|
36
|
+
import fs from 'node:fs'
|
|
37
|
+
import path from 'node:path'
|
|
38
|
+
import { randomBytes } from 'node:crypto'
|
|
39
|
+
import { credentialsFile } from './credentials.js'
|
|
40
|
+
|
|
41
|
+
export const POLICY_AUDIENCES = ['server', 'owner']
|
|
42
|
+
export const POLICY_APPROVALS = ['none', 'owner', 'admins']
|
|
43
|
+
export const POLICY_AUTO_KINDS = ['edits', 'commands']
|
|
44
|
+
export const COMMANDS_MAX = 20
|
|
45
|
+
export const COMMAND_MAX = 200
|
|
46
|
+
export const ALLOW_PATHS_MAX = 40
|
|
47
|
+
export const DEFAULT_MACHINE_POLICY = {
|
|
48
|
+
audience: 'server',
|
|
49
|
+
approvals: 'none',
|
|
50
|
+
commands: [],
|
|
51
|
+
auto: [],
|
|
52
|
+
allowPaths: [],
|
|
53
|
+
base: null,
|
|
54
|
+
remote: 'origin',
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const policyFile = (env = process.env) => path.join(path.dirname(credentialsFile(env)), 'agent-policy.json')
|
|
58
|
+
|
|
59
|
+
// The file, read only once it is this person's and only theirs to write:
|
|
60
|
+
// owner and mode are checked on the open file, before a byte of it is trusted.
|
|
61
|
+
function readAll(file) {
|
|
62
|
+
let fd
|
|
63
|
+
try {
|
|
64
|
+
fd = fs.openSync(file, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0))
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error.code === 'ENOENT') return { version: 1, connections: {} }
|
|
67
|
+
if (error.code === 'ELOOP') throw new Error(`${file} is a symbolic link. A policy file is a plain file of your own.`, { cause: error })
|
|
68
|
+
throw error
|
|
69
|
+
}
|
|
70
|
+
let text
|
|
71
|
+
try {
|
|
72
|
+
const stat = fs.fstatSync(fd)
|
|
73
|
+
if (!stat.isFile()) throw new Error(`${file} is not a plain file.`)
|
|
74
|
+
if (process.platform !== 'win32') {
|
|
75
|
+
// A policy another user owns, or others can write, is not this person's policy.
|
|
76
|
+
if (typeof process.getuid === 'function' && stat.uid !== process.getuid())
|
|
77
|
+
throw new Error(`${file} belongs to another user of this machine. Remove it, then run agent connect again.`)
|
|
78
|
+
if (stat.mode & 0o022) throw new Error(`${file} can be changed by other users of this machine. Run: chmod 600 "${file}"`)
|
|
79
|
+
}
|
|
80
|
+
text = fs.readFileSync(fd, 'utf8')
|
|
81
|
+
} finally {
|
|
82
|
+
fs.closeSync(fd)
|
|
83
|
+
}
|
|
84
|
+
let data
|
|
85
|
+
try {
|
|
86
|
+
data = JSON.parse(text)
|
|
87
|
+
} catch {
|
|
88
|
+
throw new Error(`${file} is not valid JSON. Fix it or delete it.`)
|
|
89
|
+
}
|
|
90
|
+
if (!data || typeof data.connections !== 'object' || !data.connections || Array.isArray(data.connections))
|
|
91
|
+
throw new Error(`${file} has no "connections". Fix it or delete it.`)
|
|
92
|
+
return data
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// One command as it is compared: single spaces, no control characters, no
|
|
96
|
+
// line breaks. An agent's request must equal it exactly.
|
|
97
|
+
export function cleanCommand(value) {
|
|
98
|
+
if (typeof value !== 'string') return null
|
|
99
|
+
// eslint-disable-next-line no-control-regex
|
|
100
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) return null
|
|
101
|
+
const text = value.trim().replace(/ +/g, ' ')
|
|
102
|
+
return text && text.length <= COMMAND_MAX ? text : null
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// A project-relative path with plain segments ("package.json", ".github/workflows/ci.yml").
|
|
106
|
+
export function cleanAllowPath(value) {
|
|
107
|
+
if (typeof value !== 'string' || !value || value.length > 300 || value.includes('\0') || value.includes('\\')) return null
|
|
108
|
+
const parts = value.split('/')
|
|
109
|
+
if (path.isAbsolute(value) || parts.some((part) => !part || part === '.' || part === '..')) return null
|
|
110
|
+
// Never git's own folder or Shapewall's store, whatever the file says.
|
|
111
|
+
if (['.git', '.shapewall'].includes(parts[0])) return null
|
|
112
|
+
return parts.join('/')
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const BRANCH_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*(?:\/[A-Za-z0-9][A-Za-z0-9._-]*)*$/
|
|
116
|
+
export const cleanBase = (value) =>
|
|
117
|
+
typeof value === 'string' && value.length <= 120 && BRANCH_RE.test(value) && !value.includes('..') && !value.endsWith('.lock')
|
|
118
|
+
? value
|
|
119
|
+
: null
|
|
120
|
+
const REMOTE_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,59}$/
|
|
121
|
+
export const cleanRemote = (value) => (typeof value === 'string' && REMOTE_RE.test(value) ? value : null)
|
|
122
|
+
|
|
123
|
+
// One entry of the file, as the policy in effect. A key that is missing
|
|
124
|
+
// reads as its default. A key that is there but is not a value this file
|
|
125
|
+
// knows (a typo, a wrong type, false, "", 0) reads as the NARROWEST setting
|
|
126
|
+
// for that key, never as the default: no approvals, only the person who
|
|
127
|
+
// connected it, no commands, nothing automatic, no settings files, never
|
|
128
|
+
// pushed. `warn(text)` hears about each such key by name.
|
|
129
|
+
const KNOWN_KEYS = ['audience', 'approvals', 'commands', 'auto', 'allowPaths', 'base', 'remote']
|
|
130
|
+
const unique = (list) => [...new Set(list)]
|
|
131
|
+
function clean(entry, warn = () => {}) {
|
|
132
|
+
const has = (key) =>
|
|
133
|
+
entry !== null && typeof entry === 'object' && !Array.isArray(entry) && Object.hasOwn(entry, key) && entry[key] !== undefined
|
|
134
|
+
if (entry !== undefined && entry !== null && (typeof entry !== 'object' || Array.isArray(entry))) {
|
|
135
|
+
warn('this connection\'s entry is not an object: read as "read and answer only, for the person who connected it"')
|
|
136
|
+
return { ...DEFAULT_MACHINE_POLICY, audience: 'owner', remote: null }
|
|
137
|
+
}
|
|
138
|
+
for (const key of Object.keys(entry || {})) if (!KNOWN_KEYS.includes(key)) warn(`"${key}" is not a setting this version knows: ignored`)
|
|
139
|
+
const bad = (key, narrow) => {
|
|
140
|
+
warn(`"${key}" has a value this version does not know: read as ${narrow}`)
|
|
141
|
+
}
|
|
142
|
+
let audience = DEFAULT_MACHINE_POLICY.audience
|
|
143
|
+
if (has('audience')) {
|
|
144
|
+
if (POLICY_AUDIENCES.includes(entry.audience)) audience = entry.audience
|
|
145
|
+
else {
|
|
146
|
+
audience = 'owner'
|
|
147
|
+
bad('audience', '"owner" (only the person who connected it)')
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
let approvals = 'none'
|
|
151
|
+
if (has('approvals')) {
|
|
152
|
+
if (POLICY_APPROVALS.includes(entry.approvals)) approvals = entry.approvals
|
|
153
|
+
else bad('approvals', '"none" (read and answer only)')
|
|
154
|
+
}
|
|
155
|
+
const on = approvals !== 'none'
|
|
156
|
+
// A list with one entry it cannot read is not trusted for the rest either.
|
|
157
|
+
const list = (key, each, max) => {
|
|
158
|
+
if (!has(key)) return []
|
|
159
|
+
const value = entry[key]
|
|
160
|
+
if (!Array.isArray(value) || value.length > max || value.some((item) => each(item) === null)) {
|
|
161
|
+
bad(key, 'empty')
|
|
162
|
+
return []
|
|
163
|
+
}
|
|
164
|
+
return unique(value.map(each))
|
|
165
|
+
}
|
|
166
|
+
const commands = list('commands', cleanCommand, COMMANDS_MAX)
|
|
167
|
+
const auto = list('auto', (kind) => (POLICY_AUTO_KINDS.includes(kind) ? kind : null), POLICY_AUTO_KINDS.length)
|
|
168
|
+
const allowPaths = list('allowPaths', cleanAllowPath, ALLOW_PATHS_MAX)
|
|
169
|
+
let base = null
|
|
170
|
+
if (has('base') && entry.base !== null) {
|
|
171
|
+
base = cleanBase(entry.base)
|
|
172
|
+
if (!base) bad('base', 'none (the commit the checkout is on)')
|
|
173
|
+
}
|
|
174
|
+
let remote = DEFAULT_MACHINE_POLICY.remote
|
|
175
|
+
if (has('remote')) {
|
|
176
|
+
remote = entry.remote === null ? null : cleanRemote(entry.remote)
|
|
177
|
+
if (entry.remote !== null && !remote) bad('remote', 'none (a fix is never pushed)')
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
audience,
|
|
181
|
+
approvals,
|
|
182
|
+
commands: on ? commands : [],
|
|
183
|
+
auto: on ? auto.filter((kind) => kind !== 'commands' || commands.length > 0) : [],
|
|
184
|
+
allowPaths: on ? allowPaths : [],
|
|
185
|
+
base,
|
|
186
|
+
remote,
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export { clean as cleanMachinePolicy }
|
|
190
|
+
|
|
191
|
+
// The policy for one connection (defaults when nothing is saved).
|
|
192
|
+
// `warn(text)`: told about every value in the file that was not understood.
|
|
193
|
+
export function readMachinePolicy(origin, slug = 'default', env = process.env, { warn = () => {} } = {}) {
|
|
194
|
+
const file = policyFile(env)
|
|
195
|
+
return clean(readAll(file).connections[`${origin}#${slug}`], (text) => warn(`${file}: ${text}`))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Saves what was chosen for one connection; returns the policy in effect.
|
|
199
|
+
export function saveMachinePolicy(origin, slug, changes, env = process.env) {
|
|
200
|
+
const file = policyFile(env)
|
|
201
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
|
|
202
|
+
const data = readAll(file)
|
|
203
|
+
const next = clean({ ...clean(data.connections[`${origin}#${slug}`]), ...changes })
|
|
204
|
+
data.version = 1
|
|
205
|
+
data.connections[`${origin}#${slug}`] = next
|
|
206
|
+
const temp = `${file}.${process.pid}.${randomBytes(8).toString('hex')}.tmp`
|
|
207
|
+
try {
|
|
208
|
+
fs.writeFileSync(temp, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600, flag: 'wx' })
|
|
209
|
+
fs.renameSync(temp, file)
|
|
210
|
+
} catch (error) {
|
|
211
|
+
fs.rmSync(temp, { force: true })
|
|
212
|
+
throw error
|
|
213
|
+
}
|
|
214
|
+
if (process.platform !== 'win32') fs.chmodSync(file, 0o600)
|
|
215
|
+
return next
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// The policy in one line each, for the terminal when the agent connects.
|
|
219
|
+
export function describeMachinePolicy(policy) {
|
|
220
|
+
if (policy.approvals === 'none') return ['Reads and answers. No edits, no commands (this machine refuses both).']
|
|
221
|
+
const who = policy.approvals === 'owner' ? 'the person who connected it' : 'the people the server lets answer (admins by default)'
|
|
222
|
+
const lines = [
|
|
223
|
+
`Edits: asked on the board, allowed by ${who}. Each fix is made on a new branch in a folder of its own, never in this checkout.`,
|
|
224
|
+
policy.commands.length
|
|
225
|
+
? `Commands: only ${policy.commands.map((command) => `"${command}"`).join(', ')}, each asked on the board.`
|
|
226
|
+
: 'Commands: never.',
|
|
227
|
+
]
|
|
228
|
+
if (policy.auto.length)
|
|
229
|
+
lines.push(
|
|
230
|
+
`May be allowed without asking: ${policy.auto.join(' and ')}, once ${policy.approvals === 'owner' ? 'the person who connected it (nobody else)' : "someone with the agent's rule-changing role (an admin by default)"} switches that on in the console. Still only inside the project, never a file that runs something, and such a fix is not pushed until a person has looked at its whole diff on the board.`,
|
|
231
|
+
)
|
|
232
|
+
if (policy.allowPaths.length) lines.push(`Settings files it may ask to edit: ${policy.allowPaths.join(', ')}.`)
|
|
233
|
+
lines.push(
|
|
234
|
+
policy.base
|
|
235
|
+
? `A fix starts from the branch ${policy.base}.`
|
|
236
|
+
: 'A fix starts from the commit this checkout is on (refused while it has uncommitted changes; --base BRANCH lifts that).',
|
|
237
|
+
)
|
|
238
|
+
lines.push(
|
|
239
|
+
policy.remote ? `A finished fix is pushed to the remote "${policy.remote}" when this machine may push there.` : 'Fixes are not pushed.',
|
|
240
|
+
)
|
|
241
|
+
return lines
|
|
242
|
+
}
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
// folder 0700, the file 0600, written whole through a temporary file. The
|
|
4
4
|
// --remote commands read it when $SHAPEWALL_TOKEN and --token-file are not
|
|
5
5
|
// set. Nothing here ever prints a token.
|
|
6
|
+
//
|
|
7
|
+
// A connected agent (`shapewall agent connect`) has a token of its own,
|
|
8
|
+
// kept apart under `agents`, by server and connection name ("default"
|
|
9
|
+
// unless --as names another): the same file, the same rules.
|
|
6
10
|
|
|
7
11
|
import fs from 'node:fs'
|
|
8
12
|
import { randomBytes } from 'node:crypto'
|
|
@@ -40,14 +44,44 @@ export function readCredential(origin, env = process.env) {
|
|
|
40
44
|
return entry && typeof entry.token === 'string' ? entry : null
|
|
41
45
|
}
|
|
42
46
|
|
|
47
|
+
// The saved token of a connected agent: one per server and connection name.
|
|
48
|
+
export const AGENT_SLUG_RE = /^[a-z0-9][a-z0-9-]{0,31}$/
|
|
49
|
+
const agentKey = (origin, slug) => `${origin}#${slug}`
|
|
50
|
+
export function readAgentCredential(origin, slug = 'default', env = process.env) {
|
|
51
|
+
const entry = readAll(credentialsFile(env)).agents?.[agentKey(origin, slug)]
|
|
52
|
+
return entry && typeof entry.token === 'string' ? entry : null
|
|
53
|
+
}
|
|
54
|
+
export function saveAgentCredential(origin, slug, entry, env = process.env) {
|
|
55
|
+
return save(env, (data) => {
|
|
56
|
+
data.agents = data.agents && typeof data.agents === 'object' && !Array.isArray(data.agents) ? data.agents : {}
|
|
57
|
+
data.agents[agentKey(origin, slug)] = entry
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Forgets a connected agent's saved token (the server refused it).
|
|
62
|
+
export function forgetAgentCredential(origin, slug = 'default', env = process.env) {
|
|
63
|
+
if (!readAgentCredential(origin, slug, env)) return false
|
|
64
|
+
save(env, (data) => {
|
|
65
|
+
delete data.agents[agentKey(origin, slug)]
|
|
66
|
+
})
|
|
67
|
+
return true
|
|
68
|
+
}
|
|
69
|
+
|
|
43
70
|
export function saveCredential(origin, entry, env = process.env) {
|
|
71
|
+
return save(env, (data) => {
|
|
72
|
+
data.servers[origin] = entry
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Reads the file, lets `change` edit it, and writes it back whole.
|
|
77
|
+
function save(env, change) {
|
|
44
78
|
const file = credentialsFile(env)
|
|
45
79
|
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
|
|
46
80
|
// mkdir's mode only applies to a folder it creates: make an existing one private too.
|
|
47
81
|
if (process.platform !== 'win32') fs.chmodSync(path.dirname(file), 0o700)
|
|
48
82
|
const data = readAll(file)
|
|
49
83
|
data.version = 1
|
|
50
|
-
data
|
|
84
|
+
change(data)
|
|
51
85
|
// A fresh, unguessable temporary file ('wx' never follows a planted symlink).
|
|
52
86
|
const temp = `${file}.${process.pid}.${randomBytes(8).toString('hex')}.tmp`
|
|
53
87
|
try {
|
package/src/remote/login.js
CHANGED
|
@@ -85,12 +85,21 @@ export async function login(
|
|
|
85
85
|
sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
86
86
|
signal = null,
|
|
87
87
|
purpose = null,
|
|
88
|
+
// `shapewall agent connect`: what this machine will run ({ kind, name }),
|
|
89
|
+
// shown on the approval page, and what to do with the approved token
|
|
90
|
+
// in place of saving it as this server's login.
|
|
91
|
+
agent = null,
|
|
92
|
+
onApproved = null,
|
|
88
93
|
} = {},
|
|
89
94
|
) {
|
|
90
95
|
out = safeOut(out)
|
|
91
96
|
const origin = remoteOrigin(flags, env)
|
|
92
97
|
const machine = os.hostname().split('.')[0] || 'this computer'
|
|
93
|
-
const started = await post(fetchImpl, origin, '/api/v1/device/start', {
|
|
98
|
+
const started = await post(fetchImpl, origin, '/api/v1/device/start', {
|
|
99
|
+
name: machine,
|
|
100
|
+
...(['import', 'agent'].includes(purpose) ? { purpose } : {}),
|
|
101
|
+
...(purpose === 'agent' && agent ? { agent } : {}),
|
|
102
|
+
})
|
|
94
103
|
if (started.status !== 201 || !started.data?.deviceCode) {
|
|
95
104
|
throw new Error(
|
|
96
105
|
terminalSafe(started.data?.error?.message) || `Could not start a login on ${origin} (${started.status}). Is it a Shapewall server?`,
|
|
@@ -102,7 +111,7 @@ export async function login(
|
|
|
102
111
|
// The page is ours to build: the server's verificationUrl is not used.
|
|
103
112
|
const page = deviceLoginUrl(origin)
|
|
104
113
|
if (!page) throw new Error(`Cannot build a login page for ${origin}; open its /device page yourself.`)
|
|
105
|
-
out(
|
|
114
|
+
out(`${purpose === 'agent' ? 'To connect this agent' : 'To log in'}, open ${page} and enter this code:`)
|
|
106
115
|
out('')
|
|
107
116
|
out(` ${userCode}`)
|
|
108
117
|
out('')
|
|
@@ -130,6 +139,7 @@ export async function login(
|
|
|
130
139
|
}
|
|
131
140
|
const result = polled.data
|
|
132
141
|
if (typeof result.token !== 'string' || !/^swp_[A-Za-z0-9_-]{43}$/.test(result.token)) throw new Error('The server sent no token.')
|
|
142
|
+
if (onApproved) return onApproved(result, { origin, machine })
|
|
133
143
|
const file = saveCredential(
|
|
134
144
|
origin,
|
|
135
145
|
{
|