@limrun/api 0.77.1 → 0.77.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/README.md +92 -2
- package/client.d.mts +18 -0
- package/client.d.mts.map +1 -1
- package/client.d.ts +18 -0
- package/client.d.ts.map +1 -1
- package/client.js +42 -6
- package/client.js.map +1 -1
- package/client.mjs +43 -7
- package/client.mjs.map +1 -1
- package/destination-tunnel.d.mts.map +1 -1
- package/destination-tunnel.d.ts.map +1 -1
- package/destination-tunnel.js +3 -2
- package/destination-tunnel.js.map +1 -1
- package/destination-tunnel.mjs +3 -2
- package/destination-tunnel.mjs.map +1 -1
- package/gradle-snapshot.d.mts +2 -2
- package/gradle-snapshot.d.mts.map +1 -1
- package/gradle-snapshot.d.ts +2 -2
- package/gradle-snapshot.d.ts.map +1 -1
- package/gradle-snapshot.js +1 -1
- package/gradle-snapshot.js.map +1 -1
- package/gradle-snapshot.mjs +1 -1
- package/gradle-snapshot.mjs.map +1 -1
- package/index.d.mts +1 -0
- package/index.d.mts.map +1 -1
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/index.mjs.map +1 -1
- package/internal/instance-phase-stream.d.mts +10 -1
- package/internal/instance-phase-stream.d.mts.map +1 -1
- package/internal/instance-phase-stream.d.ts +10 -1
- package/internal/instance-phase-stream.d.ts.map +1 -1
- package/internal/instance-phase-stream.js +17 -6
- package/internal/instance-phase-stream.js.map +1 -1
- package/internal/instance-phase-stream.mjs +17 -6
- package/internal/instance-phase-stream.mjs.map +1 -1
- package/lib/workload-credentials.d.mts +14 -3
- package/lib/workload-credentials.d.mts.map +1 -1
- package/lib/workload-credentials.d.ts +14 -3
- package/lib/workload-credentials.d.ts.map +1 -1
- package/lib/workload-credentials.js +50 -14
- package/lib/workload-credentials.js.map +1 -1
- package/lib/workload-credentials.mjs +46 -13
- package/lib/workload-credentials.mjs.map +1 -1
- package/package.json +1 -1
- package/resources/access-tokens.d.mts +7 -4
- package/resources/access-tokens.d.mts.map +1 -1
- package/resources/access-tokens.d.ts +7 -4
- package/resources/access-tokens.d.ts.map +1 -1
- package/resources/assets.d.mts +19 -4
- package/resources/assets.d.mts.map +1 -1
- package/resources/assets.d.ts +19 -4
- package/resources/assets.d.ts.map +1 -1
- package/resources/gradle-instances-helpers.d.mts +1 -1
- package/resources/gradle-instances.d.mts.map +1 -1
- package/resources/gradle-instances.d.ts.map +1 -1
- package/resources/gradle-instances.js +12 -4
- package/resources/gradle-instances.js.map +1 -1
- package/resources/gradle-instances.mjs +12 -4
- package/resources/gradle-instances.mjs.map +1 -1
- package/resources/index.d.mts +2 -0
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +2 -0
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +2 -0
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +2 -0
- package/resources/index.mjs.map +1 -1
- package/resources/macos-instances-helpers.d.mts +271 -0
- package/resources/macos-instances-helpers.d.mts.map +1 -0
- package/resources/macos-instances-helpers.d.ts +271 -0
- package/resources/macos-instances-helpers.d.ts.map +1 -0
- package/resources/macos-instances-helpers.js +298 -0
- package/resources/macos-instances-helpers.js.map +1 -0
- package/resources/macos-instances-helpers.mjs +296 -0
- package/resources/macos-instances-helpers.mjs.map +1 -0
- package/resources/macos-instances.d.mts +19 -0
- package/resources/macos-instances.d.mts.map +1 -1
- package/resources/macos-instances.d.ts +19 -0
- package/resources/macos-instances.d.ts.map +1 -1
- package/resources/support-tickets.d.mts +3 -0
- package/resources/support-tickets.d.mts.map +1 -1
- package/resources/support-tickets.d.ts +3 -0
- package/resources/support-tickets.d.ts.map +1 -1
- package/resources/support-tickets.js +8 -0
- package/resources/support-tickets.js.map +1 -1
- package/resources/support-tickets.mjs +8 -0
- package/resources/support-tickets.mjs.map +1 -1
- package/resources/xcode-instances-helpers.d.mts +1 -0
- package/resources/xcode-instances-helpers.d.mts.map +1 -1
- package/resources/xcode-instances-helpers.d.ts +1 -0
- package/resources/xcode-instances-helpers.d.ts.map +1 -1
- package/resources/xcode-instances-helpers.js +15 -9
- package/resources/xcode-instances-helpers.js.map +1 -1
- package/resources/xcode-instances-helpers.mjs +15 -9
- package/resources/xcode-instances-helpers.mjs.map +1 -1
- package/src/client.ts +80 -9
- package/src/destination-tunnel.ts +3 -2
- package/src/gradle-snapshot.ts +3 -4
- package/src/index.ts +16 -0
- package/src/internal/instance-phase-stream.ts +30 -8
- package/src/lib/workload-credentials.ts +70 -13
- package/src/resources/access-tokens.ts +7 -4
- package/src/resources/assets.ts +24 -4
- package/src/resources/gradle-instances.ts +15 -3
- package/src/resources/index.ts +11 -0
- package/src/resources/macos-instances-helpers.ts +655 -0
- package/src/resources/macos-instances.ts +30 -0
- package/src/resources/support-tickets.ts +15 -0
- package/src/resources/xcode-instances-helpers.ts +16 -8
- package/src/version.ts +1 -1
- package/src/xcode-cache.ts +3 -4
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/xcode-cache.d.mts +2 -2
- package/xcode-cache.d.mts.map +1 -1
- package/xcode-cache.d.ts +2 -2
- package/xcode-cache.d.ts.map +1 -1
- package/xcode-cache.js +1 -1
- package/xcode-cache.js.map +1 -1
- package/xcode-cache.mjs +1 -1
- package/xcode-cache.mjs.map +1 -1
package/src/client.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
WorkloadCredentials,
|
|
3
|
+
injectedWorkloadIdentityReader,
|
|
4
|
+
workloadTokenFileReader,
|
|
5
|
+
workloadTokenReader,
|
|
6
|
+
type WorkloadIdentityReader,
|
|
7
|
+
type WorkloadToken,
|
|
8
|
+
} from './lib/workload-credentials';
|
|
2
9
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3
10
|
|
|
4
11
|
import type { RequestInit, RequestInfo, BodyInit } from './internal/builtin-types';
|
|
@@ -117,9 +124,62 @@ import {
|
|
|
117
124
|
} from './internal/utils/log';
|
|
118
125
|
import { isEmptyObj } from './internal/utils/values';
|
|
119
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Picks the workload identity source: explicit options before environment variables, and a token
|
|
129
|
+
* before a file. Null on either option turns off discovery of its environment variable.
|
|
130
|
+
*/
|
|
131
|
+
function workloadIdentity(opts: {
|
|
132
|
+
workloadToken?: WorkloadToken | null | undefined;
|
|
133
|
+
workloadTokenFile?: string | null | undefined;
|
|
134
|
+
workloadTokenInjected?: boolean | null | undefined;
|
|
135
|
+
}): WorkloadIdentityReader | undefined {
|
|
136
|
+
const reader = workloadTokenSource(opts);
|
|
137
|
+
const injected =
|
|
138
|
+
opts.workloadTokenInjected === null ?
|
|
139
|
+
false
|
|
140
|
+
: opts.workloadTokenInjected ??
|
|
141
|
+
/^(1|true)$/i.test(readEnv('LIMRUN_WORKLOAD_IDENTITY_TOKEN_INJECTED') ?? '');
|
|
142
|
+
if (!injected) return reader;
|
|
143
|
+
// The proxy decides which JWT the exchange carries, so a second source would be ignored.
|
|
144
|
+
if (reader)
|
|
145
|
+
throw new Error(
|
|
146
|
+
'workloadTokenInjected cannot be combined with workloadToken, workloadTokenFile, or their environment variables',
|
|
147
|
+
);
|
|
148
|
+
return injectedWorkloadIdentityReader;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function workloadTokenSource(opts: {
|
|
152
|
+
workloadToken?: WorkloadToken | null | undefined;
|
|
153
|
+
workloadTokenFile?: string | null | undefined;
|
|
154
|
+
}): WorkloadIdentityReader | undefined {
|
|
155
|
+
if (opts.workloadToken) return workloadTokenReader(opts.workloadToken);
|
|
156
|
+
if (opts.workloadTokenFile) return workloadTokenFileReader(opts.workloadTokenFile);
|
|
157
|
+
const token = opts.workloadToken === null ? undefined : readEnv('LIMRUN_WORKLOAD_IDENTITY_TOKEN');
|
|
158
|
+
if (token) return workloadTokenReader(token);
|
|
159
|
+
const file = opts.workloadTokenFile === null ? undefined : readEnv('LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE');
|
|
160
|
+
return file ? workloadTokenFileReader(file) : undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
120
163
|
export interface ClientOptions {
|
|
164
|
+
/**
|
|
165
|
+
* Workload JWT from your identity provider, or a function that returns the current one. A string is
|
|
166
|
+
* exchanged as is on every renewal, so pass a function when the JWT expires before the client is done.
|
|
167
|
+
* Takes precedence over workloadTokenFile. Defaults to LIMRUN_WORKLOAD_IDENTITY_TOKEN. Null disables discovery.
|
|
168
|
+
*/
|
|
169
|
+
workloadToken?: WorkloadToken | null;
|
|
121
170
|
/** Projected workload JWT file. Defaults to LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE. Null disables discovery. */
|
|
122
171
|
workloadTokenFile?: string | null;
|
|
172
|
+
/**
|
|
173
|
+
* Exchange without sending a JWT, for platforms whose egress proxy adds it to requests for the token
|
|
174
|
+
* service in the X-Limrun-Subject-Token header, so the workload never handles it. Requires bindingId
|
|
175
|
+
* and cannot be combined with workloadToken or workloadTokenFile.
|
|
176
|
+
* Defaults to LIMRUN_WORKLOAD_IDENTITY_TOKEN_INJECTED=true. Null disables discovery.
|
|
177
|
+
*/
|
|
178
|
+
workloadTokenInjected?: boolean | null;
|
|
179
|
+
/** Customer federation binding ID. Defaults to LIMRUN_WORKLOAD_IDENTITY_BINDING_ID. */
|
|
180
|
+
bindingId?: string | null;
|
|
181
|
+
/** Token service base URL. Federation defaults to https://sts.limrun.com. */
|
|
182
|
+
stsBaseURL?: string;
|
|
123
183
|
/**
|
|
124
184
|
* Defaults to process.env['LIM_API_KEY'].
|
|
125
185
|
*/
|
|
@@ -237,15 +297,23 @@ export class Limrun {
|
|
|
237
297
|
apiKey = readEnv('LIM_API_KEY') ?? null,
|
|
238
298
|
...opts
|
|
239
299
|
}: ClientOptions = {}) {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
: opts.
|
|
300
|
+
// An API key wins, so workload identity settings it overrides cannot fail construction.
|
|
301
|
+
const identity = apiKey ? undefined : workloadIdentity(opts);
|
|
302
|
+
const bindingId =
|
|
303
|
+
opts.bindingId === null ? undefined : opts.bindingId ?? readEnv('LIMRUN_WORKLOAD_IDENTITY_BINDING_ID');
|
|
304
|
+
if (!apiKey && identity === injectedWorkloadIdentityReader && !bindingId)
|
|
305
|
+
throw new Error('workloadTokenInjected requires bindingId or LIMRUN_WORKLOAD_IDENTITY_BINDING_ID');
|
|
306
|
+
if (!apiKey && bindingId && !identity)
|
|
307
|
+
throw new Error(
|
|
308
|
+
'Federation requires workloadToken, workloadTokenFile, LIMRUN_WORKLOAD_IDENTITY_TOKEN, or LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE',
|
|
309
|
+
);
|
|
244
310
|
const options: ClientOptions = {
|
|
245
311
|
apiKey,
|
|
246
312
|
...opts,
|
|
247
313
|
baseURL:
|
|
248
|
-
baseURL ||
|
|
314
|
+
baseURL ||
|
|
315
|
+
(!apiKey && identity && !bindingId ? readEnv('DIRECTOR_URL') : undefined) ||
|
|
316
|
+
`https://api.limrun.com`,
|
|
249
317
|
};
|
|
250
318
|
|
|
251
319
|
this.baseURL = options.baseURL!;
|
|
@@ -278,12 +346,15 @@ export class Limrun {
|
|
|
278
346
|
this._options = options;
|
|
279
347
|
|
|
280
348
|
this.apiKey = apiKey;
|
|
281
|
-
if (!apiKey &&
|
|
349
|
+
if (!apiKey && identity)
|
|
282
350
|
this.workloadCredentials = new WorkloadCredentials(
|
|
283
|
-
|
|
284
|
-
|
|
351
|
+
opts.stsBaseURL ??
|
|
352
|
+
readEnv('LIMRUN_STS_BASE_URL') ??
|
|
353
|
+
(bindingId ? 'https://sts.limrun.com' : this.baseURL),
|
|
354
|
+
identity,
|
|
285
355
|
this.fetch,
|
|
286
356
|
this.fetchOptions,
|
|
357
|
+
bindingId,
|
|
287
358
|
);
|
|
288
359
|
}
|
|
289
360
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
import net from 'net';
|
|
3
|
-
import
|
|
3
|
+
// A default import: browsers bundle this module (through the macOS client) and only Node runs it.
|
|
4
|
+
import url from 'url';
|
|
4
5
|
import {
|
|
5
6
|
DestinationTunnelProtocolError,
|
|
6
7
|
readArray,
|
|
@@ -637,7 +638,7 @@ function canonicalizeDestinationTunnelDomain(domain: string): string {
|
|
|
637
638
|
if (base.endsWith('.') || base.startsWith('.')) invalid();
|
|
638
639
|
if (net.isIP(base) !== 0) invalid();
|
|
639
640
|
if (base === 'localhost') invalid();
|
|
640
|
-
if (domainToASCII(base) !== base) invalid();
|
|
641
|
+
if (url.domainToASCII(base) !== base) invalid();
|
|
641
642
|
const labels = base.split('.');
|
|
642
643
|
for (const label of labels) {
|
|
643
644
|
if (label.length === 0 || label.length > 63) invalid();
|
package/src/gradle-snapshot.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { followPhaseStream } from './internal/instance-phase-stream';
|
|
1
|
+
import { followPhaseStream, type PhaseStreamAuth } from './internal/instance-phase-stream';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A disk snapshot of a Gradle build sandbox, restored after the instance is assigned and saved at
|
|
@@ -82,9 +82,8 @@ export type GradleSnapshotSide = 'restore' | 'save';
|
|
|
82
82
|
|
|
83
83
|
export type GradleSnapshotFollowTarget = {
|
|
84
84
|
baseURL: string;
|
|
85
|
-
apiKey: string;
|
|
86
85
|
instanceId: string;
|
|
87
|
-
};
|
|
86
|
+
} & PhaseStreamAuth;
|
|
88
87
|
|
|
89
88
|
export type GradleSnapshotFollowOptions = {
|
|
90
89
|
/** Defaults to 'restore'. */
|
|
@@ -174,7 +173,7 @@ export async function followGradleSnapshot(
|
|
|
174
173
|
while (base.endsWith('/')) base = base.slice(0, -1);
|
|
175
174
|
const result = await followPhaseStream<GradleInstanceSnapshot>({
|
|
176
175
|
url: `${base}/v1/gradle_instances/${encodeURIComponent(target.instanceId)}/snapshot`,
|
|
177
|
-
|
|
176
|
+
auth: target,
|
|
178
177
|
instanceId: target.instanceId,
|
|
179
178
|
eventName: 'snapshot',
|
|
180
179
|
timeoutMs,
|
package/src/index.ts
CHANGED
|
@@ -171,6 +171,22 @@ export {
|
|
|
171
171
|
type GradleRunOptions,
|
|
172
172
|
type GradleBuildLog,
|
|
173
173
|
} from './resources/gradle-instances-helpers';
|
|
174
|
+
export {
|
|
175
|
+
type MacosCreateClientParams,
|
|
176
|
+
type MacosClient,
|
|
177
|
+
type MacosPoint,
|
|
178
|
+
type MacosModifier,
|
|
179
|
+
type MacosSshAccess,
|
|
180
|
+
type MacosSnapshotRestoreOptions,
|
|
181
|
+
type MacosSnapshotProgress,
|
|
182
|
+
type MacosTunnelOptions,
|
|
183
|
+
type MacosRecordingOptions,
|
|
184
|
+
type MacosRecordingDestination,
|
|
185
|
+
type MacosStreamHandlers,
|
|
186
|
+
type MacosLaunchMode,
|
|
187
|
+
type MacosLaunchAppOptions,
|
|
188
|
+
type MacosLaunchAppResult,
|
|
189
|
+
} from './resources/macos-instances-helpers';
|
|
174
190
|
export { type AssetUploadOptions } from './resources/daemon-client-shared';
|
|
175
191
|
export {
|
|
176
192
|
type SessionArtifact,
|
|
@@ -2,6 +2,15 @@ import { createEventSource, type EventSourceMessage } from 'eventsource-client';
|
|
|
2
2
|
|
|
3
3
|
import { nodeProxyTransport } from './proxy-transport';
|
|
4
4
|
|
|
5
|
+
/** Credentials for a phase stream: a fixed API key, or headers resolved on every connection. */
|
|
6
|
+
export type PhaseStreamAuth =
|
|
7
|
+
| { apiKey: string; getHeaders?: never }
|
|
8
|
+
| {
|
|
9
|
+
apiKey?: never;
|
|
10
|
+
/** Resolves current credentials on every connection, including reconnects. */
|
|
11
|
+
getHeaders: () => Promise<Headers>;
|
|
12
|
+
};
|
|
13
|
+
|
|
5
14
|
/**
|
|
6
15
|
* How to follow one instance's phase stream: an endpoint that answers JSON, or an event per
|
|
7
16
|
* phase change when asked for `text/event-stream`, and a `gone` event when the instance leaves.
|
|
@@ -9,7 +18,7 @@ import { nodeProxyTransport } from './proxy-transport';
|
|
|
9
18
|
*/
|
|
10
19
|
export type PhaseStreamOptions<T> = {
|
|
11
20
|
url: string;
|
|
12
|
-
|
|
21
|
+
auth: PhaseStreamAuth;
|
|
13
22
|
instanceId: string;
|
|
14
23
|
/** The event carrying a state; `gone` is handled for every stream. */
|
|
15
24
|
eventName: string;
|
|
@@ -37,7 +46,7 @@ export type PhaseStreamResult<T> = {
|
|
|
37
46
|
* wait times out. The server emits on phase change only, so every callback is a real transition.
|
|
38
47
|
*/
|
|
39
48
|
export function followPhaseStream<T>(options: PhaseStreamOptions<T>): Promise<PhaseStreamResult<T>> {
|
|
40
|
-
const { url,
|
|
49
|
+
const { url, auth } = options;
|
|
41
50
|
return new Promise<PhaseStreamResult<T>>((resolve, reject) => {
|
|
42
51
|
let last: T | undefined;
|
|
43
52
|
let lastKey = '';
|
|
@@ -61,14 +70,28 @@ export function followPhaseStream<T>(options: PhaseStreamOptions<T>): Promise<Ph
|
|
|
61
70
|
const resolveGone = () =>
|
|
62
71
|
finish(() => (last !== undefined ? resolve({ state: last, gone: true }) : reject(options.goneError())));
|
|
63
72
|
|
|
73
|
+
const fetchAuthenticated: typeof nodeProxyTransport.fetch = async (input, init) => {
|
|
74
|
+
let headers: Headers;
|
|
75
|
+
try {
|
|
76
|
+
headers = new Headers(
|
|
77
|
+
auth.getHeaders ? await auth.getHeaders() : { Authorization: `Bearer ${auth.apiKey}` },
|
|
78
|
+
);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// Credential errors need caller action; EventSource otherwise retries them until timeout.
|
|
81
|
+
finish(() => reject(error));
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
new Headers(init?.headers).forEach((value, key) => headers.set(key, value));
|
|
85
|
+
return nodeProxyTransport.fetch(input, { ...init, headers });
|
|
86
|
+
};
|
|
87
|
+
|
|
64
88
|
let confirming = false;
|
|
65
89
|
const confirmGone = () => {
|
|
66
90
|
if (confirming || settled) return;
|
|
67
91
|
confirming = true;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})
|
|
92
|
+
fetchAuthenticated(url, {
|
|
93
|
+
headers: { Accept: 'application/json' },
|
|
94
|
+
})
|
|
72
95
|
.then(async (res) => {
|
|
73
96
|
confirming = false;
|
|
74
97
|
if (res.status !== 404) {
|
|
@@ -87,7 +110,7 @@ export function followPhaseStream<T>(options: PhaseStreamOptions<T>): Promise<Ph
|
|
|
87
110
|
// Without this the client treats it as a connection to retry and follows a gone instance
|
|
88
111
|
// until the timeout, which is minutes of a command that looks hung and says nothing.
|
|
89
112
|
const fetchOrGone: typeof nodeProxyTransport.fetch = async (input, init) => {
|
|
90
|
-
const res = await
|
|
113
|
+
const res = await fetchAuthenticated(input, init);
|
|
91
114
|
if (res.status === 404) resolveGone();
|
|
92
115
|
return res;
|
|
93
116
|
};
|
|
@@ -97,7 +120,6 @@ export function followPhaseStream<T>(options: PhaseStreamOptions<T>): Promise<Ph
|
|
|
97
120
|
fetch: fetchOrGone,
|
|
98
121
|
onConnect: () => options.onOpen?.(),
|
|
99
122
|
headers: {
|
|
100
|
-
Authorization: `Bearer ${apiKey}`,
|
|
101
123
|
Accept: 'text/event-stream',
|
|
102
124
|
},
|
|
103
125
|
onMessage: (message: EventSourceMessage) => {
|
|
@@ -1,19 +1,42 @@
|
|
|
1
1
|
import type { MergedRequestInit } from '../internal/types';
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/** A workload JWT, or a function that returns the current one on every exchange. */
|
|
4
|
+
export type WorkloadToken = string | (() => string | Promise<string>);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns the identity JWT to exchange, read again for every exchange. Null means an egress proxy
|
|
8
|
+
* adds the JWT to the exchange request, so the client sends none.
|
|
9
|
+
*/
|
|
10
|
+
export type WorkloadIdentityReader = () => Promise<string | null>;
|
|
11
|
+
|
|
12
|
+
export const injectedWorkloadIdentityReader: WorkloadIdentityReader = async () => null;
|
|
13
|
+
|
|
14
|
+
export function workloadTokenReader(token: WorkloadToken): WorkloadIdentityReader {
|
|
15
|
+
return async () => (typeof token === 'function' ? await token() : token);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function workloadTokenFileReader(path: string): WorkloadIdentityReader {
|
|
19
|
+
return async () => {
|
|
20
|
+
const { readFile } = await import('node:fs/promises');
|
|
21
|
+
return readFile(path, 'utf8');
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Refreshes workload credentials without retaining the identity JWT. */
|
|
4
26
|
export class WorkloadCredentials {
|
|
5
|
-
private cached?: { token: string; expires: number };
|
|
27
|
+
private cached?: { token: string; expires: number; refreshAt: number };
|
|
6
28
|
private pending: Promise<string> | undefined;
|
|
7
29
|
|
|
8
30
|
constructor(
|
|
9
31
|
private endpoint: string,
|
|
10
|
-
private
|
|
32
|
+
private readIdentity: WorkloadIdentityReader,
|
|
11
33
|
private fetcher: typeof fetch,
|
|
12
34
|
private fetchOptions?: MergedRequestInit,
|
|
35
|
+
private bindingId?: string,
|
|
13
36
|
) {}
|
|
14
37
|
|
|
15
38
|
async get(): Promise<string> {
|
|
16
|
-
if (this.cached && this.cached.
|
|
39
|
+
if (this.cached && this.cached.refreshAt > Date.now()) return this.cached.token;
|
|
17
40
|
if (!this.pending)
|
|
18
41
|
this.pending = this.exchange().finally(() => {
|
|
19
42
|
this.pending = undefined;
|
|
@@ -22,23 +45,57 @@ export class WorkloadCredentials {
|
|
|
22
45
|
}
|
|
23
46
|
|
|
24
47
|
private async exchange(): Promise<string> {
|
|
25
|
-
const
|
|
26
|
-
const jwt =
|
|
48
|
+
const identity = await this.readIdentity();
|
|
49
|
+
const jwt =
|
|
50
|
+
identity === null ? undefined
|
|
51
|
+
: typeof identity === 'string' ? identity.trim()
|
|
52
|
+
: '';
|
|
53
|
+
if (jwt !== undefined && (!jwt || jwt.length > 32768))
|
|
54
|
+
throw new Error('Workload identity token must be a JWT of at most 32768 characters');
|
|
55
|
+
const start = Date.now();
|
|
27
56
|
const response = await this.fetcher(`${this.endpoint.replace(/\/$/, '')}/v1/access_tokens`, {
|
|
28
57
|
...(this.fetchOptions as RequestInit),
|
|
29
58
|
method: 'POST',
|
|
30
|
-
headers:
|
|
31
|
-
|
|
59
|
+
headers:
|
|
60
|
+
this.bindingId ?
|
|
61
|
+
{ 'Content-Type': 'application/x-www-form-urlencoded' }
|
|
62
|
+
: { Authorization: `Bearer ${jwt}`, 'Content-Type': 'application/json' },
|
|
63
|
+
body:
|
|
64
|
+
this.bindingId ?
|
|
65
|
+
new URLSearchParams({
|
|
66
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
|
|
67
|
+
subject_token_type: 'urn:ietf:params:oauth:token-type:jwt',
|
|
68
|
+
...(jwt !== undefined && { subject_token: jwt }),
|
|
69
|
+
audience: this.bindingId,
|
|
70
|
+
}).toString()
|
|
71
|
+
: JSON.stringify({}),
|
|
32
72
|
signal: AbortSignal.timeout(30_000),
|
|
33
73
|
redirect: 'error',
|
|
34
74
|
});
|
|
35
75
|
if (!response.ok) throw new Error(`Workload token exchange returned HTTP ${response.status}`);
|
|
36
|
-
const body = (await response.json()) as {
|
|
37
|
-
|
|
38
|
-
|
|
76
|
+
const body = (await response.json()) as {
|
|
77
|
+
token?: string;
|
|
78
|
+
expiresAt: string;
|
|
79
|
+
access_token?: string;
|
|
80
|
+
expires_in?: number;
|
|
81
|
+
token_type?: string;
|
|
82
|
+
};
|
|
83
|
+
const token = this.bindingId ? body.access_token : body.token;
|
|
84
|
+
if (typeof token !== 'string' || !token)
|
|
85
|
+
throw new Error('Workload token exchange returned no access token');
|
|
86
|
+
if (
|
|
87
|
+
this.bindingId &&
|
|
88
|
+
(body.token_type !== 'Bearer' ||
|
|
89
|
+
typeof body.expires_in !== 'number' ||
|
|
90
|
+
!Number.isInteger(body.expires_in) ||
|
|
91
|
+
body.expires_in < 1 ||
|
|
92
|
+
body.expires_in > 14400)
|
|
93
|
+
)
|
|
94
|
+
throw new Error('Workload token exchange returned an invalid token response');
|
|
95
|
+
const expires = this.bindingId ? start + body.expires_in! * 1000 : Date.parse(body.expiresAt);
|
|
39
96
|
if (!Number.isFinite(expires) || expires <= Date.now())
|
|
40
97
|
throw new Error('Workload token exchange returned an invalid expiration');
|
|
41
|
-
this.cached = { token:
|
|
42
|
-
return
|
|
98
|
+
this.cached = { token, expires, refreshAt: expires - Math.min(60_000, (expires - start) * 0.2) };
|
|
99
|
+
return token;
|
|
43
100
|
}
|
|
44
101
|
}
|
|
@@ -27,12 +27,15 @@ export interface AccessTokenCreateParams {
|
|
|
27
27
|
/**
|
|
28
28
|
* Permissions in the form `<resource>:<id|*>:<action>`, e.g. `device:*:install`,
|
|
29
29
|
* `asset:asset_01h455vb4pex5vsknk084sn02q:read` or `applerelay:*:connect`.
|
|
30
|
-
* Instance control accepts `ios:<id>:
|
|
31
|
-
* and `gradle:<id>:
|
|
30
|
+
* Instance control accepts `ios:<id>:control`, `android:<id>:control`, `xcode:<id>:control`,
|
|
31
|
+
* and `gradle:<id>:control`. Use the returned instance ID, including `sandbox_` for Xcode.
|
|
32
32
|
* iOS and Android accept `ios:<id>:watch` and `android:<id>:watch` for watch-only
|
|
33
33
|
* streaming, or `ios:<id>:stream` and `android:<id>:stream` for
|
|
34
34
|
* interactive streaming without device API or app-management access.
|
|
35
|
-
*
|
|
35
|
+
* Control includes reads and runtime operations, but excludes instance create/update/delete.
|
|
36
|
+
* Grant `create`, `update`, and `delete` separately; `all` includes every action.
|
|
37
|
+
* Creation requires target `*`. Instance targets also accept `*`. Any supported action may be delegated when
|
|
38
|
+
* covered by the caller, including `read`, build `work`, and device `tunnel`.
|
|
36
39
|
* See https://docs.limrun.com/docs/reference/permissions for targets and actions.
|
|
37
40
|
*/
|
|
38
41
|
permissions?: Array<string>;
|
|
@@ -42,7 +45,7 @@ export interface AccessTokenCreateParams {
|
|
|
42
45
|
|
|
43
46
|
/**
|
|
44
47
|
* How long the token stays valid. Defaults to 3600 (1 hour), maximum is 14400 (4
|
|
45
|
-
* hours).
|
|
48
|
+
* hours), capped by the source credential's expiry.
|
|
46
49
|
*/
|
|
47
50
|
ttlSeconds?: number;
|
|
48
51
|
}
|
package/src/resources/assets.ts
CHANGED
|
@@ -68,7 +68,7 @@ export class Assets extends APIResource {
|
|
|
68
68
|
export interface Asset {
|
|
69
69
|
id: string;
|
|
70
70
|
|
|
71
|
-
kind: 'App' | 'Keychain';
|
|
71
|
+
kind: 'App' | 'Keychain' | 'MacOSSnapshot';
|
|
72
72
|
|
|
73
73
|
name: string;
|
|
74
74
|
|
|
@@ -102,6 +102,26 @@ export interface Asset {
|
|
|
102
102
|
signedDownloadUrl?: string;
|
|
103
103
|
|
|
104
104
|
signedUploadUrl?: string;
|
|
105
|
+
|
|
106
|
+
/** What a kind records about its file. A `MacOSSnapshot` records the macOS build it restores onto. */
|
|
107
|
+
metadata?: AssetSnapshotMetadata;
|
|
108
|
+
|
|
109
|
+
/** SHA-256 of the uploaded file, for assets uploaded in parts. */
|
|
110
|
+
sha256?: string;
|
|
111
|
+
|
|
112
|
+
sizeBytes?: number;
|
|
113
|
+
|
|
114
|
+
/** When the last complete upload finished. */
|
|
115
|
+
publishedAt?: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The metadata of a `MacOSSnapshot` asset: a macOS instance's saved changes. */
|
|
119
|
+
export interface AssetSnapshotMetadata {
|
|
120
|
+
/** The guest's macOS build; a snapshot restores only onto the same one. */
|
|
121
|
+
compatKey: string;
|
|
122
|
+
format: string;
|
|
123
|
+
uncompressedSizeBytes: number;
|
|
124
|
+
sourceInstanceId?: string;
|
|
105
125
|
}
|
|
106
126
|
|
|
107
127
|
export type AssetListResponse = Array<Asset>;
|
|
@@ -109,7 +129,7 @@ export type AssetListResponse = Array<Asset>;
|
|
|
109
129
|
export interface AssetGetOrCreateResponse {
|
|
110
130
|
id: string;
|
|
111
131
|
|
|
112
|
-
kind: 'App' | 'Keychain';
|
|
132
|
+
kind: 'App' | 'Keychain' | 'MacOSSnapshot';
|
|
113
133
|
|
|
114
134
|
name: string;
|
|
115
135
|
|
|
@@ -152,7 +172,7 @@ export interface AssetListParams {
|
|
|
152
172
|
/**
|
|
153
173
|
* Filters assets by kind.
|
|
154
174
|
*/
|
|
155
|
-
kindFilter?: 'App' | 'Keychain';
|
|
175
|
+
kindFilter?: 'App' | 'Keychain' | 'MacOSSnapshot';
|
|
156
176
|
|
|
157
177
|
/**
|
|
158
178
|
* Maximum number of items to be returned. The default is 50.
|
|
@@ -237,7 +257,7 @@ export interface AssetGetOrCreateParams {
|
|
|
237
257
|
deeplink?: string;
|
|
238
258
|
}
|
|
239
259
|
|
|
240
|
-
export type AssetKind = 'App' | 'Keychain';
|
|
260
|
+
export type AssetKind = 'App' | 'Keychain' | 'MacOSSnapshot';
|
|
241
261
|
|
|
242
262
|
export type AssetPlatform = 'ios' | 'android' | 'xcode';
|
|
243
263
|
|
|
@@ -76,9 +76,21 @@ export class GradleInstances extends APIResource {
|
|
|
76
76
|
|
|
77
77
|
/** Follow the disk snapshot restore or save until it reaches a terminal phase. */
|
|
78
78
|
followSnapshot(id: string, options?: GradleSnapshotFollowOptions): Promise<GradleSnapshotFollowResult> {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
return followGradleSnapshot(
|
|
80
|
+
{
|
|
81
|
+
baseURL: this._client.baseURL,
|
|
82
|
+
instanceId: id,
|
|
83
|
+
// Resolved per connection, so federated and workload credentials refresh on reconnect.
|
|
84
|
+
getHeaders: async () => {
|
|
85
|
+
const { req } = await this._client.buildRequest({
|
|
86
|
+
method: 'get',
|
|
87
|
+
path: path`/v1/gradle_instances/${id}/snapshot`,
|
|
88
|
+
});
|
|
89
|
+
return req.headers;
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
options,
|
|
93
|
+
);
|
|
82
94
|
}
|
|
83
95
|
}
|
|
84
96
|
|
package/src/resources/index.ts
CHANGED
|
@@ -124,6 +124,17 @@ export {
|
|
|
124
124
|
type GradleBuildOptions,
|
|
125
125
|
type GradleBuildLog,
|
|
126
126
|
} from './gradle-instances-helpers';
|
|
127
|
+
// Side-effect import, as for Gradle: grafts createClient onto MacosInstances.
|
|
128
|
+
import './macos-instances-helpers';
|
|
129
|
+
export {
|
|
130
|
+
type MacosCreateClientParams,
|
|
131
|
+
type MacosClient,
|
|
132
|
+
type MacosPoint,
|
|
133
|
+
type MacosModifier,
|
|
134
|
+
type MacosSshAccess,
|
|
135
|
+
type MacosSnapshotRestoreOptions,
|
|
136
|
+
type MacosSnapshotProgress,
|
|
137
|
+
} from './macos-instances-helpers';
|
|
127
138
|
|
|
128
139
|
export {
|
|
129
140
|
AccessTokens,
|