smc-hub
Version:
CoCalc: Backend webserver component
274 lines • 13.3 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
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.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.project_datastore_get = exports.project_datastore_del = exports.project_datastore_set = exports.project_has_network_access = void 0;
var lodash_1 = require("lodash");
var async_utils_1 = require("smc-util/async-utils");
var query_1 = require("./query");
var debug_1 = __importDefault(require("debug"));
var L = debug_1.default("hub:project-queries");
var const_1 = require("smc-webapp/project/settings/const");
function project_has_network_access(db, project_id) {
return __awaiter(this, void 0, void 0, function () {
var x, err_1, account_id;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, async_utils_1.callback2(db.get_project, {
project_id: project_id,
columns: ["users", "settings"],
})];
case 1:
x = _a.sent();
return [3 /*break*/, 3];
case 2:
err_1 = _a.sent();
// error probably means there is no such project or project_id is badly formatted.
return [2 /*return*/, false];
case 3:
if (x.settings != null && x.settings.network) {
return [2 /*return*/, true];
}
if (x.users != null) {
for (account_id in x.users) {
if (x.users[account_id] != null &&
x.users[account_id].upgrades != null &&
x.users[account_id].upgrades.network) {
return [2 /*return*/, true];
}
}
}
return [2 /*return*/, false];
}
});
});
}
exports.project_has_network_access = project_has_network_access;
function get_datastore(opts) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var db, account_id, project_id, q;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
db = opts.db, account_id = opts.account_id, project_id = opts.project_id;
return [4 /*yield*/, query_1.query({
db: db,
table: "projects",
select: ["addons", "users"],
where: { project_id: project_id },
one: true,
})];
case 1:
q = _b.sent();
// this access test is absolutely critial to have! (only project queries set access_check to false)
if (q.users[account_id] == null)
throw Error("access denied");
return [2 /*return*/, (_a = q.addons) === null || _a === void 0 ? void 0 : _a.datastore];
}
});
});
}
function project_datastore_set(db, account_id, project_id, config) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var _b, _c, _d, key, val, old_name, conf_new, ds_prev, prev_name;
var e_1, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
// L("project_datastore_set", config);
if (config.name == null)
throw Error("configuration 'name' is not defined");
if (typeof config.type !== "string")
throw Error("configuration 'type' is not defined (must be 'gcs', 'sshfs', ...)");
try {
// check data from user
for (_b = __values(Object.entries(config)), _c = _b.next(); !_c.done; _c = _b.next()) {
_d = __read(_c.value, 2), key = _d[0], val = _d[1];
if (typeof val !== "string" && typeof val !== "boolean") {
throw new Error("Invalid value -- '" + key + "' is not a valid type");
}
if (typeof val === "string" && val.length > 100000) {
throw new Error("Invalid value -- '" + key + "' is too long");
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_e = _b.return)) _e.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
old_name = config.__old_name;
conf_new = lodash_1.omit(config, "name", "secret", "__old_name");
return [4 /*yield*/, get_datastore({ db: db, account_id: account_id, project_id: project_id })];
case 1:
ds_prev = _g.sent();
prev_name = old_name != null ? old_name : config.name;
// if a user wants to update the settings, they don't need to have the secret.
// an empty value or the dummy text signals to keep the secret as it is...
if (ds_prev != null &&
ds_prev[prev_name] != null &&
(config.secret === const_1.DUMMY_SECRET || config.secret === "")) {
conf_new.secret = ds_prev[prev_name].secret;
}
else {
conf_new.secret = Buffer.from((_a = config.secret) !== null && _a !== void 0 ? _a : "").toString("base64");
}
return [4 /*yield*/, query_1.query({
db: db,
query: "UPDATE projects",
where: { "project_id = $::UUID": project_id },
jsonb_merge: { addons: { datastore: (_f = {}, _f[config.name] = conf_new, _f) } },
})];
case 2:
_g.sent();
return [2 /*return*/];
}
});
});
}
exports.project_datastore_set = project_datastore_set;
function project_datastore_del(db, account_id, project_id, name) {
return __awaiter(this, void 0, void 0, function () {
var ds;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
L("project_datastore_del", name);
if (typeof name !== "string" || name.length == 0) {
throw Error("Datastore name not properly set.");
}
return [4 /*yield*/, get_datastore({ db: db, account_id: account_id, project_id: project_id })];
case 1:
ds = _a.sent();
delete ds[name];
return [4 /*yield*/, query_1.query({
db: db,
query: "UPDATE projects",
where: { "project_id = $::UUID": project_id },
jsonb_set: { addons: { datastore: ds } },
})];
case 2:
_a.sent();
return [2 /*return*/];
}
});
});
}
exports.project_datastore_del = project_datastore_del;
function project_datastore_get(db, account_id, project_id) {
return __awaiter(this, void 0, void 0, function () {
var ds, _a, _b, _c, k, v, err_2;
var e_2, _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 2, , 3]);
return [4 /*yield*/, get_datastore({
db: db,
account_id: account_id,
project_id: project_id,
})];
case 1:
ds = _e.sent();
if (ds != null) {
try {
for (_a = __values(Object.entries(ds)), _b = _a.next(); !_b.done; _b = _a.next()) {
_c = __read(_b.value, 2), k = _c[0], v = _c[1];
ds[k] = lodash_1.omit(v, "secret");
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
}
finally { if (e_2) throw e_2.error; }
}
}
return [2 /*return*/, {
addons: { datastore: ds },
}];
case 2:
err_2 = _e.sent();
return [2 /*return*/, { type: "error", error: "" + err_2 }];
case 3: return [2 /*return*/];
}
});
});
}
exports.project_datastore_get = project_datastore_get;
//# sourceMappingURL=project-queries.js.map