liveperson-functions-cli
Version:
LivePerson Functions CLI
116 lines • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CsdsClient = void 0;
const fs = require("fs");
const os = require("os");
const path_1 = require("path");
const systeminformation_1 = require("systeminformation");
const crypto = require("crypto");
const errorCodes_1 = require("../errors/errorCodes");
const internalError_1 = require("../errors/internalError");
const httpClient_1 = require("../http-client/httpClient");
const const_1 = require("../shared/const");
/**
* CSDS client that internally uses the Toolbelt.HTTPClient because it has the right proxy settings.
*/
class CsdsClient {
/**
* @param ttlInSeconds TTL of the domains cache in seconds
*/
constructor(ttlInSeconds = 600, accountId = undefined, lastCacheTimestamp = 0) {
this.ttlInSeconds = ttlInSeconds;
this.accountId = accountId;
this.lastCacheTimestamp = lastCacheTimestamp;
this.domains = [];
}
/**
* Get the host for a CSDS service name.
* The CsdsClient will get all hosts for the account and cache them as configured in ttInSeconds (see constructor).
* @param service
*/
async get(service) {
const domains = await this.getCachedDomains();
const domain = domains.find(({ service: s }) => s === service);
if (domain) {
return domain.baseURI;
}
throw new internalError_1.InternalError(errorCodes_1.ErrorCodes.Csds.NotFound, `Service "${service}" could not be found.`);
}
async getCachedDomains() {
if (!this.accountId) {
this.accountId = await getAccountId();
}
if (!this.isCacheExpired()) {
return this.domains;
}
try {
const { baseURIs } = await (0, httpClient_1.httpClient)(this.getUrl(), {
json: true,
});
if (baseURIs && baseURIs.length !== 0) {
this.lastCacheTimestamp = Date.now();
this.domains = baseURIs;
return baseURIs;
}
return [];
}
catch (error) {
throw new internalError_1.InternalError(errorCodes_1.ErrorCodes.Csds.Failure, error.message);
}
}
isCacheExpired() {
return Date.now() > this.lastCacheTimestamp + this.ttlInSeconds * 1000;
}
getUrl() {
return `https://${this.getCsdsDomain()}/api/account/${this.accountId}/service/baseURI.json?version=1.0`;
}
getCsdsDomain() {
var _a, _b, _c;
if (((_a = this.accountId) === null || _a === void 0 ? void 0 : _a.startsWith('le')) || ((_b = this.accountId) === null || _b === void 0 ? void 0 : _b.startsWith('qa'))) {
return 'lp-csds-qa.dev.lprnd.net';
}
if ((_c = this.accountId) === null || _c === void 0 ? void 0 : _c.startsWith('fr')) {
return 'adminlogin-z0-intg.liveperson.net';
}
return 'api.liveperson.net';
}
}
exports.CsdsClient = CsdsClient;
async function getAccountId() {
try {
if (process.env[const_1.Environment.General.Account]) {
return process.env[const_1.Environment.General.Account];
}
const tempFile = JSON.parse(fs.readFileSync((0, path_1.join)(os.tmpdir(), 'faas-tmp.json'), 'utf8'));
const decryptedTempfile = await decrypt(tempFile);
return Object.keys(decryptedTempfile).find((e) => decryptedTempfile[`${e}`].active);
}
catch (error) {
console.log(error);
throw new Error(`For local usage of the CSDS Client an accountId is required.
Please login or set the env variable BRAND_ID`);
}
}
async function getCryptoConfig() {
let systemUUID;
try {
const { uuid } = await (0, systeminformation_1.system)();
systemUUID = uuid;
}
catch {
systemUUID = '01:02:03:04:05:06';
}
return {
algorithm: 'aes256',
key: crypto.scryptSync(systemUUID, 'faas-cli', 32),
iv: 'faas-cli-vectors',
};
}
async function decrypt(data) {
const { algorithm, key, iv } = await getCryptoConfig();
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(data, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return JSON.parse(decrypted);
}
//# sourceMappingURL=csdsClient.js.map