exports.id = 672;
exports.ids = [672];
exports.modules = {

/***/ 713428:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

"use strict";

/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const pool_1 = __importDefault(__webpack_require__(541808));
const software_envs_1 = __webpack_require__(465704);
const defaults_1 = __webpack_require__(294557);
const misc_1 = __webpack_require__(854006);
const uuid_1 = __webpack_require__(509557);
const public_path_1 = __webpack_require__(867000);
async function createProject({ account_id, title, description, image, license, public_path_id, }) {
    if (!(0, misc_1.is_valid_uuid_string)(account_id)) {
        throw Error("account_id must be a valid uuid");
    }
    const project_id = (0, uuid_1.v4)();
    const pool = (0, pool_1.default)();
    const users = { [account_id]: { group: "owner" } };
    let site_license;
    if (public_path_id) {
        const site_license_id = await (0, public_path_1.associatedLicense)(public_path_id);
        if (site_license_id) {
            if (!license) {
                license = site_license_id;
            }
            else {
                license = license + "," + site_license_id;
            }
        }
    }
    if (license) {
        site_license = {};
        for (const license_id of license.split(",")) {
            site_license[license_id] = {};
        }
    }
    else {
        site_license = undefined;
    }
    const envs = await (0, software_envs_1.getSoftwareEnvironments)("server");
    await pool.query("INSERT INTO projects (project_id, title, description, users, site_license, compute_image, created, last_edited) VALUES($1, $2, $3, $4, $5, $6, NOW(), NOW())", [
        project_id,
        title,
        description,
        JSON.stringify(users),
        site_license != null ? JSON.stringify(site_license) : undefined,
        image ?? envs?.default ?? defaults_1.DEFAULT_COMPUTE_IMAGE,
    ]);
    return project_id;
}
exports["default"] = createProject;
//# sourceMappingURL=create.js.map

/***/ }),

/***/ 465704:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

"use strict";

/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getSoftwareEnvironments = void 0;
// This is used by the hub to adjust the "customization" variable for the user visible site, and also by manage in on-prem, to actually get the associated configuration
const logger_1 = __importDefault(__webpack_require__(227554));
const sanitize_software_envs_1 = __webpack_require__(13489);
const fs_1 = __webpack_require__(657147);
const promises_1 = __webpack_require__(773292);
const path_1 = __webpack_require__(371017);
const logger = (0, logger_1.default)("hub:webapp-config");
const L = logger.debug;
const W = logger.warn;
const cache = {
    server: null,
    webapp: null,
};
/**
 * A configuration for available software environments could be stored at the location of $COCALC_SOFTWARE_ENVIRONMENTS.
 */
async function getSoftwareEnvironments(purpose) {
    if (cache[purpose] === null) {
        cache[purpose] = (await readConfig(purpose)) ?? false;
    }
    const data = cache[purpose];
    return data === false ? null : data;
}
exports.getSoftwareEnvironments = getSoftwareEnvironments;
async function readConfig(purpose) {
    const dir = process.env.COCALC_SOFTWARE_ENVIRONMENTS;
    if (!dir)
        return null;
    // Check if a file "software.json" and "registry" exist and are readable at the directory "dir":
    const softwareFn = (0, path_1.join)(dir, "software.json");
    const registryFn = (0, path_1.join)(dir, "registry");
    if (!(await isReadable(softwareFn))) {
        W(`WARNING: $COCALC_SOFTWARE_ENVIRONMENTS is defined but ${softwareFn} does not exist`);
        return null;
    }
    if (!(await isReadable(registryFn))) {
        W(`WARNING: $COCALC_SOFTWARE_ENVIRONMENTS is defined but ${registryFn} does not exist`);
        return null;
    }
    // read the content of registry and trim it to a one line string
    const registry = (await (0, promises_1.readFile)(registryFn)).toString().trim();
    // parse the content of softwareFn as json
    try {
        const software = JSON.parse((await (0, promises_1.readFile)(softwareFn)).toString());
        const dbg = (...msg) => L(...msg);
        const sanitized = (0, sanitize_software_envs_1.sanitizeSoftwareEnv)({ software, registry, purpose }, dbg);
        return sanitized;
    }
    catch (err) {
        W(`WARNING: ${softwareFn} is not a valid JSON file -- ${err}`);
        return null;
    }
}
async function isReadable(path) {
    try {
        await (0, promises_1.access)(path, fs_1.constants.R_OK);
    }
    catch (err) {
        return false;
    }
    return true;
}
//# sourceMappingURL=software-envs.js.map

/***/ }),

/***/ 630900:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

