UNPKG

@confluentinc/schemaregistry

Version:
130 lines (129 loc) 4.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockDekRegistryClient = void 0; const constants_1 = require("./constants"); const json_stringify_deterministic_1 = __importDefault(require("json-stringify-deterministic")); const rest_error_1 = require("../../../rest-error"); class MockDekRegistryClient { constructor(config) { this.clientConfig = config; this.kekCache = new Map(); this.dekCache = new Map(); } config() { return this.clientConfig; } async registerKek(name, kmsType, kmsKeyId, shared, kmsProps, doc) { const cacheKey = (0, json_stringify_deterministic_1.default)({ name, deleted: false }); const cachedKek = this.kekCache.get(cacheKey); if (cachedKek) { return cachedKek; } const kek = { name, kmsType, kmsKeyId, ...kmsProps && { kmsProps }, ...doc && { doc }, shared }; this.kekCache.set(cacheKey, kek); return kek; } async getKek(name, deleted = false) { const cacheKey = (0, json_stringify_deterministic_1.default)({ name, deleted }); const cachedKek = this.kekCache.get(cacheKey); if (cachedKek && (!cachedKek.deleted || deleted)) { return cachedKek; } throw new rest_error_1.RestError(`Kek not found: ${name}`, 404, 40400); } async registerDek(kekName, subject, algorithm, version = 1, encryptedKeyMaterial) { const cacheKey = (0, json_stringify_deterministic_1.default)({ kekName, subject, version, algorithm, deleted: false }); const cachedDek = this.dekCache.get(cacheKey); if (cachedDek) { return cachedDek; } const dek = { kekName, subject, algorithm, ...encryptedKeyMaterial && { encryptedKeyMaterial }, version, ts: constants_1.MOCK_TS }; this.dekCache.set(cacheKey, dek); return dek; } async getDek(kekName, subject, algorithm, version = 1, deleted = false) { if (version === -1) { let latestVersion = 0; for (const key of this.dekCache.keys()) { const parsedKey = JSON.parse(key); if (parsedKey.kekName === kekName && parsedKey.subject === subject && parsedKey.algorithm === algorithm && parsedKey.deleted === deleted) { latestVersion = Math.max(latestVersion, parsedKey.version); } } if (latestVersion === 0) { throw new rest_error_1.RestError(`Dek not found: ${subject}`, 404, 40400); } version = latestVersion; } const cacheKey = (0, json_stringify_deterministic_1.default)({ kekName, subject, version, algorithm, deleted: false }); const cachedDek = this.dekCache.get(cacheKey); if (cachedDek) { return cachedDek; } throw new rest_error_1.RestError(`Dek not found: ${subject}`, 404, 40400); } async getDekEncryptedKeyMaterialBytes(dek) { if (!dek.encryptedKeyMaterial) { return null; } if (!dek.encryptedKeyMaterialBytes) { try { const bytes = Buffer.from(dek.encryptedKeyMaterial, 'base64'); dek.encryptedKeyMaterialBytes = bytes; } catch (err) { if (err instanceof Error) { throw new Error(`Failed to decode base64 string: ${err.message}`); } throw new Error(`Unknown error: ${err}`); } } return dek.encryptedKeyMaterialBytes; } async getDekKeyMaterialBytes(dek) { if (!dek.keyMaterial) { return null; } if (!dek.keyMaterialBytes) { try { const bytes = Buffer.from(dek.keyMaterial, 'base64'); dek.keyMaterialBytes = bytes; } catch (err) { if (err instanceof Error) { throw new Error(`Failed to decode base64 string: ${err.message}`); } throw new Error(`Unknown error: ${err}`); } } return dek.keyMaterialBytes; } async setDekKeyMaterial(dek, keyMaterialBytes) { if (keyMaterialBytes) { const str = keyMaterialBytes.toString('base64'); dek.keyMaterial = str; } } async close() { return; } } exports.MockDekRegistryClient = MockDekRegistryClient;