UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

511 lines 24.8 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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; return g = { next: verb(0), "throw": verb(1), "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 (_) 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 }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CopyPath = void 0; // Copy Operations Provider // Used in the "Client" var access = require("./access"); var async_utils_1 = require("smc-util/async-utils"); var message = __importStar(require("smc-util/message")); var one_result = require("./postgres").one_result; var misc_1 = require("smc-util/misc"); // this is specific to queries built here function sanitize(val, deflt, max, name) { if (val != null) { var o = typeof val == "string" ? parseInt(val) : val; if (isNaN(o) || o < 0 || o > max) { throw new Error("ILLEGAL VALUE " + name + "='" + val + "' (must be in [0, " + max + "])"); } return o; } else { return deflt; } } // thrown errors are an object, but the response needs a string function err2str(err) { if (typeof err === "string") { return err; } else if (err.message != null) { return err.message; } else { return "ERROR: " + misc_1.to_json(err); } } // transforms copy_op data from the database to the specific object we want to return function row_to_copy_op(copy_op) { return { copy_path_id: copy_op.id, time: copy_op.time, source_project_id: copy_op.source_project_id, source_path: copy_op.source_path, target_project_id: copy_op.target_project_id, target_path: copy_op.target_path, overwrite_newer: copy_op.overwrite_newer, delete_missing: copy_op.delete_missing, backup: copy_op.backup, started: copy_op.started, finished: copy_op.finished, scheduled: copy_op.scheduled, error: copy_op.error, }; } var CopyPath = /** @class */ (function () { function CopyPath(client) { this.client = client; this._init_errors(); this.copy = this.copy.bind(this); this.status = this.status.bind(this); this.delete = this.delete.bind(this); this._status_query = this._status_query.bind(this); this._status_single = this._status_single.bind(this); this._get_status = this._get_status.bind(this); this._read_access = this._read_access.bind(this); this._write_access = this._write_access.bind(this); } CopyPath.prototype._init_errors = function () { // client.dbg returns a function this.dbg = function (method) { return this.client.dbg("CopyPath::" + method); }; this.err = function (method) { return function (msg) { throw new Error("CopyPath::" + method + ": " + msg); }; }; this.throw = function (msg) { throw new Error(msg); }; }; CopyPath.prototype.copy = function (mesg) { return __awaiter(this, void 0, void 0, function () { var write, read, projectControl, project, copy_id, resp, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: this.client.touch(); _a.label = 1; case 1: _a.trys.push([1, 4, , 5]); // prereq checks if (!misc_1.is_valid_uuid_string(mesg.src_project_id)) { this.throw("src_project_id='" + mesg.src_project_id + "' not valid"); } if (!misc_1.is_valid_uuid_string(mesg.target_project_id)) { this.throw("target_project_id='" + mesg.target_project_id + "' not valid"); } if (mesg.src_path == null) { this.throw("src_path must be defined"); } write = this._write_access(mesg.target_project_id); read = this._read_access(mesg.src_project_id); return [4 /*yield*/, Promise.all([write, read])]; case 2: _a.sent(); projectControl = this.client.compute_server; project = projectControl(mesg.src_project_id); return [4 /*yield*/, project.copyPath({ path: mesg.src_path, target_project_id: mesg.target_project_id, target_path: mesg.target_path, overwrite_newer: mesg.overwrite_newer, delete_missing: mesg.delete_missing, backup: mesg.backup, timeout: mesg.timeout, wait_until_done: mesg.wait_until_done, scheduled: mesg.scheduled, })]; case 3: copy_id = _a.sent(); // if we're still here, the copy was ok! if (copy_id != null) { resp = message.copy_path_between_projects_response({ id: mesg.id, copy_path_id: copy_id, }); this.client.push_to_client(resp); } else { this.client.push_to_client(message.success({ id: mesg.id })); } return [3 /*break*/, 5]; case 4: err_1 = _a.sent(); this.client.error_to_client({ id: mesg.id, error: err2str(err_1) }); return [3 /*break*/, 5]; case 5: return [2 /*return*/]; } }); }); }; CopyPath.prototype.status = function (mesg) { return __awaiter(this, void 0, void 0, function () { var search_many; return __generator(this, function (_a) { switch (_a.label) { case 0: this.client.touch(); search_many = mesg.src_project_id != null || mesg.target_project_id != null; if (!search_many && mesg.copy_path_id == null) { this.client.error_to_client({ id: mesg.id, error: "'copy_path_id' (UUID) of a copy operation or 'src_project_id/target_project_id' must be defined", }); return [2 /*return*/]; } if (!search_many) return [3 /*break*/, 2]; return [4 /*yield*/, this._status_query(mesg)]; case 1: _a.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, this._status_single(mesg)]; case 3: _a.sent(); _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; CopyPath.prototype._status_query = function (mesg) { return __awaiter(this, void 0, void 0, function () { var dbg, err, where, offset, limit, copy_ops, status_data, _a, _b, row, err_2; var e_1, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: dbg = this.dbg("status_query"); err = this.err("status_query"); _d.label = 1; case 1: _d.trys.push([1, 7, , 8]); // prereq checks -- at least src or target must be set if (mesg.src_project_id == null && mesg.target_project_id == null) { // serious error: this should never happen, actually err("At least one of \"src_project_id\" or \"target_project_id\" must be given!"); } where = []; if (!(mesg.src_project_id != null)) return [3 /*break*/, 3]; return [4 /*yield*/, this._read_access(mesg.src_project_id)]; case 2: _d.sent(); where.push({ "source_project_id = $::UUID": mesg.src_project_id }); _d.label = 3; case 3: if (!(mesg.target_project_id != null)) return [3 /*break*/, 5]; return [4 /*yield*/, this._write_access(mesg.target_project_id)]; case 4: _d.sent(); where.push({ "target_project_id = $::UUID": mesg.target_project_id }); _d.label = 5; case 5: if (mesg.src_path != null) { where.push({ "source_path = $": mesg.src_path }); } // all failed ones are implicitly also finished if (mesg.failed === true || mesg.failed === "true") { where.push("error IS NOT NULL"); mesg.pending = false; } if (mesg.pending === true || mesg.pending === "true") { where.push("finished IS NULL"); } else { where.push("finished IS NOT NULL"); } offset = sanitize(mesg.offset, 0, 100 * 1000, "offset"); limit = sanitize(mesg.limit, 1000, 1000, "limit"); dbg("offset=" + offset + " limit=" + limit); copy_ops = []; return [4 /*yield*/, async_utils_1.callback2(this.client.database._query, { query: "SELECT * FROM copy_paths", where: where, offset: offset, limit: limit, order_by: "time DESC", // most recent first })]; case 6: status_data = _d.sent(); if (status_data == null) { this.throw("Can't find copy operations for given src_project_id/target_project_id"); } try { for (_a = __values(Array.from(status_data.rows)), _b = _a.next(); !_b.done; _b = _a.next()) { row = _b.value; // be explicit about what we return copy_ops.push(row_to_copy_op(row)); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } } // we're good this.client.push_to_client(message.copy_path_status_response({ id: mesg.id, data: copy_ops, })); return [3 /*break*/, 8]; case 7: err_2 = _d.sent(); this.client.error_to_client({ id: mesg.id, error: err2str(err_2) }); return [3 /*break*/, 8]; case 8: return [2 /*return*/]; } }); }); }; CopyPath.prototype._get_status = function (mesg) { return __awaiter(this, void 0, void 0, function () { var dbg, where, statuses, copy_op, write, read; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (mesg.copy_path_id == null) { this.throw("ERROR: copy_path_id missing"); } dbg = this.dbg("_get_status"); where = [{ "id = $::UUID": mesg.copy_path_id }]; // not_yet_done is set internally for deleting a scheduled copy op if (mesg.not_yet_done) { where.push("scheduled IS NOT NULL"); where.push("finished IS NULL"); } return [4 /*yield*/, async_utils_1.callback2(this.client.database._query, { query: "SELECT * FROM copy_paths", where: where, })]; case 1: statuses = _a.sent(); copy_op = (function () { var copy_op; one_result(function (_, x) { if (x == null) { if (mesg.not_yet_done) { _this.throw("Copy operation '" + mesg.copy_path_id + "' either does not exist or already finished"); } else { _this.throw("Can't find copy operation with ID=" + mesg.copy_path_id); } } else { copy_op = x; dbg("copy_op=" + misc_1.to_json(copy_op)); } })(undefined, statuses); return copy_op; })(); if (copy_op == null) { this.throw("Can't find copy operation with ID=" + mesg.copy_path_id); return [2 /*return*/]; } write = this._write_access(copy_op.target_project_id); read = this._read_access(copy_op.source_project_id); return [4 /*yield*/, Promise.all([write, read])]; case 2: _a.sent(); return [2 /*return*/, copy_op]; } }); }); }; CopyPath.prototype._status_single = function (mesg) { return __awaiter(this, void 0, void 0, function () { var copy_op, data, err_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this._get_status(mesg)]; case 1: copy_op = _a.sent(); data = row_to_copy_op(copy_op); this.client.push_to_client(message.copy_path_status_response({ id: mesg.id, data: data })); return [3 /*break*/, 3]; case 2: err_3 = _a.sent(); this.client.error_to_client({ id: mesg.id, error: err2str(err_3) }); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; CopyPath.prototype.delete = function (mesg) { return __awaiter(this, void 0, void 0, function () { var dbg, copy_op, err_4; return __generator(this, function (_a) { switch (_a.label) { case 0: this.client.touch(); dbg = this.dbg("delete"); // this filters possible results mesg.not_yet_done = true; _a.label = 1; case 1: _a.trys.push([1, 6, , 7]); return [4 /*yield*/, this._get_status(mesg)]; case 2: copy_op = _a.sent(); if (!(copy_op == null)) return [3 /*break*/, 3]; this.client.error_to_client({ id: mesg.id, error: "opy op '" + mesg.copy_path_id + "' cannot be deleted.", }); return [3 /*break*/, 5]; case 3: return [4 /*yield*/, async_utils_1.callback2(this.client.database._query, { query: "DELETE FROM copy_paths", where: { "id = $::UUID": mesg.copy_path_id }, })]; case 4: _a.sent(); // no error this.client.push_to_client(message.copy_path_status_response({ id: mesg.id, data: "copy_path_id = '" + mesg.copy_path_id + "' deleted", })); _a.label = 5; case 5: return [3 /*break*/, 7]; case 6: err_4 = _a.sent(); dbg("stauts err=" + err2str(err_4)); this.client.error_to_client({ id: mesg.id, error: err2str(err_4) }); return [3 /*break*/, 7]; case 7: return [2 /*return*/]; } }); }); }; CopyPath.prototype._read_access = function (src_project_id) { return __awaiter(this, void 0, void 0, function () { var read_ok; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!misc_1.is_valid_uuid_string(src_project_id)) { this.throw("invalid src_project_id=" + src_project_id); } return [4 /*yield*/, async_utils_1.callback2(access.user_has_read_access_to_project, { project_id: src_project_id, account_id: this.client.account_id, account_groups: this.client.groups, database: this.client.database, })]; case 1: read_ok = _a.sent(); // this.dbg("_read_access")(read_ok); if (!read_ok) { this.throw("ACCESS BLOCKED -- No read access to source project -- " + src_project_id); return [2 /*return*/, false]; } return [2 /*return*/, true]; } }); }); }; CopyPath.prototype._write_access = function (target_project_id) { return __awaiter(this, void 0, void 0, function () { var write_ok; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!misc_1.is_valid_uuid_string(target_project_id)) { this.throw("invalid target_project_id=" + target_project_id); } return [4 /*yield*/, async_utils_1.callback2(access.user_has_write_access_to_project, { database: this.client.database, project_id: target_project_id, account_id: this.client.account_id, account_groups: this.client.groups, })]; case 1: write_ok = _a.sent(); // this.dbg("_write_access")(write_ok); if (!write_ok) { this.throw("ACCESS BLOCKED -- No write access to target project -- " + target_project_id); return [2 /*return*/, false]; } return [2 /*return*/, true]; } }); }); }; return CopyPath; }()); exports.CopyPath = CopyPath; //# sourceMappingURL=copy-path.js.map