@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
86 lines (85 loc) • 3.51 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 EncryptionKeyStoreProvider_exports = {};
__export(EncryptionKeyStoreProvider_exports, {
EncryptionKeyStoreProvider: () => EncryptionKeyStoreProvider
});
module.exports = __toCommonJS(EncryptionKeyStoreProvider_exports);
var import_common = require("../common/index.js");
var import_time = require("../utils/time.js");
class EncryptionKeyStoreProvider {
constructor(keyEncryptionKeyResolver, cacheTimeToLive) {
this.keyEncryptionKeyResolver = keyEncryptionKeyResolver;
this.cacheTimeToLive = cacheTimeToLive;
this.keyEncryptionKeyResolver = keyEncryptionKeyResolver;
this.providerName = keyEncryptionKeyResolver.encryptionKeyResolverName;
this.unwrappedEncryptionKeyCache = {};
this.cacheTimeToLive = cacheTimeToLive;
this.clearCacheOnTtlExpiry();
}
RsaOaepEncryptionAlgorithm = "RSA-OAEP";
// interval for clear cache to run
cacheRefresher;
// cache to store the unwrapped encryption key. Key is the path of the encryption key
unwrappedEncryptionKeyCache;
providerName;
async wrapKey(encryptionKeyId, algorithm, key) {
const uInt8ArrayKey = new Uint8Array(key);
const wrappedEncryptionKey = await this.keyEncryptionKeyResolver.wrapKey(
encryptionKeyId,
algorithm,
uInt8ArrayKey
);
return Buffer.from(wrappedEncryptionKey);
}
async unwrapKey(encryptionKeyId, algorithm, wrappedKey) {
if (this.cacheTimeToLive === 0) {
const res = await this.keyEncryptionKeyResolver.unwrapKey(
encryptionKeyId,
algorithm,
wrappedKey
);
return Buffer.from(res);
}
if (!this.unwrappedEncryptionKeyCache[encryptionKeyId]) {
const wrappedKeyUint8Array = new Uint8Array(wrappedKey);
const plainEncryptionKey = await this.keyEncryptionKeyResolver.unwrapKey(
encryptionKeyId,
algorithm,
wrappedKeyUint8Array
);
const plainEncryptionKeyBuffer = Buffer.from(plainEncryptionKey);
this.unwrappedEncryptionKeyCache[encryptionKeyId] = [/* @__PURE__ */ new Date(), plainEncryptionKeyBuffer];
}
return this.unwrappedEncryptionKeyCache[encryptionKeyId][1];
}
async clearCacheOnTtlExpiry() {
this.cacheRefresher = (0, import_time.startBackgroundTask)(async () => {
const now = /* @__PURE__ */ new Date();
for (const key in this.unwrappedEncryptionKeyCache) {
if (now.getTime() - this.unwrappedEncryptionKeyCache[key][0].getTime() > this.cacheTimeToLive) {
delete this.unwrappedEncryptionKeyCache[key];
}
}
}, import_common.Constants.EncryptionCacheRefreshIntervalInMs);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
EncryptionKeyStoreProvider
});