tydet-core-icm
Version:
Internal validator tool
722 lines • 34.4 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ICM = void 0;
var tydet_core_1 = require("tydet-core");
var icm_error_1 = require("./icm.error");
var fs_1 = require("fs");
var tydet_utils_1 = require("tydet-utils");
var jwt = require("jsonwebtoken");
var axios_1 = require("axios");
var os_1 = require("os");
var PATH_FILE = "PATH_FILE";
var ICM = /** @class */ (function (_super) {
__extends(ICM, _super);
function ICM(icmPathFile) {
var map = new Map();
map.set(PATH_FILE, icmPathFile);
return _super.call(this, map) || this;
}
ICM.prototype.beforeMount = function (context) {
return __awaiter(this, void 0, void 0, function () {
var errors;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
errors = {};
if (!this.params.has(PATH_FILE)) {
throw new icm_error_1.IcmError("Path to Key File is missing");
}
return [4 /*yield*/, _super.prototype.beforeMount.call(this, context)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
ICM.prototype.onMount = function () {
return __awaiter(this, void 0, void 0, function () {
var f;
return __generator(this, function (_a) {
try {
this.pathFile = this.params.get(PATH_FILE);
f = (0, fs_1.readFileSync)(this.pathFile, "utf8");
this.icmFile = JSON.parse(f);
if (this.icmFile.type != "internal-credential-key") {
throw new icm_error_1.IcmError("Invalid key file");
}
this.baseUrl = this.icmFile.auth_url.replace("/auth/token", "");
}
catch (err) {
throw new icm_error_1.IcmError("Missing or invalid key file");
}
return [2 /*return*/];
});
});
};
ICM.prototype.onReset = function () {
return __awaiter(this, void 0, void 0, function () {
var f;
return __generator(this, function (_a) {
this.lastSync = null;
this.icmFile = null;
this.icmFile = null;
this.accessToken = null;
this.baseUrl = os_1.devNull;
try {
this.pathFile = this.params.get(PATH_FILE);
f = (0, fs_1.readFileSync)(this.pathFile, "utf8");
this.icmFile = JSON.parse(f);
if (this.icmFile.type != "internal-credential-key") {
throw new icm_error_1.IcmError("Invalid key file");
}
this.baseUrl = this.icmFile.auth_url.replace("/auth/token", "");
}
catch (err) {
throw new icm_error_1.IcmError("Missing or invalid key file");
}
return [2 /*return*/];
});
});
};
ICM.prototype.onEject = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.lastSync = null;
this.icmFile = null;
this.icmFile = null;
this.accessToken = null;
this.baseUrl = os_1.devNull;
return [2 /*return*/];
});
});
};
ICM.prototype.createJwt = function () {
var payload = {
iss: this.icmFile.private_key_name,
iid: this.icmFile.private_key_id,
sub: this.icmFile.access_domain,
aud: this.icmFile.auth_url,
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 60
};
var token = jwt.sign(payload, this.icmFile.private_key, { algorithm: "RS256" });
return token;
};
ICM.prototype.requestToken = function (requester) {
return __awaiter(this, void 0, void 0, function () {
var result, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, axios_1.default.post(this.icmFile.auth_url, {
assertion: requester
})];
case 1:
result = _a.sent();
return [2 /*return*/, result.data.data.access_token];
case 2:
err_1 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred when requesting an access token with the provider", err_1);
case 3: return [2 /*return*/];
}
});
});
};
ICM.prototype.getAccessToken = function () {
return __awaiter(this, void 0, void 0, function () {
var token, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(this.lastSync == null || tydet_utils_1.DateUtils.minutesOfDifference(new Date(), this.lastSync) > 50)) return [3 /*break*/, 2];
token = this.createJwt();
_a = this;
return [4 /*yield*/, this.requestToken(token)];
case 1:
_a.accessToken = _b.sent();
this.lastSync = new Date();
_b.label = 2;
case 2: return [2 /*return*/, this.accessToken];
}
});
});
};
ICM.prototype.getTokenInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.icmFile.auth_url + "/info", { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_2 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getTokenInfo", err_2);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.getProjects = function () {
return __awaiter(this, arguments, void 0, function (per, page) {
var at, result, err_3;
if (per === void 0) { per = 1000; }
if (page === void 0) { page = 1; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.baseUrl + "/projects?per=".concat(per > 1000 ? 1000 : per, "&page=").concat(page), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_3 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getProjects", err_3);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.createProject = function (name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.post(this.baseUrl + "/projects", {
name: name
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_4 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request createProject", err_4);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.getProject = function (name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.baseUrl + "/projects/".concat(name), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_5 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getProject", err_5);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.updateProject = function (projectName, newName) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.put(this.baseUrl + "/projects/".concat(projectName), {
name: newName
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_6 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getProject", err_6);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.removeProject = function (name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_7;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.delete(this.baseUrl + "/projects/".concat(name), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, true];
case 4:
err_7 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getProject", err_7);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.getEnvironments = function (project_1) {
return __awaiter(this, arguments, void 0, function (project, per, page) {
var at, result, err_8;
if (per === void 0) { per = 1000; }
if (page === void 0) { page = 1; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.baseUrl + "/projects/".concat(project, "/environments?per=").concat(per > 1000 ? 1000 : per, "&page=").concat(page), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_8 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getEnvironments", err_8);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.createEnvironment = function (project, name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_9;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.post(this.baseUrl + "/projects/".concat(project, "/environments"), {
name: name
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_9 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request createEnvironment", err_9);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.getEnvironment = function (project, name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_10;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.baseUrl + "/projects/".concat(project, "/environments/").concat(name), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_10 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getEnvironment", err_10);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.updateEnvironment = function (project, environment, newName) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_11;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.put(this.baseUrl + "/projects/".concat(project, "/environments/").concat(environment), {
name: newName
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_11 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request updateEnvironment", err_11);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.removeEnvironment = function (project, name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_12;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.delete(this.baseUrl + "/projects/".concat(project, "/environments/").concat(name), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, true];
case 4:
err_12 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request removeEnvironment", err_12);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.getParameters = function (project_1) {
return __awaiter(this, arguments, void 0, function (project, per, page) {
var at, result, err_13;
if (per === void 0) { per = 1000; }
if (page === void 0) { page = 1; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.baseUrl + "/projects/".concat(project, "/parameters?per=").concat(per > 1000 ? 1000 : per, "&page=").concat(page), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_13 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getParameters", err_13);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.createParameter = function (project, name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_14;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.post(this.baseUrl + "/projects/".concat(project, "/parameters"), {
name: name
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_14 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request createParameter", err_14);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.getParameter = function (project, name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_15;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.baseUrl + "/projects/".concat(project, "/parameters/").concat(name), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_15 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getParameter", err_15);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.updateParameter = function (project, parameter, newName) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_16;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.put(this.baseUrl + "/projects/".concat(project, "/parameters/").concat(parameter), {
name: newName
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data];
case 4:
err_16 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request updateParameter", err_16);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.removeParameter = function (project, name) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_17;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.delete(this.baseUrl + "/projects/".concat(project, "/parameters/").concat(name), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, true];
case 4:
err_17 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request removeParameter", err_17);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.createValue = function (project, parameter, environment, value) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_18;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.post(this.baseUrl + "/projects/".concat(project, "/value/").concat(parameter, "/").concat(environment), {
value: value
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, true];
case 4:
err_18 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request createValue", err_18);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.getValue = function (project, parameter, environment) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_19;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.get(this.baseUrl + "/projects/".concat(project, "/value/").concat(parameter, "/").concat(environment), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, result.data.data.value];
case 4:
err_19 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request getValue", err_19);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.updateValue = function (project, parameter, environment, value) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_20;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.put(this.baseUrl + "/projects/".concat(project, "/value/").concat(parameter, "/").concat(environment), {
value: value
}, { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, true];
case 4:
err_20 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request updateValue", err_20);
case 5: return [2 /*return*/];
}
});
});
};
ICM.prototype.removeValue = function (project, parameter, environment) {
return __awaiter(this, void 0, void 0, function () {
var at, result, err_21;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
at = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, axios_1.default.delete(this.baseUrl + "/projects/".concat(project, "/value/").concat(parameter, "/").concat(environment), { headers: {
"Authorization": "Bearer ".concat(at)
} })];
case 3:
result = _a.sent();
return [2 /*return*/, true];
case 4:
err_21 = _a.sent();
throw new icm_error_1.IcmError("An error ocurred with the request removeValue", err_21);
case 5: return [2 /*return*/];
}
});
});
};
return ICM;
}(tydet_core_1.Service));
exports.ICM = ICM;
//# sourceMappingURL=icm.service.js.map