@lightdash-tools/cli 0.2.6 → 0.3.2
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/README.md +72 -51
- package/dist/commands/agents.d.ts +11 -0
- package/dist/commands/agents.js +377 -0
- package/dist/commands/ai-agents.d.ts +2 -2
- package/dist/commands/ai-agents.js +68 -6
- package/dist/commands/content.js +2 -2
- package/dist/index.js +8 -2
- package/dist/utils/safety.d.ts +8 -1
- package/dist/utils/safety.js +87 -2
- package/package.json +3 -3
- package/src/commands/agents.ts +511 -0
- package/src/commands/ai-agents.ts +87 -9
- package/src/commands/content.ts +3 -3
- package/src/index.ts +11 -1
- package/src/utils/safety.ts +103 -3
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { Command } from 'commander';
|
|
7
|
+
import { initAuditLog } from '@lightdash-tools/common';
|
|
7
8
|
import { registerOrganizationCommand } from './commands/organization';
|
|
8
9
|
import { registerOrganizationRolesCommand } from './commands/organization-roles';
|
|
9
10
|
import { registerProjectsCommand } from './commands/projects';
|
|
@@ -14,6 +15,7 @@ import { registerSpaceAccessCommand } from './commands/space-access';
|
|
|
14
15
|
import { registerChartsCommand } from './commands/charts';
|
|
15
16
|
import { registerDashboardsCommand } from './commands/dashboards';
|
|
16
17
|
import { registerAiAgentsCommand } from './commands/ai-agents';
|
|
18
|
+
import { registerAgentsCommand } from './commands/agents';
|
|
17
19
|
import { registerGroupsCommand } from './commands/groups';
|
|
18
20
|
import { registerUsersCommand } from './commands/users';
|
|
19
21
|
import { registerQueryCommand } from './commands/query';
|
|
@@ -23,16 +25,23 @@ import { registerSchedulersCommand } from './commands/schedulers';
|
|
|
23
25
|
import { registerTagsCommand } from './commands/tags';
|
|
24
26
|
import { registerContentCommand } from './commands/content';
|
|
25
27
|
|
|
28
|
+
// Initialise audit log before any command runs (uses LIGHTDASH_AUDIT_LOG env var).
|
|
29
|
+
initAuditLog(process.env.LIGHTDASH_AUDIT_LOG);
|
|
30
|
+
|
|
26
31
|
const program = new Command();
|
|
27
32
|
|
|
28
33
|
program
|
|
29
34
|
.name('lightdash-ai')
|
|
30
35
|
.description('CLI for Lightdash AI')
|
|
31
|
-
.version('0.2
|
|
36
|
+
.version('0.3.2')
|
|
32
37
|
.option(
|
|
33
38
|
'--safety-mode <mode>',
|
|
34
39
|
'Safety mode (read-only, write-idempotent, write-destructive)',
|
|
35
40
|
'read-only',
|
|
41
|
+
)
|
|
42
|
+
.option(
|
|
43
|
+
'--projects <uuids>',
|
|
44
|
+
'Comma-separated list of allowed project UUIDs (security guardrail)',
|
|
36
45
|
);
|
|
37
46
|
|
|
38
47
|
// Register all commands (organization and projects first so subcommands can attach)
|
|
@@ -46,6 +55,7 @@ registerSpaceAccessCommand(program);
|
|
|
46
55
|
registerChartsCommand(program);
|
|
47
56
|
registerDashboardsCommand(program);
|
|
48
57
|
registerAiAgentsCommand(program);
|
|
58
|
+
registerAgentsCommand(program);
|
|
49
59
|
registerGroupsCommand(program);
|
|
50
60
|
registerUsersCommand(program);
|
|
51
61
|
registerQueryCommand(program);
|
package/src/utils/safety.ts
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
isAllowed,
|
|
3
|
+
getSafetyModeFromEnv,
|
|
4
|
+
SafetyMode,
|
|
5
|
+
logAuditEntry,
|
|
6
|
+
getSessionId,
|
|
7
|
+
areAllProjectsAllowed,
|
|
8
|
+
getAllowedProjectUuidsFromEnv,
|
|
9
|
+
extractProjectUuids,
|
|
10
|
+
} from '@lightdash-tools/common';
|
|
2
11
|
import type { ToolAnnotations } from '@lightdash-tools/common';
|
|
3
12
|
import type { Command } from 'commander';
|
|
4
13
|
|
|
5
14
|
/**
|
|
6
15
|
* Resolves the safety mode from the command line options or environment variables.
|
|
16
|
+
* CLI --safety-mode takes priority over LIGHTDASH_TOOL_SAFETY_MODE.
|
|
7
17
|
*/
|
|
8
18
|
export function getSafetyMode(cmd: Command): SafetyMode {
|
|
9
19
|
const options = cmd.optsWithGlobals() as { safetyMode?: string };
|
|
@@ -14,20 +24,110 @@ export function getSafetyMode(cmd: Command): SafetyMode {
|
|
|
14
24
|
}
|
|
15
25
|
|
|
16
26
|
/**
|
|
17
|
-
*
|
|
27
|
+
* Resolves the allowed project UUIDs from the command line options or environment variables.
|
|
28
|
+
* We specifically want the root --projects flag as the security guardrail.
|
|
29
|
+
* CLI root flags take priority over LIGHTDASH_TOOLS_ALLOWED_PROJECTS.
|
|
30
|
+
*/
|
|
31
|
+
export function getAllowedProjects(cmd: Command): string[] {
|
|
32
|
+
let root = cmd;
|
|
33
|
+
while (root.parent) {
|
|
34
|
+
root = root.parent;
|
|
35
|
+
}
|
|
36
|
+
const options = root.opts() as { projects?: string };
|
|
37
|
+
const raw = options.projects;
|
|
38
|
+
if (raw) {
|
|
39
|
+
return raw
|
|
40
|
+
.split(',')
|
|
41
|
+
.map((s) => s.trim())
|
|
42
|
+
.filter(Boolean);
|
|
43
|
+
}
|
|
44
|
+
return getAllowedProjectUuidsFromEnv();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Builds a human-readable command path (e.g. "lightdash-ai charts list"). */
|
|
48
|
+
function getCommandPath(cmd: Command): string {
|
|
49
|
+
const parts: string[] = [];
|
|
50
|
+
let current: Command | null = cmd;
|
|
51
|
+
while (current) {
|
|
52
|
+
const n = current.name();
|
|
53
|
+
if (n) parts.unshift(n);
|
|
54
|
+
current = current.parent;
|
|
55
|
+
}
|
|
56
|
+
return parts.join(' ');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Wraps a CLI action with safety-mode enforcement and audit logging.
|
|
18
61
|
*/
|
|
19
62
|
export function wrapAction<T extends unknown[]>(
|
|
20
63
|
annotations: ToolAnnotations,
|
|
21
64
|
action: (...args: T) => Promise<void> | void,
|
|
22
65
|
) {
|
|
23
66
|
return async function (this: Command, ...args: T) {
|
|
67
|
+
const start = Date.now();
|
|
68
|
+
const commandPath = getCommandPath(this);
|
|
24
69
|
const mode = getSafetyMode(this);
|
|
70
|
+
const allowedProjects = getAllowedProjects(this);
|
|
71
|
+
const targetProjects = extractProjectUuids(args);
|
|
72
|
+
|
|
73
|
+
// ── Safety Mode Enforcement ──────────────────────────────────────────────
|
|
25
74
|
if (!isAllowed(mode, annotations)) {
|
|
75
|
+
logAuditEntry({
|
|
76
|
+
timestamp: new Date().toISOString(),
|
|
77
|
+
sessionId: getSessionId(),
|
|
78
|
+
tool: commandPath,
|
|
79
|
+
projectUuids: targetProjects.length > 0 ? targetProjects : undefined,
|
|
80
|
+
status: 'blocked',
|
|
81
|
+
durationMs: Date.now() - start,
|
|
82
|
+
});
|
|
26
83
|
console.error(
|
|
27
84
|
`Error: This command is disabled in ${mode} mode. To enable it, use --safety-mode or set LIGHTDASH_TOOL_SAFETY_MODE.`,
|
|
28
85
|
);
|
|
29
86
|
process.exit(1);
|
|
30
87
|
}
|
|
31
|
-
|
|
88
|
+
|
|
89
|
+
// ── Project Guardrail Enforcement ────────────────────────────────────────
|
|
90
|
+
if (allowedProjects.length > 0) {
|
|
91
|
+
const deniedProjects = targetProjects.filter(
|
|
92
|
+
(p) => !areAllProjectsAllowed(allowedProjects, [p]),
|
|
93
|
+
);
|
|
94
|
+
if (deniedProjects.length > 0) {
|
|
95
|
+
logAuditEntry({
|
|
96
|
+
timestamp: new Date().toISOString(),
|
|
97
|
+
sessionId: getSessionId(),
|
|
98
|
+
tool: commandPath,
|
|
99
|
+
projectUuids: targetProjects,
|
|
100
|
+
status: 'blocked',
|
|
101
|
+
durationMs: Date.now() - start,
|
|
102
|
+
});
|
|
103
|
+
console.error(
|
|
104
|
+
`Error: Project(s) [${deniedProjects.join(', ')}] are not in the list of allowed projects. Allowed: [${allowedProjects.join(', ')}].`,
|
|
105
|
+
);
|
|
106
|
+
process.exit(1);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
const result = await action.apply(this, args);
|
|
112
|
+
logAuditEntry({
|
|
113
|
+
timestamp: new Date().toISOString(),
|
|
114
|
+
sessionId: getSessionId(),
|
|
115
|
+
tool: commandPath,
|
|
116
|
+
projectUuids: targetProjects.length > 0 ? targetProjects : undefined,
|
|
117
|
+
status: 'success',
|
|
118
|
+
durationMs: Date.now() - start,
|
|
119
|
+
});
|
|
120
|
+
return result;
|
|
121
|
+
} catch (err) {
|
|
122
|
+
logAuditEntry({
|
|
123
|
+
timestamp: new Date().toISOString(),
|
|
124
|
+
sessionId: getSessionId(),
|
|
125
|
+
tool: commandPath,
|
|
126
|
+
projectUuids: targetProjects.length > 0 ? targetProjects : undefined,
|
|
127
|
+
status: 'error',
|
|
128
|
+
durationMs: Date.now() - start,
|
|
129
|
+
});
|
|
130
|
+
throw err;
|
|
131
|
+
}
|
|
32
132
|
};
|
|
33
133
|
}
|