UNPKG

firebase-tools

Version:
110 lines (105 loc) 4.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.command = void 0; const command_1 = require("../command"); const requirePermissions_1 = require("../requirePermissions"); const projectUtils_1 = require("../projectUtils"); const ailogic = require("../gcp/ailogic"); const clc = require("colorette"); const utils = require("../utils"); const error_1 = require("../error"); const prompt_1 = require("../prompt"); function parseBool(pathStr, value) { const normalized = value.toLowerCase(); if (normalized !== "true" && normalized !== "false") { throw new error_1.FirebaseError(`Value for ${clc.bold(pathStr)} must be 'true' or 'false'.`); } return normalized === "true"; } function buildUpdate(pathStr, value) { switch (pathStr) { case "security.auth-only": { const boolVal = parseBool(pathStr, value); return { config: { trafficFilter: { firebaseAuthRequired: boolVal } }, updateMask: "trafficFilter.firebaseAuthRequired", normalizedValue: String(boolVal), confirm: boolVal ? { message: `Enabling ${clc.bold(pathStr)} will reject requests from unauthenticated users. Continue?`, isAlreadyEnabled: (current) => current.trafficFilter?.firebaseAuthRequired ?? false, } : undefined, }; } case "security.template-only": { const boolVal = parseBool(pathStr, value); return { config: { trafficFilter: { templateOnly: boolVal } }, updateMask: "trafficFilter.templateOnly", normalizedValue: String(boolVal), confirm: boolVal ? { message: `Enabling ${clc.bold(pathStr)} will reject requests not using templates. Continue?`, isAlreadyEnabled: (current) => current.trafficFilter?.templateOnly ?? false, } : undefined, }; } case "monitoring.state": { const boolVal = parseBool(pathStr, value); return { config: { telemetryConfig: { mode: boolVal ? "ALL" : "NONE" } }, updateMask: "telemetryConfig.mode", normalizedValue: String(boolVal), }; } case "monitoring.sample-rate-percentage": { if (!/^\d+$/.test(value) || Number(value) < 1 || Number(value) > 100) { throw new error_1.FirebaseError(`Value for ${clc.bold(pathStr)} must be an integer in the range 1-100.`); } return { config: { telemetryConfig: { samplingRate: Number(value) / 100 } }, updateMask: "telemetryConfig.samplingRate", normalizedValue: String(Number(value)), }; } default: throw new error_1.FirebaseError(`Unknown configuration path: ${pathStr}`); } } exports.command = new command_1.Command("ailogic:config:set <path> <value>") .description("set one configuration value") .help(`sets one AI Logic configuration value on the active project. Valid values for <path>, and the <value> each accepts: security.auth-only true|false - only allow requests from authenticated users security.template-only true|false - only allow requests that use a server template monitoring.state true|false - turn AI monitoring on or off monitoring.sample-rate-percentage 1-100 - percentage of requests sampled for monitoring For example, to only allow requests from authenticated users: firebase ailogic:config:set security.auth-only true`) .option("-f, --force", "bypass confirmation prompt") .before(requirePermissions_1.requirePermissions, [ "firebasevertexai.config.update", "firebasevertexai.config.get", "serviceusage.services.get", ]) .action(async (pathStr, value, options) => { const projectId = (0, projectUtils_1.needProjectId)(options); ailogic.assertKnownConfigPath(pathStr, ailogic.WRITABLE_CONFIG_PATHS); const update = buildUpdate(pathStr, value); await ailogic.ensureAILogicApiEnabled(projectId, options); if (update.confirm && !update.confirm.isAlreadyEnabled(await ailogic.getConfig(projectId))) { const confirmed = await (0, prompt_1.confirm)({ message: update.confirm.message, force: options.force, nonInteractive: options.nonInteractive, }); if (!confirmed) { throw new error_1.FirebaseError("Command aborted.", { exit: 1 }); } } await ailogic.updateConfig(projectId, update.config, [update.updateMask]); utils.logSuccess(`Updated ${clc.bold(pathStr)} = ${update.normalizedValue}`); return { path: pathStr, value: update.normalizedValue }; });