@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
108 lines (107 loc) • 4.16 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var ProtectedDataEncryptionKeyCache_exports = {};
__export(ProtectedDataEncryptionKeyCache_exports, {
ProtectedDataEncryptionKeyCache: () => ProtectedDataEncryptionKeyCache
});
module.exports = __toCommonJS(ProtectedDataEncryptionKeyCache_exports);
var import_node_crypto = require("node:crypto");
var import_ProtectedDataEncryptionKey = require("../EncryptionKey/ProtectedDataEncryptionKey.js");
var import_common = require("../../common/index.js");
var import_time = require("../../utils/time.js");
class ProtectedDataEncryptionKeyCache {
constructor(cacheTimeToLive) {
this.cacheTimeToLive = cacheTimeToLive;
this.cache = /* @__PURE__ */ new Map();
this.clearCacheOnTtlExpiry();
}
// key is JSON.stringify([encryptionKeyId, keyEncryptionKey.name, keyEncryptionKey.path, encryptedValue.toString("hex")])
cache;
// interval for clear cache to run
cacheRefresher;
get(key) {
if (!this.cache.has(key)) {
return void 0;
}
return this.cache.get(key)[1];
}
set(key, protectedDataEncryptionKey) {
if (this.cacheTimeToLive === 0) {
return;
}
this.cache.set(key, [/* @__PURE__ */ new Date(), protectedDataEncryptionKey]);
}
async clearCacheOnTtlExpiry() {
this.cacheRefresher = (0, import_time.startBackgroundTask)(async () => {
const now = /* @__PURE__ */ new Date();
for (const key of this.cache.keys()) {
if (now.getTime() - this.cache.get(key)[0].getTime() > this.cacheTimeToLive) {
this.cache.delete(key);
}
}
}, import_common.Constants.EncryptionCacheRefreshIntervalInMs);
}
async createProtectedDataEncryptionKey(name, keyEncryptionKey, encryptedValue) {
let rawKey;
let encryptedKey;
if (encryptedValue) {
rawKey = await keyEncryptionKey.unwrapEncryptionKey(encryptedValue);
encryptedKey = encryptedValue;
} else {
rawKey = this.generateColumnEncryptionKey();
encryptedKey = await keyEncryptionKey.wrapEncryptionKey(rawKey);
}
const newKey = new import_ProtectedDataEncryptionKey.ProtectedDataEncryptionKey(name, keyEncryptionKey, rawKey, encryptedKey);
if (this.cacheTimeToLive !== 0) {
const key = JSON.stringify([
name,
keyEncryptionKey.name,
keyEncryptionKey.path,
encryptedKey.toString("hex")
]);
this.set(key, newKey);
}
return newKey;
}
async getOrCreate(name, keyEncryptionKey, encryptedValue, forceRefresh) {
const encryptedValueBuffer = encryptedValue ? Buffer.from(encryptedValue) : void 0;
if (this.cacheTimeToLive === 0 || forceRefresh) {
return this.createProtectedDataEncryptionKey(name, keyEncryptionKey, encryptedValueBuffer);
}
if (encryptedValueBuffer) {
const key = JSON.stringify([
name,
keyEncryptionKey.name,
keyEncryptionKey.path,
encryptedValueBuffer.toString("hex")
]);
const protectedDataEncryptionKey = this.get(key);
if (protectedDataEncryptionKey) {
return protectedDataEncryptionKey;
}
}
return this.createProtectedDataEncryptionKey(name, keyEncryptionKey, encryptedValueBuffer);
}
generateColumnEncryptionKey() {
return (0, import_node_crypto.randomBytes)(32);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ProtectedDataEncryptionKeyCache
});