var crypto = __webpack_require__(706113);

var rn = function(max) {
  var rnBytes = crypto.randomBytes(2);
  var randomNum = rnBytes.readUInt8(0) * 256 + rnBytes.readUInt8(1);
  return randomNum % max;
}

var digits = '0123456789'
var base62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'
var base30 = '0123456789ABCDFHKLMNPQRSTUVWXYZ'  // 1-9, A-Z exclude(E, G, I, J, O) for human read

exports.generate = function(len, chars) {
  len = len || 16;
  chars = chars || base62;
  var key = '';
  var charsLen = chars.length;
  for (var i=0; i<len; i++) {
    key += chars[rn(charsLen)];
  }
  return key;
};

exports.generateDigits = function(len) {
  len = len || 16;
  return exports.generate(len, digits)
};

exports.generateBase30 = function(len) {
  len = len || 16;
  return exports.generate(len, base30)
}


/***/ }),

/***/ 13489:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

"use strict";

/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.sanitizeSoftwareEnv = void 0;
const lodash_1 = __webpack_require__(131248);
const defaults_1 = __webpack_require__(294557);
const WEBAPP_RELEVANT = [
    "tag",
    "title",
    "registry",
    "descr",
    "order",
    "short",
    "group",
    "hidden",
];
// test if an object is an Environments map – does not need to be a precise test, because the sanitization fixes it
function isEnvironments(envs) {
    return (0, lodash_1.isObject)(envs) && Object.values(envs).every(lodash_1.isObject);
}
/**
 * Check that the "software environment" object is valid, set defaults, default exists, etc.
 *
 * If there is a problem, it logs it to the given logger and returns "null".
 *
 * purpose: "server" returns all values, while "webapp" only filters those, which are relevant for the webapp (and does not expose extra information)
 */
function sanitizeSoftwareEnv(opts, L) {
    const { software, registry, purpose } = opts;
    const envs = software["environments"];
    if ((0, lodash_1.isEmpty)(envs)) {
        L(`No software environments defined`);
        return null;
    }
    if (!isEnvironments(envs)) {
        L(`Software envs must be a map of strings to environment objects`);
        return null;
    }
    // make sure this is an array of strings
    const groups = (software["groups"] ?? []).map((x) => `${x}`);
    for (const key of Object.keys(envs)) {
        // if purpose is "webapp", only pick these entries in the env object: title, registry, tag, descr, order, short, group
        const env = purpose === "webapp" ? (0, lodash_1.pick)(envs[key], WEBAPP_RELEVANT) : envs[key];
        // if no registry is set, we're only using the id/key and the data
        // if the registry is set (in particular for on-prem) we use registry:tag to set the image
        if (registry != null) {
            if (typeof env["tag"] !== "string") {
                L(`WARNING: Environment ${key} has no "tag" field -- ignoring`);
                delete envs[key];
                continue;
            }
            env["registry"] = fallback(env["registry"], registry);
        }
        const group = fallback(env["group"], "General");
        env["group"] = group;
        env["title"] = fallback(env["title"], env["tag"], key);
        env["descr"] = fallback(env["descr"], "");
        if (!env["descr"])
            delete env["descr"];
        env["order"] = typeof env["order"] === "number" ? env["order"] : 0;
        if (env["order"] === 0)
            delete env["order"];
        if (!!env["hidden"]) {
            env["hidden"] = true;
        }
        else {
            delete env["hidden"];
        }
        envs[key] = { ...env, id: key };
        // if group is not in groups, add it
        if (!groups.includes(group)) {
            groups.push(group);
        }
    }
    // test that there is at leat one environemnt left in envs
    if ((0, lodash_1.isEmpty)(envs)) {
        L(`No software environments left after sanitization`);
        return null;
    }
    const swDflt = software["default"];
    // we check that the default is a string and that it exists in envs
    const dflt = typeof swDflt === "string" && envs[swDflt] != null
        ? swDflt
        : Object.keys(envs)[0];
    // this is a fallback entry, when projects were created before the software env was configured
    if (envs[defaults_1.DEFAULT_COMPUTE_IMAGE] == null) {
        envs[defaults_1.DEFAULT_COMPUTE_IMAGE] = { ...envs[dflt], hidden: true };
    }
    return { groups, default: dflt, environments: envs };
}
exports.sanitizeSoftwareEnv = sanitizeSoftwareEnv;
function fallback(a, b, c) {
    if (typeof a === "string")
        return a;
    if (typeof b === "string")
        return b;
    return c;
}
//# sourceMappingURL=sanitize-software-envs.js.map

/***/ })

};
;