@infosupport/huddle-cli 0.0.0-experiment-66.29.1 → 0.0.0-experiment-66.31.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/firewall.js +140 -0
- package/dist/index.js +26 -0
- package/package.json +1 -1
package/dist/firewall.js
CHANGED
|
@@ -5,8 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.runFirewallList = runFirewallList;
|
|
7
7
|
exports.runFirewallAdd = runFirewallAdd;
|
|
8
|
+
exports.runFirewallDelete = runFirewallDelete;
|
|
8
9
|
exports.runFirewallExport = runFirewallExport;
|
|
9
10
|
exports.runFirewallImport = runFirewallImport;
|
|
11
|
+
exports.runFirewallGroup = runFirewallGroup;
|
|
12
|
+
exports.runFirewallFolder = runFirewallFolder;
|
|
10
13
|
const fs_1 = __importDefault(require("fs"));
|
|
11
14
|
const api_1 = require("./api");
|
|
12
15
|
const utils_1 = require("./utils");
|
|
@@ -84,6 +87,43 @@ async function runFirewallAdd(opts) {
|
|
|
84
87
|
const verb = status === 'deny' ? (0, utils_1.red)('Denied') : (0, utils_1.green)('Allowed');
|
|
85
88
|
console.log(`${verb} ${(0, utils_1.bold)((0, utils_1.cyan)(target))} ${(0, utils_1.dim)(`(${scope})`)}`);
|
|
86
89
|
}
|
|
90
|
+
// Deletes a firewall rule. Accepts the numeric id shown by `firewall list`
|
|
91
|
+
// (deleted directly) or a domain, which is resolved to a single rule id via a
|
|
92
|
+
// lookup. `--container` narrows a domain match to one scope; an ambiguous
|
|
93
|
+
// domain (multiple matching rules) is refused with the candidate ids so the
|
|
94
|
+
// caller can re-run with an exact id.
|
|
95
|
+
async function runFirewallDelete(opts) {
|
|
96
|
+
const target = (opts.target ?? '').trim();
|
|
97
|
+
if (!target) {
|
|
98
|
+
throw new Error('Usage: huddle firewall delete <id-or-domain> [--container <id>]');
|
|
99
|
+
}
|
|
100
|
+
let id;
|
|
101
|
+
if (/^\d+$/.test(target)) {
|
|
102
|
+
id = Number(target);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
// Domain form: list all rules (optionally scoped) and resolve to one id.
|
|
106
|
+
const qs = new URLSearchParams();
|
|
107
|
+
if (opts.container)
|
|
108
|
+
qs.set('container', opts.container);
|
|
109
|
+
const query = qs.toString();
|
|
110
|
+
const rules = await (0, api_1.get)(`/api/rules${query ? `?${query}` : ''}`);
|
|
111
|
+
const matches = rules.filter((r) => r.domain === target);
|
|
112
|
+
if (matches.length === 0) {
|
|
113
|
+
const scope = opts.container ? ` for container ${opts.container}` : '';
|
|
114
|
+
throw new Error(`No firewall rule found for "${target}"${scope}.`);
|
|
115
|
+
}
|
|
116
|
+
if (matches.length > 1) {
|
|
117
|
+
const ids = matches
|
|
118
|
+
.map((r) => ` ${r.id} ${r.status.padEnd(9)} ${formatTarget(r)} ${r.container_id ?? '(global)'}`)
|
|
119
|
+
.join('\n');
|
|
120
|
+
throw new Error(`"${target}" matches ${matches.length} rules — delete by id (or narrow with --container):\n${ids}`);
|
|
121
|
+
}
|
|
122
|
+
id = matches[0].id;
|
|
123
|
+
}
|
|
124
|
+
await (0, api_1.del)(`/api/rules/${id}`);
|
|
125
|
+
console.log(`${(0, utils_1.green)('Deleted')} rule ${(0, utils_1.bold)((0, utils_1.cyan)(String(id)))}`);
|
|
126
|
+
}
|
|
87
127
|
function printRulesTable(rules) {
|
|
88
128
|
const headers = ['ID', 'Status', 'Domain/path', 'Container', 'Requests', 'Seen'];
|
|
89
129
|
const rows = rules.map((r) => [
|
|
@@ -138,3 +178,103 @@ async function runFirewallImport(opts) {
|
|
|
138
178
|
const res = await (0, api_1.post)(`/api/rules/import${suffix}`, { mode, rules: doc.rules });
|
|
139
179
|
console.error((0, utils_1.green)(`[OK] Imported (${mode}): ${res.imported} added, ${res.updated} updated, ${res.skipped} skipped`));
|
|
140
180
|
}
|
|
181
|
+
async function resolveGroupByName(name) {
|
|
182
|
+
const groups = await (0, api_1.get)('/api/groups');
|
|
183
|
+
const match = groups.filter((g) => g.name.toLowerCase() === name.toLowerCase());
|
|
184
|
+
if (match.length === 0)
|
|
185
|
+
throw new Error(`No group named "${name}". Run \`huddle firewall group list\`.`);
|
|
186
|
+
return match[0];
|
|
187
|
+
}
|
|
188
|
+
async function runFirewallGroup(opts) {
|
|
189
|
+
const action = opts.action ?? 'list';
|
|
190
|
+
if (action === 'list') {
|
|
191
|
+
const groups = await (0, api_1.get)('/api/groups');
|
|
192
|
+
if (groups.length === 0) {
|
|
193
|
+
console.log((0, utils_1.dim)('No firewall groups yet.'));
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const headers = ['ID', 'Name', 'Rules', 'Shared', 'Source'];
|
|
197
|
+
const rows = groups.map((g) => [String(g.id), g.name, String(g.rule_count), g.shared ? 'yes' : 'no', g.source]);
|
|
198
|
+
(0, utils_1.printTable)(headers, rows);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (action === 'export') {
|
|
202
|
+
const name = (opts.arg ?? '').trim();
|
|
203
|
+
if (!name)
|
|
204
|
+
throw new Error('Usage: huddle firewall group export <name> [--out <file>]');
|
|
205
|
+
const g = await resolveGroupByName(name);
|
|
206
|
+
const env = await (0, api_1.get)(`/api/groups/${g.id}/export`);
|
|
207
|
+
const json = JSON.stringify(env, null, 2);
|
|
208
|
+
if (opts.out) {
|
|
209
|
+
fs_1.default.writeFileSync(opts.out, `${json}\n`);
|
|
210
|
+
console.error((0, utils_1.green)(`[OK] Exported group "${g.name}" to ${opts.out}`));
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
console.log(json);
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (action === 'import') {
|
|
218
|
+
const file = (opts.arg ?? '').trim();
|
|
219
|
+
if (!file)
|
|
220
|
+
throw new Error('Usage: huddle firewall group import <file> [--replace]');
|
|
221
|
+
let raw;
|
|
222
|
+
try {
|
|
223
|
+
raw = fs_1.default.readFileSync(file, 'utf8');
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
throw new Error(`Cannot read ${file}`);
|
|
227
|
+
}
|
|
228
|
+
let envelope;
|
|
229
|
+
try {
|
|
230
|
+
envelope = JSON.parse(raw);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
throw new Error(`${file} is not valid JSON`);
|
|
234
|
+
}
|
|
235
|
+
const mode = opts.replace ? 'replace' : 'merge';
|
|
236
|
+
const res = await (0, api_1.post)('/api/groups/import', { mode, envelope });
|
|
237
|
+
console.error((0, utils_1.green)(`[OK] Imported group "${res.group.name}" (${mode}): ${res.imported} added, ${res.updated} updated, ${res.skipped} skipped`));
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (action === 'apply') {
|
|
241
|
+
const name = (opts.arg ?? '').trim();
|
|
242
|
+
if (!name)
|
|
243
|
+
throw new Error('Usage: huddle firewall group apply <name> [--container <id>]');
|
|
244
|
+
const g = await resolveGroupByName(name);
|
|
245
|
+
const container = opts.container ?? null;
|
|
246
|
+
const res = await (0, api_1.post)(`/api/groups/${g.id}/apply`, { container });
|
|
247
|
+
const scope = container ? `container ${container}` : 'global';
|
|
248
|
+
console.log((0, utils_1.green)(`[OK] Applied "${g.name}" to ${scope}: ${res.applied} added, ${res.updated} updated`));
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
throw new Error(`Unknown group action: ${action}. Use list | export | import | apply.`);
|
|
252
|
+
}
|
|
253
|
+
async function runFirewallFolder(opts) {
|
|
254
|
+
const action = opts.action ?? 'show';
|
|
255
|
+
if (action === 'show') {
|
|
256
|
+
const s = await (0, api_1.get)('/api/settings');
|
|
257
|
+
console.log(s.firewallRulesFolder ? s.firewallRulesFolder : (0, utils_1.dim)('(no firewall rules folder configured)'));
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (action === 'set') {
|
|
261
|
+
const path = (opts.path ?? '').trim();
|
|
262
|
+
if (!path)
|
|
263
|
+
throw new Error('Usage: huddle firewall folder set <path>');
|
|
264
|
+
await (0, api_1.post)('/api/settings', { firewallRulesFolder: path });
|
|
265
|
+
console.log((0, utils_1.green)(`[OK] Firewall rules folder set to ${(0, utils_1.cyan)(path)} (re-read now and on startup)`));
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (action === 'reload') {
|
|
269
|
+
const res = await (0, api_1.post)('/api/firewall-rules-folder/reload', {});
|
|
270
|
+
if (!res.folder) {
|
|
271
|
+
console.log((0, utils_1.dim)('No firewall rules folder configured. Use `huddle firewall folder set <path>`.'));
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
console.log((0, utils_1.green)(`[OK] Reloaded ${res.folder}: ${res.groups} group(s), ${res.imported} rule(s), ${res.errors.length} error(s)`));
|
|
275
|
+
for (const e of res.errors)
|
|
276
|
+
console.error((0, utils_1.red)(` [!] ${e.file}: ${e.message}`));
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
throw new Error(`Unknown folder action: ${action}. Use show | set | reload.`);
|
|
280
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -100,8 +100,16 @@ Usage:
|
|
|
100
100
|
huddle fw list [options] Alias for firewall list
|
|
101
101
|
huddle firewall add <domain> Add a custom firewall rule (wildcards
|
|
102
102
|
supported: *.example.com and /path/*)
|
|
103
|
+
huddle firewall delete <id> Delete a firewall rule by id (or domain;
|
|
104
|
+
narrow with --container)
|
|
103
105
|
huddle firewall export [options] Export firewall rules as JSON
|
|
104
106
|
huddle firewall import <file> Import firewall rules from a JSON file
|
|
107
|
+
huddle firewall group list List firewall groups (#69)
|
|
108
|
+
huddle firewall group export <name> Export a group as JSON (--out <file>)
|
|
109
|
+
huddle firewall group import <file> Import a group (--replace to mirror)
|
|
110
|
+
huddle firewall group apply <name> Apply a group (--container <id> or global)
|
|
111
|
+
huddle firewall folder set <path> Set the team-managed rules folder
|
|
112
|
+
huddle firewall folder reload Re-read the team-managed rules folder
|
|
105
113
|
huddle experiment use <nr> Activate the experimental build of issue/PR <nr>
|
|
106
114
|
and run init
|
|
107
115
|
huddle experiment reset Back to the stable release
|
|
@@ -244,6 +252,12 @@ async function main() {
|
|
|
244
252
|
container: flagString(flags, 'container'),
|
|
245
253
|
});
|
|
246
254
|
}
|
|
255
|
+
else if (subCmd === 'delete' || subCmd === 'remove' || subCmd === 'rm') {
|
|
256
|
+
await (0, firewall_1.runFirewallDelete)({
|
|
257
|
+
target: positional[2],
|
|
258
|
+
container: flagString(flags, 'container'),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
247
261
|
else if (subCmd === 'export') {
|
|
248
262
|
await (0, firewall_1.runFirewallExport)({
|
|
249
263
|
container: flagString(flags, 'container'),
|
|
@@ -262,6 +276,18 @@ async function main() {
|
|
|
262
276
|
container: flagString(flags, 'container'),
|
|
263
277
|
});
|
|
264
278
|
}
|
|
279
|
+
else if (subCmd === 'group' || subCmd === 'groups') {
|
|
280
|
+
await (0, firewall_1.runFirewallGroup)({
|
|
281
|
+
action: positional[2],
|
|
282
|
+
arg: positional[3],
|
|
283
|
+
out: flagString(flags, 'out'),
|
|
284
|
+
replace: flagBool(flags, 'replace'),
|
|
285
|
+
container: flagString(flags, 'container'),
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
else if (subCmd === 'folder') {
|
|
289
|
+
await (0, firewall_1.runFirewallFolder)({ action: positional[2], path: positional[3] });
|
|
290
|
+
}
|
|
265
291
|
else {
|
|
266
292
|
console.error(`Unknown firewall subcommand: ${subCmd}`);
|
|
267
293
|
process.exit(1);
|
package/package.json
CHANGED