@antasphere/cli-core 0.6.0 → 0.7.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/README.md +324 -96
- package/dist/auth.d.ts +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/config.d.ts +120 -73
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +207 -109
- package/dist/config.js.map +1 -1
- package/dist/connect-on-demand.d.ts +194 -132
- package/dist/connect-on-demand.d.ts.map +1 -1
- package/dist/connect-on-demand.js +561 -148
- package/dist/connect-on-demand.js.map +1 -1
- package/dist/connect.d.ts +8 -8
- package/dist/connect.d.ts.map +1 -1
- package/dist/index.d.ts +25 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -15
- package/dist/index.js.map +1 -1
- package/dist/kept-keys.d.ts +208 -0
- package/dist/kept-keys.d.ts.map +1 -0
- package/dist/kept-keys.js +484 -0
- package/dist/kept-keys.js.map +1 -0
- package/dist/lock.d.ts +65 -0
- package/dist/lock.d.ts.map +1 -0
- package/dist/lock.js +408 -0
- package/dist/lock.js.map +1 -0
- package/dist/migrate.d.ts +9 -3
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +32 -11
- package/dist/migrate.js.map +1 -1
- package/dist/resolve.d.ts +12 -4
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +12 -8
- package/dist/resolve.js.map +1 -1
- package/dist/sign-in.d.ts +266 -0
- package/dist/sign-in.d.ts.map +1 -0
- package/dist/sign-in.js +398 -0
- package/dist/sign-in.js.map +1 -0
- package/package.json +1 -1
- package/src/auth.ts +1 -1
- package/src/config.ts +303 -158
- package/src/connect-on-demand.ts +797 -271
- package/src/connect.ts +8 -8
- package/src/index.ts +40 -27
- package/src/kept-keys.ts +561 -0
- package/src/lock.ts +479 -0
- package/src/migrate.ts +33 -11
- package/src/resolve.ts +20 -15
- package/src/sign-in.ts +601 -0
package/dist/config.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CliUsageError } from './resolve.js';
|
|
1
2
|
/**
|
|
2
3
|
* The shared Antasphere CLI config home: `$XDG_CONFIG_HOME/antasphere/`
|
|
3
4
|
* (default `~/.config/antasphere/`) — ONE home for the whole tool family
|
|
@@ -14,38 +15,97 @@
|
|
|
14
15
|
* (e.g. tools/slideless.json)
|
|
15
16
|
*
|
|
16
17
|
* Each file holds one tool's `{ activeProfile?, profiles }`. A PROFILE IS AN
|
|
17
|
-
* INSTANCE: it names the
|
|
18
|
-
* tool's cloud URL)
|
|
19
|
-
* the tool's own key (`apiKey`, the
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
18
|
+
* INSTANCE (a site): it names the site it talks to (`baseUrl`; absent = the
|
|
19
|
+
* tool's cloud URL), remembers what that site last said of itself (`site`),
|
|
20
|
+
* and carries the credentials for it: the tool's own key (`apiKey`, the
|
|
21
|
+
* tool's login) and the KEPT KEYS the hub exchange minted (`hubKeys`, one
|
|
22
|
+
* per hub and account; `connectKeys` is their old shape, moved once), with
|
|
23
|
+
* the address of the hub its last exchange went through, in its normal form
|
|
24
|
+
* (`normalizeUrl`): `lastHub`. Profiles are named for their host by default
|
|
25
|
+
* (`cloud` for the cloud site, `share.example.com`, `localhost:3400`); the
|
|
26
|
+
* person may rename them and chooses the default with `<tool> use`. In the
|
|
27
|
+
* hub's own file, an entry
|
|
28
|
+
* that holds a key is a LOGIN (`listLogins`), which additionally carries
|
|
29
|
+
* `workspaceId`, the default organization at login (informational; the
|
|
30
|
+
* hub's server-side default is what applies).
|
|
26
31
|
*
|
|
27
32
|
* Files store API keys, so directories are created 0700 and files kept 0600
|
|
28
33
|
* on every write. All I/O is synchronous and env-injected (no process.env
|
|
29
34
|
* reads) so tests can point the whole config world at a temp dir via
|
|
30
35
|
* XDG_CONFIG_HOME.
|
|
36
|
+
*
|
|
37
|
+
* Many commands share one file (agent sessions side by side on one
|
|
38
|
+
* machine), so a file is never left half written and never overwritten from
|
|
39
|
+
* a stale copy: a write goes to a temporary file in the same directory,
|
|
40
|
+
* renamed over the real one; every read-modify-write is one `updateConfig`,
|
|
41
|
+
* holding `<file>.lock` from its read to its rename; a file that exists and
|
|
42
|
+
* does not parse is a `CliConfigError` naming it, never an empty config; and
|
|
43
|
+
* nothing is written when nothing changed.
|
|
31
44
|
*/
|
|
32
45
|
export type CliEnv = Record<string, string | undefined>;
|
|
33
46
|
/**
|
|
34
|
-
* A
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
* A config file this package will not read or write: it does not parse, it
|
|
48
|
+
* cannot be read, or another command holds its lock and never lets go. The
|
|
49
|
+
* message names the file and what to do; nothing on disk was changed. A
|
|
50
|
+
* `CliUsageError`, so a runner prints it and exits 1.
|
|
51
|
+
*/
|
|
52
|
+
export declare class CliConfigError extends CliUsageError {
|
|
53
|
+
constructor(message: string);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The OLD shape of a kept key (cli-core 0.3 to 0.6): a tool-local key the
|
|
57
|
+
* hub exchange minted, filed in `connectKeys` under the NAME of the hub
|
|
58
|
+
* login that minted it. A name strands its key when the login is renamed or
|
|
59
|
+
* re-created, so the first command that resolves a credential on the
|
|
60
|
+
* profile moves each such slot once into `hubKeys` (its login still in
|
|
61
|
+
* place with the same address), or revokes it on the profile's own site and
|
|
62
|
+
* forgets it. Nothing writes this shape any more.
|
|
40
63
|
*/
|
|
41
64
|
export interface CliConnectKey {
|
|
42
|
-
/** Full tool-local API key (`slk_`-style)
|
|
65
|
+
/** Full tool-local API key (`slk_`-style). */
|
|
43
66
|
apiKey: string;
|
|
44
67
|
/** The signed-in account's email at mint. */
|
|
45
68
|
email?: string;
|
|
46
69
|
/** ISO timestamp of the exchange that minted the key. */
|
|
47
70
|
mintedAt?: string;
|
|
48
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* What a site said of itself in its unauthenticated discovery
|
|
74
|
+
* (`GET /api/v1/instance`), remembered on its profile so a command with a
|
|
75
|
+
* kept key costs zero network: read again on every exchange, on `login`, on
|
|
76
|
+
* `profiles --check`, and once a day before a kept key is served.
|
|
77
|
+
*/
|
|
78
|
+
export interface CliSiteFacts {
|
|
79
|
+
/** The Antasphere hub the site names (`auth.hub`, `normalizeUrl`'d); null = it names none. */
|
|
80
|
+
hub: string | null;
|
|
81
|
+
/** Whether it signs in through an Antasphere hub (the probe's `cloud`). */
|
|
82
|
+
federated: boolean;
|
|
83
|
+
/** Its discovery `name`. */
|
|
84
|
+
name: string | null;
|
|
85
|
+
/** The CLI version it announces (discovery `cli.version`). */
|
|
86
|
+
cliVersion: string | null;
|
|
87
|
+
/** ISO time these facts were read. */
|
|
88
|
+
seenAt: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A KEPT KEY: a tool-local key the hub exchange minted for the profile's
|
|
92
|
+
* site, filed by the hub's address and the account's e-mail, never by a
|
|
93
|
+
* login's name, so renaming a login or signing out and in again strands
|
|
94
|
+
* nothing. USER-scoped (hub ADR 014): one key serves every org. A profile
|
|
95
|
+
* holds one per `(hub, e-mail)` (the hub compared by `sameInstance`, the
|
|
96
|
+
* e-mail case-folded); a second one for the same pair is a key waiting for
|
|
97
|
+
* its revoke, never served.
|
|
98
|
+
*/
|
|
99
|
+
export interface CliHubKey {
|
|
100
|
+
/** The hub whose login minted it, `normalizeUrl`'d. */
|
|
101
|
+
hub: string;
|
|
102
|
+
/** The account's e-mail, as the hub answered it at mint. */
|
|
103
|
+
email: string;
|
|
104
|
+
/** Full tool-local API key (`slk_`-style). */
|
|
105
|
+
apiKey: string;
|
|
106
|
+
/** ISO time of the exchange that minted it. */
|
|
107
|
+
mintedAt: string;
|
|
108
|
+
}
|
|
49
109
|
export interface CliProfile {
|
|
50
110
|
/** `<prefix>_...` API key for this profile (the tool's own login). */
|
|
51
111
|
apiKey?: string;
|
|
@@ -58,10 +118,21 @@ export interface CliProfile {
|
|
|
58
118
|
* command). Tool profiles from a tool's own login may carry the
|
|
59
119
|
* workspace that login bound. */
|
|
60
120
|
workspaceId?: string;
|
|
61
|
-
/** Tool profiles only:
|
|
62
|
-
|
|
63
|
-
|
|
121
|
+
/** Tool profiles only: what the site last said of itself. */
|
|
122
|
+
site?: CliSiteFacts;
|
|
123
|
+
/** Tool profiles only: the kept keys, one per (hub, e-mail). */
|
|
124
|
+
hubKeys?: CliHubKey[];
|
|
125
|
+
/** Tool profiles only: kept keys of the OLD shape, filed by a login's
|
|
126
|
+
* name; moved once into `hubKeys` by the first command that resolves a
|
|
127
|
+
* credential on the profile. */
|
|
64
128
|
connectKeys?: Record<string, CliConnectKey>;
|
|
129
|
+
/** Tool profiles only: the address of the hub the profile's last successful
|
|
130
|
+
* exchange went through, in its normal form (`normalizeUrl`, its path
|
|
131
|
+
* kept; no key, no e-mail), written by every exchange
|
|
132
|
+
* with the key it minted. For a site that names no hub, the hub to sign in
|
|
133
|
+
* at again while a login is in place there. A value that is not a string
|
|
134
|
+
* is ignored. */
|
|
135
|
+
lastHub?: string;
|
|
65
136
|
}
|
|
66
137
|
export interface CliConfig {
|
|
67
138
|
activeProfile?: string;
|
|
@@ -73,66 +144,42 @@ export declare const HUB_TOOL = "hub";
|
|
|
73
144
|
export declare function assertToolNamespace(tool: string): void;
|
|
74
145
|
/** Where a tool's config lives for this environment; null when no home is known. */
|
|
75
146
|
export declare function configPath(env: CliEnv, tool: string): string | null;
|
|
76
|
-
/** Tolerant parse (shared with the migrator): corrupt or foreign shapes → empty. */
|
|
77
|
-
export declare function parseConfig(raw: string): CliConfig;
|
|
78
|
-
/** Load a tool's config; a missing/unreadable/corrupt file is an empty config. */
|
|
79
|
-
export declare function loadConfig(env: CliEnv, tool: string): CliConfig;
|
|
80
|
-
/** Persist a tool's config (0700 dirs / 0600 file). Throws when no home is known. */
|
|
81
|
-
export declare function saveConfig(env: CliEnv, tool: string, config: CliConfig): string;
|
|
82
|
-
/** Delete a tool's config file (config clear). No-op when absent. */
|
|
83
|
-
export declare function clearConfig(env: CliEnv, tool: string): void;
|
|
84
|
-
/** `ant_abcd1234_…` → `ant_abcd1234_…c3f9` — safe for terminals and logs. */
|
|
85
|
-
export declare function redactKey(key: string): string;
|
|
86
147
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* runs never misses the cache).
|
|
148
|
+
* Tolerant parse, kept for foreign files (the legacy migrator reads a
|
|
149
|
+
* pre-cli-core file with it): corrupt or foreign shapes → empty. A tool's
|
|
150
|
+
* own file is read strictly by `loadConfig`.
|
|
91
151
|
*/
|
|
92
|
-
export declare function
|
|
93
|
-
/**
|
|
94
|
-
* The cached connect key the connect-on-demand flow may SERVE: the slot's
|
|
95
|
-
* key, only when the hub profile carries an e-mail address, the cached
|
|
96
|
-
* entry carries one too, and the two are the same address (trimmed,
|
|
97
|
-
* case-insensitive). A key is the credential of the person who minted it
|
|
98
|
-
* (PRDCT-3032), so the cache serves only that person; a hub profile with no
|
|
99
|
-
* address is someone unknown, and an entry with no address was minted by
|
|
100
|
-
* someone unknown: neither is served (PRDCT-3046 F7). `lookupConnectKey`
|
|
101
|
-
* stays the plain slot read.
|
|
102
|
-
*/
|
|
103
|
-
export declare function cachedConnectKey(profile: CliProfile | undefined, hubProfileName: string, hubEmail: string | undefined): string | undefined;
|
|
104
|
-
/** How a profile signs in, for a `profiles`-style listing. */
|
|
105
|
-
export type ProfileCredential = 'key' | 'hub' | 'none';
|
|
152
|
+
export declare function parseConfig(raw: string): CliConfig;
|
|
106
153
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
154
|
+
* Load a tool's config. A missing file (or no config home) is an empty
|
|
155
|
+
* config; a file that exists must be a JSON object whose `profiles`, when
|
|
156
|
+
* present, is an object of objects, else it is read twice more over 150 ms
|
|
157
|
+
* (an older CLI may be rewriting it in place) and then throws a
|
|
158
|
+
* `CliConfigError` naming it. Reading takes no lock: a write is a rename.
|
|
111
159
|
*/
|
|
112
|
-
export declare function
|
|
113
|
-
export interface StoreConnectKeyOptions {
|
|
114
|
-
/**
|
|
115
|
-
* The instance the key was minted for. Written on the profile when it is
|
|
116
|
-
* created here or has no `baseUrl` of its own (a blank one counts as
|
|
117
|
-
* none); a `baseUrl` that is there is never overwritten (a profile is
|
|
118
|
-
* never repointed).
|
|
119
|
-
*/
|
|
120
|
-
baseUrl?: string;
|
|
121
|
-
}
|
|
160
|
+
export declare function loadConfig(env: CliEnv, tool: string): CliConfig;
|
|
122
161
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
162
|
+
* The one way to change a tool's config: a read-modify-write holding
|
|
163
|
+
* `<file>.lock` for its whole span, so two commands that both change
|
|
164
|
+
* something both keep it. `mutate` gets a draft of the file as it is now
|
|
165
|
+
* (strict `loadConfig`) and changes it in place, or returns the config to
|
|
166
|
+
* write; nothing is written when the result is the file's current bytes.
|
|
167
|
+
* Answers the file, whether it changed, and the config it now holds. Throws
|
|
168
|
+
* when no config home is known, and a `CliConfigError` when the file does
|
|
169
|
+
* not parse, when its lock is never released, or when the config to write is
|
|
170
|
+
* one the strict loader would refuse (nothing is written then).
|
|
129
171
|
*/
|
|
130
|
-
export declare function
|
|
172
|
+
export declare function updateConfig(env: CliEnv, tool: string, mutate: (config: CliConfig) => CliConfig | void): {
|
|
173
|
+
path: string;
|
|
174
|
+
changed: boolean;
|
|
175
|
+
config: CliConfig;
|
|
176
|
+
};
|
|
177
|
+
/** Delete a tool's config file (config clear), under its lock. No-op when absent. */
|
|
178
|
+
export declare function clearConfig(env: CliEnv, tool: string): void;
|
|
131
179
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* it belongs to the tool's own login flow.
|
|
180
|
+
* `ant_abcd1234_…` → `ant_abcd1234_…c3f9`.
|
|
181
|
+
*
|
|
182
|
+
* @deprecated No listing prints any part of a key any more, redacted or not.
|
|
136
183
|
*/
|
|
137
|
-
export declare function
|
|
184
|
+
export declare function redactKey(key: string): string;
|
|
138
185
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"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"}
|
package/dist/config.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { chmodSync, closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import { errorCode, sleepSync, withFileLock } from './lock.js';
|
|
5
|
+
import { CliUsageError } from './resolve.js';
|
|
6
|
+
/**
|
|
7
|
+
* A config file this package will not read or write: it does not parse, it
|
|
8
|
+
* cannot be read, or another command holds its lock and never lets go. The
|
|
9
|
+
* message names the file and what to do; nothing on disk was changed. A
|
|
10
|
+
* `CliUsageError`, so a runner prints it and exits 1.
|
|
11
|
+
*/
|
|
12
|
+
export class CliConfigError extends CliUsageError {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message, { exitCode: 1 });
|
|
15
|
+
this.name = 'CliConfigError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
3
18
|
/** The hub/account tool — its file is the config home's root `config.json`. */
|
|
4
19
|
export const HUB_TOOL = 'hub';
|
|
5
20
|
const TOOL_RE = /^[a-z][a-z0-9-]*$/;
|
|
@@ -19,7 +34,12 @@ export function configPath(env, tool) {
|
|
|
19
34
|
return tool === HUB_TOOL ? join(home, 'config.json') : join(home, 'tools', `${tool}.json`);
|
|
20
35
|
}
|
|
21
36
|
const EMPTY = { profiles: {} };
|
|
22
|
-
|
|
37
|
+
const NO_HOME = 'Cannot locate a config directory — set HOME or XDG_CONFIG_HOME, or pass --api-key/--api-url explicitly.';
|
|
38
|
+
/**
|
|
39
|
+
* Tolerant parse, kept for foreign files (the legacy migrator reads a
|
|
40
|
+
* pre-cli-core file with it): corrupt or foreign shapes → empty. A tool's
|
|
41
|
+
* own file is read strictly by `loadConfig`.
|
|
42
|
+
*/
|
|
23
43
|
export function parseConfig(raw) {
|
|
24
44
|
try {
|
|
25
45
|
const parsed = JSON.parse(raw);
|
|
@@ -33,133 +53,211 @@ export function parseConfig(raw) {
|
|
|
33
53
|
return { ...EMPTY, profiles: {} };
|
|
34
54
|
}
|
|
35
55
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
56
|
+
function isObject(value) {
|
|
57
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Why a parsed config file is refused, or undefined when it reads as one: a
|
|
61
|
+
* JSON object whose `profiles`, when present, is an object of objects. The
|
|
62
|
+
* checks of the strict loader, and of `updateConfig` before it writes.
|
|
63
|
+
*/
|
|
64
|
+
function invalidReason(parsed) {
|
|
65
|
+
if (!isObject(parsed))
|
|
66
|
+
return 'its top level is not an object';
|
|
67
|
+
const profiles = parsed.profiles === undefined ? {} : parsed.profiles;
|
|
68
|
+
if (!isObject(profiles))
|
|
69
|
+
return '"profiles" is not an object';
|
|
70
|
+
for (const [name, profile] of Object.entries(profiles)) {
|
|
71
|
+
if (!isObject(profile))
|
|
72
|
+
return `profile "${name}" is not an object`;
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
function readConfigFile(path) {
|
|
77
|
+
let raw;
|
|
78
|
+
try {
|
|
79
|
+
raw = readFileSync(path, 'utf8');
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
const code = errorCode(e);
|
|
83
|
+
if (code === 'ENOENT')
|
|
84
|
+
return { missing: true };
|
|
85
|
+
throw new CliConfigError(`The config file ${path} cannot be read (${code ?? 'unknown error'}). Nothing was changed; ` +
|
|
86
|
+
'fix it or move it aside, then retry.');
|
|
87
|
+
}
|
|
88
|
+
let parsed;
|
|
41
89
|
try {
|
|
42
|
-
|
|
90
|
+
parsed = JSON.parse(raw);
|
|
43
91
|
}
|
|
44
92
|
catch {
|
|
45
|
-
return {
|
|
93
|
+
return { invalid: 'it is not JSON' };
|
|
94
|
+
}
|
|
95
|
+
const invalid = invalidReason(parsed);
|
|
96
|
+
if (invalid !== undefined)
|
|
97
|
+
return { invalid };
|
|
98
|
+
// An object of objects, `invalidReason` says so.
|
|
99
|
+
const file = parsed;
|
|
100
|
+
const profiles = file.profiles === undefined ? {} : file.profiles;
|
|
101
|
+
// The two known keys in the file's own order (a non-string activeProfile
|
|
102
|
+
// and unknown keys are dropped), so a file this package wrote serializes
|
|
103
|
+
// back to its very bytes: a change that changes nothing writes nothing.
|
|
104
|
+
const known = {};
|
|
105
|
+
for (const key of Object.keys(file)) {
|
|
106
|
+
if (key === 'profiles')
|
|
107
|
+
known.profiles = profiles;
|
|
108
|
+
else if (key === 'activeProfile' && typeof file.activeProfile === 'string') {
|
|
109
|
+
known.activeProfile = file.activeProfile;
|
|
110
|
+
}
|
|
46
111
|
}
|
|
112
|
+
return { config: { ...known, profiles: known.profiles ?? {} } };
|
|
47
113
|
}
|
|
48
|
-
/**
|
|
49
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Load a tool's config. A missing file (or no config home) is an empty
|
|
116
|
+
* config; a file that exists must be a JSON object whose `profiles`, when
|
|
117
|
+
* present, is an object of objects, else it is read twice more over 150 ms
|
|
118
|
+
* (an older CLI may be rewriting it in place) and then throws a
|
|
119
|
+
* `CliConfigError` naming it. Reading takes no lock: a write is a rename.
|
|
120
|
+
*/
|
|
121
|
+
export function loadConfig(env, tool) {
|
|
50
122
|
const path = configPath(env, tool);
|
|
51
|
-
if (!path)
|
|
52
|
-
|
|
123
|
+
if (!path)
|
|
124
|
+
return { ...EMPTY, profiles: {} };
|
|
125
|
+
let read = readConfigFile(path);
|
|
126
|
+
for (const pause of [50, 100]) {
|
|
127
|
+
if (!('invalid' in read))
|
|
128
|
+
break;
|
|
129
|
+
sleepSync(pause);
|
|
130
|
+
read = readConfigFile(path);
|
|
131
|
+
}
|
|
132
|
+
if ('invalid' in read) {
|
|
133
|
+
throw new CliConfigError(`The config file ${path} is not valid: ${read.invalid}. Nothing was changed; fix it or move it aside, ` +
|
|
134
|
+
'then retry.');
|
|
135
|
+
}
|
|
136
|
+
return 'missing' in read ? { ...EMPTY, profiles: {} } : read.config;
|
|
137
|
+
}
|
|
138
|
+
/** Rename over the target, retried for up to 1 s while Windows (a reader holding the file) refuses it. */
|
|
139
|
+
function renameOver(from, to) {
|
|
140
|
+
const deadline = Date.now() + 1_000;
|
|
141
|
+
for (;;) {
|
|
142
|
+
try {
|
|
143
|
+
renameSync(from, to);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
const code = errorCode(e);
|
|
148
|
+
if ((code !== 'EPERM' && code !== 'EACCES' && code !== 'EBUSY') || Date.now() >= deadline)
|
|
149
|
+
throw e;
|
|
150
|
+
sleepSync(10);
|
|
151
|
+
}
|
|
53
152
|
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Write a config file's bytes (`data`): nothing when it already holds them
|
|
156
|
+
* (`false`); otherwise a temporary file beside the target (0600, synced)
|
|
157
|
+
* renamed over it, so a reader sees the old file or the new one, never half
|
|
158
|
+
* of one (`true`). A symlinked file stays a symlink: its target is replaced.
|
|
159
|
+
*/
|
|
160
|
+
function writeConfigFile(path, data, home) {
|
|
161
|
+
let current;
|
|
162
|
+
try {
|
|
163
|
+
current = readFileSync(path);
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
if (errorCode(e) !== 'ENOENT')
|
|
167
|
+
throw e;
|
|
168
|
+
}
|
|
169
|
+
if (current?.equals(Buffer.from(data)))
|
|
170
|
+
return false;
|
|
54
171
|
const dir = dirname(path);
|
|
55
172
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
56
173
|
// mkdir's mode only applies on creation — re-assert on every write, on
|
|
57
174
|
// every directory level we own (tools/ AND the antasphere home itself).
|
|
58
175
|
chmodSync(dir, 0o700);
|
|
59
|
-
const home = dirname(configPath(env, HUB_TOOL));
|
|
60
176
|
if (dir !== home)
|
|
61
177
|
chmodSync(home, 0o700);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
178
|
+
let symlink = false;
|
|
179
|
+
try {
|
|
180
|
+
symlink = lstatSync(path).isSymbolicLink();
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
if (errorCode(e) !== 'ENOENT')
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
const target = symlink ? realpathSync(path) : path;
|
|
187
|
+
const tmp = join(dirname(target), `.${basename(target)}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`);
|
|
188
|
+
const fd = openSync(tmp, 'wx', 0o600);
|
|
189
|
+
let open = true;
|
|
190
|
+
try {
|
|
191
|
+
writeFileSync(fd, data);
|
|
192
|
+
fsyncSync(fd);
|
|
193
|
+
closeSync(fd);
|
|
194
|
+
open = false;
|
|
195
|
+
chmodSync(tmp, 0o600);
|
|
196
|
+
renameOver(tmp, target);
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
if (open) {
|
|
200
|
+
try {
|
|
201
|
+
closeSync(fd);
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
// The write's own error is the one to report.
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
rmSync(tmp, { force: true });
|
|
208
|
+
throw e;
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
65
211
|
}
|
|
66
|
-
/**
|
|
212
|
+
/**
|
|
213
|
+
* The one way to change a tool's config: a read-modify-write holding
|
|
214
|
+
* `<file>.lock` for its whole span, so two commands that both change
|
|
215
|
+
* something both keep it. `mutate` gets a draft of the file as it is now
|
|
216
|
+
* (strict `loadConfig`) and changes it in place, or returns the config to
|
|
217
|
+
* write; nothing is written when the result is the file's current bytes.
|
|
218
|
+
* Answers the file, whether it changed, and the config it now holds. Throws
|
|
219
|
+
* when no config home is known, and a `CliConfigError` when the file does
|
|
220
|
+
* not parse, when its lock is never released, or when the config to write is
|
|
221
|
+
* one the strict loader would refuse (nothing is written then).
|
|
222
|
+
*/
|
|
223
|
+
export function updateConfig(env, tool, mutate) {
|
|
224
|
+
const path = configPath(env, tool);
|
|
225
|
+
if (!path)
|
|
226
|
+
throw new Error(NO_HOME);
|
|
227
|
+
const home = dirname(configPath(env, HUB_TOOL));
|
|
228
|
+
return withFileLock(`${path}.lock`, () => {
|
|
229
|
+
const draft = structuredClone(loadConfig(env, tool));
|
|
230
|
+
const config = mutate(draft) ?? draft;
|
|
231
|
+
const data = `${JSON.stringify(config, null, 2)}\n`;
|
|
232
|
+
// What is written is read back by the strict loader: never write a file it would refuse.
|
|
233
|
+
let invalid;
|
|
234
|
+
try {
|
|
235
|
+
invalid = invalidReason(JSON.parse(data));
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
invalid = 'it is not JSON';
|
|
239
|
+
}
|
|
240
|
+
if (invalid !== undefined) {
|
|
241
|
+
throw new CliConfigError(`refusing to write an invalid config: ${invalid}. Nothing was written to ${path}.`);
|
|
242
|
+
}
|
|
243
|
+
return { path, changed: writeConfigFile(path, data, home), config };
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/** Delete a tool's config file (config clear), under its lock. No-op when absent. */
|
|
67
247
|
export function clearConfig(env, tool) {
|
|
68
248
|
const path = configPath(env, tool);
|
|
69
|
-
if (path
|
|
70
|
-
|
|
249
|
+
if (!path || !existsSync(path))
|
|
250
|
+
return;
|
|
251
|
+
withFileLock(`${path}.lock`, () => rmSync(path, { force: true }));
|
|
71
252
|
}
|
|
72
|
-
/**
|
|
253
|
+
/**
|
|
254
|
+
* `ant_abcd1234_…` → `ant_abcd1234_…c3f9`.
|
|
255
|
+
*
|
|
256
|
+
* @deprecated No listing prints any part of a key any more, redacted or not.
|
|
257
|
+
*/
|
|
73
258
|
export function redactKey(key) {
|
|
74
259
|
if (key.length <= 16)
|
|
75
260
|
return '****';
|
|
76
261
|
return `${key.slice(0, 13)}…${key.slice(-4)}`;
|
|
77
262
|
}
|
|
78
|
-
/**
|
|
79
|
-
* The cached user-scoped connect key a HUB PROFILE minted for this tool
|
|
80
|
-
* profile, if any. The slot is the hub profile NAME — stable across runs
|
|
81
|
-
* and orgs (the key identifies the user; selecting a different org between
|
|
82
|
-
* runs never misses the cache).
|
|
83
|
-
*/
|
|
84
|
-
export function lookupConnectKey(profile, hubProfileName) {
|
|
85
|
-
return profile?.connectKeys?.[hubProfileName]?.apiKey;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* The cached connect key the connect-on-demand flow may SERVE: the slot's
|
|
89
|
-
* key, only when the hub profile carries an e-mail address, the cached
|
|
90
|
-
* entry carries one too, and the two are the same address (trimmed,
|
|
91
|
-
* case-insensitive). A key is the credential of the person who minted it
|
|
92
|
-
* (PRDCT-3032), so the cache serves only that person; a hub profile with no
|
|
93
|
-
* address is someone unknown, and an entry with no address was minted by
|
|
94
|
-
* someone unknown: neither is served (PRDCT-3046 F7). `lookupConnectKey`
|
|
95
|
-
* stays the plain slot read.
|
|
96
|
-
*/
|
|
97
|
-
export function cachedConnectKey(profile, hubProfileName, hubEmail) {
|
|
98
|
-
const entry = profile?.connectKeys?.[hubProfileName];
|
|
99
|
-
const hub = hubEmail?.trim().toLowerCase();
|
|
100
|
-
const minted = entry?.email?.trim().toLowerCase();
|
|
101
|
-
if (!hub || !minted || hub !== minted)
|
|
102
|
-
return undefined;
|
|
103
|
-
return entry?.apiKey;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* What a profile signs in with, in the order the tool resolves it: its own
|
|
107
|
-
* `apiKey` (`key`, the tool's login), else a key the hub exchange minted
|
|
108
|
-
* (`hub`: under the named hub profile, or under any when none is named),
|
|
109
|
-
* else `none`. A listing can then say which profiles ride the hub login.
|
|
110
|
-
*/
|
|
111
|
-
export function profileCredential(profile, hubProfileName) {
|
|
112
|
-
if (!profile)
|
|
113
|
-
return 'none';
|
|
114
|
-
if (profile.apiKey)
|
|
115
|
-
return 'key';
|
|
116
|
-
const slots = profile.connectKeys ?? {};
|
|
117
|
-
const connected = hubProfileName
|
|
118
|
-
? Boolean(slots[hubProfileName]?.apiKey)
|
|
119
|
-
: Object.values(slots).some((k) => k.apiKey);
|
|
120
|
-
return connected ? 'hub' : 'none';
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Cache an exchange-minted tool key on the SELECTED profile under the hub
|
|
124
|
-
* profile's name. A profile not on disk yet is created (with `baseUrl` when
|
|
125
|
-
* given) and made active only when the tool had NO profile at all: the
|
|
126
|
-
* first cloud connect on a fresh machine. An existing profile is never
|
|
127
|
-
* renamed, repointed or made active; every other field is preserved.
|
|
128
|
-
* Returns the file written.
|
|
129
|
-
*/
|
|
130
|
-
export function storeConnectKey(env, tool, profileName, hubProfileName, entry, opts = {}) {
|
|
131
|
-
const config = loadConfig(env, tool);
|
|
132
|
-
const first = Object.keys(config.profiles).length === 0;
|
|
133
|
-
const profile = config.profiles[profileName] ?? {};
|
|
134
|
-
config.profiles[profileName] = {
|
|
135
|
-
...profile,
|
|
136
|
-
...(!profile.baseUrl?.trim() && opts.baseUrl ? { baseUrl: opts.baseUrl } : {}),
|
|
137
|
-
connectKeys: { ...profile.connectKeys, [hubProfileName]: entry }
|
|
138
|
-
};
|
|
139
|
-
if (first)
|
|
140
|
-
config.activeProfile = profileName;
|
|
141
|
-
return saveConfig(env, tool, config);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Drop one hub profile's cached connect key from a profile (disconnect this
|
|
145
|
-
* tool from the hub account, or evict a key the hub revoked). Returns
|
|
146
|
-
* whether anything was removed; the tool's own `apiKey` is never touched —
|
|
147
|
-
* it belongs to the tool's own login flow.
|
|
148
|
-
*/
|
|
149
|
-
export function removeConnectKey(env, tool, profileName, hubProfileName) {
|
|
150
|
-
const config = loadConfig(env, tool);
|
|
151
|
-
const profile = config.profiles[profileName];
|
|
152
|
-
if (!profile?.connectKeys || !(hubProfileName in profile.connectKeys))
|
|
153
|
-
return false;
|
|
154
|
-
const { [hubProfileName]: _dropped, ...rest } = profile.connectKeys;
|
|
155
|
-
if (Object.keys(rest).length === 0) {
|
|
156
|
-
const { connectKeys: _empty, ...bare } = profile;
|
|
157
|
-
config.profiles[profileName] = bare;
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
config.profiles[profileName] = { ...profile, connectKeys: rest };
|
|
161
|
-
}
|
|
162
|
-
saveConfig(env, tool, config);
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
263
|
//# sourceMappingURL=config.js.map
|