@cubist-labs/cubesigner-sdk 0.4.209 → 0.4.217
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/dist/package.json +1 -1
- package/dist/src/acl.d.ts +12 -0
- package/dist/src/acl.d.ts.map +1 -0
- package/dist/src/acl.js +3 -0
- package/dist/src/client/api_client.d.ts +24 -6
- package/dist/src/client/api_client.d.ts.map +1 -1
- package/dist/src/client/api_client.js +40 -14
- package/dist/src/client/base_client.d.ts +1 -0
- package/dist/src/client/base_client.d.ts.map +1 -1
- package/dist/src/client/base_client.js +1 -1
- package/dist/src/contact.d.ts +9 -1
- package/dist/src/contact.d.ts.map +1 -1
- package/dist/src/contact.js +14 -1
- package/dist/src/evm/index.d.ts +7 -0
- package/dist/src/evm/index.d.ts.map +1 -1
- package/dist/src/evm/index.js +28 -29
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/org.d.ts +60 -19
- package/dist/src/org.d.ts.map +1 -1
- package/dist/src/org.js +82 -25
- package/dist/src/policy.d.ts +109 -35
- package/dist/src/policy.d.ts.map +1 -1
- package/dist/src/policy.js +88 -42
- package/dist/src/schema.d.ts +158 -16
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/schema.js +1 -1
- package/dist/src/schema_types.d.ts +12 -2
- package/dist/src/schema_types.d.ts.map +1 -1
- package/dist/src/schema_types.js +1 -1
- package/dist/src/scopes.d.ts.map +1 -1
- package/dist/src/scopes.js +2 -1
- package/package.json +1 -1
- package/src/acl.ts +13 -0
- package/src/client/api_client.ts +54 -12
- package/src/client/base_client.ts +1 -1
- package/src/contact.ts +16 -1
- package/src/evm/index.ts +4 -4
- package/src/index.ts +2 -0
- package/src/org.ts +118 -31
- package/src/policy.ts +144 -49
- package/src/schema.ts +184 -25
- package/src/schema_types.ts +14 -3
- package/src/scopes.ts +1 -0
package/src/policy.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ApiClient,
|
|
3
|
+
C2FResponse,
|
|
3
4
|
CubeSignerResponse,
|
|
4
5
|
EditPolicy,
|
|
5
6
|
Empty,
|
|
6
|
-
|
|
7
|
+
InvokeC2FResponse,
|
|
7
8
|
JsonValue,
|
|
8
9
|
KeyPolicy,
|
|
9
10
|
KeyPolicyRule,
|
|
@@ -14,8 +15,10 @@ import type {
|
|
|
14
15
|
RolePolicy,
|
|
15
16
|
RolePolicyRule,
|
|
16
17
|
UpdatePolicyRequest,
|
|
17
|
-
WasmPolicyResponse,
|
|
18
18
|
WasmRule,
|
|
19
|
+
Acl,
|
|
20
|
+
AceAttribute,
|
|
21
|
+
PolicyAction,
|
|
19
22
|
} from ".";
|
|
20
23
|
|
|
21
24
|
import { loadSubtleCrypto } from ".";
|
|
@@ -25,43 +28,77 @@ import { loadSubtleCrypto } from ".";
|
|
|
25
28
|
*/
|
|
26
29
|
export type PolicyRule = KeyPolicyRule | RolePolicyRule | WasmRule;
|
|
27
30
|
|
|
31
|
+
/**
|
|
32
|
+
* A helper type for {@link PolicyInfo} with a more detailed `acl` type.
|
|
33
|
+
*/
|
|
34
|
+
type NamedPolicyInfo = PolicyInfo & {
|
|
35
|
+
acl?: Acl<PolicyAction, PolicyCtx>;
|
|
36
|
+
};
|
|
37
|
+
|
|
28
38
|
/**
|
|
29
39
|
* The policy info for a named key policy.
|
|
30
40
|
*/
|
|
31
|
-
export type KeyPolicyInfo =
|
|
41
|
+
export type KeyPolicyInfo = NamedPolicyInfo & {
|
|
32
42
|
policy_type: "Key";
|
|
33
43
|
};
|
|
34
44
|
|
|
35
45
|
/**
|
|
36
46
|
* The policy info for a named role policy.
|
|
37
47
|
*/
|
|
38
|
-
export type RolePolicyInfo =
|
|
48
|
+
export type RolePolicyInfo = NamedPolicyInfo & {
|
|
39
49
|
policy_type: "Role";
|
|
40
50
|
};
|
|
41
51
|
|
|
42
52
|
/**
|
|
43
53
|
* The policy info for a named wasm policy.
|
|
44
54
|
*/
|
|
45
|
-
export type WasmPolicyInfo =
|
|
55
|
+
export type WasmPolicyInfo = NamedPolicyInfo & {
|
|
46
56
|
policy_type: "Wasm";
|
|
47
57
|
};
|
|
48
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The policy info for a Confidential Cloud Function.
|
|
61
|
+
*/
|
|
62
|
+
export type C2FInfo = WasmPolicyInfo;
|
|
63
|
+
|
|
49
64
|
/**
|
|
50
65
|
* A helper type for valid named policy version strings.
|
|
51
66
|
*/
|
|
52
67
|
export type Version = `v${number}` | `latest`;
|
|
53
68
|
|
|
69
|
+
/** A policy access control entry. */
|
|
70
|
+
export type PolicyAcl = Acl<PolicyAction, PolicyCtx>;
|
|
71
|
+
|
|
72
|
+
/** Additional contexts when using policies. */
|
|
73
|
+
export type PolicyCtx = {
|
|
74
|
+
/**
|
|
75
|
+
* The resources (keys, roles, and key-in-roles) that the access control entry
|
|
76
|
+
* applies to.
|
|
77
|
+
*/
|
|
78
|
+
resources: AceAttribute<PolicyResource>;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** A resource a policy is invoked with or attached to. */
|
|
82
|
+
export type PolicyResource =
|
|
83
|
+
/** A key or role id. */
|
|
84
|
+
| string
|
|
85
|
+
/** Keys attached to roles. */
|
|
86
|
+
| { key_ids: "*" | string[]; role_ids: "*" | string[] };
|
|
87
|
+
|
|
54
88
|
/**
|
|
55
|
-
* Upload the given Wasm
|
|
89
|
+
* Upload the given Wasm Confidential Cloud Function.
|
|
56
90
|
*
|
|
57
91
|
* @param apiClient The API client to use.
|
|
58
|
-
* @param policy The Wasm
|
|
59
|
-
* @returns The Wasm
|
|
92
|
+
* @param policy The Wasm function.
|
|
93
|
+
* @returns The Wasm function object hash to use for creating/updating C2F policies.
|
|
60
94
|
* @throws if uploading the policy fails.
|
|
61
95
|
* @internal
|
|
62
96
|
*/
|
|
63
|
-
export async function
|
|
64
|
-
|
|
97
|
+
export async function uploadWasmFunction(
|
|
98
|
+
apiClient: ApiClient,
|
|
99
|
+
policy: Uint8Array,
|
|
100
|
+
): Promise<string> {
|
|
101
|
+
// get the SHA-256 hash of the function to get the upload url.
|
|
65
102
|
const subtle = await loadSubtleCrypto();
|
|
66
103
|
const hashBytes = await subtle.digest("SHA-256", policy);
|
|
67
104
|
const hash = "0x" + Buffer.from(hashBytes).toString("hex");
|
|
@@ -69,25 +106,36 @@ export async function uploadWasmPolicy(apiClient: ApiClient, policy: Uint8Array)
|
|
|
69
106
|
// get the upload URL
|
|
70
107
|
const { signed_url } = await apiClient.wasmPolicyUpload({ hash });
|
|
71
108
|
|
|
72
|
-
// upload the
|
|
109
|
+
// upload the wasm object
|
|
73
110
|
const resp = await fetch(signed_url, {
|
|
74
111
|
method: "PUT",
|
|
75
112
|
body: policy,
|
|
76
113
|
});
|
|
77
114
|
|
|
78
115
|
if (!resp.ok) {
|
|
79
|
-
throw new Error(`Failed to upload
|
|
116
|
+
throw new Error(`Failed to upload function with status: ${resp.status}: ${resp.statusText}`);
|
|
80
117
|
}
|
|
81
118
|
|
|
82
119
|
return hash;
|
|
83
120
|
}
|
|
84
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Upload the given Wasm policy.
|
|
124
|
+
*
|
|
125
|
+
* @param apiClient The API client to use.
|
|
126
|
+
* @param policy The Wasm function.
|
|
127
|
+
* @returns The Wasm function object hash to use for creating/updating C2F policies.
|
|
128
|
+
* @throws if uploading the policy fails.
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
export const uploadWasmPolicy = uploadWasmFunction;
|
|
132
|
+
|
|
85
133
|
/**
|
|
86
134
|
* Abstract class for shared methods between key, role and Wasm policies.
|
|
87
135
|
*/
|
|
88
136
|
export abstract class NamedPolicy {
|
|
89
137
|
protected readonly apiClient: ApiClient;
|
|
90
|
-
protected data:
|
|
138
|
+
protected data: NamedPolicyInfo;
|
|
91
139
|
|
|
92
140
|
/**
|
|
93
141
|
* Helper method for creating a named policy from a policy info.
|
|
@@ -103,7 +151,7 @@ export abstract class NamedPolicy {
|
|
|
103
151
|
case "Role":
|
|
104
152
|
return new NamedRolePolicy(apiClient, info as RolePolicyInfo);
|
|
105
153
|
case "Wasm":
|
|
106
|
-
return new
|
|
154
|
+
return new C2FFunction(apiClient, info as C2FInfo);
|
|
107
155
|
}
|
|
108
156
|
}
|
|
109
157
|
|
|
@@ -129,7 +177,7 @@ export abstract class NamedPolicy {
|
|
|
129
177
|
if (version == `v${this.data.version}`) {
|
|
130
178
|
versionInfo = this.data;
|
|
131
179
|
} else {
|
|
132
|
-
versionInfo = await this.apiClient.policyGet(this.id, version);
|
|
180
|
+
versionInfo = (await this.apiClient.policyGet(this.id, version)) as NamedPolicyInfo;
|
|
133
181
|
}
|
|
134
182
|
|
|
135
183
|
return new NamedPolicyRules(this.apiClient, versionInfo);
|
|
@@ -199,9 +247,9 @@ export abstract class NamedPolicy {
|
|
|
199
247
|
}
|
|
200
248
|
|
|
201
249
|
/**
|
|
202
|
-
* Sets a new metadata value for the
|
|
250
|
+
* Sets a new metadata value for the named policy (overwriting the existing value).
|
|
203
251
|
*
|
|
204
|
-
* @param metadata The new metadata for the
|
|
252
|
+
* @param metadata The new metadata for the named policy.
|
|
205
253
|
* @param mfaReceipt Optional MFA receipt(s).
|
|
206
254
|
* @throws if MFA is required and no receipts are provided
|
|
207
255
|
*/
|
|
@@ -210,7 +258,7 @@ export abstract class NamedPolicy {
|
|
|
210
258
|
}
|
|
211
259
|
|
|
212
260
|
/**
|
|
213
|
-
* Fetch and return the edit policy for the
|
|
261
|
+
* Fetch and return the edit policy for the named policy.
|
|
214
262
|
*
|
|
215
263
|
* @returns The edit policy for this named policy.
|
|
216
264
|
*/
|
|
@@ -230,6 +278,27 @@ export abstract class NamedPolicy {
|
|
|
230
278
|
await this.update({ edit_policy: editPolicy }, mfaReceipt);
|
|
231
279
|
}
|
|
232
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Fetch and return the access control entries for the named policy.
|
|
283
|
+
*
|
|
284
|
+
* @returns The access control entries for this named policy.
|
|
285
|
+
*/
|
|
286
|
+
async acl(): Promise<PolicyAcl | undefined> {
|
|
287
|
+
const data = await this.fetch();
|
|
288
|
+
return data.acl;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Sets new access control entries for the named policy (overwriting the existing entries).
|
|
293
|
+
*
|
|
294
|
+
* @param acl The access control entries to set.
|
|
295
|
+
* @param mfaReceipt Optional MFA receipt(s).
|
|
296
|
+
* @throws if MFA is required and no receipts are provided
|
|
297
|
+
*/
|
|
298
|
+
async setAcl(acl: PolicyAcl, mfaReceipt?: MfaReceipts) {
|
|
299
|
+
await this.update({ acl }, mfaReceipt);
|
|
300
|
+
}
|
|
301
|
+
|
|
233
302
|
/**
|
|
234
303
|
* @returns a list of all keys, roles, and key-in-roles that all versions of this policy
|
|
235
304
|
* are attached to.
|
|
@@ -282,7 +351,7 @@ export abstract class NamedPolicy {
|
|
|
282
351
|
* @param data The JSON response from the API server.
|
|
283
352
|
* @internal
|
|
284
353
|
*/
|
|
285
|
-
protected constructor(apiClient: ApiClient, data:
|
|
354
|
+
protected constructor(apiClient: ApiClient, data: NamedPolicyInfo) {
|
|
286
355
|
this.apiClient = apiClient;
|
|
287
356
|
this.data = data;
|
|
288
357
|
}
|
|
@@ -299,9 +368,9 @@ export abstract class NamedPolicy {
|
|
|
299
368
|
protected async update(
|
|
300
369
|
request: UpdatePolicyRequest,
|
|
301
370
|
mfaReceipt?: MfaReceipts,
|
|
302
|
-
): Promise<
|
|
371
|
+
): Promise<NamedPolicyInfo> {
|
|
303
372
|
const resp = await this.apiClient.policyUpdate(this.id, request, mfaReceipt);
|
|
304
|
-
this.data = resp.data();
|
|
373
|
+
this.data = resp.data() as NamedPolicyInfo;
|
|
305
374
|
return this.data;
|
|
306
375
|
}
|
|
307
376
|
|
|
@@ -312,8 +381,8 @@ export abstract class NamedPolicy {
|
|
|
312
381
|
* @returns The policy information.
|
|
313
382
|
* @internal
|
|
314
383
|
*/
|
|
315
|
-
protected async fetch(version: Version = "latest"): Promise<
|
|
316
|
-
this.data = await this.apiClient.policyGet(this.id, version);
|
|
384
|
+
protected async fetch(version: Version = "latest"): Promise<NamedPolicyInfo> {
|
|
385
|
+
this.data = (await this.apiClient.policyGet(this.id, version)) as NamedPolicyInfo;
|
|
317
386
|
return this.data;
|
|
318
387
|
}
|
|
319
388
|
}
|
|
@@ -387,44 +456,47 @@ export class NamedRolePolicy extends NamedPolicy {
|
|
|
387
456
|
}
|
|
388
457
|
|
|
389
458
|
/**
|
|
390
|
-
* A representation of a
|
|
459
|
+
* A representation of a Confidential Cloud Function (C2F).
|
|
460
|
+
*
|
|
461
|
+
* This class extends NamedPolicy because C2F functions can be attached
|
|
462
|
+
* to keys and roles like a named policy.
|
|
391
463
|
*/
|
|
392
|
-
export class
|
|
393
|
-
override data:
|
|
464
|
+
export class C2FFunction extends NamedPolicy {
|
|
465
|
+
override data: C2FInfo;
|
|
394
466
|
|
|
395
467
|
/**
|
|
396
|
-
* Update
|
|
468
|
+
* Update this C2F function with a new Wasm function.
|
|
397
469
|
*
|
|
398
|
-
* @param policy The new Wasm
|
|
470
|
+
* @param policy The new Wasm function.
|
|
399
471
|
* @param mfaReceipt Optional MFA receipt(s).
|
|
400
|
-
* @throws if uploading the
|
|
472
|
+
* @throws if uploading the function fails.
|
|
401
473
|
* @throws if MFA is required and no receipts are provided.
|
|
402
474
|
*/
|
|
403
|
-
async
|
|
475
|
+
async setWasmFunction(policy: Uint8Array, mfaReceipt?: MfaReceipts) {
|
|
404
476
|
// upload the policy object
|
|
405
|
-
const hash = await
|
|
477
|
+
const hash = await uploadWasmFunction(this.apiClient, policy);
|
|
406
478
|
|
|
407
479
|
// update this policy with the new policy verison.
|
|
408
480
|
const body: UpdatePolicyRequest = { rules: [{ hash }] };
|
|
409
|
-
this.data = (await this.update(body, mfaReceipt)) as
|
|
481
|
+
this.data = (await this.update(body, mfaReceipt)) as C2FInfo;
|
|
410
482
|
}
|
|
411
483
|
|
|
412
484
|
/**
|
|
413
|
-
* Invoke this
|
|
485
|
+
* Invoke this Confidential Cloud Function.
|
|
414
486
|
*
|
|
415
|
-
* @param keyId The optional key id that the
|
|
416
|
-
* @param version The version of the
|
|
417
|
-
* @param request The optional sign request body that will be sent to the
|
|
418
|
-
* @param roleId The optional role id that the
|
|
487
|
+
* @param keyId The optional key id that the function will be invoked with.
|
|
488
|
+
* @param version The version of the function to invoke. Defaults to "latest".
|
|
489
|
+
* @param request The optional sign request body that will be sent to the function.
|
|
490
|
+
* @param roleId The optional role id that the function will be invoked by.
|
|
419
491
|
* If `undefined`, the policy will be invoked by the user session.
|
|
420
|
-
* @returns The result of invoking the
|
|
492
|
+
* @returns The result of invoking the function.
|
|
421
493
|
*/
|
|
422
494
|
async invoke(
|
|
423
495
|
keyId?: string,
|
|
424
496
|
version: Version = "latest",
|
|
425
497
|
request?: JsonValue,
|
|
426
498
|
roleId?: string,
|
|
427
|
-
): Promise<
|
|
499
|
+
): Promise<C2FInvocation> {
|
|
428
500
|
// TODO Ideally, `version` should be the first parameter. But for backwards
|
|
429
501
|
// compatibility, we keep `keyId` as the first parameter for now.
|
|
430
502
|
const resp = await this.apiClient.policyInvoke(this.id, version, {
|
|
@@ -435,6 +507,17 @@ export class NamedWasmPolicy extends NamedPolicy {
|
|
|
435
507
|
return new PolicyInvocation(resp);
|
|
436
508
|
}
|
|
437
509
|
|
|
510
|
+
// Backwards compability with Named Wasm Policy names
|
|
511
|
+
/**
|
|
512
|
+
* Update the policy with the new Wasm policy.
|
|
513
|
+
*
|
|
514
|
+
* @param policy The new Wasm policy object.
|
|
515
|
+
* @param mfaReceipt Optional MFA receipt(s).
|
|
516
|
+
* @throws if uploading the policy object fails.
|
|
517
|
+
* @throws if MFA is required and no receipts are provided.
|
|
518
|
+
*/
|
|
519
|
+
setWasmPolicy = this.setWasmFunction;
|
|
520
|
+
|
|
438
521
|
// --------------------------------------------------------------------------
|
|
439
522
|
// -- INTERNAL --------------------------------------------------------------
|
|
440
523
|
// --------------------------------------------------------------------------
|
|
@@ -446,7 +529,7 @@ export class NamedWasmPolicy extends NamedPolicy {
|
|
|
446
529
|
* @param data The JSON response from the API server.
|
|
447
530
|
* @internal
|
|
448
531
|
*/
|
|
449
|
-
constructor(apiClient: ApiClient, data:
|
|
532
|
+
constructor(apiClient: ApiClient, data: C2FInfo) {
|
|
450
533
|
super(apiClient, data);
|
|
451
534
|
this.data = data;
|
|
452
535
|
}
|
|
@@ -458,7 +541,7 @@ export class NamedWasmPolicy extends NamedPolicy {
|
|
|
458
541
|
export class NamedPolicyRules {
|
|
459
542
|
/** The CubeSigner instance that this policy is associated with */
|
|
460
543
|
readonly #apiClient: ApiClient;
|
|
461
|
-
#data:
|
|
544
|
+
#data: NamedPolicyInfo;
|
|
462
545
|
|
|
463
546
|
/**
|
|
464
547
|
* @returns The ID of the policy.
|
|
@@ -505,7 +588,7 @@ export class NamedPolicyRules {
|
|
|
505
588
|
* @param data The JSON response from the API server.
|
|
506
589
|
* @internal
|
|
507
590
|
*/
|
|
508
|
-
constructor(apiClient: ApiClient, data:
|
|
591
|
+
constructor(apiClient: ApiClient, data: NamedPolicyInfo) {
|
|
509
592
|
this.#apiClient = apiClient;
|
|
510
593
|
this.#data = data;
|
|
511
594
|
}
|
|
@@ -516,20 +599,20 @@ export class NamedPolicyRules {
|
|
|
516
599
|
* @returns The policy information.
|
|
517
600
|
* @internal
|
|
518
601
|
*/
|
|
519
|
-
private async fetch(): Promise<
|
|
520
|
-
this.#data = await this.#apiClient.policyGet(this.id, this.version);
|
|
602
|
+
private async fetch(): Promise<NamedPolicyInfo> {
|
|
603
|
+
this.#data = (await this.#apiClient.policyGet(this.id, this.version)) as NamedPolicyInfo;
|
|
521
604
|
return this.#data;
|
|
522
605
|
}
|
|
523
606
|
}
|
|
524
607
|
|
|
525
608
|
/**
|
|
526
|
-
* The result of invoking a
|
|
609
|
+
* The result of invoking a Confidential Cloud Function.
|
|
527
610
|
*/
|
|
528
|
-
export class
|
|
529
|
-
readonly #data:
|
|
611
|
+
export class C2FInvocation {
|
|
612
|
+
readonly #data: InvokeC2FResponse;
|
|
530
613
|
|
|
531
614
|
/** @returns The policy response itself. */
|
|
532
|
-
get response():
|
|
615
|
+
get response(): C2FResponse {
|
|
533
616
|
return this.#data.response;
|
|
534
617
|
}
|
|
535
618
|
|
|
@@ -563,7 +646,19 @@ export class PolicyInvocation {
|
|
|
563
646
|
* @param data The JSON response from the API server.
|
|
564
647
|
* @internal
|
|
565
648
|
*/
|
|
566
|
-
constructor(data:
|
|
649
|
+
constructor(data: InvokeC2FResponse) {
|
|
567
650
|
this.#data = data;
|
|
568
651
|
}
|
|
569
652
|
}
|
|
653
|
+
|
|
654
|
+
// Backwards compability with Named Wasm Policy names
|
|
655
|
+
|
|
656
|
+
/** A representation of a Wasm policy. */
|
|
657
|
+
export type NamedWasmPolicy = C2FFunction;
|
|
658
|
+
/** A representation of a Wasm policy. */
|
|
659
|
+
export const NamedWasmPolicy = C2FFunction;
|
|
660
|
+
|
|
661
|
+
/** The result of invoking a named WASM policy. */
|
|
662
|
+
export type PolicyInvocation = C2FInvocation;
|
|
663
|
+
/** The result of invoking a named WASM policy. */
|
|
664
|
+
export const PolicyInvocation = C2FInvocation;
|