@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
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClientAuth = void 0;
|
|
4
|
+
const error_1 = require("../../core/error.js");
|
|
5
|
+
const env_1 = require("../../internal/utils/env.js");
|
|
6
|
+
const log_1 = require("../../internal/utils/log.js");
|
|
7
|
+
const credential_chain_1 = require("./credential-chain.js");
|
|
8
|
+
const token_cache_1 = require("./token-cache.js");
|
|
9
|
+
const types_1 = require("./types.js");
|
|
10
|
+
// A 401 can only arrive for a token that was sent within the last request
|
|
11
|
+
// timeout, so the few most recent tokens are enough to recognize one.
|
|
12
|
+
const MAX_RECENT_TOKENS = 4;
|
|
13
|
+
let warnedAPIKeyWins = false;
|
|
14
|
+
/**
|
|
15
|
+
* How one client authenticates: with its API key, or with an access token
|
|
16
|
+
* from `credentials`. The client calls this from its constructor,
|
|
17
|
+
* `withOptions`, `authHeaders` and its 401 handling.
|
|
18
|
+
*/
|
|
19
|
+
class ClientAuth {
|
|
20
|
+
constructor(provider, fromEnv = false, warnAPIKeyWins = false) {
|
|
21
|
+
this.provider = provider;
|
|
22
|
+
this.fromEnv = fromEnv;
|
|
23
|
+
this.warnAPIKeyWins = warnAPIKeyWins;
|
|
24
|
+
this.tokenCache = null;
|
|
25
|
+
/** What the token cache was built for: tokens are requested for this API, through this fetch. */
|
|
26
|
+
this.target = null;
|
|
27
|
+
/** The access tokens most recently handed to requests, oldest first. */
|
|
28
|
+
this.recentTokens = [];
|
|
29
|
+
/**
|
|
30
|
+
* For each request options object, the requests sent with it that have
|
|
31
|
+
* already had a 401 answered with a fresh token. An options object the
|
|
32
|
+
* caller sends more than once has one entry per request.
|
|
33
|
+
*/
|
|
34
|
+
this.refreshed = new WeakMap();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Picks the credential for a new client. In order:
|
|
38
|
+
*
|
|
39
|
+
* 1. the `credentials` or `apiKey` option;
|
|
40
|
+
* 2. `KERNEL_API_KEY`;
|
|
41
|
+
* 3. workload identity federation configured in the environment.
|
|
42
|
+
*
|
|
43
|
+
* @param options The options the caller passed to the constructor.
|
|
44
|
+
* @param apiKey The `apiKey` option, or else `KERNEL_API_KEY`.
|
|
45
|
+
*/
|
|
46
|
+
static resolve(options = {}, apiKey) {
|
|
47
|
+
if (options.__auth)
|
|
48
|
+
return options.__auth;
|
|
49
|
+
if (options.credentials != null) {
|
|
50
|
+
if (options.apiKey) {
|
|
51
|
+
throw new error_1.KernelError('The `apiKey` and `credentials` options are mutually exclusive; pass only one of them.');
|
|
52
|
+
}
|
|
53
|
+
return new ClientAuth(options.credentials);
|
|
54
|
+
}
|
|
55
|
+
if (apiKey !== undefined) {
|
|
56
|
+
const keyIsFromEnv = options.apiKey === undefined;
|
|
57
|
+
return new ClientAuth(null, false, keyIsFromEnv && !!(0, env_1.readEnv)('KERNEL_FEDERATION_RULE_ID'));
|
|
58
|
+
}
|
|
59
|
+
return new ClientAuth((0, credential_chain_1.federationCredentialsFromEnv)(), true);
|
|
60
|
+
}
|
|
61
|
+
/** Whether requests carry an access token from `credentials` instead of the API key. */
|
|
62
|
+
get usesCredentials() {
|
|
63
|
+
return this.provider != null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Connects the credentials to the client that will use them, so token
|
|
67
|
+
* requests go through the client's `fetch` and `fetchOptions` and are for
|
|
68
|
+
* its base URL. The client calls this when it is constructed and again
|
|
69
|
+
* before each request.
|
|
70
|
+
*
|
|
71
|
+
* Returns this object while the client still targets the API the token
|
|
72
|
+
* cache was built for, through the same fetch. That is how a copy made by
|
|
73
|
+
* `withOptions`, which starts with its parent's auth, shares the parent's
|
|
74
|
+
* token cache. Otherwise returns a new object with its own cache over the
|
|
75
|
+
* same provider, so a token is never sent to an API it was not requested
|
|
76
|
+
* for; this covers a copy with another base URL and a client whose
|
|
77
|
+
* `baseURL` is assigned after construction.
|
|
78
|
+
*/
|
|
79
|
+
bind(client, clientFetch) {
|
|
80
|
+
if (this.warnAPIKeyWins && !warnedAPIKeyWins) {
|
|
81
|
+
warnedAPIKeyWins = true;
|
|
82
|
+
(0, log_1.loggerFor)(client).warn('Both KERNEL_API_KEY and KERNEL_FEDERATION_RULE_ID are set; using the API key. Unset KERNEL_API_KEY to use workload identity federation.');
|
|
83
|
+
}
|
|
84
|
+
const provider = this.provider;
|
|
85
|
+
if (!provider)
|
|
86
|
+
return this;
|
|
87
|
+
const target = { baseURL: client.baseURL, fetch: clientFetch, fetchOptions: client.fetchOptions };
|
|
88
|
+
if (this.target) {
|
|
89
|
+
const sameTarget = this.target.baseURL === target.baseURL &&
|
|
90
|
+
this.target.fetch === target.fetch &&
|
|
91
|
+
this.target.fetchOptions === target.fetchOptions;
|
|
92
|
+
return sameTarget ? this : new ClientAuth(provider, this.fromEnv).bind(client, clientFetch);
|
|
93
|
+
}
|
|
94
|
+
if (this.fromEnv) {
|
|
95
|
+
(0, types_1.requireSecureTokenEndpoint)((0, types_1.resolveAuthBaseURL)(undefined, target.baseURL));
|
|
96
|
+
}
|
|
97
|
+
this.target = target;
|
|
98
|
+
const fetch = (input, init) => clientFetch.call(undefined, input, { ...target.fetchOptions, ...init });
|
|
99
|
+
this.tokenCache = new token_cache_1.TokenCache((opts) => provider({ ...opts, client: { fetch, baseURL: target.baseURL } }),
|
|
100
|
+
// The error is not logged: this client cannot vouch for what a
|
|
101
|
+
// provider or a custom fetch puts in one. It is thrown to the caller
|
|
102
|
+
// if the refresh still fails once the token is about to expire.
|
|
103
|
+
(err) => (0, log_1.loggerFor)(client).debug(`advisory credential refresh failed${err instanceof types_1.WorkloadIdentityError && err.statusCode != null ?
|
|
104
|
+
` with status ${err.statusCode}`
|
|
105
|
+
: ''}; serving the cached token`));
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
/** The `Authorization` header for one attempt of a request, or `undefined` in API key mode. */
|
|
109
|
+
async bearerAuth() {
|
|
110
|
+
if (!this.tokenCache)
|
|
111
|
+
return undefined;
|
|
112
|
+
const token = await this.tokenCache.getToken();
|
|
113
|
+
if (!this.recentTokens.includes(token)) {
|
|
114
|
+
this.recentTokens.push(token);
|
|
115
|
+
if (this.recentTokens.length > MAX_RECENT_TOKENS)
|
|
116
|
+
this.recentTokens.shift();
|
|
117
|
+
}
|
|
118
|
+
return { Authorization: `Bearer ${token}` };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Handles a 401 for an attempt of `request`. Returns true when the attempt
|
|
122
|
+
* should be sent again: the rejected token has been dropped, so the retry
|
|
123
|
+
* exchanges a fresh identity token.
|
|
124
|
+
*
|
|
125
|
+
* Returns false when the 401 should surface. That is when this request
|
|
126
|
+
* (`sent.requestID` names it across all of its attempts) has already been
|
|
127
|
+
* retried for a 401, or when the attempt did not carry an access token
|
|
128
|
+
* from `credentials` to the API: it used the API key or the caller's own
|
|
129
|
+
* `Authorization` header, or was answered by a browser VM it was routed
|
|
130
|
+
* to directly.
|
|
131
|
+
*/
|
|
132
|
+
refreshAfter401(request, sent, response) {
|
|
133
|
+
const token = sent.headers.get('authorization')?.replace(/^Bearer /, '');
|
|
134
|
+
if (!this.tokenCache || !token || !this.recentTokens.includes(token))
|
|
135
|
+
return false;
|
|
136
|
+
if (response.url && new URL(response.url).origin !== new URL(sent.url).origin)
|
|
137
|
+
return false;
|
|
138
|
+
let refreshed = this.refreshed.get(request);
|
|
139
|
+
if (!refreshed)
|
|
140
|
+
this.refreshed.set(request, (refreshed = new Set()));
|
|
141
|
+
if (refreshed.has(sent.requestID))
|
|
142
|
+
return false;
|
|
143
|
+
refreshed.add(sent.requestID);
|
|
144
|
+
// Does nothing when another request has already replaced this token.
|
|
145
|
+
this.tokenCache.invalidate(token);
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The auth options for a copy made by `withOptions(options)`. A copy that
|
|
150
|
+
* sets neither `apiKey` nor `credentials` inherits this client's auth; see
|
|
151
|
+
* {@link bind} for when it also shares the token cache.
|
|
152
|
+
*/
|
|
153
|
+
cloneOptions(options) {
|
|
154
|
+
if (options.apiKey != null && options.credentials != null) {
|
|
155
|
+
// The constructor rejects the pair.
|
|
156
|
+
return { __auth: undefined };
|
|
157
|
+
}
|
|
158
|
+
if (options.apiKey != null)
|
|
159
|
+
return { credentials: null, __auth: undefined };
|
|
160
|
+
if (options.credentials != null)
|
|
161
|
+
return { apiKey: undefined, __auth: undefined };
|
|
162
|
+
return { __auth: this };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.ClientAuth = ClientAuth;
|
|
166
|
+
//# sourceMappingURL=client-auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-auth.js","sourceRoot":"","sources":["../../src/lib/credentials/client-auth.ts"],"names":[],"mappings":";;;AACA,+CAA+C;AAE/C,qDAAmD;AACnD,qDAAqD;AACrD,4DAAkE;AAClE,kDAA2C;AAE3C,sCAAgG;AAShG,0EAA0E;AAC1E,sEAAsE;AACtE,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAE5B,IAAI,gBAAgB,GAAG,KAAK,CAAC;AAE7B;;;;GAIG;AACH,MAAa,UAAU;IAarB,YACU,QAAoC,EACpC,UAAU,KAAK,EACf,iBAAiB,KAA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import { KernelError } from "../../core/error.mjs";
|
|
2
|
+
import { readEnv } from "../../internal/utils/env.mjs";
|
|
3
|
+
import { loggerFor } from "../../internal/utils/log.mjs";
|
|
4
|
+
import { federationCredentialsFromEnv } from "./credential-chain.mjs";
|
|
5
|
+
import { TokenCache } from "./token-cache.mjs";
|
|
6
|
+
import { WorkloadIdentityError, requireSecureTokenEndpoint, resolveAuthBaseURL } from "./types.mjs";
|
|
7
|
+
// A 401 can only arrive for a token that was sent within the last request
|
|
8
|
+
// timeout, so the few most recent tokens are enough to recognize one.
|
|
9
|
+
const MAX_RECENT_TOKENS = 4;
|
|
10
|
+
let warnedAPIKeyWins = false;
|
|
11
|
+
/**
|
|
12
|
+
* How one client authenticates: with its API key, or with an access token
|
|
13
|
+
* from `credentials`. The client calls this from its constructor,
|
|
14
|
+
* `withOptions`, `authHeaders` and its 401 handling.
|
|
15
|
+
*/
|
|
16
|
+
export class ClientAuth {
|
|
17
|
+
constructor(provider, fromEnv = false, warnAPIKeyWins = false) {
|
|
18
|
+
this.provider = provider;
|
|
19
|
+
this.fromEnv = fromEnv;
|
|
20
|
+
this.warnAPIKeyWins = warnAPIKeyWins;
|
|
21
|
+
this.tokenCache = null;
|
|
22
|
+
/** What the token cache was built for: tokens are requested for this API, through this fetch. */
|
|
23
|
+
this.target = null;
|
|
24
|
+
/** The access tokens most recently handed to requests, oldest first. */
|
|
25
|
+
this.recentTokens = [];
|
|
26
|
+
/**
|
|
27
|
+
* For each request options object, the requests sent with it that have
|
|
28
|
+
* already had a 401 answered with a fresh token. An options object the
|
|
29
|
+
* caller sends more than once has one entry per request.
|
|
30
|
+
*/
|
|
31
|
+
this.refreshed = new WeakMap();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Picks the credential for a new client. In order:
|
|
35
|
+
*
|
|
36
|
+
* 1. the `credentials` or `apiKey` option;
|
|
37
|
+
* 2. `KERNEL_API_KEY`;
|
|
38
|
+
* 3. workload identity federation configured in the environment.
|
|
39
|
+
*
|
|
40
|
+
* @param options The options the caller passed to the constructor.
|
|
41
|
+
* @param apiKey The `apiKey` option, or else `KERNEL_API_KEY`.
|
|
42
|
+
*/
|
|
43
|
+
static resolve(options = {}, apiKey) {
|
|
44
|
+
if (options.__auth)
|
|
45
|
+
return options.__auth;
|
|
46
|
+
if (options.credentials != null) {
|
|
47
|
+
if (options.apiKey) {
|
|
48
|
+
throw new KernelError('The `apiKey` and `credentials` options are mutually exclusive; pass only one of them.');
|
|
49
|
+
}
|
|
50
|
+
return new ClientAuth(options.credentials);
|
|
51
|
+
}
|
|
52
|
+
if (apiKey !== undefined) {
|
|
53
|
+
const keyIsFromEnv = options.apiKey === undefined;
|
|
54
|
+
return new ClientAuth(null, false, keyIsFromEnv && !!readEnv('KERNEL_FEDERATION_RULE_ID'));
|
|
55
|
+
}
|
|
56
|
+
return new ClientAuth(federationCredentialsFromEnv(), true);
|
|
57
|
+
}
|
|
58
|
+
/** Whether requests carry an access token from `credentials` instead of the API key. */
|
|
59
|
+
get usesCredentials() {
|
|
60
|
+
return this.provider != null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Connects the credentials to the client that will use them, so token
|
|
64
|
+
* requests go through the client's `fetch` and `fetchOptions` and are for
|
|
65
|
+
* its base URL. The client calls this when it is constructed and again
|
|
66
|
+
* before each request.
|
|
67
|
+
*
|
|
68
|
+
* Returns this object while the client still targets the API the token
|
|
69
|
+
* cache was built for, through the same fetch. That is how a copy made by
|
|
70
|
+
* `withOptions`, which starts with its parent's auth, shares the parent's
|
|
71
|
+
* token cache. Otherwise returns a new object with its own cache over the
|
|
72
|
+
* same provider, so a token is never sent to an API it was not requested
|
|
73
|
+
* for; this covers a copy with another base URL and a client whose
|
|
74
|
+
* `baseURL` is assigned after construction.
|
|
75
|
+
*/
|
|
76
|
+
bind(client, clientFetch) {
|
|
77
|
+
if (this.warnAPIKeyWins && !warnedAPIKeyWins) {
|
|
78
|
+
warnedAPIKeyWins = true;
|
|
79
|
+
loggerFor(client).warn('Both KERNEL_API_KEY and KERNEL_FEDERATION_RULE_ID are set; using the API key. Unset KERNEL_API_KEY to use workload identity federation.');
|
|
80
|
+
}
|
|
81
|
+
const provider = this.provider;
|
|
82
|
+
if (!provider)
|
|
83
|
+
return this;
|
|
84
|
+
const target = { baseURL: client.baseURL, fetch: clientFetch, fetchOptions: client.fetchOptions };
|
|
85
|
+
if (this.target) {
|
|
86
|
+
const sameTarget = this.target.baseURL === target.baseURL &&
|
|
87
|
+
this.target.fetch === target.fetch &&
|
|
88
|
+
this.target.fetchOptions === target.fetchOptions;
|
|
89
|
+
return sameTarget ? this : new ClientAuth(provider, this.fromEnv).bind(client, clientFetch);
|
|
90
|
+
}
|
|
91
|
+
if (this.fromEnv) {
|
|
92
|
+
requireSecureTokenEndpoint(resolveAuthBaseURL(undefined, target.baseURL));
|
|
93
|
+
}
|
|
94
|
+
this.target = target;
|
|
95
|
+
const fetch = (input, init) => clientFetch.call(undefined, input, { ...target.fetchOptions, ...init });
|
|
96
|
+
this.tokenCache = new TokenCache((opts) => provider({ ...opts, client: { fetch, baseURL: target.baseURL } }),
|
|
97
|
+
// The error is not logged: this client cannot vouch for what a
|
|
98
|
+
// provider or a custom fetch puts in one. It is thrown to the caller
|
|
99
|
+
// if the refresh still fails once the token is about to expire.
|
|
100
|
+
(err) => loggerFor(client).debug(`advisory credential refresh failed${err instanceof WorkloadIdentityError && err.statusCode != null ?
|
|
101
|
+
` with status ${err.statusCode}`
|
|
102
|
+
: ''}; serving the cached token`));
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
/** The `Authorization` header for one attempt of a request, or `undefined` in API key mode. */
|
|
106
|
+
async bearerAuth() {
|
|
107
|
+
if (!this.tokenCache)
|
|
108
|
+
return undefined;
|
|
109
|
+
const token = await this.tokenCache.getToken();
|
|
110
|
+
if (!this.recentTokens.includes(token)) {
|
|
111
|
+
this.recentTokens.push(token);
|
|
112
|
+
if (this.recentTokens.length > MAX_RECENT_TOKENS)
|
|
113
|
+
this.recentTokens.shift();
|
|
114
|
+
}
|
|
115
|
+
return { Authorization: `Bearer ${token}` };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Handles a 401 for an attempt of `request`. Returns true when the attempt
|
|
119
|
+
* should be sent again: the rejected token has been dropped, so the retry
|
|
120
|
+
* exchanges a fresh identity token.
|
|
121
|
+
*
|
|
122
|
+
* Returns false when the 401 should surface. That is when this request
|
|
123
|
+
* (`sent.requestID` names it across all of its attempts) has already been
|
|
124
|
+
* retried for a 401, or when the attempt did not carry an access token
|
|
125
|
+
* from `credentials` to the API: it used the API key or the caller's own
|
|
126
|
+
* `Authorization` header, or was answered by a browser VM it was routed
|
|
127
|
+
* to directly.
|
|
128
|
+
*/
|
|
129
|
+
refreshAfter401(request, sent, response) {
|
|
130
|
+
const token = sent.headers.get('authorization')?.replace(/^Bearer /, '');
|
|
131
|
+
if (!this.tokenCache || !token || !this.recentTokens.includes(token))
|
|
132
|
+
return false;
|
|
133
|
+
if (response.url && new URL(response.url).origin !== new URL(sent.url).origin)
|
|
134
|
+
return false;
|
|
135
|
+
let refreshed = this.refreshed.get(request);
|
|
136
|
+
if (!refreshed)
|
|
137
|
+
this.refreshed.set(request, (refreshed = new Set()));
|
|
138
|
+
if (refreshed.has(sent.requestID))
|
|
139
|
+
return false;
|
|
140
|
+
refreshed.add(sent.requestID);
|
|
141
|
+
// Does nothing when another request has already replaced this token.
|
|
142
|
+
this.tokenCache.invalidate(token);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The auth options for a copy made by `withOptions(options)`. A copy that
|
|
147
|
+
* sets neither `apiKey` nor `credentials` inherits this client's auth; see
|
|
148
|
+
* {@link bind} for when it also shares the token cache.
|
|
149
|
+
*/
|
|
150
|
+
cloneOptions(options) {
|
|
151
|
+
if (options.apiKey != null && options.credentials != null) {
|
|
152
|
+
// The constructor rejects the pair.
|
|
153
|
+
return { __auth: undefined };
|
|
154
|
+
}
|
|
155
|
+
if (options.apiKey != null)
|
|
156
|
+
return { credentials: null, __auth: undefined };
|
|
157
|
+
if (options.credentials != null)
|
|
158
|
+
return { apiKey: undefined, __auth: undefined };
|
|
159
|
+
return { __auth: this };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=client-auth.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-auth.mjs","sourceRoot":"","sources":["../../src/lib/credentials/client-auth.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,6BAAyB;AAE/C,OAAO,EAAE,OAAO,EAAE,qCAAiC;AACnD,OAAO,EAAE,SAAS,EAAE,qCAAiC;AACrD,OAAO,EAAE,4BAA4B,EAAE,+BAA2B;AAClE,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAE3C,OAAO,EAAE,qBAAqB,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,oBAAgB;AAShG,0EAA0E;AAC1E,sEAAsE;AACtE,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAE5B,IAAI,gBAAgB,GAAG,KAAK,CAAC;AAE7B;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAarB,YACU,QAAoC,EACpC,UAAU,KAAK,EACf,iBAAiB,KAAK;QAFtB,aAAQ,GAAR,QAAQ,CAA4B;QACpC,YAAO,GAAP,OAAO,CAAQ;QACf,mBAAc,GAAd,cAAc,CAAQ;QAfxB,eAAU,GAAsB,IAAI,CAAC;QAC7C,iGAAiG;QACzF,WAAM,GAAoE,IAAI,CAAC;QACvF,wEAAwE;QAChE,iBAAY,GAAa,EAAE,CAAC;QACpC;;;;WAIG;QACK,cAAS,GAAG,IAAI,OAAO,EAAuB,CAAC;IAMpD,CAAC;IAEJ;;;;;;;;;OASG;IACH,MAAM,CAAC,OAAO,CAAC,UAAuB,EAAE,EAAE,MAA0B;QAClE,IAAI,OAAO,CAAC,MAAM;YAAE,OAAO,OAAO,CAAC,MAAM,CA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CAAC;YAAE,OAAO,KAAK,CAAC;QAChD,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC9B,qEAAqE;QACrE,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;OAIG;IACH,YAAY,CAAC,OAAoB;QAC/B,IAAI,OAAO,CAAC,MAAM,IAAI,IAAI,IAAI,OAAO,CAAC,WAAW,IAAI,IAAI,EAAE,CAAC;YAC1D,oCAAoC;YACpC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QAC/B,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,IAAI,IAAI;YAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QAC5E,IAAI,OAAO,CAAC,WAAW,IAAI,IAAI;YAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QACjF,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IAC1B,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AccessTokenProvider } from "./types.mjs";
|
|
2
|
+
export declare const IDENTITY_TOKEN_SOURCES: readonly ["github_actions", "gcp_metadata", "aws_sts", "file", "env"];
|
|
3
|
+
export type IdentityTokenSource = (typeof IDENTITY_TOKEN_SOURCES)[number];
|
|
4
|
+
/**
|
|
5
|
+
* Builds workload identity credentials from the environment, or returns
|
|
6
|
+
* `null` when the environment does not configure them.
|
|
7
|
+
*
|
|
8
|
+
* `KERNEL_FEDERATION_RULE_ID` names the federation rule. The identity token
|
|
9
|
+
* comes from the source `KERNEL_IDENTITY_TOKEN_SOURCE` selects. When that is
|
|
10
|
+
* unset, the first of these that applies is used:
|
|
11
|
+
*
|
|
12
|
+
* 1. `KERNEL_IDENTITY_TOKEN_FILE`
|
|
13
|
+
* 2. `KERNEL_IDENTITY_TOKEN` (good for one exchange only)
|
|
14
|
+
* 3. GitHub Actions, when `ACTIONS_ID_TOKEN_REQUEST_URL` is present
|
|
15
|
+
*
|
|
16
|
+
* A source that is selected but not usable throws, rather than leaving the
|
|
17
|
+
* caller to find out on the first request.
|
|
18
|
+
*/
|
|
19
|
+
export declare function federationCredentialsFromEnv(): AccessTokenProvider | null;
|
|
20
|
+
//# sourceMappingURL=credential-chain.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-chain.d.mts","sourceRoot":"","sources":["../../src/lib/credentials/credential-chain.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,mBAAmB,EAAyB,oBAAgB;AAG1E,eAAO,MAAM,sBAAsB,uEAAwE,CAAC;AAC5G,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1E;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,4BAA4B,IAAI,mBAAmB,GAAG,IAAI,CAQzE"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AccessTokenProvider } from "./types.js";
|
|
2
|
+
export declare const IDENTITY_TOKEN_SOURCES: readonly ["github_actions", "gcp_metadata", "aws_sts", "file", "env"];
|
|
3
|
+
export type IdentityTokenSource = (typeof IDENTITY_TOKEN_SOURCES)[number];
|
|
4
|
+
/**
|
|
5
|
+
* Builds workload identity credentials from the environment, or returns
|
|
6
|
+
* `null` when the environment does not configure them.
|
|
7
|
+
*
|
|
8
|
+
* `KERNEL_FEDERATION_RULE_ID` names the federation rule. The identity token
|
|
9
|
+
* comes from the source `KERNEL_IDENTITY_TOKEN_SOURCE` selects. When that is
|
|
10
|
+
* unset, the first of these that applies is used:
|
|
11
|
+
*
|
|
12
|
+
* 1. `KERNEL_IDENTITY_TOKEN_FILE`
|
|
13
|
+
* 2. `KERNEL_IDENTITY_TOKEN` (good for one exchange only)
|
|
14
|
+
* 3. GitHub Actions, when `ACTIONS_ID_TOKEN_REQUEST_URL` is present
|
|
15
|
+
*
|
|
16
|
+
* A source that is selected but not usable throws, rather than leaving the
|
|
17
|
+
* caller to find out on the first request.
|
|
18
|
+
*/
|
|
19
|
+
export declare function federationCredentialsFromEnv(): AccessTokenProvider | null;
|
|
20
|
+
//# sourceMappingURL=credential-chain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-chain.d.ts","sourceRoot":"","sources":["../../src/lib/credentials/credential-chain.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,mBAAmB,EAAyB,mBAAgB;AAG1E,eAAO,MAAM,sBAAsB,uEAAwE,CAAC;AAC5G,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1E;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,4BAA4B,IAAI,mBAAmB,GAAG,IAAI,CAQzE"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IDENTITY_TOKEN_SOURCES = void 0;
|
|
4
|
+
exports.federationCredentialsFromEnv = federationCredentialsFromEnv;
|
|
5
|
+
const env_1 = require("../../internal/utils/env.js");
|
|
6
|
+
const identity_token_1 = require("./identity-token.js");
|
|
7
|
+
const oidc_federation_1 = require("./oidc-federation.js");
|
|
8
|
+
const types_1 = require("./types.js");
|
|
9
|
+
exports.IDENTITY_TOKEN_SOURCES = ['github_actions', 'gcp_metadata', 'aws_sts', 'file', 'env'];
|
|
10
|
+
/**
|
|
11
|
+
* Builds workload identity credentials from the environment, or returns
|
|
12
|
+
* `null` when the environment does not configure them.
|
|
13
|
+
*
|
|
14
|
+
* `KERNEL_FEDERATION_RULE_ID` names the federation rule. The identity token
|
|
15
|
+
* comes from the source `KERNEL_IDENTITY_TOKEN_SOURCE` selects. When that is
|
|
16
|
+
* unset, the first of these that applies is used:
|
|
17
|
+
*
|
|
18
|
+
* 1. `KERNEL_IDENTITY_TOKEN_FILE`
|
|
19
|
+
* 2. `KERNEL_IDENTITY_TOKEN` (good for one exchange only)
|
|
20
|
+
* 3. GitHub Actions, when `ACTIONS_ID_TOKEN_REQUEST_URL` is present
|
|
21
|
+
*
|
|
22
|
+
* A source that is selected but not usable throws, rather than leaving the
|
|
23
|
+
* caller to find out on the first request.
|
|
24
|
+
*/
|
|
25
|
+
function federationCredentialsFromEnv() {
|
|
26
|
+
const federationRuleId = (0, env_1.readEnv)('KERNEL_FEDERATION_RULE_ID');
|
|
27
|
+
if (!federationRuleId)
|
|
28
|
+
return null;
|
|
29
|
+
const identityTokenProvider = identityTokenProviderFromEnv();
|
|
30
|
+
if (!identityTokenProvider)
|
|
31
|
+
return null;
|
|
32
|
+
return (0, oidc_federation_1.oidcFederationProvider)({ identityTokenProvider, federationRuleId });
|
|
33
|
+
}
|
|
34
|
+
// One wrapper per value for the whole process, so a second client built from
|
|
35
|
+
// the same environment cannot send a token the first one already exchanged.
|
|
36
|
+
const staticTokenProviders = new Map();
|
|
37
|
+
function staticIdentityToken(token) {
|
|
38
|
+
let provider = staticTokenProviders.get(token);
|
|
39
|
+
if (!provider) {
|
|
40
|
+
provider = (0, identity_token_1.identityTokenFromValue)(token);
|
|
41
|
+
staticTokenProviders.set(token, provider);
|
|
42
|
+
}
|
|
43
|
+
return provider;
|
|
44
|
+
}
|
|
45
|
+
function identityTokenProviderFromEnv() {
|
|
46
|
+
const source = (0, env_1.readEnv)('KERNEL_IDENTITY_TOKEN_SOURCE');
|
|
47
|
+
const tokenFile = (0, env_1.readEnv)('KERNEL_IDENTITY_TOKEN_FILE');
|
|
48
|
+
const tokenValue = (0, env_1.readEnv)('KERNEL_IDENTITY_TOKEN');
|
|
49
|
+
switch (source) {
|
|
50
|
+
case undefined:
|
|
51
|
+
if (tokenFile)
|
|
52
|
+
return (0, identity_token_1.identityTokenFromFile)(tokenFile);
|
|
53
|
+
if (tokenValue)
|
|
54
|
+
return staticIdentityToken(tokenValue);
|
|
55
|
+
if ((0, env_1.readEnv)('ACTIONS_ID_TOKEN_REQUEST_URL'))
|
|
56
|
+
return (0, identity_token_1.identityTokenFromGitHubActions)();
|
|
57
|
+
return null;
|
|
58
|
+
case 'github_actions':
|
|
59
|
+
if (!(0, env_1.readEnv)('ACTIONS_ID_TOKEN_REQUEST_URL') || !(0, env_1.readEnv)('ACTIONS_ID_TOKEN_REQUEST_TOKEN')) {
|
|
60
|
+
throw new types_1.WorkloadIdentityError('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.');
|
|
61
|
+
}
|
|
62
|
+
return (0, identity_token_1.identityTokenFromGitHubActions)();
|
|
63
|
+
case 'gcp_metadata':
|
|
64
|
+
return (0, identity_token_1.identityTokenFromGCPMetadata)();
|
|
65
|
+
case 'aws_sts':
|
|
66
|
+
return (0, identity_token_1.identityTokenFromAWSSTS)();
|
|
67
|
+
case 'file':
|
|
68
|
+
if (!tokenFile) {
|
|
69
|
+
throw new types_1.WorkloadIdentityError('KERNEL_IDENTITY_TOKEN_SOURCE is file, but KERNEL_IDENTITY_TOKEN_FILE is not set.');
|
|
70
|
+
}
|
|
71
|
+
return (0, identity_token_1.identityTokenFromFile)(tokenFile);
|
|
72
|
+
case 'env':
|
|
73
|
+
if (!tokenValue) {
|
|
74
|
+
throw new types_1.WorkloadIdentityError('KERNEL_IDENTITY_TOKEN_SOURCE is env, but KERNEL_IDENTITY_TOKEN is not set.');
|
|
75
|
+
}
|
|
76
|
+
return staticIdentityToken(tokenValue);
|
|
77
|
+
default:
|
|
78
|
+
throw new types_1.WorkloadIdentityError(`KERNEL_IDENTITY_TOKEN_SOURCE is "${source}"; expected one of ${exports.IDENTITY_TOKEN_SOURCES.join(', ')}.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=credential-chain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-chain.js","sourceRoot":"","sources":["../../src/lib/credentials/credential-chain.ts"],"names":[],"mappings":";;;AA8BA,oEAQC;AAtCD,qDAAmD;AACnD,wDAM0B;AAC1B,0DAA2D;AAE3D,sCAAgD;AAEnC,QAAA,sBAAsB,GAAG,CAAC,gBAAgB,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,CAAU,CAAC;AAG5G;;;;;;;;;;;;;;GAcG;AACH,SAAgB,4BAA4B;IAC1C,MAAM,gBAAgB,GAAG,IAAA,aAAO,EAAC,2BAA2B,CAAC,CAAC;IAC9D,IAAI,CAAC,gBAAgB;QAAE,OAAO,IAAI,CAAC;IAEnC,MAAM,qBAAqB,GAAG,4BAA4B,EAAE,CAAC;IAC7D,IAAI,CAAC,qBAAqB;QAAE,OAAO,IAAI,CAAC;IAExC,OAAO,IAAA,wCAAsB,EAAC,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,CAAC,CAAC;AAC7E,CAAC;AAED,6EAA6E;AAC7E,4EAA4E;AAC5E,MAAM,oBAAoB,GAAG,IAAI,GAAG,EAAiC,CAAC;AAEtE,SAAS,mBAAmB,CAAC,KAAa;IACxC,IAAI,QAAQ,GAAG,oBAAoB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC/C,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,QAAQ,GAAG,IAAA,uCAAsB,EAAC,KAAK,CAAC,CAAC;QACzC,oBAAoB,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,SAAS,4BAA4B;IACnC,MAAM,MAAM,GAAG,IAAA,aAAO,EAAC,8BAA8B,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,IAAA,aAAO,EAAC,4BAA4B,CAAC,CAAC;IAC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ils/env.mjs";
|
|
2
|
+
import { identityTokenFromAWSSTS, identityTokenFromFile, identityTokenFromGCPMetadata, identityTokenFromGitHubActions, identityTokenFromValue, } from "./identity-token.mjs";
|
|
3
|
+
import { oidcFederationProvider } from "./oidc-federation.mjs";
|
|
4
|
+
import { WorkloadIdentityError } from "./types.mjs";
|
|
5
|
+
export const IDENTITY_TOKEN_SOURCES = ['github_actions', 'gcp_metadata', 'aws_sts', 'file', 'env'];
|
|
6
|
+
/**
|
|
7
|
+
* Builds workload identity credentials from the environment, or returns
|
|
8
|
+
* `null` when the environment does not configure them.
|
|
9
|
+
*
|
|
10
|
+
* `KERNEL_FEDERATION_RULE_ID` names the federation rule. The identity token
|
|
11
|
+
* comes from the source `KERNEL_IDENTITY_TOKEN_SOURCE` selects. When that is
|
|
12
|
+
* unset, the first of these that applies is used:
|
|
13
|
+
*
|
|
14
|
+
* 1. `KERNEL_IDENTITY_TOKEN_FILE`
|
|
15
|
+
* 2. `KERNEL_IDENTITY_TOKEN` (good for one exchange only)
|
|
16
|
+
* 3. GitHub Actions, when `ACTIONS_ID_TOKEN_REQUEST_URL` is present
|
|
17
|
+
*
|
|
18
|
+
* A source that is selected but not usable throws, rather than leaving the
|
|
19
|
+
* caller to find out on the first request.
|
|
20
|
+
*/
|
|
21
|
+
export function federationCredentialsFromEnv() {
|
|
22
|
+
const federationRuleId = readEnv('KERNEL_FEDERATION_RULE_ID');
|
|
23
|
+
if (!federationRuleId)
|
|
24
|
+
return null;
|
|
25
|
+
const identityTokenProvider = identityTokenProviderFromEnv();
|
|
26
|
+
if (!identityTokenProvider)
|
|
27
|
+
return null;
|
|
28
|
+
return oidcFederationProvider({ identityTokenProvider, federationRuleId });
|
|
29
|
+
}
|
|
30
|
+
// One wrapper per value for the whole process, so a second client built from
|
|
31
|
+
// the same environment cannot send a token the first one already exchanged.
|
|
32
|
+
const staticTokenProviders = new Map();
|
|
33
|
+
function staticIdentityToken(token) {
|
|
34
|
+
let provider = staticTokenProviders.get(token);
|
|
35
|
+
if (!provider) {
|
|
36
|
+
provider = identityTokenFromValue(token);
|
|
37
|
+
staticTokenProviders.set(token, provider);
|
|
38
|
+
}
|
|
39
|
+
return provider;
|
|
40
|
+
}
|
|
41
|
+
function identityTokenProviderFromEnv() {
|
|
42
|
+
const source = readEnv('KERNEL_IDENTITY_TOKEN_SOURCE');
|
|
43
|
+
const tokenFile = readEnv('KERNEL_IDENTITY_TOKEN_FILE');
|
|
44
|
+
const tokenValue = readEnv('KERNEL_IDENTITY_TOKEN');
|
|
45
|
+
switch (source) {
|
|
46
|
+
case undefined:
|
|
47
|
+
if (tokenFile)
|
|
48
|
+
return identityTokenFromFile(tokenFile);
|
|
49
|
+
if (tokenValue)
|
|
50
|
+
return staticIdentityToken(tokenValue);
|
|
51
|
+
if (readEnv('ACTIONS_ID_TOKEN_REQUEST_URL'))
|
|
52
|
+
return identityTokenFromGitHubActions();
|
|
53
|
+
return null;
|
|
54
|
+
case 'github_actions':
|
|
55
|
+
if (!readEnv('ACTIONS_ID_TOKEN_REQUEST_URL') || !readEnv('ACTIONS_ID_TOKEN_REQUEST_TOKEN')) {
|
|
56
|
+
throw new WorkloadIdentityError('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.');
|
|
57
|
+
}
|
|
58
|
+
return identityTokenFromGitHubActions();
|
|
59
|
+
case 'gcp_metadata':
|
|
60
|
+
return identityTokenFromGCPMetadata();
|
|
61
|
+
case 'aws_sts':
|
|
62
|
+
return identityTokenFromAWSSTS();
|
|
63
|
+
case 'file':
|
|
64
|
+
if (!tokenFile) {
|
|
65
|
+
throw new WorkloadIdentityError('KERNEL_IDENTITY_TOKEN_SOURCE is file, but KERNEL_IDENTITY_TOKEN_FILE is not set.');
|
|
66
|
+
}
|
|
67
|
+
return identityTokenFromFile(tokenFile);
|
|
68
|
+
case 'env':
|
|
69
|
+
if (!tokenValue) {
|
|
70
|
+
throw new WorkloadIdentityError('KERNEL_IDENTITY_TOKEN_SOURCE is env, but KERNEL_IDENTITY_TOKEN is not set.');
|
|
71
|
+
}
|
|
72
|
+
return staticIdentityToken(tokenValue);
|
|
73
|
+
default:
|
|
74
|
+
throw new WorkloadIdentityError(`KERNEL_IDENTITY_TOKEN_SOURCE is "${source}"; expected one of ${IDENTITY_TOKEN_SOURCES.join(', ')}.`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=credential-chain.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-chain.mjs","sourceRoot":"","sources":["../../src/lib/credentials/credential-chain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,qCAAiC;AACnD,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,4BAA4B,EAC5B,8BAA8B,EAC9B,sBAAsB,GACvB,6BAAyB;AAC1B,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAE3D,OAAO,EAAE,qBAAqB,EAAE,oBAAgB;AAEhD,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,gBAAgB,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,CAAU,CAAC;AAG5G;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,4BAA4B;IAC1C,MAAM,gBAAgB,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAAC;IAC9D,IAAI,CAAC,gBAAgB;QAAE,OAAO,IAAI,CAAC;IAEnC,MAAM,qBAAqB,GAAG,4BAA4B,EAAE,CAAC;IAC7D,IAAI,CAAC,qBAAqB;QAAE,OAAO,IAAI,CAAC;IAExC,OAAO,sBAAsB,CAAC,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,CAAC,CAAC;AAC7E,CAAC;AAED,6EAA6E;AAC7E,4EAA4E;AAC5E,MAAM,oBAAoB,GAAG,IAAI,GAAG,EAAiC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AAC,SAAS,CAAC,CAAC;QAC1C,KAAK,KAAK;YACR,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,MAAM,IAAI,qBAAqB,CAC7B,4EAA4E,CAC7E,CAAC;YACJ,CAAC;YACD,OAAO,mBAAmB,CAAC,UAAU,CAAC,CAAC;QACzC;YACE,MAAM,IAAI,qBAAqB,CAC7B,oCAAoC,MAAM,sBAAsB,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CACrG,CAAC;IACN,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Fetch } from "../../internal/builtin-types.mjs";
|
|
2
|
+
import type { IdentityTokenProvider } from "./types.mjs";
|
|
3
|
+
export type IdentityTokenSourceOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* The audience to request. Defaults to `KERNEL_IDENTITY_TOKEN_AUDIENCE`,
|
|
6
|
+
* else the auth base URL (for example `https://auth.onkernel.com`). The
|
|
7
|
+
* federation rule's `expected_audience` must be set to the same value.
|
|
8
|
+
*/
|
|
9
|
+
audience?: string | undefined;
|
|
10
|
+
/** Defaults to the `fetch` of the client using the source, else the global `fetch`. */
|
|
11
|
+
fetch?: Fetch | undefined;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* What to tell the caller when the token endpoint reports that the token
|
|
15
|
+
* this provider returned was already used. The advice depends on where the
|
|
16
|
+
* token comes from, which only the built-in file and static sources know.
|
|
17
|
+
*/
|
|
18
|
+
export declare function replayHintFor(provider: IdentityTokenProvider): string;
|
|
19
|
+
/**
|
|
20
|
+
* Reads the token from a file on every call, so a token the platform rotates
|
|
21
|
+
* (such as a Kubernetes projected service account token) is picked up.
|
|
22
|
+
*/
|
|
23
|
+
export declare function identityTokenFromFile(path: string): IdentityTokenProvider;
|
|
24
|
+
/**
|
|
25
|
+
* Wraps one token as an {@link IdentityTokenProvider} that can be used for
|
|
26
|
+
* exactly one exchange.
|
|
27
|
+
*
|
|
28
|
+
* Most providers' tokens are single-use, and a fixed value cannot be
|
|
29
|
+
* replaced with a fresh one, so the second call throws instead of sending
|
|
30
|
+
* the same token again. Use {@link identityTokenFromFile}, a built-in source
|
|
31
|
+
* or your own callback for anything that outlives one access token.
|
|
32
|
+
*/
|
|
33
|
+
export declare function identityTokenFromValue(token: string): IdentityTokenProvider;
|
|
34
|
+
/**
|
|
35
|
+
* Requests an ID token from GitHub Actions. The workflow or job needs
|
|
36
|
+
* `permissions: id-token: write`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function identityTokenFromGitHubActions(options?: IdentityTokenSourceOptions): IdentityTokenProvider;
|
|
39
|
+
/**
|
|
40
|
+
* Requests an identity token for the attached service account from the
|
|
41
|
+
* Google Cloud metadata server.
|
|
42
|
+
*/
|
|
43
|
+
export declare function identityTokenFromGCPMetadata(options?: IdentityTokenSourceOptions): IdentityTokenProvider;
|
|
44
|
+
export type AWSSTSIdentityTokenOptions = {
|
|
45
|
+
/** See {@link IdentityTokenSourceOptions.audience}. */
|
|
46
|
+
audience?: string | undefined;
|
|
47
|
+
/** Defaults to `AWS_REGION`, else `AWS_DEFAULT_REGION`, else `us-east-1`. */
|
|
48
|
+
region?: string | undefined;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Requests a token from AWS STS `GetWebIdentityToken` with the workload's
|
|
52
|
+
* ambient AWS credentials.
|
|
53
|
+
*
|
|
54
|
+
* Needs the optional `@aws-sdk/client-sts` package, which is loaded on first
|
|
55
|
+
* use. The call goes to a regional STS endpoint through the AWS SDK's own
|
|
56
|
+
* HTTP client, not the Kernel client's `fetch`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function identityTokenFromAWSSTS(options?: AWSSTSIdentityTokenOptions): IdentityTokenProvider;
|
|
59
|
+
//# sourceMappingURL=identity-token.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity-token.d.mts","sourceRoot":"","sources":["../../src/lib/credentials/identity-token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,yCAAqC;AAG1D,OAAO,KAAK,EAAE,qBAAqB,EAAwB,oBAAgB;AAU3E,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,uFAAuF;IACvF,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC;CAC3B,CAAC;AAIF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,qBAAqB,GAAG,MAAM,CAKrE;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,qBAAqB,CA+BzE;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,qBAAqB,CAmB3E;AAED;;;GAGG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,GAAE,0BAA+B,GACvC,qBAAqB,CA0BvB;AAKD;;;GAGG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,0BAA+B,GACvC,qBAAqB,CAavB;AAED,MAAM,MAAM,0BAA0B,GAAG;IACvC,uDAAuD;IACvD,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,6EAA6E;IAC7E,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC7B,CAAC;AAYF;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,GAAE,0BAA+B,GAAG,qBAAqB,CA6BvG"}
|