slatebox 0.0.0-stage → 0.2.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.md +21 -0
- package/README.md +231 -2
- package/bin/slatebox.mjs +9 -0
- package/package.json +44 -4
- package/src/agent-prompt.mjs +79 -0
- package/src/ai/settings-adapter.mjs +198 -0
- package/src/answers.mjs +513 -0
- package/src/cli.mjs +711 -0
- package/src/cloud.mjs +261 -0
- package/src/errors.mjs +23 -0
- package/src/image.mjs +141 -0
- package/src/interview.mjs +199 -0
- package/src/move.mjs +447 -0
- package/src/output.mjs +51 -0
- package/src/plan.mjs +51 -0
- package/src/prompt.mjs +87 -0
- package/src/record.mjs +46 -0
- package/src/render/compose.mjs +47 -0
- package/src/render/helm.mjs +78 -0
- package/src/render/k8s.mjs +204 -0
- package/src/render/settings.mjs +67 -0
- package/src/runner.mjs +50 -0
- package/src/secret-values.mjs +45 -0
- package/src/secrets.mjs +124 -0
- package/src/setup-link.mjs +88 -0
- package/src/targets/address.mjs +281 -0
- package/src/targets/docker.mjs +797 -0
- package/src/targets/export.mjs +73 -0
- package/src/targets/hosts.mjs +202 -0
- package/src/targets/k8s.mjs +405 -0
- package/src/yaml.mjs +293 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// The first admin. Nobody can sign up on a new self-hosted Slatebox until its one-time setup link
|
|
2
|
+
// is used: the person who opens it creates the first account, the server administrator and the
|
|
3
|
+
// owner of the first team. The image makes the link (`slatebox setup-link` in the container: single
|
|
4
|
+
// use, 24 hours, only its hash kept, a new one replaces the old).
|
|
5
|
+
//
|
|
6
|
+
// The link is as good as an admin password, so the installer prints it only to a terminal, on
|
|
7
|
+
// stderr, never into --json or a pipe (`slatebox setup-link --force-stdout` is the one way to pipe
|
|
8
|
+
// it), and its token is added to the redactor the moment it is known.
|
|
9
|
+
|
|
10
|
+
const lastJson = text => {
|
|
11
|
+
try { return JSON.parse(String(text || '').trim().split('\n').pop()) } catch { return null }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Whether this run may print a setup link: a person at a terminal, not --json. */
|
|
15
|
+
export const showsSetupLink = ctx => Boolean(ctx.tty) && !ctx.json
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The image's setup state through exec(args) (docker exec or kubectl exec of the in-image
|
|
19
|
+
* `slatebox`): { setUp, linkExpiresAt }, or null when the image has no setup links (before 0.2.0).
|
|
20
|
+
*/
|
|
21
|
+
export async function setupState(exec) {
|
|
22
|
+
const result = await exec(['setup-link', '--check'])
|
|
23
|
+
if (result.code !== 0) return null
|
|
24
|
+
const state = lastJson(result.stdout)
|
|
25
|
+
return state && typeof state.setUp === 'boolean' ? state : null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A fresh link ({ url, expiresAt }), { setUp: true } when there is nothing to set up, or { error }. */
|
|
29
|
+
export async function makeSetupLink(ctx, exec) {
|
|
30
|
+
const result = await exec(['setup-link', '--json'])
|
|
31
|
+
if (result.code === 3) return { setUp: true }
|
|
32
|
+
const link = result.code === 0 ? lastJson(result.stdout) : null
|
|
33
|
+
if (!link?.url) return { error: String(result.stderr || '').trim().split('\n').pop() || `exit ${result.code}` }
|
|
34
|
+
const token = link.url.split('/setup/')[1]
|
|
35
|
+
ctx.redactor.add(token)
|
|
36
|
+
return link
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* After an install or change is healthy: when the server still waits for its first admin, make a
|
|
41
|
+
* link if this run may print it (ctx.setupLink = { url, expiresAt }), else note that one is
|
|
42
|
+
* needed (ctx.setupLink = { pending: true }). Images without setup links are left alone.
|
|
43
|
+
*/
|
|
44
|
+
export async function firstAdmin(ctx, exec) {
|
|
45
|
+
const state = await setupState(exec)
|
|
46
|
+
if (!state) return false
|
|
47
|
+
if (state.setUp) return true
|
|
48
|
+
if (!showsSetupLink(ctx)) {
|
|
49
|
+
ctx.setupLink = { pending: true }
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
const link = await makeSetupLink(ctx, exec)
|
|
53
|
+
if (link.url) ctx.setupLink = link
|
|
54
|
+
else if (!link.setUp) {
|
|
55
|
+
ctx.out.step('setup', 'warn', `The one-time setup link could not be made (${link.error}). Run slatebox setup-link to try again.`)
|
|
56
|
+
ctx.setupLink = { pending: true }
|
|
57
|
+
}
|
|
58
|
+
return true
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A run that changed nothing (a re-run, an upgrade with no newer image) on a server still waiting
|
|
63
|
+
* for its first admin: say how to get a link, but make none, since a new link would silently end
|
|
64
|
+
* the one the person may already hold (the same rule as `slatebox status`).
|
|
65
|
+
*/
|
|
66
|
+
export async function stillWaiting(ctx, exec) {
|
|
67
|
+
const state = await setupState(exec)
|
|
68
|
+
if (state && !state.setUp) ctx.setupLink = { pending: true }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const command = name => `slatebox setup-link${name === 'slatebox' ? '' : ` --name ${name}`}`
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The closing lines about the first admin. A line { terminal: text } carries the link itself and is
|
|
75
|
+
* written straight to the terminal (stderr), past the redactor that would hide it.
|
|
76
|
+
*/
|
|
77
|
+
export function setupLines(ctx, name) {
|
|
78
|
+
const link = ctx.setupLink
|
|
79
|
+
if (!link) return []
|
|
80
|
+
if (link.pending) {
|
|
81
|
+
return ['', 'Nobody can sign up until the first admin exists. Get the one-time setup link at a terminal:',
|
|
82
|
+
` ${command(name)}`, 'Open it to create the first account: the server administrator and the owner of the first team.']
|
|
83
|
+
}
|
|
84
|
+
return ['', 'Create the first account (the server administrator) with this one-time setup link.',
|
|
85
|
+
'It works once and expires in 24 hours; nobody can sign up before it is used:',
|
|
86
|
+
{ terminal: ` ${link.url} (printed to this terminal only)` },
|
|
87
|
+
`Lost it? ${command(name)} prints a new one and ends this one.`]
|
|
88
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// A team address, https://<name>.slatebox.team, for any install target: check the name, sign in,
|
|
2
|
+
// claim it on Slatebox Cloud, keep the install credential on this computer, and hand the tunnel
|
|
3
|
+
// connector token to the target's secrets. The container's own cloudflared then connects out to
|
|
4
|
+
// Cloudflare: no DNS, no open ports, no certificates.
|
|
5
|
+
//
|
|
6
|
+
// Every function here is safe to run again: a claim retried with the same client key is the same
|
|
7
|
+
// claim, and an install that already has its token needs no network at all.
|
|
8
|
+
import { createHash } from 'node:crypto'
|
|
9
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
import {
|
|
12
|
+
CloudError, cloudBase, cloudClient, credentialsPath, deviceLabel, newClientKey, readCredentials, removeCredentials,
|
|
13
|
+
signIn, TEAM_ADDRESS_RULE, teamAddressProblem, teamHost, writeCredentials,
|
|
14
|
+
} from '../cloud.mjs'
|
|
15
|
+
import { EXIT, InstallError } from '../errors.mjs'
|
|
16
|
+
import { installDir } from '../record.mjs'
|
|
17
|
+
|
|
18
|
+
export const TUNNEL_TOKEN = 'SLATEBOX_TUNNEL_TOKEN'
|
|
19
|
+
const PENDING_FILE = 'claim.json'
|
|
20
|
+
const E2E_PREFIX = 'e2e-'
|
|
21
|
+
|
|
22
|
+
function client(ctx, base) {
|
|
23
|
+
ctx.cloud ??= cloudClient(ctx, base || cloudBase(ctx))
|
|
24
|
+
return ctx.cloud
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A team address that cannot be had: the same answer for taken, reserved and blocked names. */
|
|
28
|
+
function unavailable(name, suggestions = []) {
|
|
29
|
+
const free = suggestions?.length ? `\n Free right now: ${suggestions.join(', ')}` : ''
|
|
30
|
+
return new InstallError(EXIT.NEEDS_INPUT, `${teamHost(name)} isn't available.${free}\n Choose another: --team-address <name>`, { id: 'address-unavailable', field: 'address.team', suggestions: suggestions || [] })
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function cloudFailure(error, name) {
|
|
34
|
+
if (error instanceof InstallError) return error
|
|
35
|
+
if (!(error instanceof CloudError)) return error
|
|
36
|
+
switch (error.code) {
|
|
37
|
+
case 'unavailable': return unavailable(name, error.suggestions)
|
|
38
|
+
case 'invalid_name': return new InstallError(EXIT.USAGE, `"${name}" is not a team address: ${TEAM_ADDRESS_RULE}.`, { field: 'address.team' })
|
|
39
|
+
case 'limit_reached': return new InstallError(EXIT.PRECONDITION, `Your Slatebox account already has the most team addresses it may hold, or claimed too many today. ${error.message} Release one you no longer use (slatebox address release on that install), or use your own domain: --domain boards.example.com`, { id: 'address-limit' })
|
|
40
|
+
case 'rate_limited': return new InstallError(EXIT.PRECONDITION, `Slatebox Cloud asks you to wait${error.retryAfter ? ` ${error.retryAfter} seconds` : ''} before trying again. Nothing was changed.`, { id: 'rate-limited', retryAfter: error.retryAfter })
|
|
41
|
+
case 'paused': return new InstallError(EXIT.PRECONDITION, 'New team addresses are paused on Slatebox Cloud right now. Nothing was changed. Try again later, or use --domain or --local.', { id: 'address-paused' })
|
|
42
|
+
case 'suspended': return new InstallError(EXIT.PRECONDITION, `${teamHost(name)} is suspended: ${error.notice || error.message}. Your Slatebox keeps running on this computer; contact abuse@slatebox.com about the address.`, { id: 'address-suspended' })
|
|
43
|
+
case 'upstream_unavailable': return new InstallError(EXIT.PRECONDITION, 'Slatebox Cloud could not set up the secure tunnel just now (Cloudflare did not answer). Nothing was left half made; run the same command again in a minute.', { id: 'upstream-unavailable' })
|
|
44
|
+
case 'not_found': return new InstallError(EXIT.PRECONDITION, `Slatebox Cloud no longer knows the team address of this install (${teamHost(name)}). It may have been released. Run slatebox install --team-address <name> to claim one again.`, { id: 'address-gone' })
|
|
45
|
+
default: return new InstallError(EXIT.PRECONDITION, `Slatebox Cloud answered: ${error.message} (${error.code}, HTTP ${error.status}).`, { id: 'cloud-error', code: error.code })
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ------------------------------------------------------------------ check
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Before the plan: is the name ours already, or free to claim? Never signs in.
|
|
53
|
+
* @returns {Promise<{name: string, host: string, creds: object|null, claim: boolean}>}
|
|
54
|
+
*/
|
|
55
|
+
export async function checkTeamAddress(ctx, answers) {
|
|
56
|
+
const name = answers.address.team
|
|
57
|
+
const problem = teamAddressProblem(name)
|
|
58
|
+
if (problem) throw new InstallError(EXIT.USAGE, `"${name}" is not a team address: ${problem}.`, { field: 'address.team' })
|
|
59
|
+
const creds = readCredentials(ctx.home, answers.name)
|
|
60
|
+
if (creds && creds.name !== name) {
|
|
61
|
+
throw new InstallError(EXIT.USAGE, `"${answers.name}" already has the team address ${teamHost(creds.name)}. To move it: slatebox rename ${name}${answers.name === 'slatebox' ? '' : ` --name ${answers.name}`}. For a second Slatebox, give it its own --name.`, { field: 'address.team' })
|
|
62
|
+
}
|
|
63
|
+
if (creds) {
|
|
64
|
+
ctx.redactor.add(creds.installCredential)
|
|
65
|
+
return { name, host: teamHost(name), creds, claim: false }
|
|
66
|
+
}
|
|
67
|
+
const cloud = client(ctx)
|
|
68
|
+
// e2e-<anything> belongs to the end-to-end test's accounts, which the Cloud knows only after
|
|
69
|
+
// the sign-in: a signed-out check always says no, so the claim itself is the check.
|
|
70
|
+
if (name.startsWith(E2E_PREFIX)) return { name, host: teamHost(name), creds: null, claim: true }
|
|
71
|
+
ctx.out.step('address', 'start', `Checking ${teamHost(name)}`)
|
|
72
|
+
let answer
|
|
73
|
+
try { answer = await cloud.availability(name) } catch (error) { throw cloudFailure(error, name) }
|
|
74
|
+
if (!answer?.available) throw unavailable(name, answer?.suggestions)
|
|
75
|
+
ctx.out.step('address', 'ok', `${teamHost(name)} is available`)
|
|
76
|
+
return { name, host: teamHost(name), creds: null, claim: true }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** For the interview: the name if it is free, or the suggestions. Never signs in. */
|
|
80
|
+
export async function availability(ctx, name) {
|
|
81
|
+
try {
|
|
82
|
+
const answer = await client(ctx).availability(name)
|
|
83
|
+
return { available: Boolean(answer?.available), suggestions: answer?.suggestions || [] }
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof CloudError && error.code === 'invalid_name') return { available: false, suggestions: [], invalid: true }
|
|
86
|
+
throw cloudFailure(error, name)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ------------------------------------------------------------------ claim and attach
|
|
91
|
+
|
|
92
|
+
function pendingPath(ctx, install) { return join(installDir(ctx.home, install), PENDING_FILE) }
|
|
93
|
+
|
|
94
|
+
/** One client key per install and name, kept until the claim is done, so a retry is the same claim. */
|
|
95
|
+
function pendingKey(ctx, install, name) {
|
|
96
|
+
const path = pendingPath(ctx, install)
|
|
97
|
+
if (existsSync(path)) {
|
|
98
|
+
try {
|
|
99
|
+
const pending = JSON.parse(readFileSync(path, 'utf8'))
|
|
100
|
+
if (pending.name === name && pending.clientKey) return pending.clientKey
|
|
101
|
+
} catch { /* write a new one */ }
|
|
102
|
+
}
|
|
103
|
+
const clientKey = newClientKey()
|
|
104
|
+
mkdirSync(installDir(ctx.home, install), { recursive: true, mode: 0o700 })
|
|
105
|
+
writeFileSync(path, `${JSON.stringify({ name, clientKey })}\n`, { mode: 0o600 })
|
|
106
|
+
chmodSync(path, 0o600)
|
|
107
|
+
return clientKey
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function connectorToken(ctx, cloud, creds, { rotate = false } = {}) {
|
|
111
|
+
const answer = await cloud.connectorToken(creds.installCredential, { rotate })
|
|
112
|
+
if (!answer?.connectorToken) throw new InstallError(EXIT.PRECONDITION, 'Slatebox Cloud sent no tunnel token.')
|
|
113
|
+
ctx.redactor.add(answer.connectorToken)
|
|
114
|
+
return answer.connectorToken
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* After the plan was approved: the tunnel token for this install. Claims the name (with a
|
|
119
|
+
* sign-in) if this computer holds no credential for it yet; otherwise uses the token already in
|
|
120
|
+
* the install's secrets, or fetches it again with the install credential.
|
|
121
|
+
* @param {{name: string, creds: object|null, claim: boolean}} check from checkTeamAddress
|
|
122
|
+
* @param {Record<string,string>} stored the install's current secrets
|
|
123
|
+
* @returns {Promise<string>} the connector token
|
|
124
|
+
*/
|
|
125
|
+
export async function ensureTeamAddress(ctx, answers, check, stored = {}) {
|
|
126
|
+
if (ctx.tunnelToken) return ctx.tunnelToken
|
|
127
|
+
const { name } = check
|
|
128
|
+
if (check.creds && stored[TUNNEL_TOKEN]) {
|
|
129
|
+
ctx.redactor.add(stored[TUNNEL_TOKEN])
|
|
130
|
+
ctx.tunnelToken = stored[TUNNEL_TOKEN]
|
|
131
|
+
return ctx.tunnelToken
|
|
132
|
+
}
|
|
133
|
+
const cloud = client(ctx, check.creds?.cloudUrl)
|
|
134
|
+
if (check.creds) {
|
|
135
|
+
// The token file was lost (a new server, a wiped folder): fetch it again with the credential.
|
|
136
|
+
try {
|
|
137
|
+
ctx.tunnelToken = await connectorToken(ctx, cloud, check.creds)
|
|
138
|
+
ctx.out.step('address', 'ok', `Fetched the secure tunnel token for ${teamHost(name)} again`)
|
|
139
|
+
return ctx.tunnelToken
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (!(error instanceof CloudError && error.status === 401)) throw cloudFailure(error, name)
|
|
142
|
+
}
|
|
143
|
+
// The credential was revoked or replaced: sign in and mint a new one (this also rotates the tunnel secret).
|
|
144
|
+
const bearer = await signIn(ctx, cloud, { purpose: `reconnect ${teamHost(name)}` })
|
|
145
|
+
let minted
|
|
146
|
+
try { minted = await cloud.mintCredential(bearer, check.creds.claimId) } catch (error) { throw cloudFailure(error, name) }
|
|
147
|
+
const creds = { ...check.creds, installCredential: minted.installCredential, renewedAt: new Date().toISOString() }
|
|
148
|
+
ctx.redactor.add(creds.installCredential)
|
|
149
|
+
writeCredentials(ctx.home, answers.name, creds)
|
|
150
|
+
check.creds = creds
|
|
151
|
+
try { ctx.tunnelToken = minted.connectorToken || await connectorToken(ctx, cloud, creds) } catch (error) { throw cloudFailure(error, name) }
|
|
152
|
+
ctx.redactor.add(ctx.tunnelToken)
|
|
153
|
+
ctx.out.step('address', 'ok', `${teamHost(name)} has a new install credential`)
|
|
154
|
+
return ctx.tunnelToken
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const clientKey = pendingKey(ctx, answers.name, name)
|
|
158
|
+
const bearer = await signIn(ctx, cloud)
|
|
159
|
+
ctx.out.step('address', 'start', `Claiming ${teamHost(name)}`)
|
|
160
|
+
let claimed
|
|
161
|
+
try {
|
|
162
|
+
claimed = await cloud.claim(bearer, {
|
|
163
|
+
name, clientKey, label: ctx.deviceLabel || deviceLabel(),
|
|
164
|
+
idempotencyKey: createHash('sha256').update(`${name}|${clientKey}`).digest('hex'),
|
|
165
|
+
})
|
|
166
|
+
} catch (error) {
|
|
167
|
+
throw cloudFailure(error, name)
|
|
168
|
+
}
|
|
169
|
+
const claim = claimed?.claim
|
|
170
|
+
if (!claim?.id) throw new InstallError(EXIT.PRECONDITION, 'Slatebox Cloud did not confirm the claim.')
|
|
171
|
+
let installCredential = claimed.installCredential
|
|
172
|
+
// A replayed claim (the first answer was lost) carries no credential: mint one.
|
|
173
|
+
if (!installCredential) {
|
|
174
|
+
try { installCredential = (await cloud.mintCredential(bearer, claim.id)).installCredential } catch (error) { throw cloudFailure(error, name) }
|
|
175
|
+
}
|
|
176
|
+
ctx.redactor.add(installCredential)
|
|
177
|
+
const creds = { claimId: claim.id, name: claim.name || name, host: claim.host || teamHost(name), cloudUrl: cloud.base, installCredential, claimedAt: new Date().toISOString() }
|
|
178
|
+
writeCredentials(ctx.home, answers.name, creds)
|
|
179
|
+
rmSync(pendingPath(ctx, answers.name), { force: true })
|
|
180
|
+
check.creds = creds
|
|
181
|
+
check.claim = false
|
|
182
|
+
const of = claimed.count && claimed.limit ? ` (${claimed.count} of ${claimed.limit} team addresses on this account)` : ''
|
|
183
|
+
ctx.out.step('address', 'ok', `Claimed ${creds.host}${of}`)
|
|
184
|
+
try {
|
|
185
|
+
ctx.tunnelToken = claimed.connectorToken || await connectorToken(ctx, cloud, creds)
|
|
186
|
+
} catch (error) {
|
|
187
|
+
throw cloudFailure(error, name)
|
|
188
|
+
}
|
|
189
|
+
ctx.redactor.add(ctx.tunnelToken)
|
|
190
|
+
return ctx.tunnelToken
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ------------------------------------------------------------------ day two
|
|
194
|
+
|
|
195
|
+
function requireCreds(ctx, record) {
|
|
196
|
+
const creds = readCredentials(ctx.home, record.name)
|
|
197
|
+
if (!creds) {
|
|
198
|
+
throw new InstallError(EXIT.PRECONDITION, `"${record.name}" has no team address on this computer (no ${credentialsPath(ctx.home, record.name)}).${record.address?.team ? ' Run slatebox install again to sign in and reconnect it.' : ' Add one with: slatebox install --team-address <name>'}`)
|
|
199
|
+
}
|
|
200
|
+
ctx.redactor.add(creds.installCredential)
|
|
201
|
+
return creds
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The address as Slatebox Cloud sees it: active, suspended (with the notice), or unknown when offline. */
|
|
205
|
+
export async function teamStatus(ctx, record) {
|
|
206
|
+
const creds = readCredentials(ctx.home, record.name)
|
|
207
|
+
if (!creds) return { name: record.address.team, host: teamHost(record.address.team), status: 'no credential on this computer' }
|
|
208
|
+
ctx.redactor.add(creds.installCredential)
|
|
209
|
+
try {
|
|
210
|
+
const answer = await client(ctx, creds.cloudUrl).self(creds.installCredential)
|
|
211
|
+
// {claim: {id, name, host, status, suspension?}} (a flat claim is read the same way).
|
|
212
|
+
const self = answer?.claim || answer || {}
|
|
213
|
+
return { name: self.name || creds.name, host: self.host || creds.host, status: self.status || 'unknown', suspension: self.suspension || null }
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (error instanceof InstallError) return { name: creds.name, host: creds.host, status: 'unknown (Slatebox Cloud not reachable)' }
|
|
216
|
+
if (error instanceof CloudError && error.code === 'suspended') return { name: creds.name, host: creds.host, status: 'suspended', suspension: { notice: error.notice || error.message } }
|
|
217
|
+
return { name: creds.name, host: creds.host, status: `unknown (${error.code || error.message})` }
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** A new tunnel secret: every connector holding the old token is dropped. Returns the new token. */
|
|
222
|
+
export async function rotateTeamAddress(ctx, record) {
|
|
223
|
+
const creds = requireCreds(ctx, record)
|
|
224
|
+
try {
|
|
225
|
+
ctx.tunnelToken = await connectorToken(ctx, client(ctx, creds.cloudUrl), creds, { rotate: true })
|
|
226
|
+
} catch (error) {
|
|
227
|
+
throw cloudFailure(error, creds.name)
|
|
228
|
+
}
|
|
229
|
+
return ctx.tunnelToken
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Move the address to a new name. The old name keeps working until renameComplete. */
|
|
233
|
+
export async function renameTeamAddress(ctx, record, next) {
|
|
234
|
+
const problem = teamAddressProblem(next)
|
|
235
|
+
if (problem) throw new InstallError(EXIT.USAGE, `"${next}" is not a team address: ${problem}.`, { field: 'address.team' })
|
|
236
|
+
const creds = requireCreds(ctx, record)
|
|
237
|
+
if (creds.name === next) throw new InstallError(EXIT.USAGE, `"${record.name}" is already at ${teamHost(next)}.`)
|
|
238
|
+
const cloud = client(ctx, creds.cloudUrl)
|
|
239
|
+
const bearer = await signIn(ctx, cloud, { purpose: `move ${teamHost(creds.name)} to ${teamHost(next)}` })
|
|
240
|
+
let answer
|
|
241
|
+
try { answer = await cloud.rename(bearer, creds.claimId, next) } catch (error) { throw cloudFailure(error, next) }
|
|
242
|
+
const renamed = { ...creds, previousName: creds.name, name: answer?.claim?.name || next, host: answer?.claim?.host || teamHost(next), renamedAt: new Date().toISOString() }
|
|
243
|
+
writeCredentials(ctx.home, record.name, renamed)
|
|
244
|
+
return renamed
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** After the install answers at the new name: drop the old one (its name is held for 30 days). */
|
|
248
|
+
export async function finishRename(ctx, record) {
|
|
249
|
+
const creds = requireCreds(ctx, record)
|
|
250
|
+
try {
|
|
251
|
+
await client(ctx, creds.cloudUrl).renameComplete(creds.installCredential)
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (error instanceof InstallError) return { done: false, why: error.message }
|
|
254
|
+
throw cloudFailure(error, creds.name)
|
|
255
|
+
}
|
|
256
|
+
const { previousName, ...rest } = creds
|
|
257
|
+
writeCredentials(ctx.home, record.name, rest)
|
|
258
|
+
return { done: true, previousName }
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Give the address back. With offlineOk, an unreachable Cloud is not an error: the credential is
|
|
263
|
+
* kept so `slatebox address release` can finish later.
|
|
264
|
+
*/
|
|
265
|
+
export async function releaseTeamAddress(ctx, record, { offlineOk = false } = {}) {
|
|
266
|
+
const creds = requireCreds(ctx, record)
|
|
267
|
+
try {
|
|
268
|
+
await client(ctx, creds.cloudUrl).releaseSelf(creds.installCredential)
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (error instanceof CloudError && (error.status === 404 || error.status === 401)) {
|
|
271
|
+
// Already released, or the credential was revoked by a release elsewhere.
|
|
272
|
+
} else if (offlineOk && error instanceof InstallError) {
|
|
273
|
+
return { released: false, host: creds.host, claimId: creds.claimId, why: error.message.split('\n')[0] }
|
|
274
|
+
} else {
|
|
275
|
+
throw cloudFailure(error, creds.name)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
removeCredentials(ctx.home, record.name)
|
|
279
|
+
rmSync(pendingPath(ctx, record.name), { force: true })
|
|
280
|
+
return { released: true, host: creds.host, claimId: creds.claimId }
|
|
281
|
+
}
|