ardent-cli-beta 0.0.20 → 0.1.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 +9 -1
- package/dist/branches/client.d.ts +3 -0
- package/dist/branches/client.js +44 -0
- package/dist/branches/connection.js +7 -4
- package/dist/branches/credentials.d.ts +9 -1
- package/dist/branches/credentials.js +53 -2
- package/dist/branches/psql.js +3 -2
- package/dist/branches/recovery.js +1 -1
- package/dist/commands/branch/delete.js +22 -3
- package/oclif.manifest.json +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -39,6 +39,12 @@ omitting it preserves unrestricted ingress. `--print` remains accepted. The cred
|
|
|
39
39
|
after exit. This uses `branch connect --print`'s lifecycle and requires a control
|
|
40
40
|
plane that issues credentials while branches start (`docs/DECISIONS.md` section 17).
|
|
41
41
|
|
|
42
|
+
For tokenized credentials, the CLI receives the database token once, keeps it
|
|
43
|
+
in memory while waiting for readiness, and composes the delivered URL template
|
|
44
|
+
locally. A lost one-time token requires revoking and issuing another credential;
|
|
45
|
+
recovery records contain only request and issuance metadata, never that token
|
|
46
|
+
(`docs/DECISIONS.md` section 20).
|
|
47
|
+
|
|
42
48
|
Use `branch create <name> --to-env` or `branch connect --to-env` to write
|
|
43
49
|
`DATABASE_URL` to `.env` in the
|
|
44
50
|
current directory instead of printing the URL. The selected `--driver` still
|
|
@@ -133,7 +139,9 @@ context and names the status command before retrying.
|
|
|
133
139
|
Creation uses the connector's latest published snapshot. If that snapshot is
|
|
134
140
|
unavailable, creation fails without choosing an older one. Names are unique within
|
|
135
141
|
a connector while the branch is not deleted. `branch delete` sends the connector
|
|
136
|
-
and name to the API, which resolves the target.
|
|
142
|
+
and name to the API, which resolves the target. When no branch has that name and
|
|
143
|
+
the argument is the ID of a deleted branch whose erasure is unconfirmed, as a
|
|
144
|
+
refused `connector delete` names one, it retries that erasure. `branch list` fetches current state
|
|
137
145
|
and marks the locally selected branch with `●` in its `Selected` column; `branch switch` changes local
|
|
138
146
|
context. Create and delete follow the operation in an interactive terminal;
|
|
139
147
|
creation also waits outside a terminal and with `--json` so it can return a usable
|
|
@@ -15,6 +15,9 @@ export interface BranchOperation {
|
|
|
15
15
|
export declare function listBranches(token: string, connectorID: string): Promise<Branch[]>;
|
|
16
16
|
export declare function createBranch(token: string, connectorID: string, name: string, allowedCIDRs?: string[]): Promise<BranchOperation>;
|
|
17
17
|
export declare function deleteBranch(token: string, connectorID: string, name: string): Promise<BranchOperation>;
|
|
18
|
+
export declare function isBranchID(value: string): boolean;
|
|
19
|
+
export declare function loadDeletedBranch(token: string, connectorID: string, id: string): Promise<Branch>;
|
|
20
|
+
export declare function retryBranchErasure(token: string, branch: Branch): Promise<BranchOperation>;
|
|
18
21
|
export type BranchLifecycleAction = 'wake' | 'suspend' | 'recover';
|
|
19
22
|
export declare function transitionBranch(token: string, connectorID: string, name: string, action: BranchLifecycleAction): Promise<BranchOperation>;
|
|
20
23
|
export declare function waitForBranchOperation(token: string, workflowID: string, onStatus?: (status: string) => void, failedReads?: 'stop' | 'retry'): Promise<void>;
|
package/dist/branches/client.js
CHANGED
|
@@ -23,6 +23,50 @@ export async function deleteBranch(token, connectorID, name) {
|
|
|
23
23
|
validateOperation(result, connectorID, name);
|
|
24
24
|
return result;
|
|
25
25
|
}
|
|
26
|
+
// A deleted branch whose erasure no node has confirmed is in no branch list
|
|
27
|
+
// and unreachable by name, yet keeps its connector from being deleted; that
|
|
28
|
+
// refusal names it by ID. These two calls resolve such an ID and retry its
|
|
29
|
+
// erasure by ID. docs/DECISIONS.md section 19 says why only a DELETED branch.
|
|
30
|
+
export function isBranchID(value) {
|
|
31
|
+
return /^branch_[0-9A-Za-z_-]+$/.test(value);
|
|
32
|
+
}
|
|
33
|
+
export async function loadDeletedBranch(token, connectorID, id) {
|
|
34
|
+
const missing = `No branch ${terminalText(id)} in the selected connector.`;
|
|
35
|
+
let branch;
|
|
36
|
+
try {
|
|
37
|
+
branch = await request(token, `/branches/${encodeURIComponent(id)}`);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof APIError && error.status === 404)
|
|
41
|
+
throw new Error(missing, { cause: error });
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
if (!isBranch(branch, connectorID) || branch.id !== id)
|
|
45
|
+
throw new Error(missing);
|
|
46
|
+
if (branch.state !== 'DELETED')
|
|
47
|
+
throw new Error(`Branch ${terminalText(id)} is not deleted. Delete it by its name, ${terminalText(JSON.stringify(branch.name))}.`);
|
|
48
|
+
return branch;
|
|
49
|
+
}
|
|
50
|
+
export async function retryBranchErasure(token, branch) {
|
|
51
|
+
let operation;
|
|
52
|
+
try {
|
|
53
|
+
// branch list omits a deleted branch and no CLI command reads one, so the
|
|
54
|
+
// refresh is the read loadDeletedBranch made.
|
|
55
|
+
operation = await requestDurableMutation(token, `/branches/${encodeURIComponent(branch.id)}`, { method: 'DELETE' }, `an authenticated GET /branches/${terminalText(encodeURIComponent(branch.id))}`);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
// This route refuses in plain text, so the API's reason does not reach here.
|
|
59
|
+
if (error instanceof APIError && error.status === 409) {
|
|
60
|
+
throw new Error(`The Ardent API refused to retry the erasure of branch ${terminalText(branch.id)}. An erasure still running can be retried once it fails, an hour after it was requested; otherwise report the branch ID to Ardent support.`, { cause: error });
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
if (!operation || typeof operation.operation_id !== 'string' || !operation.operation_id ||
|
|
65
|
+
typeof operation.workflow_id !== 'string' || !operation.workflow_id) {
|
|
66
|
+
throw new Error(`The Ardent API accepted the erasure retry of branch ${terminalText(branch.id)} but returned no valid operation; completion is unknown and work may still be running.`);
|
|
67
|
+
}
|
|
68
|
+
return { branch, operation };
|
|
69
|
+
}
|
|
26
70
|
// The control plane addresses these transitions by branch ID, so the command
|
|
27
71
|
// resolves the name in the selected connector first and acts on exactly that
|
|
28
72
|
// branch. The response is the operation alone; the resolved branch is returned
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { acknowledgeBranchCredential, CredentialIssueRefused, issueBranchCredential, revokeBranchCredential, waitForBranchCredential } from './credentials.js';
|
|
2
|
+
import { acknowledgeBranchCredential, composeCredentialDelivery, CredentialIssueRefused, issuedConnectKey, issueBranchCredential, revokeBranchCredential, waitForBranchCredential } from './credentials.js';
|
|
3
3
|
import { CLIENT_REQUIREMENTS, connectionString } from './drivers.js';
|
|
4
4
|
import { connectionEnvironment, runPsql } from './psql.js';
|
|
5
5
|
import { CredentialRecoveryCleanupError, forgetCredentialRecovery, openCredentialWitness, rememberCredentialIssuance, rememberCredentialRequest } from './recovery.js';
|
|
@@ -15,6 +15,7 @@ export async function connectToBranch(connection) {
|
|
|
15
15
|
try {
|
|
16
16
|
const recovery = await rememberCredentialRequest(connection.configDir, branchID, token, witness.port, requestID, sourceRole);
|
|
17
17
|
let issuanceID;
|
|
18
|
+
let connectKey;
|
|
18
19
|
let issueRefused = false;
|
|
19
20
|
// Set once a printed credential's recovery record is gone: the
|
|
20
21
|
// credential is the user's from then on, and nothing here revokes it.
|
|
@@ -25,7 +26,10 @@ export async function connectToBranch(connection) {
|
|
|
25
26
|
let failure;
|
|
26
27
|
try {
|
|
27
28
|
try {
|
|
28
|
-
|
|
29
|
+
const issued = await issueBranchCredential(token, branchID, requestID, sourceRole);
|
|
30
|
+
issuanceID = issued.issuanceID;
|
|
31
|
+
await rememberCredentialIssuance(recovery, issuanceID);
|
|
32
|
+
connectKey = issuedConnectKey(issued);
|
|
29
33
|
}
|
|
30
34
|
catch (error) {
|
|
31
35
|
if (error instanceof CredentialIssueRefused) {
|
|
@@ -42,8 +46,7 @@ export async function connectToBranch(connection) {
|
|
|
42
46
|
}
|
|
43
47
|
throw error;
|
|
44
48
|
}
|
|
45
|
-
await
|
|
46
|
-
const delivery = await deliverWhenServing(token, issuanceID, connection.serving);
|
|
49
|
+
const delivery = composeCredentialDelivery(await deliverWhenServing(token, issuanceID, connection.serving), connectKey);
|
|
47
50
|
if (print) {
|
|
48
51
|
// Checked before acknowledgement, so a form that would not verify
|
|
49
52
|
// the server is revoked, never handed over.
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { type ConnectionStrings } from './drivers.js';
|
|
2
|
+
export interface IssuedCredential {
|
|
3
|
+
issuanceID: string;
|
|
4
|
+
connectKey?: unknown;
|
|
5
|
+
connectKeyRequired?: unknown;
|
|
6
|
+
}
|
|
2
7
|
interface DeliveredCredential {
|
|
3
8
|
issuance_id: string;
|
|
4
9
|
delivery_id: string;
|
|
5
10
|
uri: string;
|
|
6
11
|
connection_strings?: ConnectionStrings;
|
|
12
|
+
connect_key_required?: boolean;
|
|
7
13
|
}
|
|
8
14
|
export declare class CredentialIssueRefused extends Error {
|
|
9
15
|
readonly code?: string | undefined;
|
|
@@ -11,7 +17,9 @@ export declare class CredentialIssueRefused extends Error {
|
|
|
11
17
|
}
|
|
12
18
|
export declare function validSourceRole(role: string): boolean;
|
|
13
19
|
export declare function refusedBeforeIssuance(error: unknown): boolean;
|
|
14
|
-
export declare function issueBranchCredential(token: string, branchID: string, requestID: string, sourceRole?: string): Promise<
|
|
20
|
+
export declare function issueBranchCredential(token: string, branchID: string, requestID: string, sourceRole?: string): Promise<IssuedCredential>;
|
|
21
|
+
export declare function issuedConnectKey(issued: IssuedCredential): string | undefined;
|
|
22
|
+
export declare function composeCredentialDelivery(delivered: DeliveredCredential, connectKey?: string): DeliveredCredential;
|
|
15
23
|
export declare function waitForBranchCredential(token: string, issuanceID: string, stop?: AbortSignal, served?: Promise<void>): Promise<DeliveredCredential>;
|
|
16
24
|
export declare function acknowledgeBranchCredential(token: string, issuanceID: string, deliveryID: string): Promise<void>;
|
|
17
25
|
export declare function revokeBranchCredential(token: string, issuanceID: string): Promise<void>;
|
|
@@ -11,7 +11,7 @@ export class CredentialIssueRefused extends Error {
|
|
|
11
11
|
}
|
|
12
12
|
// The issuance refusals that come before any issuance is recorded: a request
|
|
13
13
|
// refused with one of these left nothing to revoke.
|
|
14
|
-
const sourceRoleIssueRefusals = new Set(['invalid_source_role', 'source_role_unsupported', 'source_role_held', 'source_role_revocation_pending']);
|
|
14
|
+
const sourceRoleIssueRefusals = new Set(['invalid_source_role', 'source_role_unsupported', 'source_role_held', 'source_role_revocation_pending', 'connect_key_unsupported']);
|
|
15
15
|
// sourceRolePattern is the name the API accepts for a source role: an
|
|
16
16
|
// unquoted lower-case PostgreSQL identifier that begins with neither pg_ nor
|
|
17
17
|
// ardent. The CLI checks it before it records or sends anything, so a mistyped
|
|
@@ -53,7 +53,58 @@ export async function issueBranchCredential(token, branchID, requestID, sourceRo
|
|
|
53
53
|
if (!result || typeof result.issuance_id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(result.issuance_id) || result.status !== 'pending') {
|
|
54
54
|
throw new Error('Ardent API returned an invalid branch credential issuance.');
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
// Return the identity before checking the one-time secret, so the caller
|
|
57
|
+
// records and revokes an issuance even when its secret is absent or malformed.
|
|
58
|
+
return { issuanceID: result.issuance_id, connectKey: result.connect_key, connectKeyRequired: result.connect_key_required };
|
|
59
|
+
}
|
|
60
|
+
export function issuedConnectKey(issued) {
|
|
61
|
+
if (issued.connectKeyRequired === undefined || issued.connectKeyRequired === false) {
|
|
62
|
+
if (issued.connectKey !== undefined)
|
|
63
|
+
throw new Error('Ardent API returned an unexpected database token. Revoke this credential and issue another.');
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
if (issued.connectKeyRequired !== true || typeof issued.connectKey !== 'string' ||
|
|
67
|
+
!/^bck_[a-z2-7]{16}_[A-Za-z0-9_-]{43}$/.test(issued.connectKey) ||
|
|
68
|
+
Buffer.from(issued.connectKey.slice(21), 'base64url').toString('base64url') !== issued.connectKey.slice(21)) {
|
|
69
|
+
throw new Error('The one-time database token was not received. Revoke this credential and issue another; the token cannot be recovered.');
|
|
70
|
+
}
|
|
71
|
+
return issued.connectKey;
|
|
72
|
+
}
|
|
73
|
+
// Tokenized delivery contains templates, never a recoverable database token.
|
|
74
|
+
// Compose only the username field locally; neither password nor query settings
|
|
75
|
+
// are interpolated. The ordinary driver checks still validate TLS afterwards.
|
|
76
|
+
export function composeCredentialDelivery(delivered, connectKey) {
|
|
77
|
+
if (delivered.connect_key_required !== undefined && typeof delivered.connect_key_required !== 'boolean') {
|
|
78
|
+
throw new Error('Ardent API returned an invalid database token requirement.');
|
|
79
|
+
}
|
|
80
|
+
if (Boolean(connectKey) !== (delivered.connect_key_required === true)) {
|
|
81
|
+
throw new Error('Ardent API returned an inconsistent database token requirement.');
|
|
82
|
+
}
|
|
83
|
+
if (!connectKey)
|
|
84
|
+
return delivered;
|
|
85
|
+
const compose = (value, jdbc = false) => {
|
|
86
|
+
try {
|
|
87
|
+
if (jdbc && !value.startsWith('jdbc:postgresql://'))
|
|
88
|
+
throw new Error();
|
|
89
|
+
const url = new URL(jdbc ? value.slice('jdbc:'.length) : value);
|
|
90
|
+
const user = jdbc ? url.searchParams.get('user') : decodeURIComponent(url.username);
|
|
91
|
+
if (!user || !/^[a-z_][a-z0-9_]{0,62}\+<connect-key>$/.test(user) || (jdbc && url.searchParams.getAll('user').length !== 1))
|
|
92
|
+
throw new Error();
|
|
93
|
+
const username = `${user.slice(0, -'<connect-key>'.length)}${connectKey}`;
|
|
94
|
+
if (jdbc)
|
|
95
|
+
url.searchParams.set('user', username);
|
|
96
|
+
else
|
|
97
|
+
url.username = username;
|
|
98
|
+
return `${jdbc ? 'jdbc:' : ''}${url.toString()}`;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
throw new Error('Ardent API returned an invalid database token template.');
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const uri = compose(delivered.uri);
|
|
105
|
+
const strings = delivered.connection_strings;
|
|
106
|
+
const connectionStrings = strings && Object.fromEntries(DRIVERS.map((driver) => [driver, compose(strings[driver], driver === 'jdbc')]));
|
|
107
|
+
return { ...delivered, uri, connection_strings: connectionStrings };
|
|
57
108
|
}
|
|
58
109
|
// stop, when given, ends the wait with its abort reason: a caller that learns
|
|
59
110
|
// the branch failed has no delivery left to wait for. served, when given,
|
package/dist/branches/psql.js
CHANGED
|
@@ -26,7 +26,8 @@ export function connectionEnvironment(uri) {
|
|
|
26
26
|
}
|
|
27
27
|
const parameters = [...url.searchParams.entries()];
|
|
28
28
|
const controls = /[\u0000-\u001f\u007f]/;
|
|
29
|
-
// A terminating environment's username is <role>+<
|
|
29
|
+
// A terminating environment's username is <role>+<database token> (or a
|
|
30
|
+
// legacy API key), which its node
|
|
30
31
|
// proxy splits; a passthrough environment's is the role alone, because no
|
|
31
32
|
// proxy is there to split it. Either way neither half may be empty.
|
|
32
33
|
const [role, ...apiKey] = user.split('+');
|
|
@@ -80,7 +81,7 @@ export async function checkPsql() {
|
|
|
80
81
|
});
|
|
81
82
|
}
|
|
82
83
|
export async function runPsql(connection) {
|
|
83
|
-
// The API key is
|
|
84
|
+
// The database token or legacy API key is in PGUSER. Neither it nor the URI reaches argv.
|
|
84
85
|
const environment = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith('PG')));
|
|
85
86
|
await new Promise((resolve, reject) => {
|
|
86
87
|
const child = spawn('script', terminalCommand(['-X', '-v', 'PROMPT1=ardent=> ', '-v', 'PROMPT2=ardent-> ', '-v', 'PROMPT3=ardent>> ']), {
|
|
@@ -185,7 +185,7 @@ export async function recoverBranchCredentials(directory, token) {
|
|
|
185
185
|
if (!recovery.issuanceID) {
|
|
186
186
|
let issuanceID;
|
|
187
187
|
try {
|
|
188
|
-
issuanceID = await issueBranchCredential(token, branchID, requestID, recovery.sourceRole);
|
|
188
|
+
issuanceID = (await issueBranchCredential(token, branchID, requestID, recovery.sourceRole)).issuanceID;
|
|
189
189
|
}
|
|
190
190
|
catch (error) {
|
|
191
191
|
// The API refuses a source role before it records an issuance and
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Args, ux } from '@oclif/core';
|
|
2
|
+
import { APIError } from '../../api/client.js';
|
|
2
3
|
import { ArdentCommand } from '../../command.js';
|
|
3
|
-
import { branchName, deleteBranch, waitForBranchOperation } from '../../branches/client.js';
|
|
4
|
+
import { branchName, deleteBranch, isBranchID, loadDeletedBranch, retryBranchErasure, waitForBranchOperation } from '../../branches/client.js';
|
|
4
5
|
import { replaceBranchCache } from '../../branches/context.js';
|
|
5
6
|
import { requireConnector } from '../../connectors/context.js';
|
|
6
7
|
import { terminalText } from '../../terminal.js';
|
|
7
8
|
export default class BranchDelete extends ArdentCommand {
|
|
8
|
-
static args = { name: Args.string({ description: 'Branch name', required: true }) };
|
|
9
|
+
static args = { name: Args.string({ description: 'Branch name, or the ID of a deleted branch whose erasure to retry', required: true }) };
|
|
9
10
|
static strict = false;
|
|
10
11
|
static description = 'Delete a branch in the selected connector';
|
|
11
12
|
static helpGroup = 'Branches';
|
|
@@ -20,7 +21,25 @@ export default class BranchDelete extends ArdentCommand {
|
|
|
20
21
|
else if (!this.jsonEnabled())
|
|
21
22
|
this.log(`Deleting branch ${target}...`);
|
|
22
23
|
try {
|
|
23
|
-
|
|
24
|
+
let result;
|
|
25
|
+
try {
|
|
26
|
+
result = await deleteBranch(session.token, session.selectedConnector.id, name);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
// A name that matches no branch may be the ID of a deleted branch
|
|
30
|
+
// whose erasure is unconfirmed (retryBranchErasure).
|
|
31
|
+
if (!(error instanceof APIError && error.status === 404 && error.code === 'branch_not_found' && isBranchID(name)))
|
|
32
|
+
throw error;
|
|
33
|
+
const branch = await loadDeletedBranch(session.token, session.selectedConnector.id, name);
|
|
34
|
+
const retry = `Retrying the erasure of deleted branch ${terminalText(branch.name)} (${terminalText(branch.id)}) in connector ${terminalText(session.selectedConnector.name)}`;
|
|
35
|
+
if (interactive) {
|
|
36
|
+
ux.action.stop('no branch has that name');
|
|
37
|
+
ux.action.start(retry);
|
|
38
|
+
}
|
|
39
|
+
else if (!this.jsonEnabled())
|
|
40
|
+
this.log(`${retry}...`);
|
|
41
|
+
result = await retryBranchErasure(session.token, branch);
|
|
42
|
+
}
|
|
24
43
|
if (interactive) {
|
|
25
44
|
await waitForBranchOperation(session.token, result.operation.workflow_id, (status) => { ux.action.status = status; });
|
|
26
45
|
await replaceBranchCache(this.config.configDir, session, (session.branches ?? []).filter(({ id }) => id !== result.branch.id));
|
package/oclif.manifest.json
CHANGED
|
@@ -239,7 +239,7 @@
|
|
|
239
239
|
"aliases": [],
|
|
240
240
|
"args": {
|
|
241
241
|
"name": {
|
|
242
|
-
"description": "Branch name",
|
|
242
|
+
"description": "Branch name, or the ID of a deleted branch whose erasure to retry",
|
|
243
243
|
"name": "name",
|
|
244
244
|
"required": true
|
|
245
245
|
}
|
|
@@ -908,5 +908,5 @@
|
|
|
908
908
|
]
|
|
909
909
|
}
|
|
910
910
|
},
|
|
911
|
-
"version": "0.0
|
|
911
|
+
"version": "0.1.0"
|
|
912
912
|
}
|