@qnsp/tenant-sdk 0.3.0 → 0.3.2

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/src/index.ts DELETED
@@ -1,916 +0,0 @@
1
- import { performance } from "node:perf_hooks";
2
-
3
- import type {
4
- TenantClientTelemetry,
5
- TenantClientTelemetryConfig,
6
- TenantClientTelemetryEvent,
7
- } from "./observability.js";
8
- import { createTenantClientTelemetry, isTenantClientTelemetry } from "./observability.js";
9
- import { validateUUID } from "./validation.js";
10
-
11
- /**
12
- * @qnsp/tenant-sdk
13
- *
14
- * TypeScript SDK client for the QNSP tenant-service API.
15
- * Provides a high-level interface for tenant lifecycle and subscription management.
16
- */
17
-
18
- export interface TenantClientConfig {
19
- readonly baseUrl: string;
20
- readonly apiKey: string;
21
- readonly timeoutMs?: number;
22
- readonly telemetry?: TenantClientTelemetry | TenantClientTelemetryConfig;
23
- readonly maxRetries?: number;
24
- readonly retryDelayMs?: number;
25
- }
26
-
27
- type InternalTenantClientConfig = {
28
- readonly baseUrl: string;
29
- readonly apiKey: string;
30
- readonly timeoutMs: number;
31
- readonly maxRetries: number;
32
- readonly retryDelayMs: number;
33
- };
34
-
35
- export type TenantStatus = "active" | "suspended" | "pending" | "deleted";
36
-
37
- export type HsmMode = "none" | "supported" | "required";
38
-
39
- /**
40
- * Crypto policy tier determines which PQC algorithms are allowed.
41
- * Maps to tenant_crypto_policies.policy_tier in tenant-service.
42
- */
43
- export type CryptoPolicyTier = "default" | "strict" | "maximum" | "government";
44
-
45
- /**
46
- * Crypto policy v1 uses profiles + tiers (evidence-first model).
47
- */
48
- export type QnspTier =
49
- | "TIER0_LEGACY"
50
- | "TIER1_APPROVED"
51
- | "TIER2_HIGH_ASSURANCE"
52
- | "TIER3_DIVERSITY"
53
- | "TIER4_EXPERIMENTAL";
54
-
55
- export type TenantType =
56
- | "FREE_FOREVER"
57
- | "DEV_STARTER"
58
- | "DEV_PRO"
59
- | "DEV_ELITE"
60
- | "BUSINESS_TEAM"
61
- | "BUSINESS_ADVANCED"
62
- | "BUSINESS_ELITE"
63
- | "ENTERPRISE_STANDARD"
64
- | "ENTERPRISE_PRO"
65
- | "ENTERPRISE_ELITE"
66
- | "PUBLIC_SECTOR";
67
-
68
- export type CryptoProfileId =
69
- | "gov-high-assurance"
70
- | "defense-long-life-data"
71
- | "financial-hybrid-pqc"
72
- | "research-eval";
73
-
74
- export type TenantCryptoPolicyV1Action = "CREATE" | "UPDATE" | "ROLLBACK";
75
-
76
- export interface CryptoPolicyV1 {
77
- readonly version: "v1";
78
- readonly tenantType: TenantType;
79
- readonly profile: CryptoProfileId;
80
- readonly enabledTiers: readonly QnspTier[];
81
- readonly tier0Expiry?: string;
82
- readonly tier4Acknowledgement?: {
83
- readonly nonCompliant: true;
84
- readonly noProductionSecrets: true;
85
- readonly approvedBy: string;
86
- readonly approvedAt: string;
87
- };
88
- readonly overrides?: {
89
- readonly allowFalcon?: boolean;
90
- };
91
- readonly requirements: {
92
- readonly fipsAligned: boolean;
93
- readonly evidenceRequired: boolean;
94
- readonly cryptoAgilityMetadataRequired: boolean;
95
- readonly statefulLifecycleGuards: boolean;
96
- readonly downgradeDetection: boolean;
97
- };
98
- }
99
-
100
- export interface TenantCryptoPolicyV1Record {
101
- readonly id: string;
102
- readonly tenantId: string;
103
- readonly version: "v1";
104
- readonly policy: CryptoPolicyV1;
105
- readonly policyHash: string;
106
- readonly etag: string;
107
- readonly createdAt: string;
108
- readonly createdByPrincipal: string;
109
- readonly createdByIp: string | null;
110
- readonly updatedAt: string;
111
- readonly updatedByPrincipal: string;
112
- readonly updatedByIp: string | null;
113
- }
114
-
115
- export interface TenantCryptoPolicyV1HistoryRecord {
116
- readonly id: string;
117
- readonly tenantId: string;
118
- readonly version: "v1";
119
- readonly policy: CryptoPolicyV1;
120
- readonly policyHash: string;
121
- readonly action: TenantCryptoPolicyV1Action;
122
- readonly reason: string | null;
123
- readonly changedAt: string;
124
- readonly changedByPrincipal: string;
125
- readonly changedByIp: string | null;
126
- }
127
-
128
- export interface TenantCryptoPolicyV1HistoryResponse {
129
- readonly tenantId: string;
130
- readonly items: readonly TenantCryptoPolicyV1HistoryRecord[];
131
- }
132
-
133
- /**
134
- * Tenant crypto policy configuration.
135
- */
136
- export interface TenantCryptoPolicy {
137
- readonly tenantId: string;
138
- readonly policyTier: CryptoPolicyTier;
139
- readonly customAllowedKemAlgorithms: readonly string[] | null;
140
- readonly customAllowedSignatureAlgorithms: readonly string[] | null;
141
- readonly customAllowedSymmetricAlgorithms?: readonly string[] | null;
142
- readonly customForbiddenAlgorithms?: readonly string[] | null;
143
- readonly requireHsmForRootKeys: boolean;
144
- readonly maxKeyAgeDays: number;
145
- readonly enforcementMode?: "audit" | "enforce";
146
- readonly createdAt: string;
147
- readonly updatedAt: string;
148
- }
149
-
150
- /**
151
- * Input for creating or updating a tenant crypto policy.
152
- */
153
- export interface TenantCryptoPolicyInput {
154
- readonly policyTier: CryptoPolicyTier;
155
- readonly customAllowedKemAlgorithms?: readonly string[] | null;
156
- readonly customAllowedSignatureAlgorithms?: readonly string[] | null;
157
- readonly requireHsmForRootKeys?: boolean;
158
- readonly maxKeyAgeDays?: number;
159
- }
160
-
161
- /**
162
- * Algorithm configuration per crypto policy tier.
163
- */
164
- export interface TierAlgorithmConfig {
165
- readonly kemAlgorithms: readonly string[];
166
- readonly signatureAlgorithms: readonly string[];
167
- readonly defaultKemAlgorithm: string;
168
- readonly defaultSignatureAlgorithm: string;
169
- }
170
-
171
- /**
172
- * Default algorithms per crypto policy tier.
173
- */
174
- export const CRYPTO_POLICY_ALGORITHMS: Record<CryptoPolicyTier, TierAlgorithmConfig> = {
175
- default: {
176
- kemAlgorithms: ["kyber-512", "kyber-768", "kyber-1024"],
177
- signatureAlgorithms: ["dilithium-2", "dilithium-3", "dilithium-5"],
178
- defaultKemAlgorithm: "kyber-768",
179
- defaultSignatureAlgorithm: "dilithium-3",
180
- },
181
- strict: {
182
- kemAlgorithms: ["kyber-768", "kyber-1024"],
183
- signatureAlgorithms: ["dilithium-3", "dilithium-5", "falcon-1024"],
184
- defaultKemAlgorithm: "kyber-768",
185
- defaultSignatureAlgorithm: "dilithium-3",
186
- },
187
- maximum: {
188
- kemAlgorithms: ["kyber-1024"],
189
- signatureAlgorithms: ["dilithium-5", "falcon-1024", "sphincs-shake-256f-simple"],
190
- defaultKemAlgorithm: "kyber-1024",
191
- defaultSignatureAlgorithm: "dilithium-5",
192
- },
193
- government: {
194
- kemAlgorithms: ["kyber-1024"],
195
- signatureAlgorithms: ["dilithium-5", "sphincs-shake-256f-simple"],
196
- defaultKemAlgorithm: "kyber-1024",
197
- defaultSignatureAlgorithm: "dilithium-5",
198
- },
199
- };
200
-
201
- /**
202
- * Mapping from internal algorithm names to NIST/standards display names.
203
- * Covers all 90 PQC algorithms supported by QNSP.
204
- * Canonical source: @qnsp/cryptography pqc-standards.ts ALGORITHM_NIST_NAMES
205
- */
206
- export const ALGORITHM_TO_NIST: Record<string, string> = {
207
- // FIPS 203 — ML-KEM
208
- "kyber-512": "ML-KEM-512",
209
- "kyber-768": "ML-KEM-768",
210
- "kyber-1024": "ML-KEM-1024",
211
- // FIPS 204 — ML-DSA
212
- "dilithium-2": "ML-DSA-44",
213
- "dilithium-3": "ML-DSA-65",
214
- "dilithium-5": "ML-DSA-87",
215
- // FIPS 205 — SLH-DSA (SHA-2 variants)
216
- "sphincs-sha2-128f-simple": "SLH-DSA-SHA2-128f",
217
- "sphincs-sha2-128s-simple": "SLH-DSA-SHA2-128s",
218
- "sphincs-sha2-192f-simple": "SLH-DSA-SHA2-192f",
219
- "sphincs-sha2-192s-simple": "SLH-DSA-SHA2-192s",
220
- "sphincs-sha2-256f-simple": "SLH-DSA-SHA2-256f",
221
- "sphincs-sha2-256s-simple": "SLH-DSA-SHA2-256s",
222
- // FIPS 205 — SLH-DSA (SHAKE variants)
223
- "sphincs-shake-128f-simple": "SLH-DSA-SHAKE-128f",
224
- "sphincs-shake-128s-simple": "SLH-DSA-SHAKE-128s",
225
- "sphincs-shake-192f-simple": "SLH-DSA-SHAKE-192f",
226
- "sphincs-shake-192s-simple": "SLH-DSA-SHAKE-192s",
227
- "sphincs-shake-256f-simple": "SLH-DSA-SHAKE-256f",
228
- "sphincs-shake-256s-simple": "SLH-DSA-SHAKE-256s",
229
- // FN-DSA (FIPS 206 draft)
230
- "falcon-512": "FN-DSA-512",
231
- "falcon-1024": "FN-DSA-1024",
232
- // HQC (NIST selected March 2025)
233
- "hqc-128": "HQC-128",
234
- "hqc-192": "HQC-192",
235
- "hqc-256": "HQC-256",
236
- // BIKE (NIST Round 4)
237
- "bike-l1": "BIKE-L1",
238
- "bike-l3": "BIKE-L3",
239
- "bike-l5": "BIKE-L5",
240
- // Classic McEliece (ISO standard)
241
- "mceliece-348864": "Classic-McEliece-348864",
242
- "mceliece-460896": "Classic-McEliece-460896",
243
- "mceliece-6688128": "Classic-McEliece-6688128",
244
- "mceliece-6960119": "Classic-McEliece-6960119",
245
- "mceliece-8192128": "Classic-McEliece-8192128",
246
- // FrodoKEM (ISO standard)
247
- "frodokem-640-aes": "FrodoKEM-640-AES",
248
- "frodokem-640-shake": "FrodoKEM-640-SHAKE",
249
- "frodokem-976-aes": "FrodoKEM-976-AES",
250
- "frodokem-976-shake": "FrodoKEM-976-SHAKE",
251
- "frodokem-1344-aes": "FrodoKEM-1344-AES",
252
- "frodokem-1344-shake": "FrodoKEM-1344-SHAKE",
253
- // NTRU (lattice-based, re-added in liboqs 0.15)
254
- "ntru-hps-2048-509": "NTRU-HPS-2048-509",
255
- "ntru-hps-2048-677": "NTRU-HPS-2048-677",
256
- "ntru-hps-4096-821": "NTRU-HPS-4096-821",
257
- "ntru-hps-4096-1229": "NTRU-HPS-4096-1229",
258
- "ntru-hrss-701": "NTRU-HRSS-701",
259
- "ntru-hrss-1373": "NTRU-HRSS-1373",
260
- // NTRU-Prime
261
- sntrup761: "sntrup761",
262
- // MAYO (NIST Additional Signatures Round 2)
263
- "mayo-1": "MAYO-1",
264
- "mayo-2": "MAYO-2",
265
- "mayo-3": "MAYO-3",
266
- "mayo-5": "MAYO-5",
267
- // CROSS (NIST Additional Signatures Round 2)
268
- "cross-rsdp-128-balanced": "CROSS-RSDP-128-balanced",
269
- "cross-rsdp-128-fast": "CROSS-RSDP-128-fast",
270
- "cross-rsdp-128-small": "CROSS-RSDP-128-small",
271
- "cross-rsdp-192-balanced": "CROSS-RSDP-192-balanced",
272
- "cross-rsdp-192-fast": "CROSS-RSDP-192-fast",
273
- "cross-rsdp-192-small": "CROSS-RSDP-192-small",
274
- "cross-rsdp-256-balanced": "CROSS-RSDP-256-balanced",
275
- "cross-rsdp-256-fast": "CROSS-RSDP-256-fast",
276
- "cross-rsdp-256-small": "CROSS-RSDP-256-small",
277
- "cross-rsdpg-128-balanced": "CROSS-RSDPG-128-balanced",
278
- "cross-rsdpg-128-fast": "CROSS-RSDPG-128-fast",
279
- "cross-rsdpg-128-small": "CROSS-RSDPG-128-small",
280
- "cross-rsdpg-192-balanced": "CROSS-RSDPG-192-balanced",
281
- "cross-rsdpg-192-fast": "CROSS-RSDPG-192-fast",
282
- "cross-rsdpg-192-small": "CROSS-RSDPG-192-small",
283
- "cross-rsdpg-256-balanced": "CROSS-RSDPG-256-balanced",
284
- "cross-rsdpg-256-fast": "CROSS-RSDPG-256-fast",
285
- "cross-rsdpg-256-small": "CROSS-RSDPG-256-small",
286
- // UOV (NIST Additional Signatures Round 2)
287
- "ov-Is": "UOV-Is",
288
- "ov-Ip": "UOV-Ip",
289
- "ov-III": "UOV-III",
290
- "ov-V": "UOV-V",
291
- "ov-Is-pkc": "UOV-Is-pkc",
292
- "ov-Ip-pkc": "UOV-Ip-pkc",
293
- "ov-III-pkc": "UOV-III-pkc",
294
- "ov-V-pkc": "UOV-V-pkc",
295
- "ov-Is-pkc-skc": "UOV-Is-pkc-skc",
296
- "ov-Ip-pkc-skc": "UOV-Ip-pkc-skc",
297
- "ov-III-pkc-skc": "UOV-III-pkc-skc",
298
- "ov-V-pkc-skc": "UOV-V-pkc-skc",
299
- // SNOVA (NIST Additional Signatures Round 2, liboqs 0.14+)
300
- "snova-24-5-4": "SNOVA-24-5-4",
301
- "snova-24-5-4-shake": "SNOVA-24-5-4-SHAKE",
302
- "snova-24-5-4-esk": "SNOVA-24-5-4-ESK",
303
- "snova-24-5-4-shake-esk": "SNOVA-24-5-4-SHAKE-ESK",
304
- "snova-25-8-3": "SNOVA-25-8-3",
305
- "snova-37-17-2": "SNOVA-37-17-2",
306
- "snova-37-8-4": "SNOVA-37-8-4",
307
- "snova-24-5-5": "SNOVA-24-5-5",
308
- "snova-56-25-2": "SNOVA-56-25-2",
309
- "snova-49-11-3": "SNOVA-49-11-3",
310
- "snova-60-10-4": "SNOVA-60-10-4",
311
- "snova-29-6-5": "SNOVA-29-6-5",
312
- };
313
-
314
- /**
315
- * Convert internal algorithm name to NIST standardized name.
316
- */
317
- export function toNistAlgorithmName(internal: string): string {
318
- return ALGORITHM_TO_NIST[internal] ?? internal;
319
- }
320
-
321
- /**
322
- * Get algorithm config for a crypto policy tier.
323
- */
324
- export function getAlgorithmConfigForTier(tier: CryptoPolicyTier): TierAlgorithmConfig {
325
- return CRYPTO_POLICY_ALGORITHMS[tier];
326
- }
327
-
328
- export interface TenantSecurityEnvelope {
329
- readonly controlPlaneTokenSha256: string | null;
330
- readonly pqcSignatures: readonly {
331
- readonly provider: string;
332
- readonly algorithm: string;
333
- readonly value: string;
334
- readonly publicKey: string;
335
- }[];
336
- readonly hardwareProvider: string | null;
337
- readonly attestationStatus: string | null;
338
- readonly attestationProof: string | null;
339
- }
340
-
341
- export interface TenantSignature {
342
- readonly provider: string;
343
- readonly algorithm: string;
344
- readonly value: string;
345
- readonly publicKey: string;
346
- }
347
-
348
- export interface TenantDomain {
349
- readonly id: string;
350
- readonly domain: string;
351
- readonly verified: boolean;
352
- readonly createdAt: string;
353
- readonly updatedAt: string;
354
- }
355
-
356
- export interface Tenant {
357
- readonly id: string;
358
- readonly name: string;
359
- readonly slug: string;
360
- readonly status: TenantStatus;
361
- readonly plan: string;
362
- readonly region: string;
363
- readonly complianceTags: readonly string[];
364
- readonly hsmMode: HsmMode;
365
- readonly metadata: Record<string, unknown>;
366
- readonly security: TenantSecurityEnvelope;
367
- readonly domains: readonly TenantDomain[];
368
- readonly createdAt: string;
369
- readonly updatedAt: string;
370
- }
371
-
372
- export interface CreateTenantRequest {
373
- readonly name: string;
374
- readonly slug: string;
375
- readonly plan?: string;
376
- readonly region?: string;
377
- readonly complianceTags?: readonly string[];
378
- readonly hsmMode?: HsmMode;
379
- readonly metadata?: Record<string, unknown>;
380
- readonly domains?: readonly {
381
- readonly domain: string;
382
- readonly verified?: boolean;
383
- }[];
384
- readonly security: TenantSecurityEnvelope;
385
- readonly signature?: TenantSignature;
386
- }
387
-
388
- export interface UpdateTenantRequest {
389
- readonly plan?: string;
390
- readonly status?: TenantStatus;
391
- readonly complianceTags?: readonly string[];
392
- readonly hsmMode?: HsmMode;
393
- readonly metadata?: Record<string, unknown>;
394
- readonly security: TenantSecurityEnvelope;
395
- readonly signature?: TenantSignature;
396
- }
397
-
398
- export interface ListTenantsResponse {
399
- readonly items: readonly Tenant[];
400
- readonly nextCursor: string | null;
401
- }
402
-
403
- interface RequestOptions {
404
- readonly body?: unknown;
405
- readonly headers?: Record<string, string>;
406
- readonly signal?: AbortSignal;
407
- readonly operation?: string;
408
- readonly telemetryRoute?: string;
409
- readonly telemetryTarget?: string;
410
- }
411
-
412
- export class TenantClient {
413
- private readonly config: InternalTenantClientConfig;
414
- private readonly telemetry: TenantClientTelemetry | null;
415
- private readonly targetService: string;
416
-
417
- constructor(config: TenantClientConfig) {
418
- if (!config.apiKey || config.apiKey.trim().length === 0) {
419
- throw new Error(
420
- "QNSP Tenant SDK: apiKey is required. " +
421
- "Get your free API key at https://cloud.qnsp.cuilabs.io/signup — " +
422
- "no credit card required (FREE tier: 5 GB storage, 2,000 API calls/month). " +
423
- "Docs: https://docs.qnsp.cuilabs.io/sdk/tenant-sdk",
424
- );
425
- }
426
-
427
- const baseUrl = config.baseUrl.replace(/\/$/, "");
428
-
429
- // Enforce HTTPS in production (allow HTTP only for localhost in development)
430
- if (!baseUrl.startsWith("https://")) {
431
- const isLocalhost =
432
- baseUrl.startsWith("http://localhost") || baseUrl.startsWith("http://127.0.0.1");
433
- const isDevelopment =
434
- process.env["NODE_ENV"] === "development" || process.env["NODE_ENV"] === "test";
435
- if (!isLocalhost || !isDevelopment) {
436
- throw new Error(
437
- "baseUrl must use HTTPS in production. HTTP is only allowed for localhost in development.",
438
- );
439
- }
440
- }
441
-
442
- this.config = {
443
- baseUrl,
444
- apiKey: config.apiKey,
445
- timeoutMs: config.timeoutMs ?? 30_000,
446
- maxRetries: config.maxRetries ?? 3,
447
- retryDelayMs: config.retryDelayMs ?? 1_000,
448
- };
449
-
450
- this.telemetry = config.telemetry
451
- ? isTenantClientTelemetry(config.telemetry)
452
- ? config.telemetry
453
- : createTenantClientTelemetry(config.telemetry)
454
- : null;
455
-
456
- try {
457
- this.targetService = new URL(this.config.baseUrl).host;
458
- } catch {
459
- this.targetService = "tenant-service";
460
- }
461
- }
462
-
463
- private async request<T>(method: string, path: string, options?: RequestOptions): Promise<T> {
464
- return this.requestWithRetry<T>(method, path, options, 0);
465
- }
466
-
467
- private async requestWithRetry<T>(
468
- method: string,
469
- path: string,
470
- options: RequestOptions | undefined,
471
- attempt: number,
472
- ): Promise<T> {
473
- const url = `${this.config.baseUrl}${path}`;
474
- const headers: Record<string, string> = {
475
- "Content-Type": "application/json",
476
- ...options?.headers,
477
- };
478
-
479
- headers["Authorization"] = `Bearer ${this.config.apiKey}`;
480
-
481
- const controller = new AbortController();
482
- const timeoutId = setTimeout(() => controller.abort(), this.config.timeoutMs);
483
- const signal = options?.signal ?? controller.signal;
484
- const route = options?.telemetryRoute ?? new URL(path, this.config.baseUrl).pathname;
485
- const target = options?.telemetryTarget ?? this.targetService;
486
- const start = performance.now();
487
- let status: "ok" | "error" = "ok";
488
- let httpStatus: number | undefined;
489
- let errorMessage: string | undefined;
490
-
491
- try {
492
- const init: RequestInit = {
493
- method,
494
- headers,
495
- signal,
496
- };
497
-
498
- if (options?.body !== undefined) {
499
- init.body = JSON.stringify(options.body);
500
- }
501
-
502
- const response = await fetch(url, init);
503
-
504
- clearTimeout(timeoutId);
505
- httpStatus = response.status;
506
-
507
- // Handle rate limiting (429) with retry logic
508
- if (response.status === 429) {
509
- if (attempt < this.config.maxRetries) {
510
- const retryAfterHeader = response.headers.get("Retry-After");
511
- let delayMs = this.config.retryDelayMs;
512
-
513
- if (retryAfterHeader) {
514
- const retryAfterSeconds = Number.parseInt(retryAfterHeader, 10);
515
- if (!Number.isNaN(retryAfterSeconds) && retryAfterSeconds > 0) {
516
- delayMs = retryAfterSeconds * 1_000;
517
- }
518
- } else {
519
- // Exponential backoff: 2^attempt * baseDelay, capped at 30 seconds
520
- delayMs = Math.min(2 ** attempt * this.config.retryDelayMs, 30_000);
521
- }
522
-
523
- await new Promise((resolve) => setTimeout(resolve, delayMs));
524
- return this.requestWithRetry<T>(method, path, options, attempt + 1);
525
- }
526
-
527
- status = "error";
528
- errorMessage = `HTTP ${response.status}`;
529
- throw new Error(
530
- `Tenant API error: Rate limit exceeded after ${this.config.maxRetries} retries`,
531
- );
532
- }
533
-
534
- if (!response.ok) {
535
- status = "error";
536
- // Sanitize error message to prevent information disclosure
537
- // Don't include full response text in error to avoid leaking sensitive data
538
- errorMessage = `HTTP ${response.status}`;
539
- throw new Error(`Tenant API error: ${response.status} ${response.statusText}`);
540
- }
541
-
542
- if (response.status === 204) {
543
- return undefined as T;
544
- }
545
-
546
- return (await response.json()) as T;
547
- } catch (error) {
548
- clearTimeout(timeoutId);
549
- status = "error";
550
- if (!errorMessage && error instanceof Error) {
551
- errorMessage = error.message;
552
- }
553
- if (error instanceof Error && error.name === "AbortError") {
554
- errorMessage = `timeout after ${this.config.timeoutMs}ms`;
555
- throw new Error(`Request timeout after ${this.config.timeoutMs}ms`);
556
- }
557
- throw error;
558
- } finally {
559
- const durationMs = performance.now() - start;
560
- const event: TenantClientTelemetryEvent = {
561
- operation: options?.operation ?? `${method} ${route}`,
562
- method,
563
- route,
564
- target,
565
- status,
566
- durationMs,
567
- ...(typeof httpStatus === "number" ? { httpStatus } : {}),
568
- ...(status === "error" && errorMessage ? { error: errorMessage } : {}),
569
- };
570
- this.recordTelemetryEvent(event);
571
- }
572
- }
573
-
574
- private recordTelemetryEvent(event: TenantClientTelemetryEvent): void {
575
- if (!this.telemetry) {
576
- return;
577
- }
578
- this.telemetry.record(event);
579
- }
580
-
581
- /**
582
- * Create a new tenant.
583
- * Requires PQC-signed security envelope and optional signature.
584
- */
585
- async createTenant(request: CreateTenantRequest): Promise<Tenant> {
586
- // Validation is handled by the service, but we validate format here for early feedback
587
- return this.request<Tenant>("POST", "/tenant/v1/tenants", {
588
- body: {
589
- name: request.name,
590
- slug: request.slug,
591
- ...(request.plan !== undefined ? { plan: request.plan } : {}),
592
- ...(request.region !== undefined ? { region: request.region } : {}),
593
- ...(request.complianceTags !== undefined ? { complianceTags: request.complianceTags } : {}),
594
- ...(request.hsmMode !== undefined ? { hsmMode: request.hsmMode } : {}),
595
- ...(request.metadata !== undefined ? { metadata: request.metadata } : {}),
596
- ...(request.domains !== undefined ? { domains: request.domains } : {}),
597
- security: request.security,
598
- ...(request.signature !== undefined ? { signature: request.signature } : {}),
599
- },
600
- operation: "createTenant",
601
- });
602
- }
603
-
604
- /**
605
- * Update a tenant's plan, status, compliance tags, or metadata.
606
- * Requires PQC-signed security envelope and optional signature.
607
- */
608
- async updateTenant(id: string, request: UpdateTenantRequest): Promise<Tenant> {
609
- validateUUID(id, "id");
610
-
611
- return this.request<Tenant>("PATCH", `/tenant/v1/tenants/${id}`, {
612
- body: {
613
- ...(request.plan !== undefined ? { plan: request.plan } : {}),
614
- ...(request.status !== undefined ? { status: request.status } : {}),
615
- ...(request.complianceTags !== undefined ? { complianceTags: request.complianceTags } : {}),
616
- ...(request.hsmMode !== undefined ? { hsmMode: request.hsmMode } : {}),
617
- ...(request.metadata !== undefined ? { metadata: request.metadata } : {}),
618
- security: request.security,
619
- ...(request.signature !== undefined ? { signature: request.signature } : {}),
620
- },
621
- operation: "updateTenant",
622
- });
623
- }
624
-
625
- /**
626
- * Get a tenant by ID.
627
- * Returns the tenant with domains and security envelope.
628
- */
629
- async getTenant(id: string): Promise<Tenant> {
630
- validateUUID(id, "id");
631
-
632
- return this.request<Tenant>("GET", `/tenant/v1/tenants/${id}`, {
633
- operation: "getTenant",
634
- });
635
- }
636
-
637
- /**
638
- * List tenants with cursor-based pagination.
639
- * Returns a list of tenants and an optional next cursor for pagination.
640
- */
641
- async listTenants(options?: {
642
- readonly limit?: number;
643
- readonly cursor?: string;
644
- }): Promise<ListTenantsResponse> {
645
- const params = new URLSearchParams();
646
- if (options?.limit !== undefined) {
647
- params.set("limit", String(options.limit));
648
- }
649
- if (options?.cursor !== undefined) {
650
- params.set("cursor", options.cursor);
651
- }
652
- const queryString = params.toString();
653
- const path = queryString ? `/tenant/v1/tenants?${queryString}` : "/tenant/v1/tenants";
654
-
655
- return this.request<ListTenantsResponse>("GET", path, {
656
- operation: "listTenants",
657
- });
658
- }
659
-
660
- /**
661
- * Get the crypto policy for a tenant.
662
- * Returns the tenant's crypto policy configuration including allowed algorithms.
663
- * If no policy exists, a default policy is created and returned.
664
- */
665
- async getTenantCryptoPolicy(tenantId: string): Promise<TenantCryptoPolicy> {
666
- validateUUID(tenantId, "tenantId");
667
-
668
- return this.request<TenantCryptoPolicy>("GET", `/tenant/v1/tenants/${tenantId}/crypto-policy`, {
669
- operation: "getTenantCryptoPolicy",
670
- });
671
- }
672
-
673
- /**
674
- * Get the v1 crypto policy for a tenant (profiles + tiers model).
675
- * If no policy exists, a default policy is created and returned.
676
- */
677
- async getTenantCryptoPolicyV1(tenantId: string): Promise<TenantCryptoPolicyV1Record> {
678
- validateUUID(tenantId, "tenantId");
679
-
680
- return this.request<TenantCryptoPolicyV1Record>(
681
- "GET",
682
- `/tenant/v1/tenants/${tenantId}/crypto-policy-v1`,
683
- {
684
- operation: "getTenantCryptoPolicyV1",
685
- },
686
- );
687
- }
688
-
689
- /**
690
- * List v1 crypto policy history entries.
691
- */
692
- async listTenantCryptoPolicyV1History(
693
- tenantId: string,
694
- options?: { readonly limit?: number },
695
- ): Promise<TenantCryptoPolicyV1HistoryResponse> {
696
- validateUUID(tenantId, "tenantId");
697
- const params = new URLSearchParams();
698
- if (options?.limit !== undefined) {
699
- params.set("limit", String(options.limit));
700
- }
701
- const query = params.toString();
702
- const path = query
703
- ? `/tenant/v1/tenants/${tenantId}/crypto-policy-v1/history?${query}`
704
- : `/tenant/v1/tenants/${tenantId}/crypto-policy-v1/history`;
705
-
706
- return this.request<TenantCryptoPolicyV1HistoryResponse>("GET", path, {
707
- operation: "listTenantCryptoPolicyV1History",
708
- });
709
- }
710
-
711
- /**
712
- * Create or update the crypto policy for a tenant.
713
- * Sets the policy tier and optional custom algorithm restrictions.
714
- */
715
- async upsertTenantCryptoPolicy(
716
- tenantId: string,
717
- policy: TenantCryptoPolicyInput,
718
- ): Promise<TenantCryptoPolicy> {
719
- validateUUID(tenantId, "tenantId");
720
-
721
- return this.request<TenantCryptoPolicy>("PUT", `/tenant/v1/tenants/${tenantId}/crypto-policy`, {
722
- body: {
723
- policyTier: policy.policyTier,
724
- ...(policy.customAllowedKemAlgorithms !== undefined
725
- ? { customAllowedKemAlgorithms: policy.customAllowedKemAlgorithms }
726
- : {}),
727
- ...(policy.customAllowedSignatureAlgorithms !== undefined
728
- ? { customAllowedSignatureAlgorithms: policy.customAllowedSignatureAlgorithms }
729
- : {}),
730
- ...(policy.requireHsmForRootKeys !== undefined
731
- ? { requireHsmForRootKeys: policy.requireHsmForRootKeys }
732
- : {}),
733
- ...(policy.maxKeyAgeDays !== undefined ? { maxKeyAgeDays: policy.maxKeyAgeDays } : {}),
734
- },
735
- operation: "upsertTenantCryptoPolicy",
736
- });
737
- }
738
-
739
- /**
740
- * Update the v1 crypto policy for a tenant (requires If-Match with current ETag).
741
- */
742
- async updateTenantCryptoPolicyV1(
743
- tenantId: string,
744
- policy: CryptoPolicyV1,
745
- etag: string,
746
- ): Promise<TenantCryptoPolicyV1Record> {
747
- validateUUID(tenantId, "tenantId");
748
- if (!etag) {
749
- throw new Error("etag is required for updateTenantCryptoPolicyV1");
750
- }
751
-
752
- return this.request<TenantCryptoPolicyV1Record>(
753
- "PUT",
754
- `/tenant/v1/tenants/${tenantId}/crypto-policy-v1`,
755
- {
756
- body: { policy },
757
- headers: {
758
- "If-Match": etag,
759
- },
760
- operation: "updateTenantCryptoPolicyV1",
761
- },
762
- );
763
- }
764
-
765
- /**
766
- * Enable Tier0 legacy algorithms (time-bounded) for a tenant.
767
- */
768
- async enableTier0Legacy(
769
- tenantId: string,
770
- input: { readonly expiry: string },
771
- etag: string,
772
- ): Promise<TenantCryptoPolicyV1Record> {
773
- validateUUID(tenantId, "tenantId");
774
- if (!etag) {
775
- throw new Error("etag is required for enableTier0Legacy");
776
- }
777
-
778
- return this.request<TenantCryptoPolicyV1Record>(
779
- "POST",
780
- `/tenant/v1/tenants/${tenantId}/crypto-policy-v1/tier0/enable`,
781
- {
782
- body: { expiry: input.expiry },
783
- headers: {
784
- "If-Match": etag,
785
- },
786
- operation: "enableTier0Legacy",
787
- },
788
- );
789
- }
790
-
791
- /**
792
- * Disable Tier0 legacy algorithms for a tenant.
793
- */
794
- async disableTier0Legacy(tenantId: string, etag: string): Promise<TenantCryptoPolicyV1Record> {
795
- validateUUID(tenantId, "tenantId");
796
- if (!etag) {
797
- throw new Error("etag is required for disableTier0Legacy");
798
- }
799
-
800
- return this.request<TenantCryptoPolicyV1Record>(
801
- "POST",
802
- `/tenant/v1/tenants/${tenantId}/crypto-policy-v1/tier0/disable`,
803
- {
804
- body: {},
805
- headers: {
806
- "If-Match": etag,
807
- },
808
- operation: "disableTier0Legacy",
809
- },
810
- );
811
- }
812
-
813
- /**
814
- * Enable Tier4 experimental algorithms with acknowledgement.
815
- */
816
- async enableTier4Experimental(
817
- tenantId: string,
818
- input: { readonly approvedBy: string },
819
- etag: string,
820
- ): Promise<TenantCryptoPolicyV1Record> {
821
- validateUUID(tenantId, "tenantId");
822
- if (!etag) {
823
- throw new Error("etag is required for enableTier4Experimental");
824
- }
825
-
826
- return this.request<TenantCryptoPolicyV1Record>(
827
- "POST",
828
- `/tenant/v1/tenants/${tenantId}/crypto-policy-v1/tier4/enable`,
829
- {
830
- body: { approvedBy: input.approvedBy },
831
- headers: {
832
- "If-Match": etag,
833
- },
834
- operation: "enableTier4Experimental",
835
- },
836
- );
837
- }
838
-
839
- /**
840
- * Roll back the v1 crypto policy to a previous history record or policy hash.
841
- */
842
- async rollbackTenantCryptoPolicyV1(
843
- tenantId: string,
844
- input: { readonly historyId?: string; readonly policyHash?: string },
845
- etag: string,
846
- ): Promise<TenantCryptoPolicyV1Record> {
847
- validateUUID(tenantId, "tenantId");
848
- if (!etag) {
849
- throw new Error("etag is required for rollbackTenantCryptoPolicyV1");
850
- }
851
- if (!input.historyId && !input.policyHash) {
852
- throw new Error("historyId or policyHash is required for rollbackTenantCryptoPolicyV1");
853
- }
854
-
855
- return this.request<TenantCryptoPolicyV1Record>(
856
- "POST",
857
- `/tenant/v1/tenants/${tenantId}/crypto-policy-v1/rollback`,
858
- {
859
- body: {
860
- ...(input.historyId ? { historyId: input.historyId } : {}),
861
- ...(input.policyHash ? { policyHash: input.policyHash } : {}),
862
- },
863
- headers: {
864
- "If-Match": etag,
865
- },
866
- operation: "rollbackTenantCryptoPolicyV1",
867
- },
868
- );
869
- }
870
-
871
- /**
872
- * Get the allowed KEM algorithms for a tenant based on their crypto policy.
873
- * Convenience method that fetches the policy and returns the allowed algorithms.
874
- */
875
- async getAllowedKemAlgorithms(tenantId: string): Promise<readonly string[]> {
876
- const policy = await this.getTenantCryptoPolicy(tenantId);
877
- if (policy.customAllowedKemAlgorithms && policy.customAllowedKemAlgorithms.length > 0) {
878
- return policy.customAllowedKemAlgorithms;
879
- }
880
- return CRYPTO_POLICY_ALGORITHMS[policy.policyTier].kemAlgorithms;
881
- }
882
-
883
- /**
884
- * Get the allowed signature algorithms for a tenant based on their crypto policy.
885
- * Convenience method that fetches the policy and returns the allowed algorithms.
886
- */
887
- async getAllowedSignatureAlgorithms(tenantId: string): Promise<readonly string[]> {
888
- const policy = await this.getTenantCryptoPolicy(tenantId);
889
- if (
890
- policy.customAllowedSignatureAlgorithms &&
891
- policy.customAllowedSignatureAlgorithms.length > 0
892
- ) {
893
- return policy.customAllowedSignatureAlgorithms;
894
- }
895
- return CRYPTO_POLICY_ALGORITHMS[policy.policyTier].signatureAlgorithms;
896
- }
897
-
898
- /**
899
- * Get the default KEM algorithm for a tenant based on their crypto policy tier.
900
- */
901
- async getDefaultKemAlgorithm(tenantId: string): Promise<string> {
902
- const policy = await this.getTenantCryptoPolicy(tenantId);
903
- return CRYPTO_POLICY_ALGORITHMS[policy.policyTier].defaultKemAlgorithm;
904
- }
905
-
906
- /**
907
- * Get the default signature algorithm for a tenant based on their crypto policy tier.
908
- */
909
- async getDefaultSignatureAlgorithm(tenantId: string): Promise<string> {
910
- const policy = await this.getTenantCryptoPolicy(tenantId);
911
- return CRYPTO_POLICY_ALGORITHMS[policy.policyTier].defaultSignatureAlgorithm;
912
- }
913
- }
914
-
915
- export * from "./observability.js";
916
- export * from "./validation.js";