@kuflow/kuflow-temporal-worker
Version:
Worker library used by KuFlow SDKs and Temporal.
111 lines • 4.8 kB
JavaScript
;
/**
* The MIT License
* Copyright © 2021-present KuFlow S.L.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.encryptionState = exports.EncryptionWrapper = exports.EncryptionState = exports.METADATA_VALUE_KUFLOW_ENCODING_ENCRYPTED = exports.METADATA_KEY_ENCODING_ENCRYPTED_KEY_ID = exports.METADATA_KEY_ENCODING = exports.HEADER_VALUE_KUFLOW_ENCODING_ENCRYPTED = exports.HEADER_KEY_KUFLOW_ENCODING_ENCRYPTED_KEY_ID = exports.HEADER_KEY_KUFLOW_ENCODING = void 0;
exports.retrieveEncryptionState = retrieveEncryptionState;
exports.isEncryptionRequired = isEncryptionRequired;
exports.addEncryptionEncoding = addEncryptionEncoding;
exports.markObjectsToBeEncrypted = markObjectsToBeEncrypted;
const common_1 = require("@temporalio/common");
exports.HEADER_KEY_KUFLOW_ENCODING = 'x-kuflow-encoding';
exports.HEADER_KEY_KUFLOW_ENCODING_ENCRYPTED_KEY_ID = 'x-kuflow-encoding-encrypted-key-id';
exports.HEADER_VALUE_KUFLOW_ENCODING_ENCRYPTED = 'binary/encrypted?vendor=KuFlow';
exports.METADATA_KEY_ENCODING = common_1.METADATA_ENCODING_KEY;
exports.METADATA_KEY_ENCODING_ENCRYPTED_KEY_ID = common_1.METADATA_ENCODING_KEY + '-encrypted-key-id';
exports.METADATA_VALUE_KUFLOW_ENCODING_ENCRYPTED = 'binary/encrypted?vendor=KuFlow';
class EncryptionState {
static empty() {
return new EncryptionState(undefined);
}
static of(keyId) {
return new EncryptionState(keyId);
}
_keyId = undefined;
get keyId() {
return this._keyId;
}
set keyId(value) {
this._keyId = value;
}
constructor(keyId) {
this._keyId = keyId;
}
merge(other) {
if (other != null) {
this._keyId = other.keyId;
}
else {
this._keyId = undefined;
}
}
}
exports.EncryptionState = EncryptionState;
function retrieveEncryptionState(header) {
if (!isEncryptionRequired(header)) {
return EncryptionState.empty();
}
const keyIdPayload = header[exports.HEADER_KEY_KUFLOW_ENCODING_ENCRYPTED_KEY_ID];
if (keyIdPayload == null) {
throw new common_1.ValueError(`Header ${exports.HEADER_KEY_KUFLOW_ENCODING_ENCRYPTED_KEY_ID} is required`);
}
const keyId = common_1.defaultPayloadConverter.fromPayload(keyIdPayload);
return EncryptionState.of(keyId);
}
function isEncryptionRequired(header) {
if (header[exports.HEADER_KEY_KUFLOW_ENCODING] == null) {
return false;
}
const value = common_1.defaultPayloadConverter.fromPayload(header[exports.HEADER_KEY_KUFLOW_ENCODING]);
return value === exports.HEADER_VALUE_KUFLOW_ENCODING_ENCRYPTED;
}
function addEncryptionEncoding(encryptionState, headers) {
if (encryptionState.keyId == null) {
return headers;
}
return {
...headers,
[exports.HEADER_KEY_KUFLOW_ENCODING]: common_1.defaultPayloadConverter.toPayload(exports.HEADER_VALUE_KUFLOW_ENCODING_ENCRYPTED),
[exports.HEADER_KEY_KUFLOW_ENCODING_ENCRYPTED_KEY_ID]: common_1.defaultPayloadConverter.toPayload(encryptionState.keyId),
};
}
function markObjectsToBeEncrypted(encryptionState, args) {
if (encryptionState.keyId == null) {
return args;
}
return args.map(arg => EncryptionWrapper.of(encryptionState, arg));
}
class EncryptionWrapper {
encryptionState;
value;
static of(encryptionState, value) {
return new EncryptionWrapper(encryptionState, value);
}
constructor(encryptionState, value) {
this.encryptionState = encryptionState;
this.value = value;
}
}
exports.EncryptionWrapper = EncryptionWrapper;
exports.encryptionState = EncryptionState.empty();
//# sourceMappingURL=kuflow-encryption-instrumentation.js.map