@sjawhar/claude-envoy 5.24.0 → 5.25.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/.claude-plugin/plugin.json +1 -1
- package/.omp-plugin/plugin.json +1 -1
- package/dist/codex-hook.js +352 -117
- package/dist/dispatch.js +648 -407
- package/dist/envoy-channel.js +2404 -2169
- package/dist/session-hook.js +330 -95
- package/package.json +1 -1
package/dist/dispatch.js
CHANGED
|
@@ -980,10 +980,10 @@ __export(exports_coerce, {
|
|
|
980
980
|
});
|
|
981
981
|
|
|
982
982
|
// ../envoy-client/bin/dispatch.ts
|
|
983
|
-
import { readFileSync as
|
|
983
|
+
import { readFileSync as readFileSync9 } from "fs";
|
|
984
984
|
|
|
985
985
|
// ../envoy-client/src/dispatch-cli.ts
|
|
986
|
-
import { closeSync as closeSync5, constants as constants4, fstatSync as fstatSync3, openSync as openSync5, readFileSync as
|
|
986
|
+
import { closeSync as closeSync5, constants as constants4, fstatSync as fstatSync3, openSync as openSync5, readFileSync as readFileSync8 } from "fs";
|
|
987
987
|
|
|
988
988
|
// ../envoy-client/src/askgate-marker.ts
|
|
989
989
|
import { createHash, randomBytes } from "crypto";
|
|
@@ -19230,6 +19230,10 @@ var FORM_SESSION = "session";
|
|
|
19230
19230
|
var FORM_LAUNCHER_CREDENTIAL = "launcher_credential";
|
|
19231
19231
|
var FORM_LAUNCHER_CREDENTIAL_DECLINED = "false";
|
|
19232
19232
|
var FORM_PROJECTS = "projects";
|
|
19233
|
+
var FORM_SCOPE = "scope";
|
|
19234
|
+
var SCOPE_READ = "read";
|
|
19235
|
+
var FORM_AS_OF = "as_of";
|
|
19236
|
+
var HEADER_DPOP_HOLDER = "DPoP-Holder";
|
|
19233
19237
|
var SUBJECT_TOKEN_TYPE_KUBERNETES = "urn:envoy:params:oauth:token-type:kubernetes-service-account";
|
|
19234
19238
|
var SUBJECT_TOKEN_TYPE_GOOGLE = "urn:envoy:params:oauth:token-type:google-identity";
|
|
19235
19239
|
var SUBJECT_TOKEN_TYPE_PERSON = "urn:envoy:params:oauth:token-type:person-google-identity";
|
|
@@ -19342,6 +19346,7 @@ var KNOWN_BROKER_REFUSAL_PATTERNS = [
|
|
|
19342
19346
|
"The machine login the subject token belongs to has ended\\.",
|
|
19343
19347
|
"The machine login has ended\\.",
|
|
19344
19348
|
"Another live machine login holds the key this proof was signed with\\. A (?:machine makes a key of its own|sign-in makes a box key of its own)\\.",
|
|
19349
|
+
"A live read token is bound to the key this proof was signed with\\. Make a new key\\.",
|
|
19345
19350
|
`${PERSON_SLOT} is a restricted member, whose token names its projects: set AGENT_SECRETS_PROJECTS to the projects this session works on\\.`,
|
|
19346
19351
|
`The token names a project its person cannot see: ${PROJECT_SLOT}\\.`,
|
|
19347
19352
|
`The subject token was refused: its entry names the project ${PROJECT_SLOT}, which its person ${PERSON_SLOT} cannot see\\.`,
|
|
@@ -19354,6 +19359,19 @@ var KNOWN_BROKER_REFUSAL_PATTERNS = [
|
|
|
19354
19359
|
`The session name '${SESSION_SLOT}' belongs to another daemon: a pod trades only under a session name the daemon that registered it holds\\.`,
|
|
19355
19360
|
`The session name '${SESSION_SLOT}' is bound to no daemon: a pod trades only under a session name the daemon that registered it holds\\.`,
|
|
19356
19361
|
`${PERSON_SLOT}'s credentials are held by Dispatch; they work again when \\k<person> signs in to Dispatch, or when Dispatch can confirm \\k<person> with Google again`,
|
|
19362
|
+
"The 'as_of' parameter names a cut, and only a trade that asks for the scope read may name one\\.",
|
|
19363
|
+
"The 'as_of' parameter is not an RFC 3339 time with at most six fractional digits\\.",
|
|
19364
|
+
"The 'as_of' parameter names the zero time, which is no cut: a trade that reads the present names no cut\\.",
|
|
19365
|
+
"The 'as_of' parameter names a time later than this trade, and a cut is a time that has passed\\.",
|
|
19366
|
+
"A read trade makes or moves no launcher credential, so it cannot send launcher_credential=true\\. Trade for read without the parameter\\.",
|
|
19367
|
+
`${PERSON_SLOT}'s sign-in on this machine holds no launcher credential yet, and a read trade makes none\\. Sign in with \`agent-secrets login\` first\\.`,
|
|
19368
|
+
`${PERSON_SLOT}'s sign-in on this machine holds a launcher credential that has lapsed, and a read trade renews none\\. One ordinary trade of the sign-in renews it, as \`agent-secrets\` makes; then trade for read again\\.`,
|
|
19369
|
+
"A person's read trade sends a DPoP-Holder proof: the box key proves the sign-in and is bound to no read token, so the token is bound to a key made for its holder\\.",
|
|
19370
|
+
"The DPoP-Holder header binds a read token to its holder's key, and this trade does not ask for the scope read\\. Ask for it, or send no DPoP-Holder header\\.",
|
|
19371
|
+
"The DPoP-Holder proof was refused: a trade sends one, a DPoP proof of this request signed with the key it embeds, spent nowhere before\\.",
|
|
19372
|
+
"The DPoP-Holder proof is signed with the key the DPoP proof is signed with, and the holder's key is a key of its own\\.",
|
|
19373
|
+
"The key this read token would be bound to signs launcher proofs: a live launcher credential is bound to it, and no read token is bound to such a key\\. Send a DPoP-Holder proof by a key made for the token's holder, and the token is bound to that key\\.",
|
|
19374
|
+
"The key this proof was signed with signs launcher proofs: a live launcher credential is bound to it, and no read token is bound to such a key\\. Ask for the scope read with a DPoP-Holder proof by a key made for the token's holder, or trade with a key that holds no launcher credential\\.",
|
|
19357
19375
|
...POLICY_REFUSAL_PATTERNS
|
|
19358
19376
|
].map((source) => new RegExp(`^${source}$`));
|
|
19359
19377
|
var LIBRARY_DESCRIPTIONS = [
|
|
@@ -29645,6 +29663,64 @@ async function genericGrantRequest(config, grantType, parameters, options) {
|
|
|
29645
29663
|
return result;
|
|
29646
29664
|
}
|
|
29647
29665
|
|
|
29666
|
+
// ../cipher-client/src/broker-request.ts
|
|
29667
|
+
var BROKER_URL_SETTING = "the broker URL";
|
|
29668
|
+
function statusOf(error) {
|
|
29669
|
+
let cause = error;
|
|
29670
|
+
for (let depth = 0;depth < 4; depth += 1) {
|
|
29671
|
+
if (cause instanceof Response)
|
|
29672
|
+
return cause.status;
|
|
29673
|
+
if (cause instanceof ResponseBodyError || cause instanceof WWWAuthenticateChallengeError) {
|
|
29674
|
+
return cause.status;
|
|
29675
|
+
}
|
|
29676
|
+
if (typeof cause !== "object" || cause === null || !("cause" in cause))
|
|
29677
|
+
return 0;
|
|
29678
|
+
cause = cause.cause;
|
|
29679
|
+
}
|
|
29680
|
+
return 0;
|
|
29681
|
+
}
|
|
29682
|
+
function brokerRefusal(doing, refused, endpoint, error, context = {}) {
|
|
29683
|
+
if (error instanceof ResponseBodyError && error.status >= 400 && error.status < 500) {
|
|
29684
|
+
const description = error.error_description;
|
|
29685
|
+
return new BrokerRefusedError(error.status, error.error, typeof description === "string" ? description : "", refused, context);
|
|
29686
|
+
}
|
|
29687
|
+
const status = statusOf(error);
|
|
29688
|
+
const said = status === 0 ? errorClass(error) : `HTTP ${status}: ${errorClass(error)}`;
|
|
29689
|
+
return new BrokerTradeFailedError(`${doing} ${refused} at ${urlForText(endpoint, BROKER_URL_SETTING)}: ${said}`);
|
|
29690
|
+
}
|
|
29691
|
+
function requestInit(options, signal, headers) {
|
|
29692
|
+
const signals = [options.signal, signal].filter((one) => one !== undefined);
|
|
29693
|
+
return {
|
|
29694
|
+
method: options.method,
|
|
29695
|
+
headers,
|
|
29696
|
+
body: options.body,
|
|
29697
|
+
redirect: options.redirect,
|
|
29698
|
+
...signals.length === 0 ? {} : { signal: AbortSignal.any(signals) }
|
|
29699
|
+
};
|
|
29700
|
+
}
|
|
29701
|
+
async function sendWithProof(proved, url, options, signal) {
|
|
29702
|
+
const send = async () => {
|
|
29703
|
+
const headers = new Headers(options.headers);
|
|
29704
|
+
const request = { method: options.method, url, nonce: proved.nonce.value };
|
|
29705
|
+
headers.set("DPoP", await proved.key.proof(request));
|
|
29706
|
+
if (proved.holderKey !== undefined) {
|
|
29707
|
+
headers.set(HEADER_DPOP_HOLDER, await proved.holderKey.proof(request));
|
|
29708
|
+
}
|
|
29709
|
+
return await proved.fetch(url, requestInit(options, signal, headers));
|
|
29710
|
+
};
|
|
29711
|
+
let answer = await send();
|
|
29712
|
+
const given = answer.headers.get("DPoP-Nonce") ?? "";
|
|
29713
|
+
if (given !== "")
|
|
29714
|
+
proved.nonce.value = given;
|
|
29715
|
+
if (answer.status === 400 && given !== "") {
|
|
29716
|
+
const body = await answer.clone().json().catch(() => null);
|
|
29717
|
+
const asked = typeof body === "object" && body !== null && "error" in body && body.error === "use_dpop_nonce";
|
|
29718
|
+
if (asked)
|
|
29719
|
+
answer = await send();
|
|
29720
|
+
}
|
|
29721
|
+
return answer;
|
|
29722
|
+
}
|
|
29723
|
+
|
|
29648
29724
|
// ../cipher-client/src/device-login-file.ts
|
|
29649
29725
|
import { createPrivateKey, randomUUID as randomUUID2 } from "crypto";
|
|
29650
29726
|
import { constants as constants3 } from "fs";
|
|
@@ -29679,7 +29755,13 @@ import { basename as basename2, dirname as dirname2, join as join4 } from "path"
|
|
|
29679
29755
|
|
|
29680
29756
|
// ../cipher-client/src/token-client.ts
|
|
29681
29757
|
var DEFAULT_RENEW_BEFORE_MS = 5 * 60000;
|
|
29682
|
-
|
|
29758
|
+
function renewalAt(expiresAt, now, renewBeforeMs) {
|
|
29759
|
+
const left = expiresAt - now;
|
|
29760
|
+
if (left <= 0)
|
|
29761
|
+
return now;
|
|
29762
|
+
return Math.max(expiresAt - renewBeforeMs, now + left / 2);
|
|
29763
|
+
}
|
|
29764
|
+
var EXPIRES_IN_STEP_MS = 1000;
|
|
29683
29765
|
var RETRY_FIRST_WAIT_MS = 15000;
|
|
29684
29766
|
var RETRY_MAX_WAIT_MS = 5 * 60000;
|
|
29685
29767
|
var PENDING_FIRST_WAIT_MS = 2000;
|
|
@@ -29703,66 +29785,18 @@ function sleep2(ms, signal, holdsProcess = false) {
|
|
|
29703
29785
|
signal?.addEventListener("abort", done, { once: true });
|
|
29704
29786
|
return promise;
|
|
29705
29787
|
}
|
|
29706
|
-
function statusOf(error) {
|
|
29707
|
-
let cause = error;
|
|
29708
|
-
for (let depth = 0;depth < 4; depth += 1) {
|
|
29709
|
-
if (cause instanceof Response)
|
|
29710
|
-
return cause.status;
|
|
29711
|
-
if (cause instanceof ResponseBodyError || cause instanceof WWWAuthenticateChallengeError) {
|
|
29712
|
-
return cause.status;
|
|
29713
|
-
}
|
|
29714
|
-
if (typeof cause !== "object" || cause === null || !("cause" in cause))
|
|
29715
|
-
return 0;
|
|
29716
|
-
cause = cause.cause;
|
|
29717
|
-
}
|
|
29718
|
-
return 0;
|
|
29719
|
-
}
|
|
29720
|
-
function brokerRefusal(doing, refused, endpoint, error, context = {}) {
|
|
29721
|
-
if (error instanceof ResponseBodyError && error.status >= 400 && error.status < 500) {
|
|
29722
|
-
const description = error.error_description;
|
|
29723
|
-
return new BrokerRefusedError(error.status, error.error, typeof description === "string" ? description : "", refused, context);
|
|
29724
|
-
}
|
|
29725
|
-
const status = statusOf(error);
|
|
29726
|
-
const said = status === 0 ? errorClass(error) : `HTTP ${status}: ${errorClass(error)}`;
|
|
29727
|
-
return new BrokerTradeFailedError(`${doing} ${refused} at ${urlForText(endpoint, BROKER_URL_SETTING)}: ${said}`);
|
|
29728
|
-
}
|
|
29729
|
-
function requestInit(options, signal, headers) {
|
|
29730
|
-
const signals = [options.signal, signal].filter((one) => one !== undefined);
|
|
29731
|
-
return {
|
|
29732
|
-
method: options.method,
|
|
29733
|
-
headers,
|
|
29734
|
-
body: options.body,
|
|
29735
|
-
redirect: options.redirect,
|
|
29736
|
-
...signals.length === 0 ? {} : { signal: AbortSignal.any(signals) }
|
|
29737
|
-
};
|
|
29738
|
-
}
|
|
29739
|
-
async function sendWithProof(proved, url, options, signal) {
|
|
29740
|
-
const send = async () => {
|
|
29741
|
-
const headers = new Headers(options.headers);
|
|
29742
|
-
headers.set("DPoP", await proved.key.proof({ method: options.method, url, nonce: proved.nonce.value }));
|
|
29743
|
-
return await proved.fetch(url, requestInit(options, signal, headers));
|
|
29744
|
-
};
|
|
29745
|
-
let answer = await send();
|
|
29746
|
-
const given = answer.headers.get("DPoP-Nonce") ?? "";
|
|
29747
|
-
if (given !== "")
|
|
29748
|
-
proved.nonce.value = given;
|
|
29749
|
-
if (answer.status === 400 && given !== "") {
|
|
29750
|
-
const body = await answer.clone().json().catch(() => null);
|
|
29751
|
-
const asked = typeof body === "object" && body !== null && "error" in body && body.error === "use_dpop_nonce";
|
|
29752
|
-
if (asked)
|
|
29753
|
-
answer = await send();
|
|
29754
|
-
}
|
|
29755
|
-
return answer;
|
|
29756
|
-
}
|
|
29757
29788
|
|
|
29758
29789
|
class BrokerTokenClient {
|
|
29759
29790
|
#brokerUrl;
|
|
29760
29791
|
#key;
|
|
29792
|
+
#holderKey;
|
|
29761
29793
|
#subject;
|
|
29762
29794
|
#actor;
|
|
29763
29795
|
#session;
|
|
29764
29796
|
#projects;
|
|
29765
29797
|
#launcherCredential;
|
|
29798
|
+
#scope;
|
|
29799
|
+
#asOf;
|
|
29766
29800
|
#renewBeforeMs;
|
|
29767
29801
|
#now;
|
|
29768
29802
|
#attemptMs;
|
|
@@ -29791,11 +29825,14 @@ class BrokerTokenClient {
|
|
|
29791
29825
|
throw new Error("broker client: the broker URL is not an http or https URL");
|
|
29792
29826
|
this.#brokerUrl = holdUrlUserinfo(brokerUrl);
|
|
29793
29827
|
this.#key = options.key;
|
|
29828
|
+
this.#holderKey = options.holderKey;
|
|
29794
29829
|
this.#subject = options.subject;
|
|
29795
29830
|
this.#actor = options.actor;
|
|
29796
29831
|
this.#session = options.session ?? "";
|
|
29797
29832
|
this.#projects = [...options.projects ?? []];
|
|
29798
29833
|
this.#launcherCredential = options.launcherCredential ?? true;
|
|
29834
|
+
this.#scope = options.scope ?? "";
|
|
29835
|
+
this.#asOf = options.asOf ?? "";
|
|
29799
29836
|
this.#renewBeforeMs = options.renewBeforeMs ?? DEFAULT_RENEW_BEFORE_MS;
|
|
29800
29837
|
this.#now = options.now ?? Date.now;
|
|
29801
29838
|
this.#attemptMs = options.attemptMs ?? TRADE_ATTEMPT_MS;
|
|
@@ -29805,7 +29842,7 @@ class BrokerTokenClient {
|
|
|
29805
29842
|
this.#random = options.random ?? Math.random;
|
|
29806
29843
|
}
|
|
29807
29844
|
get key() {
|
|
29808
|
-
return this.#key;
|
|
29845
|
+
return this.#holderKey ?? this.#key;
|
|
29809
29846
|
}
|
|
29810
29847
|
describe() {
|
|
29811
29848
|
return this.#subject.describe();
|
|
@@ -29814,10 +29851,11 @@ class BrokerTokenClient {
|
|
|
29814
29851
|
const waitForReady = options.waitForReady === true;
|
|
29815
29852
|
const now = this.#now();
|
|
29816
29853
|
const held = this.#held;
|
|
29817
|
-
if (held !== null && now < held.
|
|
29818
|
-
return held;
|
|
29819
|
-
if (held !== null && now < held.expiresAt.getTime() && this.#last !== null)
|
|
29820
|
-
return held;
|
|
29854
|
+
if (held !== null && now < held.renewsAt)
|
|
29855
|
+
return held.token;
|
|
29856
|
+
if (held !== null && now < held.token.expiresAt.getTime() && this.#last !== null) {
|
|
29857
|
+
return held.token;
|
|
29858
|
+
}
|
|
29821
29859
|
const closed = this.#closed.signal;
|
|
29822
29860
|
if (closed.aborted)
|
|
29823
29861
|
throw closed.reason;
|
|
@@ -29851,8 +29889,8 @@ class BrokerTokenClient {
|
|
|
29851
29889
|
const stop = () => {
|
|
29852
29890
|
this.#waiters.delete(waiter);
|
|
29853
29891
|
const held = this.#held;
|
|
29854
|
-
if (held !== null && this.#now() < held.expiresAt.getTime())
|
|
29855
|
-
resolve(held);
|
|
29892
|
+
if (held !== null && this.#now() < held.token.expiresAt.getTime())
|
|
29893
|
+
resolve(held.token);
|
|
29856
29894
|
else
|
|
29857
29895
|
reject(this.#stopped(signal?.reason));
|
|
29858
29896
|
};
|
|
@@ -29892,7 +29930,7 @@ class BrokerTokenClient {
|
|
|
29892
29930
|
});
|
|
29893
29931
|
}
|
|
29894
29932
|
drop(accessToken) {
|
|
29895
|
-
if (accessToken !== undefined && this.#held?.accessToken !== accessToken)
|
|
29933
|
+
if (accessToken !== undefined && this.#held?.token.accessToken !== accessToken)
|
|
29896
29934
|
return;
|
|
29897
29935
|
this.#held = null;
|
|
29898
29936
|
}
|
|
@@ -29923,7 +29961,10 @@ class BrokerTokenClient {
|
|
|
29923
29961
|
if (this.#pendingSaid !== null && this.#since !== null) {
|
|
29924
29962
|
this.#log(`the secrets broker answered this pod's session ${this.#session} once it was registered, ${Math.round((this.#now() - this.#since) / 1000)} s and ${this.#failed} failed trades after the first`);
|
|
29925
29963
|
}
|
|
29926
|
-
this.#held =
|
|
29964
|
+
this.#held = {
|
|
29965
|
+
token,
|
|
29966
|
+
renewsAt: renewalAt(token.expiresAt.getTime(), this.#now(), this.#renewBeforeMs)
|
|
29967
|
+
};
|
|
29927
29968
|
this.#last = null;
|
|
29928
29969
|
this.#since = null;
|
|
29929
29970
|
this.#failed = 0;
|
|
@@ -29963,8 +30004,8 @@ class BrokerTokenClient {
|
|
|
29963
30004
|
this.#since ??= now;
|
|
29964
30005
|
this.#failed += 1;
|
|
29965
30006
|
const held = this.#held;
|
|
29966
|
-
if (held !== null && now < held.expiresAt.getTime()) {
|
|
29967
|
-
this.#tell((waiter) => waiter.resolve(held));
|
|
30007
|
+
if (held !== null && now < held.token.expiresAt.getTime()) {
|
|
30008
|
+
this.#tell((waiter) => waiter.resolve(held.token));
|
|
29968
30009
|
} else {
|
|
29969
30010
|
this.#held = null;
|
|
29970
30011
|
if (failure.kind !== "pending") {
|
|
@@ -30016,7 +30057,8 @@ class BrokerTokenClient {
|
|
|
30016
30057
|
async#trade(signal) {
|
|
30017
30058
|
const source = this.#subject;
|
|
30018
30059
|
if (source.spend !== undefined) {
|
|
30019
|
-
|
|
30060
|
+
const leastLifeMs = this.#scope === SCOPE_READ ? this.#renewBeforeMs + EXPIRES_IN_STEP_MS : undefined;
|
|
30061
|
+
return await source.spend((subject) => this.#exchange(subject, signal), signal, leastLifeMs);
|
|
30020
30062
|
}
|
|
30021
30063
|
return await this.#exchange(await source.read(signal), signal);
|
|
30022
30064
|
}
|
|
@@ -30030,6 +30072,10 @@ class BrokerTokenClient {
|
|
|
30030
30072
|
form[FORM_SESSION] = this.#session;
|
|
30031
30073
|
if (this.#projects.length > 0)
|
|
30032
30074
|
form[FORM_PROJECTS] = this.#projects.join(" ");
|
|
30075
|
+
if (this.#scope !== "")
|
|
30076
|
+
form[FORM_SCOPE] = this.#scope;
|
|
30077
|
+
if (this.#asOf !== "")
|
|
30078
|
+
form[FORM_AS_OF] = this.#asOf;
|
|
30033
30079
|
if (!this.#launcherCredential) {
|
|
30034
30080
|
form[FORM_LAUNCHER_CREDENTIAL] = FORM_LAUNCHER_CREDENTIAL_DECLINED;
|
|
30035
30081
|
}
|
|
@@ -30062,7 +30108,7 @@ class BrokerTokenClient {
|
|
|
30062
30108
|
return this.#read(urlForText(endpoint, BROKER_URL_SETTING), answered, raw.body);
|
|
30063
30109
|
}
|
|
30064
30110
|
async#send(url, options, signal, raw) {
|
|
30065
|
-
const answer = await sendWithProof({ key: this.#key, fetch: this.#fetch, nonce: this.#nonce }, url, options, signal);
|
|
30111
|
+
const answer = await sendWithProof({ key: this.#key, holderKey: this.#holderKey, fetch: this.#fetch, nonce: this.#nonce }, url, options, signal);
|
|
30066
30112
|
raw.body = await answer.clone().json().catch(() => {
|
|
30067
30113
|
return;
|
|
30068
30114
|
});
|
|
@@ -30325,6 +30371,10 @@ function member(json, name) {
|
|
|
30325
30371
|
const value = json[name];
|
|
30326
30372
|
return typeof value === "string" ? value : "";
|
|
30327
30373
|
}
|
|
30374
|
+
function wholeMember(json, name) {
|
|
30375
|
+
const value = json[name];
|
|
30376
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : null;
|
|
30377
|
+
}
|
|
30328
30378
|
function checkPrivate(info, what, forbidden, opens, notOwned) {
|
|
30329
30379
|
const mode = info.mode & 511;
|
|
30330
30380
|
if ((mode & forbidden) !== 0) {
|
|
@@ -30682,7 +30732,8 @@ async function readDeviceLogin(path) {
|
|
|
30682
30732
|
expiresAt,
|
|
30683
30733
|
broker,
|
|
30684
30734
|
accessToken: accessUsable ? holdSecret(accessToken, accessExpiresAt) : "",
|
|
30685
|
-
accessExpiresAt: accessUsable ? accessExpiresAt : null
|
|
30735
|
+
accessExpiresAt: accessUsable ? accessExpiresAt : null,
|
|
30736
|
+
accessLifeSeconds: accessUsable ? wholeMember(json, "access_life_seconds") : null
|
|
30686
30737
|
};
|
|
30687
30738
|
}
|
|
30688
30739
|
function lockLost(then = "this trade refreshes and keeps nothing, and the next one reads the login again") {
|
|
@@ -30715,7 +30766,8 @@ async function saveDeviceLogin(path, login, held, keep = false) {
|
|
|
30715
30766
|
broker: login.broker,
|
|
30716
30767
|
...login.accessToken === "" || login.accessExpiresAt === null ? {} : {
|
|
30717
30768
|
access_token: login.accessToken,
|
|
30718
|
-
access_expires_at: login.accessExpiresAt.toISOString()
|
|
30769
|
+
access_expires_at: login.accessExpiresAt.toISOString(),
|
|
30770
|
+
...login.accessLifeSeconds === null ? {} : { access_life_seconds: login.accessLifeSeconds }
|
|
30719
30771
|
}
|
|
30720
30772
|
});
|
|
30721
30773
|
await deviceLoginPlace(path);
|
|
@@ -30837,6 +30889,11 @@ async function importDpopKey(pkcs8Pem, name) {
|
|
|
30837
30889
|
// ../cipher-client/src/device-login.ts
|
|
30838
30890
|
var BROKER_SETTING = "AGENT_SECRETS_URL";
|
|
30839
30891
|
var ACCESS_MARGIN_MS = 2 * TRADE_ATTEMPT_MS;
|
|
30892
|
+
function leastAccessLifeMs(leastLifeMs, lifeSeconds) {
|
|
30893
|
+
if (leastLifeMs === undefined || lifeSeconds === null)
|
|
30894
|
+
return ACCESS_MARGIN_MS;
|
|
30895
|
+
return Math.max(ACCESS_MARGIN_MS, Math.min(leastLifeMs, lifeSeconds * 1000 / 2));
|
|
30896
|
+
}
|
|
30840
30897
|
var BROKER_REQUEST_MS = TRADE_ATTEMPT_MS / 2;
|
|
30841
30898
|
var DEFAULT_POLL_SECONDS = 5;
|
|
30842
30899
|
var LEAST_POLL_SECONDS = 1;
|
|
@@ -30912,7 +30969,8 @@ function loginOf(key, broker, answered, now) {
|
|
|
30912
30969
|
expiresAt,
|
|
30913
30970
|
broker,
|
|
30914
30971
|
accessToken: holdSecret(answered.access_token, accessExpiresAt),
|
|
30915
|
-
accessExpiresAt
|
|
30972
|
+
accessExpiresAt,
|
|
30973
|
+
accessLifeSeconds: seconds
|
|
30916
30974
|
};
|
|
30917
30975
|
}
|
|
30918
30976
|
async function startDeviceLogin(options) {
|
|
@@ -31044,7 +31102,7 @@ class DeviceLoginIdentity {
|
|
|
31044
31102
|
}
|
|
31045
31103
|
return this.#key;
|
|
31046
31104
|
}
|
|
31047
|
-
async spend(trade, signal) {
|
|
31105
|
+
async spend(trade, signal, leastLifeMs) {
|
|
31048
31106
|
const { thumbprint } = await this.#loaded();
|
|
31049
31107
|
return await withDeviceLoginLock(this.#path, async (held) => {
|
|
31050
31108
|
let login = await readDeviceLogin(this.#path);
|
|
@@ -31056,7 +31114,8 @@ class DeviceLoginIdentity {
|
|
|
31056
31114
|
}
|
|
31057
31115
|
const refresh = () => refreshDeviceLogin(this.#path, login, { describe: this.describe(), held });
|
|
31058
31116
|
const expires = login.accessExpiresAt?.getTime() ?? 0;
|
|
31059
|
-
|
|
31117
|
+
const least = leastAccessLifeMs(leastLifeMs, login.accessLifeSeconds);
|
|
31118
|
+
let refreshed = login.accessToken === "" || expires - Date.now() < least;
|
|
31060
31119
|
if (refreshed)
|
|
31061
31120
|
login = await refresh();
|
|
31062
31121
|
while (true) {
|
|
@@ -31504,13 +31563,15 @@ function tradeProjects(env) {
|
|
|
31504
31563
|
}
|
|
31505
31564
|
return sorted;
|
|
31506
31565
|
}
|
|
31507
|
-
async function brokerTokenClient(env, own = "") {
|
|
31566
|
+
async function brokerTokenClient(env, own = "", read) {
|
|
31508
31567
|
const brokerUrl = value(env, BROKER_URL_VAR);
|
|
31509
31568
|
const machine = machineIdentity(env);
|
|
31510
31569
|
if (brokerUrl === "" || machine === undefined)
|
|
31511
31570
|
return;
|
|
31512
31571
|
if (!isHttpUrl(brokerUrl))
|
|
31513
31572
|
throw new Error(`${BROKER_URL_VAR} is not an http or https URL`);
|
|
31573
|
+
const narrowed = read === undefined ? {} : { scope: SCOPE_READ, asOf: read.asOf };
|
|
31574
|
+
const heldApart = async () => read === undefined ? {} : { ...narrowed, holderKey: await newDpopKey() };
|
|
31514
31575
|
if (machine instanceof DeviceLoginIdentity) {
|
|
31515
31576
|
if (personTradeOff(env)) {
|
|
31516
31577
|
throw new Error(`${PERSON_TRADE_VAR}=off turns off every trade made as a person, and ${IDENTITY_VAR}=${IDENTITY_DEVICE_LOGIN} trades this machine's device login as the person who approved it; unset one of them`);
|
|
@@ -31520,7 +31581,8 @@ async function brokerTokenClient(env, own = "") {
|
|
|
31520
31581
|
key: await machine.key(),
|
|
31521
31582
|
subject: machine,
|
|
31522
31583
|
session: tradeSession(env, own) || deviceLoginSessionName(await machine.thumbprint()),
|
|
31523
|
-
projects: tradeProjects(env)
|
|
31584
|
+
projects: tradeProjects(env),
|
|
31585
|
+
...await heldApart()
|
|
31524
31586
|
});
|
|
31525
31587
|
return { client, signIn: undefined };
|
|
31526
31588
|
}
|
|
@@ -31531,7 +31593,8 @@ async function brokerTokenClient(env, own = "") {
|
|
|
31531
31593
|
key: await newDpopKey(),
|
|
31532
31594
|
subject: machine,
|
|
31533
31595
|
session: tradeSession(env, own),
|
|
31534
|
-
launcherCredential: false
|
|
31596
|
+
launcherCredential: false,
|
|
31597
|
+
...narrowed
|
|
31535
31598
|
});
|
|
31536
31599
|
return { client, signIn: undefined };
|
|
31537
31600
|
}
|
|
@@ -31545,11 +31608,481 @@ async function brokerTokenClient(env, own = "") {
|
|
|
31545
31608
|
subject: personIdentity(record, flow),
|
|
31546
31609
|
actor: machine,
|
|
31547
31610
|
session: own.trim() || signInSessionName(record),
|
|
31548
|
-
projects: tradeProjects(env)
|
|
31611
|
+
projects: tradeProjects(env),
|
|
31612
|
+
...await heldApart()
|
|
31549
31613
|
});
|
|
31550
31614
|
return { client, signIn: { person: record.person, thumbprint: record.thumbprint } };
|
|
31551
31615
|
}
|
|
31552
31616
|
|
|
31617
|
+
// ../envoy-client/src/broker-credential.ts
|
|
31618
|
+
function sessionName(naming) {
|
|
31619
|
+
if (naming === undefined)
|
|
31620
|
+
return "";
|
|
31621
|
+
return (typeof naming === "string" ? naming : naming()).trim();
|
|
31622
|
+
}
|
|
31623
|
+
function brokerRouteConfigured(env) {
|
|
31624
|
+
if ((env[BROKER_URL_VAR] ?? "").trim() === "") {
|
|
31625
|
+
return { traded: false, gap: `${BROKER_URL_VAR} names no secrets broker to trade it at` };
|
|
31626
|
+
}
|
|
31627
|
+
if (machineIdentity(env) === undefined) {
|
|
31628
|
+
return { traded: false, gap: `${IDENTITY_VAR} names no identity this machine proves` };
|
|
31629
|
+
}
|
|
31630
|
+
return { traded: true };
|
|
31631
|
+
}
|
|
31632
|
+
var CLIENTS = new Map;
|
|
31633
|
+
function closeAll(entries) {
|
|
31634
|
+
for (const held of entries?.values() ?? [])
|
|
31635
|
+
held.made?.client?.close();
|
|
31636
|
+
}
|
|
31637
|
+
function forgetBrokerSession(session) {
|
|
31638
|
+
const key = session.trim();
|
|
31639
|
+
closeAll(CLIENTS.get(key));
|
|
31640
|
+
CLIENTS.delete(key);
|
|
31641
|
+
}
|
|
31642
|
+
function signInName(record) {
|
|
31643
|
+
return `${record.person}\x00${record.thumbprint}`;
|
|
31644
|
+
}
|
|
31645
|
+
function keyOf(env, own, read) {
|
|
31646
|
+
const values = BROKER_VARIABLES.map((name) => env[name]);
|
|
31647
|
+
if (read !== undefined)
|
|
31648
|
+
values.push({ read: true, asOf: read.asOf ?? "" });
|
|
31649
|
+
return { session: own.trim(), variables: JSON.stringify(values) };
|
|
31650
|
+
}
|
|
31651
|
+
function entryFor(key) {
|
|
31652
|
+
let sessions = CLIENTS.get(key.session);
|
|
31653
|
+
if (sessions === undefined) {
|
|
31654
|
+
sessions = new Map;
|
|
31655
|
+
CLIENTS.set(key.session, sessions);
|
|
31656
|
+
}
|
|
31657
|
+
let entry = sessions.get(key.variables);
|
|
31658
|
+
if (entry === undefined) {
|
|
31659
|
+
entry = { making: undefined, made: undefined, refused: new Map };
|
|
31660
|
+
sessions.set(key.variables, entry);
|
|
31661
|
+
}
|
|
31662
|
+
return entry;
|
|
31663
|
+
}
|
|
31664
|
+
async function make(env, own, read) {
|
|
31665
|
+
const made = await brokerTokenClient(env, own, read);
|
|
31666
|
+
const signIn = made?.signIn;
|
|
31667
|
+
return { client: made?.client, signIn: signIn === undefined ? "" : signInName(signIn) };
|
|
31668
|
+
}
|
|
31669
|
+
async function clientFor(env, own, read) {
|
|
31670
|
+
const key = keyOf(env, own, read);
|
|
31671
|
+
const entry = entryFor(key);
|
|
31672
|
+
if (entry.refused.size > 0) {
|
|
31673
|
+
const record = await chooseSignIn(env);
|
|
31674
|
+
const chosen = record === undefined ? "" : signInName(record);
|
|
31675
|
+
if (entry.refused.has(chosen))
|
|
31676
|
+
throw entry.refused.get(chosen);
|
|
31677
|
+
if (entry.made !== undefined && entry.made.signIn !== chosen) {
|
|
31678
|
+
entry.made.client?.close();
|
|
31679
|
+
entry.making = undefined;
|
|
31680
|
+
entry.made = undefined;
|
|
31681
|
+
}
|
|
31682
|
+
}
|
|
31683
|
+
let making = entry.making;
|
|
31684
|
+
if (making === undefined) {
|
|
31685
|
+
const started = make(env, key.session, read);
|
|
31686
|
+
making = started;
|
|
31687
|
+
entry.making = started;
|
|
31688
|
+
started.then((made) => {
|
|
31689
|
+
if (entry.making === started)
|
|
31690
|
+
entry.made = made;
|
|
31691
|
+
}, () => {
|
|
31692
|
+
if (entry.making === started)
|
|
31693
|
+
entry.making = undefined;
|
|
31694
|
+
});
|
|
31695
|
+
}
|
|
31696
|
+
return { entry, made: await making };
|
|
31697
|
+
}
|
|
31698
|
+
function untilAborted(promise, signal) {
|
|
31699
|
+
if (signal === undefined)
|
|
31700
|
+
return promise;
|
|
31701
|
+
if (signal.aborted)
|
|
31702
|
+
return Promise.reject(signal.reason);
|
|
31703
|
+
return new Promise((resolve, reject) => {
|
|
31704
|
+
const stop = () => reject(signal.reason);
|
|
31705
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
31706
|
+
promise.then((value) => {
|
|
31707
|
+
signal.removeEventListener("abort", stop);
|
|
31708
|
+
resolve(value);
|
|
31709
|
+
}, (error) => {
|
|
31710
|
+
signal.removeEventListener("abort", stop);
|
|
31711
|
+
reject(error);
|
|
31712
|
+
});
|
|
31713
|
+
});
|
|
31714
|
+
}
|
|
31715
|
+
async function tradedToken(env, own, { signal, waitForReady = false, read }) {
|
|
31716
|
+
const { entry, made } = await untilAborted(clientFor(env, own, read), signal);
|
|
31717
|
+
const { client } = made;
|
|
31718
|
+
if (client === undefined) {
|
|
31719
|
+
throw new LoggableError("AGENT_SECRETS_URL and AGENT_SECRETS_MACHINE_IDENTITY no longer name a broker and an identity to trade");
|
|
31720
|
+
}
|
|
31721
|
+
const { session, variables } = keyOf(env, own, read);
|
|
31722
|
+
try {
|
|
31723
|
+
return {
|
|
31724
|
+
token: await client.token(signal, { waitForReady }),
|
|
31725
|
+
key: client.key,
|
|
31726
|
+
session: own,
|
|
31727
|
+
identity: JSON.stringify([session, variables, made.signIn])
|
|
31728
|
+
};
|
|
31729
|
+
} catch (error) {
|
|
31730
|
+
if (made.signIn !== "" && isFinalRefusal(error))
|
|
31731
|
+
entry.refused.set(made.signIn, error);
|
|
31732
|
+
throw error;
|
|
31733
|
+
}
|
|
31734
|
+
}
|
|
31735
|
+
function brokerTokenFor(env, session) {
|
|
31736
|
+
return (signal, waitForReady) => tradedToken(env, sessionName(session), { signal, waitForReady });
|
|
31737
|
+
}
|
|
31738
|
+
function dropHeld(key, token) {
|
|
31739
|
+
CLIENTS.get(key.session)?.get(key.variables)?.made?.client?.drop(token);
|
|
31740
|
+
}
|
|
31741
|
+
function dropTradedToken(env, traded) {
|
|
31742
|
+
dropHeld(keyOf(env, traded.session), traded.token.accessToken);
|
|
31743
|
+
}
|
|
31744
|
+
function brokerCredential(env, session, read) {
|
|
31745
|
+
let presented = "";
|
|
31746
|
+
const presentedAs = new WeakMap;
|
|
31747
|
+
const own = (acting) => (acting ?? "").trim() || sessionName(session);
|
|
31748
|
+
return {
|
|
31749
|
+
scheme: "DPoP",
|
|
31750
|
+
async headers(method, url, acting, signal) {
|
|
31751
|
+
const traded = await tradedToken(env, own(acting), { signal, read });
|
|
31752
|
+
const { token, key, identity } = traded;
|
|
31753
|
+
presented = token.accessToken;
|
|
31754
|
+
const headers = {
|
|
31755
|
+
Authorization: `DPoP ${token.accessToken}`,
|
|
31756
|
+
DPoP: await key.proof({ method, url, accessToken: token.accessToken })
|
|
31757
|
+
};
|
|
31758
|
+
presentedAs.set(headers, {
|
|
31759
|
+
identity,
|
|
31760
|
+
key: keyOf(env, traded.session, read),
|
|
31761
|
+
token: token.accessToken
|
|
31762
|
+
});
|
|
31763
|
+
return headers;
|
|
31764
|
+
},
|
|
31765
|
+
identity: (headers) => presentedAs.get(headers)?.identity,
|
|
31766
|
+
stale(acting, headers) {
|
|
31767
|
+
const told = headers === undefined ? undefined : presentedAs.get(headers);
|
|
31768
|
+
dropHeld(told?.key ?? keyOf(env, own(acting), read), told?.token);
|
|
31769
|
+
},
|
|
31770
|
+
secret: () => presented
|
|
31771
|
+
};
|
|
31772
|
+
}
|
|
31773
|
+
|
|
31774
|
+
// ../envoy-client/src/file-token-credential.ts
|
|
31775
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
31776
|
+
var TOKEN_FILE_VAR = "DISPATCH_BROKER_TOKEN_FILE";
|
|
31777
|
+
var DPOP_KEY_FILE_VAR = "DISPATCH_BROKER_DPOP_KEY_FILE";
|
|
31778
|
+
var PKCS8_LABEL = "PRIVATE KEY";
|
|
31779
|
+
var PKCS8_PEM = new RegExp(`^-----BEGIN ${PKCS8_LABEL}-----\\r?\\n(?:[A-Za-z0-9+/=]+\\r?\\n)+-----END ${PKCS8_LABEL}-----$`);
|
|
31780
|
+
function readNamed(variable, filePath) {
|
|
31781
|
+
try {
|
|
31782
|
+
return readFileSync4(filePath, "utf8");
|
|
31783
|
+
} catch (error) {
|
|
31784
|
+
throw new Error(`${variable} names ${filePath}, which could not be read (${readErrorCode(error)})`);
|
|
31785
|
+
}
|
|
31786
|
+
}
|
|
31787
|
+
function tokenIn(filePath) {
|
|
31788
|
+
const token = readNamed(TOKEN_FILE_VAR, filePath).trim();
|
|
31789
|
+
if (token === "") {
|
|
31790
|
+
throw new Error(`${TOKEN_FILE_VAR} names ${filePath}, which holds no token: it is empty or blank`);
|
|
31791
|
+
}
|
|
31792
|
+
if (!sendable(token)) {
|
|
31793
|
+
throw new Error(`${TOKEN_FILE_VAR} names ${filePath}, which holds whitespace or a control character inside its token`);
|
|
31794
|
+
}
|
|
31795
|
+
return holdSecret(token);
|
|
31796
|
+
}
|
|
31797
|
+
async function keyIn(filePath) {
|
|
31798
|
+
const pem = readNamed(DPOP_KEY_FILE_VAR, filePath).trim();
|
|
31799
|
+
if (!PKCS8_PEM.test(pem)) {
|
|
31800
|
+
throw new Error(`${DPOP_KEY_FILE_VAR} names ${filePath}, which does not hold one P-256 private key in PKCS#8 PEM`);
|
|
31801
|
+
}
|
|
31802
|
+
return await importDpopKey(pem, `${DPOP_KEY_FILE_VAR}'s key ${filePath}`);
|
|
31803
|
+
}
|
|
31804
|
+
function fileTokenCredential(files, dispatchUrl) {
|
|
31805
|
+
const origin = httpUrl2(dispatchUrl)?.origin;
|
|
31806
|
+
let presented = "";
|
|
31807
|
+
return {
|
|
31808
|
+
scheme: "DPoP",
|
|
31809
|
+
async headers(method, url) {
|
|
31810
|
+
const target = httpUrl2(url)?.origin;
|
|
31811
|
+
if (origin === undefined || target !== origin) {
|
|
31812
|
+
throw new Error(`the token ${TOKEN_FILE_VAR} names goes to DISPATCH_URL's origin (${origin}) alone, and this request is for ${target ?? "a URL that is not http or https"}`);
|
|
31813
|
+
}
|
|
31814
|
+
const token = tokenIn(files.tokenFile);
|
|
31815
|
+
const key = await keyIn(files.keyFile);
|
|
31816
|
+
presented = token;
|
|
31817
|
+
return {
|
|
31818
|
+
Authorization: `DPoP ${token}`,
|
|
31819
|
+
DPoP: await key.proof({ method, url, accessToken: token })
|
|
31820
|
+
};
|
|
31821
|
+
},
|
|
31822
|
+
stale() {},
|
|
31823
|
+
secret: () => presented
|
|
31824
|
+
};
|
|
31825
|
+
}
|
|
31826
|
+
|
|
31827
|
+
// ../envoy-client/src/dispatch-config.ts
|
|
31828
|
+
var DEFAULT_SERVER_URL = "http://localhost:8766";
|
|
31829
|
+
function checkoutPair(checkout, serverUrl) {
|
|
31830
|
+
const { token } = checkout.dispatch;
|
|
31831
|
+
if (token === undefined) {
|
|
31832
|
+
throw new Error(`${checkout.path}: dispatch.serverUrl requires dispatch.token in the same file`);
|
|
31833
|
+
}
|
|
31834
|
+
return {
|
|
31835
|
+
kind: "file",
|
|
31836
|
+
token,
|
|
31837
|
+
field: "dispatch.token",
|
|
31838
|
+
path: checkout.path,
|
|
31839
|
+
server: serverUrl,
|
|
31840
|
+
checkout: true
|
|
31841
|
+
};
|
|
31842
|
+
}
|
|
31843
|
+
function writtenToken(config, checkout) {
|
|
31844
|
+
if (checkout?.dispatch.token !== undefined) {
|
|
31845
|
+
throw new Error(`${checkout.path}: dispatch.token requires dispatch.serverUrl in the same file`);
|
|
31846
|
+
}
|
|
31847
|
+
const own = config.dispatch;
|
|
31848
|
+
if (own.token === undefined)
|
|
31849
|
+
return;
|
|
31850
|
+
const server = own.serverUrl ?? DEFAULT_SERVER_URL;
|
|
31851
|
+
return {
|
|
31852
|
+
token: own.token,
|
|
31853
|
+
field: "dispatch.token",
|
|
31854
|
+
path: config.path,
|
|
31855
|
+
server,
|
|
31856
|
+
checkout: false
|
|
31857
|
+
};
|
|
31858
|
+
}
|
|
31859
|
+
function lostDispatchUrlNotice(env, config) {
|
|
31860
|
+
if (nonEmpty(env.DISPATCH_TOKEN_FILE) === undefined)
|
|
31861
|
+
return;
|
|
31862
|
+
const link = config.missing?.link ?? null;
|
|
31863
|
+
const old = config.missing?.oldUserFileNotice;
|
|
31864
|
+
return [
|
|
31865
|
+
"DISPATCH_TOKEN_FILE is set and DISPATCH_URL is not, so Dispatch is off: this shell lost the DISPATCH_URL it started with. Export DISPATCH_URL again, set to the Dispatch server that token is for",
|
|
31866
|
+
...link === null ? [] : [`${config.path} is a link to ${link}, which does not exist`],
|
|
31867
|
+
...old === undefined ? [] : [old]
|
|
31868
|
+
].join("; ");
|
|
31869
|
+
}
|
|
31870
|
+
function dispatchSettings(env, config, options = {}) {
|
|
31871
|
+
const target = dispatchTarget(env, config, options.tokenFiles === true);
|
|
31872
|
+
if (!target.enabled)
|
|
31873
|
+
return target;
|
|
31874
|
+
const { url, urlSetting, source } = target;
|
|
31875
|
+
let credential;
|
|
31876
|
+
try {
|
|
31877
|
+
if (source.kind === "broker")
|
|
31878
|
+
credential = brokerCredential(env, options.session, source.read);
|
|
31879
|
+
else if (source.kind === "files")
|
|
31880
|
+
credential = fileTokenCredential(source, url);
|
|
31881
|
+
else {
|
|
31882
|
+
credential = bearerCredential(source.kind === "variable" ? source.token : fileBearer(source, url, "DISPATCH", env, options.commands));
|
|
31883
|
+
}
|
|
31884
|
+
} catch (error) {
|
|
31885
|
+
return offWithError(messageFor(error));
|
|
31886
|
+
}
|
|
31887
|
+
const publicUrl = nonEmpty(env.DISPATCH_PUBLIC_URL);
|
|
31888
|
+
const publicParsed = publicUrl === undefined ? undefined : httpUrl2(publicUrl);
|
|
31889
|
+
if (publicUrl !== undefined && (publicParsed === undefined || publicParsed.port === "0")) {
|
|
31890
|
+
return offWithError("DISPATCH_PUBLIC_URL must be an http or https URL");
|
|
31891
|
+
}
|
|
31892
|
+
const base = publicUrl === undefined ? url : withoutTrailingSlashes(publicUrl);
|
|
31893
|
+
const linkNotice = linkNoticeFor(base, publicUrl === undefined ? urlSetting : "DISPATCH_PUBLIC_URL");
|
|
31894
|
+
return {
|
|
31895
|
+
enabled: true,
|
|
31896
|
+
url,
|
|
31897
|
+
credential,
|
|
31898
|
+
...publicUrl === undefined ? {} : { publicUrl: withoutTrailingSlashes(publicUrl) },
|
|
31899
|
+
...linkNotice === undefined ? {} : { linkNotice }
|
|
31900
|
+
};
|
|
31901
|
+
}
|
|
31902
|
+
function dispatchUrl(env, config) {
|
|
31903
|
+
const target = dispatchTarget(env, config);
|
|
31904
|
+
return target.enabled ? target.url : undefined;
|
|
31905
|
+
}
|
|
31906
|
+
var BESIDE_TOKEN_FILES = [
|
|
31907
|
+
"DISPATCH_TOKEN_BROKER",
|
|
31908
|
+
"DISPATCH_TOKEN_BROKER_READ",
|
|
31909
|
+
"DISPATCH_TOKEN_BROKER_AS_OF",
|
|
31910
|
+
"DISPATCH_TOKEN",
|
|
31911
|
+
"DISPATCH_TOKEN_FILE",
|
|
31912
|
+
BROKER_URL_VAR,
|
|
31913
|
+
IDENTITY_VAR
|
|
31914
|
+
];
|
|
31915
|
+
function tokenFilesSet(env) {
|
|
31916
|
+
const set = [
|
|
31917
|
+
...nonEmpty(env.DISPATCH_BROKER_TOKEN_FILE) === undefined ? [] : [TOKEN_FILE_VAR],
|
|
31918
|
+
...nonEmpty(env.DISPATCH_BROKER_DPOP_KEY_FILE) === undefined ? [] : [DPOP_KEY_FILE_VAR]
|
|
31919
|
+
];
|
|
31920
|
+
if (set.length === 0)
|
|
31921
|
+
return;
|
|
31922
|
+
return `${set.join(" and ")} ${set.length === 1 ? "is" : "are"} set`;
|
|
31923
|
+
}
|
|
31924
|
+
function settingsBesideTokenFiles(env) {
|
|
31925
|
+
return BESIDE_TOKEN_FILES.filter((name) => nonEmpty(env[name]) !== undefined).map((name) => `${name} is set`);
|
|
31926
|
+
}
|
|
31927
|
+
function tokenFilesTarget(env, config, taken) {
|
|
31928
|
+
const set = tokenFilesSet(env);
|
|
31929
|
+
if (set === undefined)
|
|
31930
|
+
return;
|
|
31931
|
+
if (!taken) {
|
|
31932
|
+
return offWithError(`${set}, and only the dispatch command reads the token files: Dispatch is off here rather than written with another credential beside them`);
|
|
31933
|
+
}
|
|
31934
|
+
const tokenFile = nonEmpty(env.DISPATCH_BROKER_TOKEN_FILE);
|
|
31935
|
+
const keyFile = nonEmpty(env.DISPATCH_BROKER_DPOP_KEY_FILE);
|
|
31936
|
+
const explicitUrl = nonEmpty(env.DISPATCH_URL);
|
|
31937
|
+
const { checkout } = config;
|
|
31938
|
+
const problems = [
|
|
31939
|
+
...tokenFile === undefined ? [`${TOKEN_FILE_VAR} is not set`] : [],
|
|
31940
|
+
...keyFile === undefined ? [`${DPOP_KEY_FILE_VAR} is not set`] : [],
|
|
31941
|
+
...explicitUrl === undefined ? ["DISPATCH_URL is not set"] : [],
|
|
31942
|
+
...settingsBesideTokenFiles(env),
|
|
31943
|
+
...config.dispatch.token === undefined ? [] : [`dispatch.token is set in ${config.path}`],
|
|
31944
|
+
...checkout?.kind === "valid" && checkout.dispatch.token !== undefined ? [`dispatch.token is set in ${checkout.path}`] : [],
|
|
31945
|
+
...checkout?.kind === "invalid" ? [`${checkout.error}, so whether it sets dispatch.token cannot be told`] : []
|
|
31946
|
+
];
|
|
31947
|
+
if (problems.length > 0 || tokenFile === undefined || keyFile === undefined || explicitUrl === undefined) {
|
|
31948
|
+
return offWithError(`${TOKEN_FILE_VAR} and ${DPOP_KEY_FILE_VAR} go together, with DISPATCH_URL, as the dispatch command's only Dispatch credential: ${problems.join("; ")}`);
|
|
31949
|
+
}
|
|
31950
|
+
if (config.dispatch.enabled === false) {
|
|
31951
|
+
return offWithNotice(`${config.path} sets dispatch.enabled to false: Dispatch is off`);
|
|
31952
|
+
}
|
|
31953
|
+
if (httpUrl2(explicitUrl) === undefined) {
|
|
31954
|
+
return offWithError("DISPATCH_URL must be an http or https URL");
|
|
31955
|
+
}
|
|
31956
|
+
return {
|
|
31957
|
+
enabled: true,
|
|
31958
|
+
url: withoutTrailingSlashes(explicitUrl),
|
|
31959
|
+
urlSetting: "DISPATCH_URL",
|
|
31960
|
+
source: { kind: "files", tokenFile, keyFile }
|
|
31961
|
+
};
|
|
31962
|
+
}
|
|
31963
|
+
function brokerRouteForced(env) {
|
|
31964
|
+
const written = (env.DISPATCH_TOKEN_BROKER ?? "").trim();
|
|
31965
|
+
if (written === "")
|
|
31966
|
+
return false;
|
|
31967
|
+
if (written === "1")
|
|
31968
|
+
return true;
|
|
31969
|
+
return offWithError("DISPATCH_TOKEN_BROKER takes 1: set it to 1 where this session's Dispatch credential is traded at the secrets broker (the person signed in on this machine, else the machine's own identity), and leave it unset where a bearer is read as usual");
|
|
31970
|
+
}
|
|
31971
|
+
var CUT_FORM = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,6}))?(?:Z|([+-])(\d{2}):(\d{2}))$/;
|
|
31972
|
+
var ZERO_TIME = Date.parse("0001-01-01T00:00:00.000Z");
|
|
31973
|
+
function isCut(text) {
|
|
31974
|
+
const parts = CUT_FORM.exec(text);
|
|
31975
|
+
if (parts === null)
|
|
31976
|
+
return false;
|
|
31977
|
+
const [year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0] = parts.slice(1, 7).map((part) => Number(part ?? "0"));
|
|
31978
|
+
const [zoneHour = 0, zoneMinute = 0] = parts.slice(9, 11).map((part) => Number(part ?? "0"));
|
|
31979
|
+
const daysInMonth = new Date(Date.UTC(year, month, 0)).getUTCDate();
|
|
31980
|
+
const exists = month >= 1 && month <= 12 && day >= 1 && day <= daysInMonth && hour <= 23 && minute <= 59 && second <= 59 && zoneHour <= 23 && zoneMinute <= 59;
|
|
31981
|
+
if (!exists)
|
|
31982
|
+
return false;
|
|
31983
|
+
const offset = (parts[8] === "-" ? -1 : 1) * (zoneHour * 60 + zoneMinute);
|
|
31984
|
+
const instant = new Date(0);
|
|
31985
|
+
instant.setUTCFullYear(year, month - 1, day);
|
|
31986
|
+
instant.setUTCHours(hour, minute - offset, second, 0);
|
|
31987
|
+
return Number(parts[7] ?? "0") !== 0 || instant.getTime() !== ZERO_TIME;
|
|
31988
|
+
}
|
|
31989
|
+
function readTradeSetting(env) {
|
|
31990
|
+
const read = (env.DISPATCH_TOKEN_BROKER_READ ?? "").trim();
|
|
31991
|
+
const cut = (env.DISPATCH_TOKEN_BROKER_AS_OF ?? "").trim();
|
|
31992
|
+
if (read !== "" && read !== "1") {
|
|
31993
|
+
return offWithError("DISPATCH_TOKEN_BROKER_READ takes 1: set it to 1 where this session's Dispatch credential is a broker token that only reads, and leave it unset where it writes");
|
|
31994
|
+
}
|
|
31995
|
+
if (read === "") {
|
|
31996
|
+
if (cut === "")
|
|
31997
|
+
return;
|
|
31998
|
+
return offWithError("DISPATCH_TOKEN_BROKER_AS_OF names the cut a read-only token reads at, and goes only beside DISPATCH_TOKEN_BROKER_READ=1: set DISPATCH_TOKEN_BROKER_READ to 1 beside it, or unset DISPATCH_TOKEN_BROKER_AS_OF");
|
|
31999
|
+
}
|
|
32000
|
+
if (cut === "")
|
|
32001
|
+
return {};
|
|
32002
|
+
if (!isCut(cut)) {
|
|
32003
|
+
return offWithError("DISPATCH_TOKEN_BROKER_AS_OF is not an RFC 3339 time with at most six fractional digits and Z or a numeric offset, such as 1999-12-31T23:59:59Z");
|
|
32004
|
+
}
|
|
32005
|
+
return { asOf: cut };
|
|
32006
|
+
}
|
|
32007
|
+
function dispatchTarget(env, config, tokenFiles = false) {
|
|
32008
|
+
if (config.kind === "failed")
|
|
32009
|
+
return offWithError(config.error);
|
|
32010
|
+
const files = tokenFilesTarget(env, config, tokenFiles);
|
|
32011
|
+
if (files !== undefined)
|
|
32012
|
+
return files;
|
|
32013
|
+
const brokerOnly = brokerRouteForced(env);
|
|
32014
|
+
if (typeof brokerOnly !== "boolean")
|
|
32015
|
+
return brokerOnly;
|
|
32016
|
+
const read = readTradeSetting(env);
|
|
32017
|
+
if (read !== undefined && "enabled" in read)
|
|
32018
|
+
return read;
|
|
32019
|
+
const forced = brokerOnly || read !== undefined;
|
|
32020
|
+
const explicitUrl = nonEmpty(env.DISPATCH_URL);
|
|
32021
|
+
const checkoutFile = explicitUrl === undefined ? config.checkout : null;
|
|
32022
|
+
if (checkoutFile?.kind === "invalid")
|
|
32023
|
+
return offWithError(checkoutFile.error);
|
|
32024
|
+
const checkout = checkoutFile?.kind === "valid" ? checkoutFile : null;
|
|
32025
|
+
const own = config.dispatch;
|
|
32026
|
+
if (own.enabled === false) {
|
|
32027
|
+
return offWithNotice(`${config.path} sets dispatch.enabled to false: Dispatch is off`);
|
|
32028
|
+
}
|
|
32029
|
+
if (checkout?.dispatch.enabled === false) {
|
|
32030
|
+
return offWithNotice(`${checkout.path} sets dispatch.enabled to false: Dispatch is off`);
|
|
32031
|
+
}
|
|
32032
|
+
const checkoutServer = checkout?.dispatch.serverUrl;
|
|
32033
|
+
const addressed = explicitUrl !== undefined || checkoutServer !== undefined || own.serverUrl !== undefined || own.enabled === true;
|
|
32034
|
+
if (!addressed) {
|
|
32035
|
+
const lost = lostDispatchUrlNotice(env, config);
|
|
32036
|
+
if (lost !== undefined)
|
|
32037
|
+
return offWithNotice(lost);
|
|
32038
|
+
return offWithNotice(config.missing === null ? `${config.path} names no Dispatch server, and DISPATCH_URL is not set: Dispatch is off` : missingConfigNotice(config.path, config.missing, "Dispatch is off"));
|
|
32039
|
+
}
|
|
32040
|
+
if (explicitUrl !== undefined && httpUrl2(explicitUrl) === undefined) {
|
|
32041
|
+
return offWithError("DISPATCH_URL must be an http or https URL");
|
|
32042
|
+
}
|
|
32043
|
+
let source = { kind: "none" };
|
|
32044
|
+
if (!forced) {
|
|
32045
|
+
try {
|
|
32046
|
+
source = checkout !== null && checkoutServer !== undefined ? checkoutPair(checkout, checkoutServer) : bearerSource(env, "DISPATCH", () => writtenToken(config, checkout));
|
|
32047
|
+
} catch (error) {
|
|
32048
|
+
return offWithError(messageFor(error));
|
|
32049
|
+
}
|
|
32050
|
+
}
|
|
32051
|
+
if (source.kind === "none") {
|
|
32052
|
+
let route;
|
|
32053
|
+
try {
|
|
32054
|
+
route = brokerRouteConfigured(env);
|
|
32055
|
+
} catch (error) {
|
|
32056
|
+
return offWithError(messageFor(error));
|
|
32057
|
+
}
|
|
32058
|
+
if (route.traded) {
|
|
32059
|
+
const brokered = withoutTrailingSlashes(explicitUrl ?? own.serverUrl ?? DEFAULT_SERVER_URL);
|
|
32060
|
+
const urlSetting = explicitUrl !== undefined ? "DISPATCH_URL" : own.serverUrl !== undefined ? `dispatch.serverUrl in ${config.path}` : DEFAULT_URL_SETTING;
|
|
32061
|
+
return { enabled: true, url: brokered, urlSetting, source: { kind: "broker", read } };
|
|
32062
|
+
}
|
|
32063
|
+
if (forced) {
|
|
32064
|
+
return offWithError(`${read === undefined ? "DISPATCH_TOKEN_BROKER" : "DISPATCH_TOKEN_BROKER_READ"} is set, so this session's Dispatch credential is traded at the secrets broker (the person signed in on this machine, else the machine's own identity), and ${route.gap}: Dispatch is off rather than written with whatever bearer this box holds`);
|
|
32065
|
+
}
|
|
32066
|
+
return offWithError(explicitUrl === undefined ? `${config.path}: dispatch is enabled and no token is: set dispatch.token, or DISPATCH_TOKEN or DISPATCH_TOKEN_FILE` : `DISPATCH_URL is set and no token is: set DISPATCH_TOKEN or DISPATCH_TOKEN_FILE, or dispatch.token in ${config.path}`);
|
|
32067
|
+
}
|
|
32068
|
+
const url = withoutTrailingSlashes(explicitUrl ?? (source.kind === "file" ? source.server : own.serverUrl ?? DEFAULT_SERVER_URL));
|
|
32069
|
+
const urlSetting = explicitUrl !== undefined ? "DISPATCH_URL" : source.kind === "file" ? `dispatch.serverUrl in ${source.path}` : own.serverUrl !== undefined ? `dispatch.serverUrl in ${config.path}` : DEFAULT_URL_SETTING;
|
|
32070
|
+
return { enabled: true, url, urlSetting, source };
|
|
32071
|
+
}
|
|
32072
|
+
var DEFAULT_URL_SETTING = "Dispatch's default URL";
|
|
32073
|
+
function linkNoticeFor(linkBase, setting) {
|
|
32074
|
+
if (isCleanUrl(linkBase))
|
|
32075
|
+
return;
|
|
32076
|
+
return `${setting} is not a plain http(s) URL (it carries user info, a query, a fragment, '%' or '\\'), so the links Dispatch commands print are dispatch:// references; set DISPATCH_PUBLIC_URL to a plain https URL for dashboard links`;
|
|
32077
|
+
}
|
|
32078
|
+
function activeDispatchSettings(resolution) {
|
|
32079
|
+
if (resolution.enabled)
|
|
32080
|
+
return resolution;
|
|
32081
|
+
if (resolution.error !== null)
|
|
32082
|
+
throw new Error(`dispatch config: ${resolution.error}`);
|
|
32083
|
+
return null;
|
|
32084
|
+
}
|
|
32085
|
+
|
|
31553
32086
|
// ../envoy-client/src/dispatch-commands/login.ts
|
|
31554
32087
|
var OK = 0;
|
|
31555
32088
|
var REFUSED = 1;
|
|
@@ -31604,6 +32137,11 @@ async function loginCommand(argv, env, io) {
|
|
|
31604
32137
|
io.print(LOGIN_HELP);
|
|
31605
32138
|
return OK;
|
|
31606
32139
|
}
|
|
32140
|
+
const tokenFiles = tokenFilesSet(env);
|
|
32141
|
+
if (tokenFiles !== undefined) {
|
|
32142
|
+
const beside = settingsBesideTokenFiles(env);
|
|
32143
|
+
return refuse(`${tokenFiles}, and the token files are the dispatch command's only Dispatch credential, which trades nothing at the secrets broker; a device login asks the broker and keeps a login in a file, so it does not run beside them${beside.length === 0 ? "" : `: ${beside.join("; ")}`}. Run dispatch login in a shell that sets neither token file.`, USAGE);
|
|
32144
|
+
}
|
|
31607
32145
|
if (parsed.kind === "refused")
|
|
31608
32146
|
return refuse(`${parsed.problem}. Run dispatch login --help.`, USAGE);
|
|
31609
32147
|
if (!parsed.device) {
|
|
@@ -33795,7 +34333,7 @@ async function reviewCommand(context) {
|
|
|
33795
34333
|
}
|
|
33796
34334
|
|
|
33797
34335
|
// ../envoy-client/src/session-answers.ts
|
|
33798
|
-
import { mkdirSync as mkdirSync2, readFileSync as
|
|
34336
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync5 } from "fs";
|
|
33799
34337
|
import { join as join7 } from "path";
|
|
33800
34338
|
|
|
33801
34339
|
// ../envoy-client/src/dispatch-session-directory.ts
|
|
@@ -33966,7 +34504,7 @@ processGlobal2[STATE_KEY] = state;
|
|
|
33966
34504
|
var CACHE_FILE = "answers.json";
|
|
33967
34505
|
function readCacheFile(directory, url) {
|
|
33968
34506
|
try {
|
|
33969
|
-
const value = JSON.parse(
|
|
34507
|
+
const value = JSON.parse(readFileSync5(join7(directory, CACHE_FILE), "utf-8"));
|
|
33970
34508
|
if (typeof value === "object" && value !== null && "dispatch_url" in value && typeof value.dispatch_url === "string" && value.dispatch_url === url && "read_at" in value && typeof value.read_at === "string" && "total" in value && typeof value.total === "number" && "items" in value && Array.isArray(value.items)) {
|
|
33971
34509
|
return {
|
|
33972
34510
|
dispatch_url: value.dispatch_url,
|
|
@@ -34167,13 +34705,29 @@ async function whoamiCommand(context) {
|
|
|
34167
34705
|
const service = identity.kind === "agent" ? identity.service ?? null : null;
|
|
34168
34706
|
const proved = identity.kind === "agent" ? identity.session ?? null : null;
|
|
34169
34707
|
const project = identity.kind === "agent" ? identity.project ?? null : null;
|
|
34708
|
+
const readOnly = identity.scopes?.includes("read") === true;
|
|
34709
|
+
const cut = identity.kind === "agent" ? identity.cut ?? null : null;
|
|
34170
34710
|
const session = proved ?? sessionId;
|
|
34171
34711
|
const unowned = "no owner, so an issue you create without an assignee takes its parent's assignee when the parent is in the same project, else the project's default assignee, which every project names, so the issue always has one.";
|
|
34172
34712
|
const answered = identity.kind === "agent" ? identity.sign_in_required : undefined;
|
|
34173
34713
|
const signInNeeded = answered ?? (owner === null && service !== null && proved !== null);
|
|
34174
|
-
const
|
|
34175
|
-
const
|
|
34176
|
-
|
|
34714
|
+
const who = owner !== null ? `acts for ${owner}` : service !== null ? `runs as service ${serviceSubjectLabel(service)}` : "runs";
|
|
34715
|
+
const named = proved !== null && proved !== sessionId ? ` Your credential names session ${proved}, which is not this host session (${sessionId})` : "";
|
|
34716
|
+
let acting;
|
|
34717
|
+
let elsewhere = named === "" ? "" : `${named}: Dispatch records every write you make as ${proved}.`;
|
|
34718
|
+
if (readOnly) {
|
|
34719
|
+
acting = `Session ${session} ${who} with a read-only credential: it may read, and Dispatch refuses every write it makes.${cut === null ? "" : ` Its cut is ${cut}, the past time it reads Dispatch at.`}`;
|
|
34720
|
+
elsewhere = named === "" ? "" : `${named}.`;
|
|
34721
|
+
} else if (owner !== null) {
|
|
34722
|
+
acting = `Session ${session} ${who}: issues you create without an assignee are assigned to ${owner}.`;
|
|
34723
|
+
} else if (signInNeeded) {
|
|
34724
|
+
acting = `Session ${session} ${who} and no person, so it may read but Dispatch refuses every write it makes: run \`agent-secrets login\` on this machine to write as yourself.`;
|
|
34725
|
+
} else if (service !== null) {
|
|
34726
|
+
acting = `Session ${session} ${who}: ${unowned}`;
|
|
34727
|
+
} else {
|
|
34728
|
+
acting = `Session ${session} runs under the shared token: ${unowned}`;
|
|
34729
|
+
}
|
|
34730
|
+
const writable = project !== null && !readOnly ? ` It may write project ${project} and no other.` : "";
|
|
34177
34731
|
return {
|
|
34178
34732
|
text: acting + elsewhere + writable,
|
|
34179
34733
|
details: { session, owner, service, project }
|
|
@@ -34451,7 +35005,7 @@ import {
|
|
|
34451
35005
|
mkdirSync as mkdirSync3,
|
|
34452
35006
|
openSync as openSync4,
|
|
34453
35007
|
readdirSync as readdirSync2,
|
|
34454
|
-
readFileSync as
|
|
35008
|
+
readFileSync as readFileSync6,
|
|
34455
35009
|
readSync as readSync2,
|
|
34456
35010
|
rmSync as rmSync4,
|
|
34457
35011
|
statSync as statSync3,
|
|
@@ -34474,7 +35028,7 @@ function ensureDirectory(dir) {
|
|
|
34474
35028
|
}
|
|
34475
35029
|
function readIfPresent(path) {
|
|
34476
35030
|
try {
|
|
34477
|
-
return
|
|
35031
|
+
return readFileSync6(path, "utf-8");
|
|
34478
35032
|
} catch (error) {
|
|
34479
35033
|
if (hasErrnoCode(error, "ENOENT"))
|
|
34480
35034
|
return;
|
|
@@ -34592,12 +35146,12 @@ function pruneSessions(root, now) {
|
|
|
34592
35146
|
|
|
34593
35147
|
// ../envoy-client/src/process-ancestry.ts
|
|
34594
35148
|
import { execFileSync } from "child_process";
|
|
34595
|
-
import { readFileSync as
|
|
35149
|
+
import { readFileSync as readFileSync7 } from "fs";
|
|
34596
35150
|
var MAX_ANCESTORS = 64;
|
|
34597
35151
|
var PARENT_LOOKUP_MS = 1000;
|
|
34598
35152
|
function procParentPid(pid) {
|
|
34599
35153
|
try {
|
|
34600
|
-
const stat =
|
|
35154
|
+
const stat = readFileSync7(`/proc/${pid}/stat`, "utf8");
|
|
34601
35155
|
const parent = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
34602
35156
|
return Number.isInteger(parent) ? parent : undefined;
|
|
34603
35157
|
} catch {
|
|
@@ -34639,7 +35193,7 @@ function isAncestor(candidate, pid = process.pid) {
|
|
|
34639
35193
|
}
|
|
34640
35194
|
function procCommand(pid) {
|
|
34641
35195
|
try {
|
|
34642
|
-
return
|
|
35196
|
+
return readFileSync7(`/proc/${pid}/comm`, "utf8").trim();
|
|
34643
35197
|
} catch {
|
|
34644
35198
|
return;
|
|
34645
35199
|
}
|
|
@@ -34658,316 +35212,6 @@ function nearestAncestorNamed(names, pid = process.pid, reader = PROC_READER) {
|
|
|
34658
35212
|
return;
|
|
34659
35213
|
}
|
|
34660
35214
|
|
|
34661
|
-
// ../envoy-client/src/broker-credential.ts
|
|
34662
|
-
function sessionName(naming) {
|
|
34663
|
-
if (naming === undefined)
|
|
34664
|
-
return "";
|
|
34665
|
-
return (typeof naming === "string" ? naming : naming()).trim();
|
|
34666
|
-
}
|
|
34667
|
-
function brokerRouteConfigured(env) {
|
|
34668
|
-
if ((env[BROKER_URL_VAR] ?? "").trim() === "") {
|
|
34669
|
-
return { traded: false, gap: `${BROKER_URL_VAR} names no secrets broker to trade it at` };
|
|
34670
|
-
}
|
|
34671
|
-
if (machineIdentity(env) === undefined) {
|
|
34672
|
-
return { traded: false, gap: `${IDENTITY_VAR} names no identity this machine proves` };
|
|
34673
|
-
}
|
|
34674
|
-
return { traded: true };
|
|
34675
|
-
}
|
|
34676
|
-
var CLIENTS = new Map;
|
|
34677
|
-
function closeAll(entries) {
|
|
34678
|
-
for (const held of entries?.values() ?? [])
|
|
34679
|
-
held.made?.client?.close();
|
|
34680
|
-
}
|
|
34681
|
-
function forgetBrokerSession(session) {
|
|
34682
|
-
const key = session.trim();
|
|
34683
|
-
closeAll(CLIENTS.get(key));
|
|
34684
|
-
CLIENTS.delete(key);
|
|
34685
|
-
}
|
|
34686
|
-
function signInName(record) {
|
|
34687
|
-
return `${record.person}\x00${record.thumbprint}`;
|
|
34688
|
-
}
|
|
34689
|
-
function keyOf(env, own) {
|
|
34690
|
-
return {
|
|
34691
|
-
session: own.trim(),
|
|
34692
|
-
variables: JSON.stringify(BROKER_VARIABLES.map((name) => env[name]))
|
|
34693
|
-
};
|
|
34694
|
-
}
|
|
34695
|
-
function entryFor(key) {
|
|
34696
|
-
let sessions = CLIENTS.get(key.session);
|
|
34697
|
-
if (sessions === undefined) {
|
|
34698
|
-
sessions = new Map;
|
|
34699
|
-
CLIENTS.set(key.session, sessions);
|
|
34700
|
-
}
|
|
34701
|
-
let entry = sessions.get(key.variables);
|
|
34702
|
-
if (entry === undefined) {
|
|
34703
|
-
entry = { making: undefined, made: undefined, refused: new Map };
|
|
34704
|
-
sessions.set(key.variables, entry);
|
|
34705
|
-
}
|
|
34706
|
-
return entry;
|
|
34707
|
-
}
|
|
34708
|
-
async function make(env, own) {
|
|
34709
|
-
const made = await brokerTokenClient(env, own);
|
|
34710
|
-
const signIn = made?.signIn;
|
|
34711
|
-
return { client: made?.client, signIn: signIn === undefined ? "" : signInName(signIn) };
|
|
34712
|
-
}
|
|
34713
|
-
async function clientFor(env, own) {
|
|
34714
|
-
const key = keyOf(env, own);
|
|
34715
|
-
const entry = entryFor(key);
|
|
34716
|
-
if (entry.refused.size > 0) {
|
|
34717
|
-
const record = await chooseSignIn(env);
|
|
34718
|
-
const chosen = record === undefined ? "" : signInName(record);
|
|
34719
|
-
if (entry.refused.has(chosen))
|
|
34720
|
-
throw entry.refused.get(chosen);
|
|
34721
|
-
if (entry.made !== undefined && entry.made.signIn !== chosen) {
|
|
34722
|
-
entry.made.client?.close();
|
|
34723
|
-
entry.making = undefined;
|
|
34724
|
-
entry.made = undefined;
|
|
34725
|
-
}
|
|
34726
|
-
}
|
|
34727
|
-
let making = entry.making;
|
|
34728
|
-
if (making === undefined) {
|
|
34729
|
-
const started = make(env, key.session);
|
|
34730
|
-
making = started;
|
|
34731
|
-
entry.making = started;
|
|
34732
|
-
started.then((made) => {
|
|
34733
|
-
if (entry.making === started)
|
|
34734
|
-
entry.made = made;
|
|
34735
|
-
}, () => {
|
|
34736
|
-
if (entry.making === started)
|
|
34737
|
-
entry.making = undefined;
|
|
34738
|
-
});
|
|
34739
|
-
}
|
|
34740
|
-
return { entry, made: await making };
|
|
34741
|
-
}
|
|
34742
|
-
function untilAborted(promise, signal) {
|
|
34743
|
-
if (signal === undefined)
|
|
34744
|
-
return promise;
|
|
34745
|
-
if (signal.aborted)
|
|
34746
|
-
return Promise.reject(signal.reason);
|
|
34747
|
-
return new Promise((resolve, reject) => {
|
|
34748
|
-
const stop = () => reject(signal.reason);
|
|
34749
|
-
signal.addEventListener("abort", stop, { once: true });
|
|
34750
|
-
promise.then((value) => {
|
|
34751
|
-
signal.removeEventListener("abort", stop);
|
|
34752
|
-
resolve(value);
|
|
34753
|
-
}, (error) => {
|
|
34754
|
-
signal.removeEventListener("abort", stop);
|
|
34755
|
-
reject(error);
|
|
34756
|
-
});
|
|
34757
|
-
});
|
|
34758
|
-
}
|
|
34759
|
-
async function tradedToken(env, own, signal, waitForReady = false) {
|
|
34760
|
-
const { entry, made } = await untilAborted(clientFor(env, own), signal);
|
|
34761
|
-
const { client } = made;
|
|
34762
|
-
if (client === undefined) {
|
|
34763
|
-
throw new LoggableError("AGENT_SECRETS_URL and AGENT_SECRETS_MACHINE_IDENTITY no longer name a broker and an identity to trade");
|
|
34764
|
-
}
|
|
34765
|
-
const { session, variables } = keyOf(env, own);
|
|
34766
|
-
try {
|
|
34767
|
-
return {
|
|
34768
|
-
token: await client.token(signal, { waitForReady }),
|
|
34769
|
-
key: client.key,
|
|
34770
|
-
session: own,
|
|
34771
|
-
identity: JSON.stringify([session, variables, made.signIn])
|
|
34772
|
-
};
|
|
34773
|
-
} catch (error) {
|
|
34774
|
-
if (made.signIn !== "" && isFinalRefusal(error))
|
|
34775
|
-
entry.refused.set(made.signIn, error);
|
|
34776
|
-
throw error;
|
|
34777
|
-
}
|
|
34778
|
-
}
|
|
34779
|
-
function brokerTokenFor(env, session) {
|
|
34780
|
-
return (signal, waitForReady) => tradedToken(env, sessionName(session), signal, waitForReady);
|
|
34781
|
-
}
|
|
34782
|
-
function dropHeld(key, token) {
|
|
34783
|
-
CLIENTS.get(key.session)?.get(key.variables)?.made?.client?.drop(token);
|
|
34784
|
-
}
|
|
34785
|
-
function dropTradedToken(env, traded) {
|
|
34786
|
-
dropHeld(keyOf(env, traded.session), traded.token.accessToken);
|
|
34787
|
-
}
|
|
34788
|
-
function brokerCredential(env, session) {
|
|
34789
|
-
let presented = "";
|
|
34790
|
-
const presentedAs = new WeakMap;
|
|
34791
|
-
const own = (acting) => (acting ?? "").trim() || sessionName(session);
|
|
34792
|
-
return {
|
|
34793
|
-
scheme: "DPoP",
|
|
34794
|
-
async headers(method, url, acting, signal) {
|
|
34795
|
-
const traded = await tradedToken(env, own(acting), signal);
|
|
34796
|
-
const { token, key, identity } = traded;
|
|
34797
|
-
presented = token.accessToken;
|
|
34798
|
-
const headers = {
|
|
34799
|
-
Authorization: `DPoP ${token.accessToken}`,
|
|
34800
|
-
DPoP: await key.proof({ method, url, accessToken: token.accessToken })
|
|
34801
|
-
};
|
|
34802
|
-
presentedAs.set(headers, {
|
|
34803
|
-
identity,
|
|
34804
|
-
key: keyOf(env, traded.session),
|
|
34805
|
-
token: token.accessToken
|
|
34806
|
-
});
|
|
34807
|
-
return headers;
|
|
34808
|
-
},
|
|
34809
|
-
identity: (headers) => presentedAs.get(headers)?.identity,
|
|
34810
|
-
stale(acting, headers) {
|
|
34811
|
-
const told = headers === undefined ? undefined : presentedAs.get(headers);
|
|
34812
|
-
dropHeld(told?.key ?? keyOf(env, own(acting)), told?.token);
|
|
34813
|
-
},
|
|
34814
|
-
secret: () => presented
|
|
34815
|
-
};
|
|
34816
|
-
}
|
|
34817
|
-
|
|
34818
|
-
// ../envoy-client/src/dispatch-config.ts
|
|
34819
|
-
var DEFAULT_SERVER_URL = "http://localhost:8766";
|
|
34820
|
-
function checkoutPair(checkout, serverUrl) {
|
|
34821
|
-
const { token } = checkout.dispatch;
|
|
34822
|
-
if (token === undefined) {
|
|
34823
|
-
throw new Error(`${checkout.path}: dispatch.serverUrl requires dispatch.token in the same file`);
|
|
34824
|
-
}
|
|
34825
|
-
return {
|
|
34826
|
-
kind: "file",
|
|
34827
|
-
token,
|
|
34828
|
-
field: "dispatch.token",
|
|
34829
|
-
path: checkout.path,
|
|
34830
|
-
server: serverUrl,
|
|
34831
|
-
checkout: true
|
|
34832
|
-
};
|
|
34833
|
-
}
|
|
34834
|
-
function writtenToken(config, checkout) {
|
|
34835
|
-
if (checkout?.dispatch.token !== undefined) {
|
|
34836
|
-
throw new Error(`${checkout.path}: dispatch.token requires dispatch.serverUrl in the same file`);
|
|
34837
|
-
}
|
|
34838
|
-
const own = config.dispatch;
|
|
34839
|
-
if (own.token === undefined)
|
|
34840
|
-
return;
|
|
34841
|
-
const server = own.serverUrl ?? DEFAULT_SERVER_URL;
|
|
34842
|
-
return {
|
|
34843
|
-
token: own.token,
|
|
34844
|
-
field: "dispatch.token",
|
|
34845
|
-
path: config.path,
|
|
34846
|
-
server,
|
|
34847
|
-
checkout: false
|
|
34848
|
-
};
|
|
34849
|
-
}
|
|
34850
|
-
function lostDispatchUrlNotice(env, config) {
|
|
34851
|
-
if (nonEmpty(env.DISPATCH_TOKEN_FILE) === undefined)
|
|
34852
|
-
return;
|
|
34853
|
-
const link = config.missing?.link ?? null;
|
|
34854
|
-
const old = config.missing?.oldUserFileNotice;
|
|
34855
|
-
return [
|
|
34856
|
-
"DISPATCH_TOKEN_FILE is set and DISPATCH_URL is not, so Dispatch is off: this shell lost the DISPATCH_URL it started with. Export DISPATCH_URL again, set to the Dispatch server that token is for",
|
|
34857
|
-
...link === null ? [] : [`${config.path} is a link to ${link}, which does not exist`],
|
|
34858
|
-
...old === undefined ? [] : [old]
|
|
34859
|
-
].join("; ");
|
|
34860
|
-
}
|
|
34861
|
-
function dispatchSettings(env, config, options = {}) {
|
|
34862
|
-
const target = dispatchTarget(env, config);
|
|
34863
|
-
if (!target.enabled)
|
|
34864
|
-
return target;
|
|
34865
|
-
const { url, urlSetting, source } = target;
|
|
34866
|
-
let credential;
|
|
34867
|
-
try {
|
|
34868
|
-
credential = source.kind === "broker" ? brokerCredential(env, options.session) : bearerCredential(source.kind === "variable" ? source.token : fileBearer(source, url, "DISPATCH", env, options.commands));
|
|
34869
|
-
} catch (error) {
|
|
34870
|
-
return offWithError(messageFor(error));
|
|
34871
|
-
}
|
|
34872
|
-
const publicUrl = nonEmpty(env.DISPATCH_PUBLIC_URL);
|
|
34873
|
-
const publicParsed = publicUrl === undefined ? undefined : httpUrl2(publicUrl);
|
|
34874
|
-
if (publicUrl !== undefined && (publicParsed === undefined || publicParsed.port === "0")) {
|
|
34875
|
-
return offWithError("DISPATCH_PUBLIC_URL must be an http or https URL");
|
|
34876
|
-
}
|
|
34877
|
-
const base = publicUrl === undefined ? url : withoutTrailingSlashes(publicUrl);
|
|
34878
|
-
const linkNotice = linkNoticeFor(base, publicUrl === undefined ? urlSetting : "DISPATCH_PUBLIC_URL");
|
|
34879
|
-
return {
|
|
34880
|
-
enabled: true,
|
|
34881
|
-
url,
|
|
34882
|
-
credential,
|
|
34883
|
-
...publicUrl === undefined ? {} : { publicUrl: withoutTrailingSlashes(publicUrl) },
|
|
34884
|
-
...linkNotice === undefined ? {} : { linkNotice }
|
|
34885
|
-
};
|
|
34886
|
-
}
|
|
34887
|
-
function dispatchUrl(env, config) {
|
|
34888
|
-
const target = dispatchTarget(env, config);
|
|
34889
|
-
return target.enabled ? target.url : undefined;
|
|
34890
|
-
}
|
|
34891
|
-
function brokerRouteForced(env) {
|
|
34892
|
-
const written = (env.DISPATCH_TOKEN_BROKER ?? "").trim();
|
|
34893
|
-
if (written === "")
|
|
34894
|
-
return false;
|
|
34895
|
-
if (written === "1")
|
|
34896
|
-
return true;
|
|
34897
|
-
return offWithError("DISPATCH_TOKEN_BROKER takes 1: set it to 1 where this session's Dispatch credential is traded at the secrets broker (the person signed in on this machine, else the machine's own identity), and leave it unset where a bearer is read as usual");
|
|
34898
|
-
}
|
|
34899
|
-
function dispatchTarget(env, config) {
|
|
34900
|
-
if (config.kind === "failed")
|
|
34901
|
-
return offWithError(config.error);
|
|
34902
|
-
const forced = brokerRouteForced(env);
|
|
34903
|
-
if (typeof forced !== "boolean")
|
|
34904
|
-
return forced;
|
|
34905
|
-
const explicitUrl = nonEmpty(env.DISPATCH_URL);
|
|
34906
|
-
const checkoutFile = explicitUrl === undefined ? config.checkout : null;
|
|
34907
|
-
if (checkoutFile?.kind === "invalid")
|
|
34908
|
-
return offWithError(checkoutFile.error);
|
|
34909
|
-
const checkout = checkoutFile?.kind === "valid" ? checkoutFile : null;
|
|
34910
|
-
const own = config.dispatch;
|
|
34911
|
-
if (own.enabled === false) {
|
|
34912
|
-
return offWithNotice(`${config.path} sets dispatch.enabled to false: Dispatch is off`);
|
|
34913
|
-
}
|
|
34914
|
-
if (checkout?.dispatch.enabled === false) {
|
|
34915
|
-
return offWithNotice(`${checkout.path} sets dispatch.enabled to false: Dispatch is off`);
|
|
34916
|
-
}
|
|
34917
|
-
const checkoutServer = checkout?.dispatch.serverUrl;
|
|
34918
|
-
const addressed = explicitUrl !== undefined || checkoutServer !== undefined || own.serverUrl !== undefined || own.enabled === true;
|
|
34919
|
-
if (!addressed) {
|
|
34920
|
-
const lost = lostDispatchUrlNotice(env, config);
|
|
34921
|
-
if (lost !== undefined)
|
|
34922
|
-
return offWithNotice(lost);
|
|
34923
|
-
return offWithNotice(config.missing === null ? `${config.path} names no Dispatch server, and DISPATCH_URL is not set: Dispatch is off` : missingConfigNotice(config.path, config.missing, "Dispatch is off"));
|
|
34924
|
-
}
|
|
34925
|
-
if (explicitUrl !== undefined && httpUrl2(explicitUrl) === undefined) {
|
|
34926
|
-
return offWithError("DISPATCH_URL must be an http or https URL");
|
|
34927
|
-
}
|
|
34928
|
-
let source = { kind: "none" };
|
|
34929
|
-
if (!forced) {
|
|
34930
|
-
try {
|
|
34931
|
-
source = checkout !== null && checkoutServer !== undefined ? checkoutPair(checkout, checkoutServer) : bearerSource(env, "DISPATCH", () => writtenToken(config, checkout));
|
|
34932
|
-
} catch (error) {
|
|
34933
|
-
return offWithError(messageFor(error));
|
|
34934
|
-
}
|
|
34935
|
-
}
|
|
34936
|
-
if (source.kind === "none") {
|
|
34937
|
-
let route;
|
|
34938
|
-
try {
|
|
34939
|
-
route = brokerRouteConfigured(env);
|
|
34940
|
-
} catch (error) {
|
|
34941
|
-
return offWithError(messageFor(error));
|
|
34942
|
-
}
|
|
34943
|
-
if (route.traded) {
|
|
34944
|
-
const brokered = withoutTrailingSlashes(explicitUrl ?? own.serverUrl ?? DEFAULT_SERVER_URL);
|
|
34945
|
-
const urlSetting = explicitUrl !== undefined ? "DISPATCH_URL" : own.serverUrl !== undefined ? `dispatch.serverUrl in ${config.path}` : DEFAULT_URL_SETTING;
|
|
34946
|
-
return { enabled: true, url: brokered, urlSetting, source: { kind: "broker" } };
|
|
34947
|
-
}
|
|
34948
|
-
if (forced) {
|
|
34949
|
-
return offWithError(`DISPATCH_TOKEN_BROKER is set, so this session's Dispatch credential is traded at the secrets broker (the person signed in on this machine, else the machine's own identity), and ${route.gap}: Dispatch is off rather than written with whatever bearer this box holds`);
|
|
34950
|
-
}
|
|
34951
|
-
return offWithError(explicitUrl === undefined ? `${config.path}: dispatch is enabled and no token is: set dispatch.token, or DISPATCH_TOKEN or DISPATCH_TOKEN_FILE` : `DISPATCH_URL is set and no token is: set DISPATCH_TOKEN or DISPATCH_TOKEN_FILE, or dispatch.token in ${config.path}`);
|
|
34952
|
-
}
|
|
34953
|
-
const url = withoutTrailingSlashes(explicitUrl ?? (source.kind === "file" ? source.server : own.serverUrl ?? DEFAULT_SERVER_URL));
|
|
34954
|
-
const urlSetting = explicitUrl !== undefined ? "DISPATCH_URL" : source.kind === "file" ? `dispatch.serverUrl in ${source.path}` : own.serverUrl !== undefined ? `dispatch.serverUrl in ${config.path}` : DEFAULT_URL_SETTING;
|
|
34955
|
-
return { enabled: true, url, urlSetting, source };
|
|
34956
|
-
}
|
|
34957
|
-
var DEFAULT_URL_SETTING = "Dispatch's default URL";
|
|
34958
|
-
function linkNoticeFor(linkBase, setting) {
|
|
34959
|
-
if (isCleanUrl(linkBase))
|
|
34960
|
-
return;
|
|
34961
|
-
return `${setting} is not a plain http(s) URL (it carries user info, a query, a fragment, '%' or '\\'), so the links Dispatch commands print are dispatch:// references; set DISPATCH_PUBLIC_URL to a plain https URL for dashboard links`;
|
|
34962
|
-
}
|
|
34963
|
-
function activeDispatchSettings(resolution) {
|
|
34964
|
-
if (resolution.enabled)
|
|
34965
|
-
return resolution;
|
|
34966
|
-
if (resolution.error !== null)
|
|
34967
|
-
throw new Error(`dispatch config: ${resolution.error}`);
|
|
34968
|
-
return null;
|
|
34969
|
-
}
|
|
34970
|
-
|
|
34971
35215
|
// ../envoy-client/src/nats-credential.ts
|
|
34972
35216
|
var NATS_AUTH_VAR = "NATS_AUTH";
|
|
34973
35217
|
function noAuthService(source) {
|
|
@@ -35132,7 +35376,6 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
|
|
|
35132
35376
|
admissions: () => admissions
|
|
35133
35377
|
};
|
|
35134
35378
|
}
|
|
35135
|
-
var RENEW_FLOOR_MS = 1000;
|
|
35136
35379
|
var RETRY_FIRST_MS = 2000;
|
|
35137
35380
|
var RETRY_LAST_MS = 60000;
|
|
35138
35381
|
var WARN_AGAIN_MS = 30 * 60000;
|
|
@@ -35189,11 +35432,7 @@ function warnedEvery(warn) {
|
|
|
35189
35432
|
};
|
|
35190
35433
|
}
|
|
35191
35434
|
function renewDelay(expiry, now) {
|
|
35192
|
-
|
|
35193
|
-
if (remaining <= 0)
|
|
35194
|
-
return 0;
|
|
35195
|
-
const ahead = remaining - Math.min(DEFAULT_RENEW_BEFORE_MS, remaining / 2);
|
|
35196
|
-
return Math.max(Math.min(RENEW_FLOOR_MS, remaining / 2), ahead);
|
|
35435
|
+
return renewalAt(expiry, now, DEFAULT_RENEW_BEFORE_MS) - now;
|
|
35197
35436
|
}
|
|
35198
35437
|
function keepNatsCredential(connection, credential, warn) {
|
|
35199
35438
|
let stopped = false;
|
|
@@ -35632,7 +35871,8 @@ function sessionDispatchSettings(file, env, options) {
|
|
|
35632
35871
|
return {
|
|
35633
35872
|
dispatch: dispatchSettings(env, config, {
|
|
35634
35873
|
commands: options.commands,
|
|
35635
|
-
session: options.session
|
|
35874
|
+
session: options.session,
|
|
35875
|
+
tokenFiles: options.tokenFiles
|
|
35636
35876
|
}),
|
|
35637
35877
|
warnings: config.kind === "read" ? config.warnings : [],
|
|
35638
35878
|
missing: config.kind === "read" ? config.missing : null
|
|
@@ -36395,7 +36635,7 @@ async function runDispatchCli(argv, rawEnv, io) {
|
|
|
36395
36635
|
}
|
|
36396
36636
|
const file = (env.DISPATCH_HOST === undefined ? null : handedConfigFile(env)) ?? sessionConfigFile(host, env);
|
|
36397
36637
|
if (parsed.dryRun && !dryRunAsksDispatch(parsed.tool, parsed.args)) {
|
|
36398
|
-
const settings = file.path === null ? null : sessionDispatchSettings(file, env, { cwd: io.cwd, session: sessionId }).dispatch;
|
|
36638
|
+
const settings = file.path === null ? null : sessionDispatchSettings(file, env, { cwd: io.cwd, session: sessionId, tokenFiles: true }).dispatch;
|
|
36399
36639
|
const { problems } = await checkDispatchCall({
|
|
36400
36640
|
tool: parsed.tool,
|
|
36401
36641
|
args: parsed.args,
|
|
@@ -36414,7 +36654,8 @@ async function runDispatchCli(argv, rawEnv, io) {
|
|
|
36414
36654
|
}
|
|
36415
36655
|
const { dispatch: resolution, warnings } = sessionDispatchSettings(file, env, {
|
|
36416
36656
|
cwd: io.cwd,
|
|
36417
|
-
session: sessionId
|
|
36657
|
+
session: sessionId,
|
|
36658
|
+
tokenFiles: true
|
|
36418
36659
|
});
|
|
36419
36660
|
const wholeJson = printsJson(parsed.tool, parsed.args);
|
|
36420
36661
|
const note = (line) => wholeJson ? io.stderr(`${line}
|
|
@@ -36463,7 +36704,7 @@ ${result.text}`);
|
|
|
36463
36704
|
const fd = openSync5(operandPath(io.cwd, file), constants4.O_RDONLY | constants4.O_NONBLOCK);
|
|
36464
36705
|
try {
|
|
36465
36706
|
if (fstatSync3(fd).isFile())
|
|
36466
|
-
bytes =
|
|
36707
|
+
bytes = readFileSync8(fd);
|
|
36467
36708
|
} finally {
|
|
36468
36709
|
closeSync5(fd);
|
|
36469
36710
|
}
|
|
@@ -36599,7 +36840,7 @@ ${result.text}`);
|
|
|
36599
36840
|
}
|
|
36600
36841
|
|
|
36601
36842
|
// ../envoy-client/bin/dispatch.ts
|
|
36602
|
-
var readText = (path) =>
|
|
36843
|
+
var readText = (path) => readFileSync9(path === "-" ? 0 : path, "utf-8");
|
|
36603
36844
|
var pending = [];
|
|
36604
36845
|
var writeTo = (stream) => (text) => {
|
|
36605
36846
|
const { promise, resolve } = Promise.withResolvers();
|