@onkernel/sdk 0.149.0 → 0.151.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/CHANGELOG.md +25 -0
- package/client.d.mts +29 -4
- package/client.d.mts.map +1 -1
- package/client.d.ts +29 -4
- package/client.d.ts.map +1 -1
- package/client.js +32 -3
- package/client.js.map +1 -1
- package/client.mjs +32 -3
- package/client.mjs.map +1 -1
- package/lib/credentials/client-auth.d.mts +88 -0
- package/lib/credentials/client-auth.d.mts.map +1 -0
- package/lib/credentials/client-auth.d.ts +88 -0
- package/lib/credentials/client-auth.d.ts.map +1 -0
- package/lib/credentials/client-auth.js +166 -0
- package/lib/credentials/client-auth.js.map +1 -0
- package/lib/credentials/client-auth.mjs +162 -0
- package/lib/credentials/client-auth.mjs.map +1 -0
- package/lib/credentials/credential-chain.d.mts +20 -0
- package/lib/credentials/credential-chain.d.mts.map +1 -0
- package/lib/credentials/credential-chain.d.ts +20 -0
- package/lib/credentials/credential-chain.d.ts.map +1 -0
- package/lib/credentials/credential-chain.js +81 -0
- package/lib/credentials/credential-chain.js.map +1 -0
- package/lib/credentials/credential-chain.mjs +77 -0
- package/lib/credentials/credential-chain.mjs.map +1 -0
- package/lib/credentials/identity-token.d.mts +59 -0
- package/lib/credentials/identity-token.d.mts.map +1 -0
- package/lib/credentials/identity-token.d.ts +59 -0
- package/lib/credentials/identity-token.d.ts.map +1 -0
- package/lib/credentials/identity-token.js +243 -0
- package/lib/credentials/identity-token.js.map +1 -0
- package/lib/credentials/identity-token.mjs +202 -0
- package/lib/credentials/identity-token.mjs.map +1 -0
- package/lib/credentials/oidc-federation.d.mts +65 -0
- package/lib/credentials/oidc-federation.d.mts.map +1 -0
- package/lib/credentials/oidc-federation.d.ts +65 -0
- package/lib/credentials/oidc-federation.d.ts.map +1 -0
- package/lib/credentials/oidc-federation.js +200 -0
- package/lib/credentials/oidc-federation.js.map +1 -0
- package/lib/credentials/oidc-federation.mjs +196 -0
- package/lib/credentials/oidc-federation.mjs.map +1 -0
- package/lib/credentials/token-cache.d.mts +64 -0
- package/lib/credentials/token-cache.d.mts.map +1 -0
- package/lib/credentials/token-cache.d.ts +64 -0
- package/lib/credentials/token-cache.d.ts.map +1 -0
- package/lib/credentials/token-cache.js +147 -0
- package/lib/credentials/token-cache.js.map +1 -0
- package/lib/credentials/token-cache.mjs +143 -0
- package/lib/credentials/token-cache.mjs.map +1 -0
- package/lib/credentials/types.d.mts +126 -0
- package/lib/credentials/types.d.mts.map +1 -0
- package/lib/credentials/types.d.ts +126 -0
- package/lib/credentials/types.d.ts.map +1 -0
- package/lib/credentials/types.js +182 -0
- package/lib/credentials/types.js.map +1 -0
- package/lib/credentials/types.mjs +169 -0
- package/lib/credentials/types.mjs.map +1 -0
- package/lib/credentials.d.mts +6 -0
- package/lib/credentials.d.mts.map +1 -0
- package/lib/credentials.d.ts +6 -0
- package/lib/credentials.d.ts.map +1 -0
- package/lib/credentials.js +19 -0
- package/lib/credentials.js.map +1 -0
- package/lib/credentials.mjs +6 -0
- package/lib/credentials.mjs.map +1 -0
- package/package.json +9 -1
- package/resources/api-keys.d.mts +192 -16
- package/resources/api-keys.d.mts.map +1 -1
- package/resources/api-keys.d.ts +192 -16
- package/resources/api-keys.d.ts.map +1 -1
- package/resources/api-keys.js +19 -7
- package/resources/api-keys.js.map +1 -1
- package/resources/api-keys.mjs +19 -7
- package/resources/api-keys.mjs.map +1 -1
- package/resources/auth/auth.d.mts +2 -2
- package/resources/auth/auth.d.mts.map +1 -1
- package/resources/auth/auth.d.ts +2 -2
- package/resources/auth/auth.d.ts.map +1 -1
- package/resources/auth/auth.js.map +1 -1
- package/resources/auth/auth.mjs.map +1 -1
- package/resources/auth/connections.d.mts +16 -2
- package/resources/auth/connections.d.mts.map +1 -1
- package/resources/auth/connections.d.ts +16 -2
- package/resources/auth/connections.d.ts.map +1 -1
- package/resources/auth/context.d.mts +69 -3
- package/resources/auth/context.d.mts.map +1 -1
- package/resources/auth/context.d.ts +69 -3
- package/resources/auth/context.d.ts.map +1 -1
- package/resources/auth/index.d.mts +1 -1
- package/resources/auth/index.d.mts.map +1 -1
- package/resources/auth/index.d.ts +1 -1
- package/resources/auth/index.d.ts.map +1 -1
- package/resources/auth/index.js.map +1 -1
- package/resources/auth/index.mjs.map +1 -1
- package/resources/browser-pools.d.mts +41 -6
- package/resources/browser-pools.d.mts.map +1 -1
- package/resources/browser-pools.d.ts +41 -6
- package/resources/browser-pools.d.ts.map +1 -1
- package/resources/browsers/browsers.d.mts +105 -45
- package/resources/browsers/browsers.d.mts.map +1 -1
- package/resources/browsers/browsers.d.ts +105 -45
- package/resources/browsers/browsers.d.ts.map +1 -1
- package/resources/browsers/browsers.js.map +1 -1
- package/resources/browsers/browsers.mjs.map +1 -1
- package/resources/browsers/index.d.mts +1 -1
- package/resources/browsers/index.d.mts.map +1 -1
- package/resources/browsers/index.d.ts +1 -1
- package/resources/browsers/index.d.ts.map +1 -1
- package/resources/browsers/index.js.map +1 -1
- package/resources/browsers/index.mjs.map +1 -1
- package/resources/browsers/telemetry.d.mts +4 -4
- package/resources/browsers/telemetry.d.mts.map +1 -1
- package/resources/browsers/telemetry.d.ts +4 -4
- package/resources/browsers/telemetry.d.ts.map +1 -1
- package/resources/index.d.mts +6 -2
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +6 -2
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +9 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +4 -0
- package/resources/index.mjs.map +1 -1
- package/resources/invocations.d.mts +13 -1
- package/resources/invocations.d.mts.map +1 -1
- package/resources/invocations.d.ts +13 -1
- package/resources/invocations.d.ts.map +1 -1
- package/resources/service-accounts.d.mts +73 -0
- package/resources/service-accounts.d.mts.map +1 -0
- package/resources/service-accounts.d.ts +73 -0
- package/resources/service-accounts.d.ts.map +1 -0
- package/resources/service-accounts.js +50 -0
- package/resources/service-accounts.js.map +1 -0
- package/resources/service-accounts.mjs +46 -0
- package/resources/service-accounts.mjs.map +1 -0
- package/resources/vaults/items.d.mts +68 -2
- package/resources/vaults/items.d.mts.map +1 -1
- package/resources/vaults/items.d.ts +68 -2
- package/resources/vaults/items.d.ts.map +1 -1
- package/resources/workload-identity-authentication-events.d.mts +36 -0
- package/resources/workload-identity-authentication-events.d.mts.map +1 -0
- package/resources/workload-identity-authentication-events.d.ts +36 -0
- package/resources/workload-identity-authentication-events.d.ts.map +1 -0
- package/resources/workload-identity-authentication-events.js +16 -0
- package/resources/workload-identity-authentication-events.js.map +1 -0
- package/resources/workload-identity-authentication-events.mjs +12 -0
- package/resources/workload-identity-authentication-events.mjs.map +1 -0
- package/resources/workload-identity-federation-rules.d.mts +194 -0
- package/resources/workload-identity-federation-rules.d.mts.map +1 -0
- package/resources/workload-identity-federation-rules.d.ts +194 -0
- package/resources/workload-identity-federation-rules.d.ts.map +1 -0
- package/resources/workload-identity-federation-rules.js +47 -0
- package/resources/workload-identity-federation-rules.js.map +1 -0
- package/resources/workload-identity-federation-rules.mjs +43 -0
- package/resources/workload-identity-federation-rules.mjs.map +1 -0
- package/resources/workload-identity-issuers.d.mts +169 -0
- package/resources/workload-identity-issuers.d.mts.map +1 -0
- package/resources/workload-identity-issuers.d.ts +169 -0
- package/resources/workload-identity-issuers.d.ts.map +1 -0
- package/resources/workload-identity-issuers.js +54 -0
- package/resources/workload-identity-issuers.js.map +1 -0
- package/resources/workload-identity-issuers.mjs +50 -0
- package/resources/workload-identity-issuers.mjs.map +1 -0
- package/src/client.ts +134 -1
- package/src/lib/credentials/client-auth.ts +194 -0
- package/src/lib/credentials/credential-chain.ts +95 -0
- package/src/lib/credentials/identity-token.ts +284 -0
- package/src/lib/credentials/oidc-federation.ts +304 -0
- package/src/lib/credentials/token-cache.ts +172 -0
- package/src/lib/credentials/types.ts +267 -0
- package/src/lib/credentials.ts +30 -0
- package/src/resources/api-keys.ts +217 -15
- package/src/resources/auth/auth.ts +6 -2
- package/src/resources/auth/connections.ts +17 -2
- package/src/resources/auth/context.ts +81 -3
- package/src/resources/auth/index.ts +1 -1
- package/src/resources/browser-pools.ts +46 -6
- package/src/resources/browsers/browsers.ts +115 -44
- package/src/resources/browsers/index.ts +1 -0
- package/src/resources/browsers/telemetry.ts +4 -3
- package/src/resources/index.ts +42 -0
- package/src/resources/invocations.ts +15 -1
- package/src/resources/service-accounts.ts +128 -0
- package/src/resources/vaults/items.ts +80 -2
- package/src/resources/workload-identity-authentication-events.ts +72 -0
- package/src/resources/workload-identity-federation-rules.ts +314 -0
- package/src/resources/workload-identity-issuers.ts +273 -0
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/src/client.ts
CHANGED
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
APIKey,
|
|
30
30
|
APIKeyCreateParams,
|
|
31
31
|
APIKeyListParams,
|
|
32
|
+
APIKeyPrincipal,
|
|
32
33
|
APIKeyRetrieveParams,
|
|
33
34
|
APIKeyRotateParams,
|
|
34
35
|
APIKeyUpdateParams,
|
|
@@ -150,6 +151,15 @@ import {
|
|
|
150
151
|
Reports,
|
|
151
152
|
ReportsOffsetPagination,
|
|
152
153
|
} from './resources/reports';
|
|
154
|
+
import {
|
|
155
|
+
ServiceAccount,
|
|
156
|
+
ServiceAccountConfig,
|
|
157
|
+
ServiceAccountCreateParams,
|
|
158
|
+
ServiceAccountListParams,
|
|
159
|
+
ServiceAccountUpdateParams,
|
|
160
|
+
ServiceAccounts,
|
|
161
|
+
ServiceAccountsOffsetPagination,
|
|
162
|
+
} from './resources/service-accounts';
|
|
153
163
|
import {
|
|
154
164
|
Task,
|
|
155
165
|
TaskCreateParams,
|
|
@@ -167,6 +177,37 @@ import {
|
|
|
167
177
|
VaultProviderConfigs,
|
|
168
178
|
VaultProviderConfigsOffsetPagination,
|
|
169
179
|
} from './resources/vault-provider-configs';
|
|
180
|
+
import {
|
|
181
|
+
WorkloadIdentityAuthenticationEvent,
|
|
182
|
+
WorkloadIdentityAuthenticationEventListParams,
|
|
183
|
+
WorkloadIdentityAuthenticationEvents,
|
|
184
|
+
WorkloadIdentityAuthenticationEventsOffsetPagination,
|
|
185
|
+
} from './resources/workload-identity-authentication-events';
|
|
186
|
+
import {
|
|
187
|
+
WorkloadIdentityAuthorization,
|
|
188
|
+
WorkloadIdentityFederationRule,
|
|
189
|
+
WorkloadIdentityFederationRuleConfig,
|
|
190
|
+
WorkloadIdentityFederationRuleCreateParams,
|
|
191
|
+
WorkloadIdentityFederationRuleListParams,
|
|
192
|
+
WorkloadIdentityFederationRuleUpdateParams,
|
|
193
|
+
WorkloadIdentityFederationRules,
|
|
194
|
+
WorkloadIdentityFederationRulesOffsetPagination,
|
|
195
|
+
WorkloadIdentityMatch,
|
|
196
|
+
WorkloadIdentityTarget,
|
|
197
|
+
} from './resources/workload-identity-federation-rules';
|
|
198
|
+
import {
|
|
199
|
+
WorkloadIdentityIssuer,
|
|
200
|
+
WorkloadIdentityIssuerConfig,
|
|
201
|
+
WorkloadIdentityIssuerCreateParams,
|
|
202
|
+
WorkloadIdentityIssuerKeyCheck,
|
|
203
|
+
WorkloadIdentityIssuerListParams,
|
|
204
|
+
WorkloadIdentityIssuerUpdateParams,
|
|
205
|
+
WorkloadIdentityIssuers,
|
|
206
|
+
WorkloadIdentityIssuersOffsetPagination,
|
|
207
|
+
WorkloadIdentityKeySource,
|
|
208
|
+
WorkloadIdentityPresetType,
|
|
209
|
+
WorkloadIdentityTokenValidation,
|
|
210
|
+
} from './resources/workload-identity-issuers';
|
|
170
211
|
import {
|
|
171
212
|
AuditLogEntriesPageTokenPagination,
|
|
172
213
|
AuditLogEntry,
|
|
@@ -177,6 +218,7 @@ import {
|
|
|
177
218
|
import { Auth } from './resources/auth/auth';
|
|
178
219
|
import {
|
|
179
220
|
BrowserAllowlistState,
|
|
221
|
+
BrowserCaptchaSolver,
|
|
180
222
|
BrowserCreateParams,
|
|
181
223
|
BrowserCreateResponse,
|
|
182
224
|
BrowserCurlParams,
|
|
@@ -290,6 +332,8 @@ import {
|
|
|
290
332
|
parseLogLevel,
|
|
291
333
|
} from './internal/utils/log';
|
|
292
334
|
import { isEmptyObj } from './internal/utils/values';
|
|
335
|
+
import { ClientAuth } from './lib/credentials/client-auth';
|
|
336
|
+
import type { AccessTokenProvider } from './lib/credentials/types';
|
|
293
337
|
|
|
294
338
|
const environments = {
|
|
295
339
|
production: 'https://api.onkernel.com/',
|
|
@@ -303,6 +347,19 @@ export interface ClientOptions {
|
|
|
303
347
|
*/
|
|
304
348
|
apiKey?: string | undefined;
|
|
305
349
|
|
|
350
|
+
/**
|
|
351
|
+
* Workload identity credentials: a function that returns a short-lived
|
|
352
|
+
* access token, such as `oidcFederationProvider(...)` from
|
|
353
|
+
* `@onkernel/sdk/lib/credentials`. The client caches the token, refreshes it
|
|
354
|
+
* before it expires, and replaces it when a request is rejected with a 401.
|
|
355
|
+
*
|
|
356
|
+
* Mutually exclusive with `apiKey`. When neither is set and `KERNEL_API_KEY`
|
|
357
|
+
* is missing, the client reads `KERNEL_FEDERATION_RULE_ID` and an identity
|
|
358
|
+
* token source from the environment. A client that authenticates this way
|
|
359
|
+
* has an empty `apiKey`.
|
|
360
|
+
*/
|
|
361
|
+
credentials?: AccessTokenProvider | null | undefined;
|
|
362
|
+
|
|
306
363
|
projectID?: string | null | undefined;
|
|
307
364
|
|
|
308
365
|
project?: string | null | undefined;
|
|
@@ -406,6 +463,7 @@ export class Kernel {
|
|
|
406
463
|
protected idempotencyHeader?: string;
|
|
407
464
|
private _options: ClientOptions;
|
|
408
465
|
public browserRouteCache: BrowserRouteCache;
|
|
466
|
+
#auth: ClientAuth;
|
|
409
467
|
|
|
410
468
|
/**
|
|
411
469
|
* API Client for interfacing with the Kernel API.
|
|
@@ -441,9 +499,11 @@ export class Kernel {
|
|
|
441
499
|
);
|
|
442
500
|
}
|
|
443
501
|
|
|
502
|
+
const auth = ClientAuth.resolve(arguments[0], apiKey);
|
|
503
|
+
if (auth.usesCredentials) apiKey = '';
|
|
444
504
|
if (apiKey === undefined) {
|
|
445
505
|
throw new Errors.KernelError(
|
|
446
|
-
"The KERNEL_API_KEY environment variable is missing or empty; either provide it, or instantiate the Kernel client with an apiKey option, like new Kernel({ apiKey: 'My API Key' }).",
|
|
506
|
+
"The KERNEL_API_KEY environment variable is missing or empty; either provide it, or instantiate the Kernel client with an apiKey option, like new Kernel({ apiKey: 'My API Key' }). To use workload identity federation instead, set KERNEL_FEDERATION_RULE_ID and an identity token source, or pass the credentials option.",
|
|
447
507
|
);
|
|
448
508
|
}
|
|
449
509
|
|
|
@@ -481,6 +541,7 @@ export class Kernel {
|
|
|
481
541
|
subresources: browserRoutingSubresourcesFromEnv(),
|
|
482
542
|
cache: this.browserRouteCache,
|
|
483
543
|
});
|
|
544
|
+
this.#auth = auth.bind(this, this.rawFetch);
|
|
484
545
|
this.#encoder = Opts.FallbackEncoder;
|
|
485
546
|
|
|
486
547
|
const customHeadersEnv = readEnv('KERNEL_CUSTOM_HEADERS');
|
|
@@ -521,6 +582,7 @@ export class Kernel {
|
|
|
521
582
|
projectID: this.projectID,
|
|
522
583
|
project: this.project,
|
|
523
584
|
...options,
|
|
585
|
+
...this.#auth.cloneOptions(options),
|
|
524
586
|
});
|
|
525
587
|
client.browserRouteCache = this.browserRouteCache;
|
|
526
588
|
client.fetch = createRoutingFetch(client.rawFetch, {
|
|
@@ -547,6 +609,9 @@ export class Kernel {
|
|
|
547
609
|
}
|
|
548
610
|
|
|
549
611
|
protected async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
|
|
612
|
+
this.#auth = this.#auth.bind(this, this.rawFetch);
|
|
613
|
+
const bearerAuth = await this.#auth.bearerAuth();
|
|
614
|
+
if (bearerAuth) return buildHeaders([bearerAuth]);
|
|
550
615
|
return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
|
|
551
616
|
}
|
|
552
617
|
|
|
@@ -743,6 +808,18 @@ export class Kernel {
|
|
|
743
808
|
} with status ${response.status} in ${headersTime - startTime}ms`;
|
|
744
809
|
|
|
745
810
|
if (!response.ok) {
|
|
811
|
+
if (
|
|
812
|
+
response.status === 401 &&
|
|
813
|
+
this.#auth.refreshAfter401(
|
|
814
|
+
options,
|
|
815
|
+
{ requestID: retryOfRequestLogID ?? requestLogID, url, headers: req.headers },
|
|
816
|
+
response,
|
|
817
|
+
)
|
|
818
|
+
) {
|
|
819
|
+
await Shims.CancelReadableStream(response.body);
|
|
820
|
+
loggerFor(this).info(`${responseInfo} - retrying with a fresh access token`);
|
|
821
|
+
return this.makeRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID);
|
|
822
|
+
}
|
|
746
823
|
const shouldRetry = await this.shouldRetry(response);
|
|
747
824
|
if (retriesRemaining && shouldRetry) {
|
|
748
825
|
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
|
|
@@ -1168,6 +1245,12 @@ export class Kernel {
|
|
|
1168
1245
|
* Create and manage API keys for organization and project-scoped access.
|
|
1169
1246
|
*/
|
|
1170
1247
|
apiKeys: API.APIKeys = new API.APIKeys(this);
|
|
1248
|
+
serviceAccounts: API.ServiceAccounts = new API.ServiceAccounts(this);
|
|
1249
|
+
workloadIdentityIssuers: API.WorkloadIdentityIssuers = new API.WorkloadIdentityIssuers(this);
|
|
1250
|
+
workloadIdentityFederationRules: API.WorkloadIdentityFederationRules =
|
|
1251
|
+
new API.WorkloadIdentityFederationRules(this);
|
|
1252
|
+
workloadIdentityAuthenticationEvents: API.WorkloadIdentityAuthenticationEvents =
|
|
1253
|
+
new API.WorkloadIdentityAuthenticationEvents(this);
|
|
1171
1254
|
/**
|
|
1172
1255
|
* Configure external credential providers like 1Password.
|
|
1173
1256
|
*/
|
|
@@ -1200,6 +1283,10 @@ Kernel.Projects = Projects;
|
|
|
1200
1283
|
Kernel.Organization = Organization;
|
|
1201
1284
|
Kernel.AuditLogs = AuditLogs;
|
|
1202
1285
|
Kernel.APIKeys = APIKeys;
|
|
1286
|
+
Kernel.ServiceAccounts = ServiceAccounts;
|
|
1287
|
+
Kernel.WorkloadIdentityIssuers = WorkloadIdentityIssuers;
|
|
1288
|
+
Kernel.WorkloadIdentityFederationRules = WorkloadIdentityFederationRules;
|
|
1289
|
+
Kernel.WorkloadIdentityAuthenticationEvents = WorkloadIdentityAuthenticationEvents;
|
|
1203
1290
|
Kernel.CredentialProviders = CredentialProviders;
|
|
1204
1291
|
Kernel.SearchResource = SearchResource;
|
|
1205
1292
|
|
|
@@ -1279,6 +1366,7 @@ export declare namespace Kernel {
|
|
|
1279
1366
|
export {
|
|
1280
1367
|
Browsers as Browsers,
|
|
1281
1368
|
type BrowserAllowlistState as BrowserAllowlistState,
|
|
1369
|
+
type BrowserCaptchaSolver as BrowserCaptchaSolver,
|
|
1282
1370
|
type BrowserFingerprint as BrowserFingerprint,
|
|
1283
1371
|
type BrowserLocation as BrowserLocation,
|
|
1284
1372
|
type BrowserLocationRequest as BrowserLocationRequest,
|
|
@@ -1458,6 +1546,7 @@ export declare namespace Kernel {
|
|
|
1458
1546
|
export {
|
|
1459
1547
|
APIKeys as APIKeys,
|
|
1460
1548
|
type APIKey as APIKey,
|
|
1549
|
+
type APIKeyPrincipal as APIKeyPrincipal,
|
|
1461
1550
|
type CreatedAPIKey as CreatedAPIKey,
|
|
1462
1551
|
type APIKeysOffsetPagination as APIKeysOffsetPagination,
|
|
1463
1552
|
type APIKeyCreateParams as APIKeyCreateParams,
|
|
@@ -1467,6 +1556,50 @@ export declare namespace Kernel {
|
|
|
1467
1556
|
type APIKeyRotateParams as APIKeyRotateParams,
|
|
1468
1557
|
};
|
|
1469
1558
|
|
|
1559
|
+
export {
|
|
1560
|
+
ServiceAccounts as ServiceAccounts,
|
|
1561
|
+
type ServiceAccount as ServiceAccount,
|
|
1562
|
+
type ServiceAccountConfig as ServiceAccountConfig,
|
|
1563
|
+
type ServiceAccountsOffsetPagination as ServiceAccountsOffsetPagination,
|
|
1564
|
+
type ServiceAccountCreateParams as ServiceAccountCreateParams,
|
|
1565
|
+
type ServiceAccountUpdateParams as ServiceAccountUpdateParams,
|
|
1566
|
+
type ServiceAccountListParams as ServiceAccountListParams,
|
|
1567
|
+
};
|
|
1568
|
+
|
|
1569
|
+
export {
|
|
1570
|
+
WorkloadIdentityIssuers as WorkloadIdentityIssuers,
|
|
1571
|
+
type WorkloadIdentityIssuer as WorkloadIdentityIssuer,
|
|
1572
|
+
type WorkloadIdentityIssuerConfig as WorkloadIdentityIssuerConfig,
|
|
1573
|
+
type WorkloadIdentityIssuerKeyCheck as WorkloadIdentityIssuerKeyCheck,
|
|
1574
|
+
type WorkloadIdentityKeySource as WorkloadIdentityKeySource,
|
|
1575
|
+
type WorkloadIdentityPresetType as WorkloadIdentityPresetType,
|
|
1576
|
+
type WorkloadIdentityTokenValidation as WorkloadIdentityTokenValidation,
|
|
1577
|
+
type WorkloadIdentityIssuersOffsetPagination as WorkloadIdentityIssuersOffsetPagination,
|
|
1578
|
+
type WorkloadIdentityIssuerCreateParams as WorkloadIdentityIssuerCreateParams,
|
|
1579
|
+
type WorkloadIdentityIssuerUpdateParams as WorkloadIdentityIssuerUpdateParams,
|
|
1580
|
+
type WorkloadIdentityIssuerListParams as WorkloadIdentityIssuerListParams,
|
|
1581
|
+
};
|
|
1582
|
+
|
|
1583
|
+
export {
|
|
1584
|
+
WorkloadIdentityFederationRules as WorkloadIdentityFederationRules,
|
|
1585
|
+
type WorkloadIdentityAuthorization as WorkloadIdentityAuthorization,
|
|
1586
|
+
type WorkloadIdentityFederationRule as WorkloadIdentityFederationRule,
|
|
1587
|
+
type WorkloadIdentityFederationRuleConfig as WorkloadIdentityFederationRuleConfig,
|
|
1588
|
+
type WorkloadIdentityMatch as WorkloadIdentityMatch,
|
|
1589
|
+
type WorkloadIdentityTarget as WorkloadIdentityTarget,
|
|
1590
|
+
type WorkloadIdentityFederationRulesOffsetPagination as WorkloadIdentityFederationRulesOffsetPagination,
|
|
1591
|
+
type WorkloadIdentityFederationRuleCreateParams as WorkloadIdentityFederationRuleCreateParams,
|
|
1592
|
+
type WorkloadIdentityFederationRuleUpdateParams as WorkloadIdentityFederationRuleUpdateParams,
|
|
1593
|
+
type WorkloadIdentityFederationRuleListParams as WorkloadIdentityFederationRuleListParams,
|
|
1594
|
+
};
|
|
1595
|
+
|
|
1596
|
+
export {
|
|
1597
|
+
WorkloadIdentityAuthenticationEvents as WorkloadIdentityAuthenticationEvents,
|
|
1598
|
+
type WorkloadIdentityAuthenticationEvent as WorkloadIdentityAuthenticationEvent,
|
|
1599
|
+
type WorkloadIdentityAuthenticationEventsOffsetPagination as WorkloadIdentityAuthenticationEventsOffsetPagination,
|
|
1600
|
+
type WorkloadIdentityAuthenticationEventListParams as WorkloadIdentityAuthenticationEventListParams,
|
|
1601
|
+
};
|
|
1602
|
+
|
|
1470
1603
|
export {
|
|
1471
1604
|
CredentialProviders as CredentialProviders,
|
|
1472
1605
|
type CreateCredentialProviderRequest as CreateCredentialProviderRequest,
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import type { Kernel } from '../../client';
|
|
2
|
+
import { KernelError } from '../../core/error';
|
|
3
|
+
import type { Fetch, RequestInit } from '../../internal/builtin-types';
|
|
4
|
+
import { readEnv } from '../../internal/utils/env';
|
|
5
|
+
import { loggerFor } from '../../internal/utils/log';
|
|
6
|
+
import { federationCredentialsFromEnv } from './credential-chain';
|
|
7
|
+
import { TokenCache } from './token-cache';
|
|
8
|
+
import type { AccessTokenProvider } from './types';
|
|
9
|
+
import { WorkloadIdentityError, requireSecureTokenEndpoint, resolveAuthBaseURL } from './types';
|
|
10
|
+
|
|
11
|
+
type AuthOptions = {
|
|
12
|
+
apiKey?: string | undefined;
|
|
13
|
+
credentials?: AccessTokenProvider | null | undefined;
|
|
14
|
+
/** Set by `withOptions` to hand the parent's auth to the copy. */
|
|
15
|
+
__auth?: ClientAuth | undefined;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// A 401 can only arrive for a token that was sent within the last request
|
|
19
|
+
// timeout, so the few most recent tokens are enough to recognize one.
|
|
20
|
+
const MAX_RECENT_TOKENS = 4;
|
|
21
|
+
|
|
22
|
+
let warnedAPIKeyWins = false;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* How one client authenticates: with its API key, or with an access token
|
|
26
|
+
* from `credentials`. The client calls this from its constructor,
|
|
27
|
+
* `withOptions`, `authHeaders` and its 401 handling.
|
|
28
|
+
*/
|
|
29
|
+
export class ClientAuth {
|
|
30
|
+
private tokenCache: TokenCache | null = null;
|
|
31
|
+
/** What the token cache was built for: tokens are requested for this API, through this fetch. */
|
|
32
|
+
private target: { baseURL: string; fetch: Fetch; fetchOptions: unknown } | null = null;
|
|
33
|
+
/** The access tokens most recently handed to requests, oldest first. */
|
|
34
|
+
private recentTokens: string[] = [];
|
|
35
|
+
/**
|
|
36
|
+
* For each request options object, the requests sent with it that have
|
|
37
|
+
* already had a 401 answered with a fresh token. An options object the
|
|
38
|
+
* caller sends more than once has one entry per request.
|
|
39
|
+
*/
|
|
40
|
+
private refreshed = new WeakMap<object, Set<string>>();
|
|
41
|
+
|
|
42
|
+
private constructor(
|
|
43
|
+
private provider: AccessTokenProvider | null,
|
|
44
|
+
private fromEnv = false,
|
|
45
|
+
private warnAPIKeyWins = false,
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Picks the credential for a new client. In order:
|
|
50
|
+
*
|
|
51
|
+
* 1. the `credentials` or `apiKey` option;
|
|
52
|
+
* 2. `KERNEL_API_KEY`;
|
|
53
|
+
* 3. workload identity federation configured in the environment.
|
|
54
|
+
*
|
|
55
|
+
* @param options The options the caller passed to the constructor.
|
|
56
|
+
* @param apiKey The `apiKey` option, or else `KERNEL_API_KEY`.
|
|
57
|
+
*/
|
|
58
|
+
static resolve(options: AuthOptions = {}, apiKey: string | undefined): ClientAuth {
|
|
59
|
+
if (options.__auth) return options.__auth;
|
|
60
|
+
|
|
61
|
+
if (options.credentials != null) {
|
|
62
|
+
if (options.apiKey) {
|
|
63
|
+
throw new KernelError(
|
|
64
|
+
'The `apiKey` and `credentials` options are mutually exclusive; pass only one of them.',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return new ClientAuth(options.credentials);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (apiKey !== undefined) {
|
|
71
|
+
const keyIsFromEnv = options.apiKey === undefined;
|
|
72
|
+
return new ClientAuth(null, false, keyIsFromEnv && !!readEnv('KERNEL_FEDERATION_RULE_ID'));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return new ClientAuth(federationCredentialsFromEnv(), true);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Whether requests carry an access token from `credentials` instead of the API key. */
|
|
79
|
+
get usesCredentials(): boolean {
|
|
80
|
+
return this.provider != null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Connects the credentials to the client that will use them, so token
|
|
85
|
+
* requests go through the client's `fetch` and `fetchOptions` and are for
|
|
86
|
+
* its base URL. The client calls this when it is constructed and again
|
|
87
|
+
* before each request.
|
|
88
|
+
*
|
|
89
|
+
* Returns this object while the client still targets the API the token
|
|
90
|
+
* cache was built for, through the same fetch. That is how a copy made by
|
|
91
|
+
* `withOptions`, which starts with its parent's auth, shares the parent's
|
|
92
|
+
* token cache. Otherwise returns a new object with its own cache over the
|
|
93
|
+
* same provider, so a token is never sent to an API it was not requested
|
|
94
|
+
* for; this covers a copy with another base URL and a client whose
|
|
95
|
+
* `baseURL` is assigned after construction.
|
|
96
|
+
*/
|
|
97
|
+
bind(client: Kernel, clientFetch: Fetch): ClientAuth {
|
|
98
|
+
if (this.warnAPIKeyWins && !warnedAPIKeyWins) {
|
|
99
|
+
warnedAPIKeyWins = true;
|
|
100
|
+
loggerFor(client).warn(
|
|
101
|
+
'Both KERNEL_API_KEY and KERNEL_FEDERATION_RULE_ID are set; using the API key. Unset KERNEL_API_KEY to use workload identity federation.',
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
const provider = this.provider;
|
|
105
|
+
if (!provider) return this;
|
|
106
|
+
|
|
107
|
+
const target = { baseURL: client.baseURL, fetch: clientFetch, fetchOptions: client.fetchOptions };
|
|
108
|
+
if (this.target) {
|
|
109
|
+
const sameTarget =
|
|
110
|
+
this.target.baseURL === target.baseURL &&
|
|
111
|
+
this.target.fetch === target.fetch &&
|
|
112
|
+
this.target.fetchOptions === target.fetchOptions;
|
|
113
|
+
return sameTarget ? this : new ClientAuth(provider, this.fromEnv).bind(client, clientFetch);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this.fromEnv) {
|
|
117
|
+
requireSecureTokenEndpoint(resolveAuthBaseURL(undefined, target.baseURL));
|
|
118
|
+
}
|
|
119
|
+
this.target = target;
|
|
120
|
+
const fetch: Fetch = (input, init) =>
|
|
121
|
+
clientFetch.call(undefined, input, { ...(target.fetchOptions as RequestInit | undefined), ...init });
|
|
122
|
+
this.tokenCache = new TokenCache(
|
|
123
|
+
(opts) => provider({ ...opts, client: { fetch, baseURL: target.baseURL } }),
|
|
124
|
+
// The error is not logged: this client cannot vouch for what a
|
|
125
|
+
// provider or a custom fetch puts in one. It is thrown to the caller
|
|
126
|
+
// if the refresh still fails once the token is about to expire.
|
|
127
|
+
(err) =>
|
|
128
|
+
loggerFor(client).debug(
|
|
129
|
+
`advisory credential refresh failed${
|
|
130
|
+
err instanceof WorkloadIdentityError && err.statusCode != null ?
|
|
131
|
+
` with status ${err.statusCode}`
|
|
132
|
+
: ''
|
|
133
|
+
}; serving the cached token`,
|
|
134
|
+
),
|
|
135
|
+
);
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The `Authorization` header for one attempt of a request, or `undefined` in API key mode. */
|
|
140
|
+
async bearerAuth(): Promise<{ Authorization: string } | undefined> {
|
|
141
|
+
if (!this.tokenCache) return undefined;
|
|
142
|
+
const token = await this.tokenCache.getToken();
|
|
143
|
+
if (!this.recentTokens.includes(token)) {
|
|
144
|
+
this.recentTokens.push(token);
|
|
145
|
+
if (this.recentTokens.length > MAX_RECENT_TOKENS) this.recentTokens.shift();
|
|
146
|
+
}
|
|
147
|
+
return { Authorization: `Bearer ${token}` };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Handles a 401 for an attempt of `request`. Returns true when the attempt
|
|
152
|
+
* should be sent again: the rejected token has been dropped, so the retry
|
|
153
|
+
* exchanges a fresh identity token.
|
|
154
|
+
*
|
|
155
|
+
* Returns false when the 401 should surface. That is when this request
|
|
156
|
+
* (`sent.requestID` names it across all of its attempts) has already been
|
|
157
|
+
* retried for a 401, or when the attempt did not carry an access token
|
|
158
|
+
* from `credentials` to the API: it used the API key or the caller's own
|
|
159
|
+
* `Authorization` header, or was answered by a browser VM it was routed
|
|
160
|
+
* to directly.
|
|
161
|
+
*/
|
|
162
|
+
refreshAfter401(
|
|
163
|
+
request: object,
|
|
164
|
+
sent: { requestID: string; url: string; headers: Headers },
|
|
165
|
+
response: Response,
|
|
166
|
+
): boolean {
|
|
167
|
+
const token = sent.headers.get('authorization')?.replace(/^Bearer /, '');
|
|
168
|
+
if (!this.tokenCache || !token || !this.recentTokens.includes(token)) return false;
|
|
169
|
+
if (response.url && new URL(response.url).origin !== new URL(sent.url).origin) return false;
|
|
170
|
+
|
|
171
|
+
let refreshed = this.refreshed.get(request);
|
|
172
|
+
if (!refreshed) this.refreshed.set(request, (refreshed = new Set()));
|
|
173
|
+
if (refreshed.has(sent.requestID)) return false;
|
|
174
|
+
refreshed.add(sent.requestID);
|
|
175
|
+
// Does nothing when another request has already replaced this token.
|
|
176
|
+
this.tokenCache.invalidate(token);
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The auth options for a copy made by `withOptions(options)`. A copy that
|
|
182
|
+
* sets neither `apiKey` nor `credentials` inherits this client's auth; see
|
|
183
|
+
* {@link bind} for when it also shares the token cache.
|
|
184
|
+
*/
|
|
185
|
+
cloneOptions(options: AuthOptions): AuthOptions {
|
|
186
|
+
if (options.apiKey != null && options.credentials != null) {
|
|
187
|
+
// The constructor rejects the pair.
|
|
188
|
+
return { __auth: undefined };
|
|
189
|
+
}
|
|
190
|
+
if (options.apiKey != null) return { credentials: null, __auth: undefined };
|
|
191
|
+
if (options.credentials != null) return { apiKey: undefined, __auth: undefined };
|
|
192
|
+
return { __auth: this };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { readEnv } from '../../internal/utils/env';
|
|
2
|
+
import {
|
|
3
|
+
identityTokenFromAWSSTS,
|
|
4
|
+
identityTokenFromFile,
|
|
5
|
+
identityTokenFromGCPMetadata,
|
|
6
|
+
identityTokenFromGitHubActions,
|
|
7
|
+
identityTokenFromValue,
|
|
8
|
+
} from './identity-token';
|
|
9
|
+
import { oidcFederationProvider } from './oidc-federation';
|
|
10
|
+
import type { AccessTokenProvider, IdentityTokenProvider } from './types';
|
|
11
|
+
import { WorkloadIdentityError } from './types';
|
|
12
|
+
|
|
13
|
+
export const IDENTITY_TOKEN_SOURCES = ['github_actions', 'gcp_metadata', 'aws_sts', 'file', 'env'] as const;
|
|
14
|
+
export type IdentityTokenSource = (typeof IDENTITY_TOKEN_SOURCES)[number];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Builds workload identity credentials from the environment, or returns
|
|
18
|
+
* `null` when the environment does not configure them.
|
|
19
|
+
*
|
|
20
|
+
* `KERNEL_FEDERATION_RULE_ID` names the federation rule. The identity token
|
|
21
|
+
* comes from the source `KERNEL_IDENTITY_TOKEN_SOURCE` selects. When that is
|
|
22
|
+
* unset, the first of these that applies is used:
|
|
23
|
+
*
|
|
24
|
+
* 1. `KERNEL_IDENTITY_TOKEN_FILE`
|
|
25
|
+
* 2. `KERNEL_IDENTITY_TOKEN` (good for one exchange only)
|
|
26
|
+
* 3. GitHub Actions, when `ACTIONS_ID_TOKEN_REQUEST_URL` is present
|
|
27
|
+
*
|
|
28
|
+
* A source that is selected but not usable throws, rather than leaving the
|
|
29
|
+
* caller to find out on the first request.
|
|
30
|
+
*/
|
|
31
|
+
export function federationCredentialsFromEnv(): AccessTokenProvider | null {
|
|
32
|
+
const federationRuleId = readEnv('KERNEL_FEDERATION_RULE_ID');
|
|
33
|
+
if (!federationRuleId) return null;
|
|
34
|
+
|
|
35
|
+
const identityTokenProvider = identityTokenProviderFromEnv();
|
|
36
|
+
if (!identityTokenProvider) return null;
|
|
37
|
+
|
|
38
|
+
return oidcFederationProvider({ identityTokenProvider, federationRuleId });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// One wrapper per value for the whole process, so a second client built from
|
|
42
|
+
// the same environment cannot send a token the first one already exchanged.
|
|
43
|
+
const staticTokenProviders = new Map<string, IdentityTokenProvider>();
|
|
44
|
+
|
|
45
|
+
function staticIdentityToken(token: string): IdentityTokenProvider {
|
|
46
|
+
let provider = staticTokenProviders.get(token);
|
|
47
|
+
if (!provider) {
|
|
48
|
+
provider = identityTokenFromValue(token);
|
|
49
|
+
staticTokenProviders.set(token, provider);
|
|
50
|
+
}
|
|
51
|
+
return provider;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function identityTokenProviderFromEnv(): IdentityTokenProvider | null {
|
|
55
|
+
const source = readEnv('KERNEL_IDENTITY_TOKEN_SOURCE');
|
|
56
|
+
const tokenFile = readEnv('KERNEL_IDENTITY_TOKEN_FILE');
|
|
57
|
+
const tokenValue = readEnv('KERNEL_IDENTITY_TOKEN');
|
|
58
|
+
|
|
59
|
+
switch (source) {
|
|
60
|
+
case undefined:
|
|
61
|
+
if (tokenFile) return identityTokenFromFile(tokenFile);
|
|
62
|
+
if (tokenValue) return staticIdentityToken(tokenValue);
|
|
63
|
+
if (readEnv('ACTIONS_ID_TOKEN_REQUEST_URL')) return identityTokenFromGitHubActions();
|
|
64
|
+
return null;
|
|
65
|
+
case 'github_actions':
|
|
66
|
+
if (!readEnv('ACTIONS_ID_TOKEN_REQUEST_URL') || !readEnv('ACTIONS_ID_TOKEN_REQUEST_TOKEN')) {
|
|
67
|
+
throw new WorkloadIdentityError(
|
|
68
|
+
'KERNEL_IDENTITY_TOKEN_SOURCE is github_actions, but ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN are not set. Add `permissions: id-token: write` to the workflow or job.',
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return identityTokenFromGitHubActions();
|
|
72
|
+
case 'gcp_metadata':
|
|
73
|
+
return identityTokenFromGCPMetadata();
|
|
74
|
+
case 'aws_sts':
|
|
75
|
+
return identityTokenFromAWSSTS();
|
|
76
|
+
case 'file':
|
|
77
|
+
if (!tokenFile) {
|
|
78
|
+
throw new WorkloadIdentityError(
|
|
79
|
+
'KERNEL_IDENTITY_TOKEN_SOURCE is file, but KERNEL_IDENTITY_TOKEN_FILE is not set.',
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
return identityTokenFromFile(tokenFile);
|
|
83
|
+
case 'env':
|
|
84
|
+
if (!tokenValue) {
|
|
85
|
+
throw new WorkloadIdentityError(
|
|
86
|
+
'KERNEL_IDENTITY_TOKEN_SOURCE is env, but KERNEL_IDENTITY_TOKEN is not set.',
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return staticIdentityToken(tokenValue);
|
|
90
|
+
default:
|
|
91
|
+
throw new WorkloadIdentityError(
|
|
92
|
+
`KERNEL_IDENTITY_TOKEN_SOURCE is "${source}"; expected one of ${IDENTITY_TOKEN_SOURCES.join(', ')}.`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|