@lightdash-tools/cli 0.4.0 → 0.6.0
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 +34 -3
- package/dist/commands/groups.d.ts +1 -1
- package/dist/commands/groups.js +1 -1
- package/dist/commands/schema.d.ts +11 -0
- package/dist/commands/schema.js +135 -0
- package/dist/commands/schema.test.d.ts +1 -0
- package/dist/commands/schema.test.js +39 -0
- package/dist/commands/users.d.ts +1 -1
- package/dist/commands/users.js +1 -1
- package/dist/index.js +7 -4
- package/dist/utils/safety.d.ts +6 -2
- package/dist/utils/safety.js +69 -3
- package/dist/utils/safety.test.js +45 -0
- package/package.json +4 -4
- package/src/commands/groups.ts +1 -1
- package/src/commands/schema.test.ts +42 -0
- package/src/commands/schema.ts +143 -0
- package/src/commands/users.ts +1 -1
- package/src/index.ts +9 -3
- package/src/utils/safety.test.ts +69 -2
- package/src/utils/safety.ts +70 -3
package/README.md
CHANGED
|
@@ -25,7 +25,11 @@ The CLI requires the following environment variables (consistent with `@lightdas
|
|
|
25
25
|
- `LIGHTDASH_URL` - Lightdash server base URL (e.g., `https://app.lightdash.cloud`)
|
|
26
26
|
- `LIGHTDASH_API_KEY` - Personal access token (PAT)
|
|
27
27
|
- `LIGHTDASH_PROXY_AUTHORIZATION` - Optional proxy authorization header
|
|
28
|
-
- `
|
|
28
|
+
- `LIGHTDASH_TOOLS_SAFETY_MODE` - Optional safety mode (`read-only`, `write-idempotent`, `write-destructive`)
|
|
29
|
+
- `LIGHTDASH_TOOLS_ALLOWED_PROJECTS` - Optional comma-separated project UUIDs (restrict operations to these projects; empty = all allowed)
|
|
30
|
+
- `LIGHTDASH_TOOLS_DRY_RUN` - Set to `1`, `true`, or `yes` to simulate mutating operations without executing
|
|
31
|
+
|
|
32
|
+
Prefer env vars from the parent process. Avoid plaintext `.env` when AI agents have file access. If using `.env`, use [dotenvx](https://dotenvx.com/) for encrypted secrets. See [docs/secrets-and-credentials.md](../../docs/secrets-and-credentials.md).
|
|
29
33
|
|
|
30
34
|
Example:
|
|
31
35
|
|
|
@@ -153,6 +157,23 @@ lightdash-ai ai-agents list
|
|
|
153
157
|
lightdash-ai ai-agents threads --page 1 --page-size 10
|
|
154
158
|
```
|
|
155
159
|
|
|
160
|
+
#### Schema Introspection (Agent-Friendly)
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
# List all introspectable resources
|
|
164
|
+
lightdash-ai schema list
|
|
165
|
+
|
|
166
|
+
# Get schema for a resource (path, method, params)
|
|
167
|
+
lightdash-ai schema get charts.list
|
|
168
|
+
lightdash-ai schema get ai-agents.settings.update
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Global Options
|
|
172
|
+
|
|
173
|
+
- `--safety-mode <mode>` - Override safety mode (`read-only`, `write-idempotent`, `write-destructive`)
|
|
174
|
+
- `--projects <uuids>` - Comma-separated list of allowed project UUIDs
|
|
175
|
+
- `--dry-run` - Simulate mutating operations without executing (same as `LIGHTDASH_TOOLS_DRY_RUN=1`)
|
|
176
|
+
|
|
156
177
|
### Help
|
|
157
178
|
|
|
158
179
|
Get help for any command:
|
|
@@ -174,7 +195,7 @@ The CLI implements a hierarchical safety model to prevent accidental destructive
|
|
|
174
195
|
|
|
175
196
|
### Configuration
|
|
176
197
|
|
|
177
|
-
You can control the safety mode via the `
|
|
198
|
+
You can control the safety mode via the `LIGHTDASH_TOOLS_SAFETY_MODE` environment variable or the global `--safety-mode` flag.
|
|
178
199
|
|
|
179
200
|
- `read-only` (default): Only allows non-modifying operations (e.g., list, get).
|
|
180
201
|
- `write-idempotent`: Allows read operations and non-destructive writes (e.g., upsert, validate run).
|
|
@@ -195,7 +216,17 @@ lightdash-ai --safety-mode read-only users delete <uuid>
|
|
|
195
216
|
```
|
|
196
217
|
|
|
197
218
|
When a command is blocked, you will see an error like:
|
|
198
|
-
`Error: This command is disabled in read-only mode. To enable it, use --safety-mode or set
|
|
219
|
+
`Error: This command is disabled in read-only mode. To enable it, use --safety-mode or set LIGHTDASH_TOOLS_SAFETY_MODE.`
|
|
220
|
+
|
|
221
|
+
## Input Validation
|
|
222
|
+
|
|
223
|
+
The CLI validates all resource IDs (project UUIDs, slugs) before executing. Invalid inputs are rejected:
|
|
224
|
+
|
|
225
|
+
- Control characters (ASCII < 0x20)
|
|
226
|
+
- Query/fragment chars (`?`, `#`, `%`)
|
|
227
|
+
- Path traversal (`..`)
|
|
228
|
+
|
|
229
|
+
This guards against adversarial or hallucinated inputs when used by AI agents. See [docs/agent-context/CONTEXT.md](../../docs/agent-context/CONTEXT.md) for agent-specific guidance.
|
|
199
230
|
|
|
200
231
|
## Error Handling
|
|
201
232
|
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
import type { Command } from 'commander';
|
|
5
5
|
/**
|
|
6
6
|
* Registers the groups command and its subcommands.
|
|
7
|
-
* Uses client.v1.groups (typed client) per ADR-0010
|
|
7
|
+
* Uses client.v1.groups (typed client) per ADR-0010.
|
|
8
8
|
*/
|
|
9
9
|
export declare function registerGroupsCommand(program: Command): void;
|
package/dist/commands/groups.js
CHANGED
|
@@ -18,7 +18,7 @@ const client_1 = require("../utils/client");
|
|
|
18
18
|
const safety_1 = require("../utils/safety");
|
|
19
19
|
/**
|
|
20
20
|
* Registers the groups command and its subcommands.
|
|
21
|
-
* Uses client.v1.groups (typed client) per ADR-0010
|
|
21
|
+
* Uses client.v1.groups (typed client) per ADR-0010.
|
|
22
22
|
*/
|
|
23
23
|
function registerGroupsCommand(program) {
|
|
24
24
|
const groupsCmd = program.command('groups').description('Manage groups');
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema introspection command for agent discoverability.
|
|
3
|
+
* Outputs machine-readable JSON describing API paths, methods, and parameters.
|
|
4
|
+
*/
|
|
5
|
+
import type { Command } from 'commander';
|
|
6
|
+
export declare function getSchema(resource: string): Record<string, unknown> | null;
|
|
7
|
+
export declare function listResources(): string[];
|
|
8
|
+
/**
|
|
9
|
+
* Registers the schema introspection command.
|
|
10
|
+
*/
|
|
11
|
+
export declare function registerSchemaCommand(program: Command): void;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Schema introspection command for agent discoverability.
|
|
4
|
+
* Outputs machine-readable JSON describing API paths, methods, and parameters.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getSchema = getSchema;
|
|
8
|
+
exports.listResources = listResources;
|
|
9
|
+
exports.registerSchemaCommand = registerSchemaCommand;
|
|
10
|
+
const common_1 = require("@lightdash-tools/common");
|
|
11
|
+
const safety_1 = require("../utils/safety");
|
|
12
|
+
/** Maps resource identifiers to API path + method + description. */
|
|
13
|
+
const SCHEMA_REGISTRY = {
|
|
14
|
+
'organization.get': {
|
|
15
|
+
path: '/api/v1/org',
|
|
16
|
+
method: 'GET',
|
|
17
|
+
description: 'Get current organization details',
|
|
18
|
+
},
|
|
19
|
+
'projects.list': {
|
|
20
|
+
path: '/api/v1/org/projects',
|
|
21
|
+
method: 'GET',
|
|
22
|
+
description: 'List all projects in the organization',
|
|
23
|
+
},
|
|
24
|
+
'projects.get': {
|
|
25
|
+
path: '/api/v1/projects/{projectUuid}',
|
|
26
|
+
method: 'GET',
|
|
27
|
+
description: 'Get project by UUID',
|
|
28
|
+
params: ['projectUuid'],
|
|
29
|
+
},
|
|
30
|
+
'charts.list': {
|
|
31
|
+
path: '/api/v1/projects/{projectUuid}/charts',
|
|
32
|
+
method: 'GET',
|
|
33
|
+
description: 'List charts in a project',
|
|
34
|
+
params: ['projectUuid'],
|
|
35
|
+
},
|
|
36
|
+
'charts.code.list': {
|
|
37
|
+
path: '/api/v1/projects/{projectUuid}/charts/code',
|
|
38
|
+
method: 'GET',
|
|
39
|
+
description: 'Get charts in code representation',
|
|
40
|
+
params: ['projectUuid'],
|
|
41
|
+
},
|
|
42
|
+
'charts.code.upsert': {
|
|
43
|
+
path: '/api/v1/projects/{projectUuid}/charts/{slug}/code',
|
|
44
|
+
method: 'POST',
|
|
45
|
+
description: 'Create or update chart by slug',
|
|
46
|
+
params: ['projectUuid', 'slug'],
|
|
47
|
+
},
|
|
48
|
+
'dashboards.list': {
|
|
49
|
+
path: '/api/v1/projects/{projectUuid}/dashboards',
|
|
50
|
+
method: 'GET',
|
|
51
|
+
description: 'List dashboards in a project',
|
|
52
|
+
params: ['projectUuid'],
|
|
53
|
+
},
|
|
54
|
+
'spaces.list': {
|
|
55
|
+
path: '/api/v1/projects/{projectUuid}/spaces',
|
|
56
|
+
method: 'GET',
|
|
57
|
+
description: 'List spaces in a project',
|
|
58
|
+
params: ['projectUuid'],
|
|
59
|
+
},
|
|
60
|
+
'ai-agents.list': {
|
|
61
|
+
path: '/api/v1/aiAgents/admin/agents',
|
|
62
|
+
method: 'GET',
|
|
63
|
+
description: 'List AI agents (admin)',
|
|
64
|
+
},
|
|
65
|
+
'ai-agents.threads': {
|
|
66
|
+
path: '/api/v1/aiAgents/admin/threads',
|
|
67
|
+
method: 'GET',
|
|
68
|
+
description: 'List AI agent threads (admin)',
|
|
69
|
+
},
|
|
70
|
+
'ai-agents.settings.get': {
|
|
71
|
+
path: '/api/v1/aiAgents/admin/settings',
|
|
72
|
+
method: 'GET',
|
|
73
|
+
description: 'Get AI organization settings',
|
|
74
|
+
},
|
|
75
|
+
'ai-agents.settings.update': {
|
|
76
|
+
path: '/api/v1/aiAgents/admin/settings',
|
|
77
|
+
method: 'PATCH',
|
|
78
|
+
description: 'Update AI organization settings',
|
|
79
|
+
},
|
|
80
|
+
'projects.validate.run': {
|
|
81
|
+
path: '/api/v1/projects/{projectUuid}/validate',
|
|
82
|
+
method: 'POST',
|
|
83
|
+
description: 'Start project validation job',
|
|
84
|
+
params: ['projectUuid'],
|
|
85
|
+
},
|
|
86
|
+
'projects.validate.results': {
|
|
87
|
+
path: '/api/v1/projects/{projectUuid}/validate',
|
|
88
|
+
method: 'GET',
|
|
89
|
+
description: 'Get latest validation results',
|
|
90
|
+
params: ['projectUuid'],
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
function getSchema(resource) {
|
|
94
|
+
var _a;
|
|
95
|
+
const entry = SCHEMA_REGISTRY[resource];
|
|
96
|
+
if (!entry)
|
|
97
|
+
return null;
|
|
98
|
+
return {
|
|
99
|
+
resource,
|
|
100
|
+
path: entry.path,
|
|
101
|
+
method: entry.method,
|
|
102
|
+
description: entry.description,
|
|
103
|
+
params: (_a = entry.params) !== null && _a !== void 0 ? _a : [],
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function listResources() {
|
|
107
|
+
return Object.keys(SCHEMA_REGISTRY).sort();
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Registers the schema introspection command.
|
|
111
|
+
*/
|
|
112
|
+
function registerSchemaCommand(program) {
|
|
113
|
+
const schemaCmd = program
|
|
114
|
+
.command('schema')
|
|
115
|
+
.description('Introspect API schema for agent discoverability');
|
|
116
|
+
schemaCmd
|
|
117
|
+
.command('list')
|
|
118
|
+
.description('List all introspectable resources')
|
|
119
|
+
.action((0, safety_1.wrapAction)(common_1.READ_ONLY_DEFAULT, () => {
|
|
120
|
+
const resources = listResources();
|
|
121
|
+
console.log(JSON.stringify({ resources }, null, 2));
|
|
122
|
+
}));
|
|
123
|
+
schemaCmd
|
|
124
|
+
.command('get <resource>')
|
|
125
|
+
.description('Get schema for a resource (e.g. charts.list, ai-agents.settings.get)')
|
|
126
|
+
.action((0, safety_1.wrapAction)(common_1.READ_ONLY_DEFAULT, (resource) => {
|
|
127
|
+
const schema = getSchema(resource);
|
|
128
|
+
if (!schema) {
|
|
129
|
+
const available = listResources().join(', ');
|
|
130
|
+
console.error(`Error: Unknown resource '${resource}'. Available: ${available}`);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
}
|
|
133
|
+
console.log(JSON.stringify(schema, null, 2));
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const schema_1 = require("./schema");
|
|
5
|
+
(0, vitest_1.describe)('schema command', () => {
|
|
6
|
+
(0, vitest_1.describe)('getSchema', () => {
|
|
7
|
+
(0, vitest_1.it)('returns JSON with path, method, params for charts.list', () => {
|
|
8
|
+
const schema = (0, schema_1.getSchema)('charts.list');
|
|
9
|
+
(0, vitest_1.expect)(schema).not.toBeNull();
|
|
10
|
+
(0, vitest_1.expect)(schema).toHaveProperty('path', '/api/v1/projects/{projectUuid}/charts');
|
|
11
|
+
(0, vitest_1.expect)(schema).toHaveProperty('method', 'GET');
|
|
12
|
+
(0, vitest_1.expect)(schema).toHaveProperty('params');
|
|
13
|
+
(0, vitest_1.expect)(schema === null || schema === void 0 ? void 0 : schema.params).toEqual(['projectUuid']);
|
|
14
|
+
(0, vitest_1.expect)(schema).toHaveProperty('description');
|
|
15
|
+
(0, vitest_1.expect)(schema).toHaveProperty('resource', 'charts.list');
|
|
16
|
+
});
|
|
17
|
+
(0, vitest_1.it)('returns schema for ai-agents.settings.update', () => {
|
|
18
|
+
const schema = (0, schema_1.getSchema)('ai-agents.settings.update');
|
|
19
|
+
(0, vitest_1.expect)(schema).not.toBeNull();
|
|
20
|
+
(0, vitest_1.expect)(schema).toHaveProperty('path', '/api/v1/aiAgents/admin/settings');
|
|
21
|
+
(0, vitest_1.expect)(schema).toHaveProperty('method', 'PATCH');
|
|
22
|
+
});
|
|
23
|
+
(0, vitest_1.it)('returns null for unknown resource', () => {
|
|
24
|
+
const schema = (0, schema_1.getSchema)('unknown.resource');
|
|
25
|
+
(0, vitest_1.expect)(schema).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
(0, vitest_1.describe)('listResources', () => {
|
|
29
|
+
(0, vitest_1.it)('returns sorted list of resource identifiers', () => {
|
|
30
|
+
const resources = (0, schema_1.listResources)();
|
|
31
|
+
(0, vitest_1.expect)(resources).toContain('charts.list');
|
|
32
|
+
(0, vitest_1.expect)(resources).toContain('ai-agents.settings.get');
|
|
33
|
+
(0, vitest_1.expect)(resources).toEqual([...resources].sort());
|
|
34
|
+
});
|
|
35
|
+
(0, vitest_1.it)('includes charts.list', () => {
|
|
36
|
+
(0, vitest_1.expect)((0, schema_1.listResources)()).toContain('charts.list');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
package/dist/commands/users.d.ts
CHANGED
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
import type { Command } from 'commander';
|
|
5
5
|
/**
|
|
6
6
|
* Registers the users command and its subcommands.
|
|
7
|
-
* Uses client.v1.users (typed client) per ADR-0010
|
|
7
|
+
* Uses client.v1.users (typed client) per ADR-0010.
|
|
8
8
|
*/
|
|
9
9
|
export declare function registerUsersCommand(program: Command): void;
|
package/dist/commands/users.js
CHANGED
|
@@ -18,7 +18,7 @@ const client_1 = require("../utils/client");
|
|
|
18
18
|
const safety_1 = require("../utils/safety");
|
|
19
19
|
/**
|
|
20
20
|
* Registers the users command and its subcommands.
|
|
21
|
-
* Uses client.v1.users (typed client) per ADR-0010
|
|
21
|
+
* Uses client.v1.users (typed client) per ADR-0010.
|
|
22
22
|
*/
|
|
23
23
|
function registerUsersCommand(program) {
|
|
24
24
|
const usersCmd = program.command('users').description('Manage users');
|
package/dist/index.js
CHANGED
|
@@ -25,15 +25,17 @@ const metrics_1 = require("./commands/metrics");
|
|
|
25
25
|
const schedulers_1 = require("./commands/schedulers");
|
|
26
26
|
const tags_1 = require("./commands/tags");
|
|
27
27
|
const content_1 = require("./commands/content");
|
|
28
|
-
|
|
29
|
-
(
|
|
28
|
+
const schema_1 = require("./commands/schema");
|
|
29
|
+
// Initialise audit log before any command runs (uses LIGHTDASH_TOOLS_AUDIT_LOG env var).
|
|
30
|
+
(0, common_1.initAuditLog)(process.env.LIGHTDASH_TOOLS_AUDIT_LOG);
|
|
30
31
|
const program = new commander_1.Command();
|
|
31
32
|
program
|
|
32
33
|
.name('lightdash-ai')
|
|
33
34
|
.description('CLI for Lightdash AI')
|
|
34
|
-
.version('0.
|
|
35
|
+
.version('0.6.0')
|
|
35
36
|
.option('--safety-mode <mode>', 'Safety mode (read-only, write-idempotent, write-destructive)', 'read-only')
|
|
36
|
-
.option('--projects <uuids>', 'Comma-separated list of allowed project UUIDs (security guardrail)')
|
|
37
|
+
.option('--projects <uuids>', 'Comma-separated list of allowed project UUIDs (security guardrail)')
|
|
38
|
+
.option('--dry-run', 'Simulate mutating operations without executing (env: LIGHTDASH_TOOLS_DRY_RUN=1)');
|
|
37
39
|
// Register all commands (organization and projects first so subcommands can attach)
|
|
38
40
|
(0, organization_1.registerOrganizationCommand)(program);
|
|
39
41
|
(0, organization_roles_1.registerOrganizationRolesCommand)(program);
|
|
@@ -54,5 +56,6 @@ program
|
|
|
54
56
|
(0, schedulers_1.registerSchedulersCommand)(program);
|
|
55
57
|
(0, tags_1.registerTagsCommand)(program);
|
|
56
58
|
(0, content_1.registerContentCommand)(program);
|
|
59
|
+
(0, schema_1.registerSchemaCommand)(program);
|
|
57
60
|
// Parse command line arguments
|
|
58
61
|
program.parse(process.argv);
|
package/dist/utils/safety.d.ts
CHANGED
|
@@ -3,13 +3,17 @@ import type { ToolAnnotations } from '@lightdash-tools/common';
|
|
|
3
3
|
import type { Command } from 'commander';
|
|
4
4
|
/**
|
|
5
5
|
* Resolves the safety mode from the command line options or environment variables.
|
|
6
|
-
* CLI --safety-mode takes priority over
|
|
6
|
+
* CLI --safety-mode takes priority over LIGHTDASH_TOOLS_SAFETY_MODE.
|
|
7
7
|
*/
|
|
8
8
|
export declare function getSafetyMode(cmd: Command): SafetyMode;
|
|
9
|
+
/**
|
|
10
|
+
* Resolves whether dry-run mode is active from CLI or environment.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isDryRun(cmd: Command): boolean;
|
|
9
13
|
/**
|
|
10
14
|
* Resolves the allowed project UUIDs from the command line options or environment variables.
|
|
11
15
|
* We specifically want the root --projects flag as the security guardrail.
|
|
12
|
-
* CLI
|
|
16
|
+
* CLI --projects takes priority over LIGHTDASH_TOOLS_ALLOWED_PROJECTS.
|
|
13
17
|
*/
|
|
14
18
|
export declare function getAllowedProjects(cmd: Command): string[];
|
|
15
19
|
/**
|
package/dist/utils/safety.js
CHANGED
|
@@ -10,12 +10,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.getSafetyMode = getSafetyMode;
|
|
13
|
+
exports.isDryRun = isDryRun;
|
|
13
14
|
exports.getAllowedProjects = getAllowedProjects;
|
|
14
15
|
exports.wrapAction = wrapAction;
|
|
15
16
|
const common_1 = require("@lightdash-tools/common");
|
|
16
17
|
/**
|
|
17
18
|
* Resolves the safety mode from the command line options or environment variables.
|
|
18
|
-
* CLI --safety-mode takes priority over
|
|
19
|
+
* CLI --safety-mode takes priority over LIGHTDASH_TOOLS_SAFETY_MODE.
|
|
19
20
|
*/
|
|
20
21
|
function getSafetyMode(cmd) {
|
|
21
22
|
const options = cmd.optsWithGlobals();
|
|
@@ -24,10 +25,24 @@ function getSafetyMode(cmd) {
|
|
|
24
25
|
}
|
|
25
26
|
return (0, common_1.getSafetyModeFromEnv)();
|
|
26
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolves whether dry-run mode is active from CLI or environment.
|
|
30
|
+
*/
|
|
31
|
+
function isDryRun(cmd) {
|
|
32
|
+
let root = cmd;
|
|
33
|
+
while (root.parent) {
|
|
34
|
+
root = root.parent;
|
|
35
|
+
}
|
|
36
|
+
const options = root.opts();
|
|
37
|
+
if (options.dryRun === true)
|
|
38
|
+
return true;
|
|
39
|
+
const v = process.env.LIGHTDASH_TOOLS_DRY_RUN;
|
|
40
|
+
return v === '1' || v === 'true' || v === 'yes';
|
|
41
|
+
}
|
|
27
42
|
/**
|
|
28
43
|
* Resolves the allowed project UUIDs from the command line options or environment variables.
|
|
29
44
|
* We specifically want the root --projects flag as the security guardrail.
|
|
30
|
-
* CLI
|
|
45
|
+
* CLI --projects takes priority over LIGHTDASH_TOOLS_ALLOWED_PROJECTS.
|
|
31
46
|
*/
|
|
32
47
|
function getAllowedProjects(cmd) {
|
|
33
48
|
let root = cmd;
|
|
@@ -67,6 +82,43 @@ function wrapAction(annotations, action) {
|
|
|
67
82
|
const mode = getSafetyMode(this);
|
|
68
83
|
const allowedProjects = getAllowedProjects(this);
|
|
69
84
|
const targetProjects = (0, common_1.extractProjectUuids)(args);
|
|
85
|
+
// ── Input Validation ─────────────────────────────────────────────────────
|
|
86
|
+
// Validate only known identifier fields (projectUuid, slug, etc.) in objects.
|
|
87
|
+
// Do NOT validate bare positional strings—they may be free-form (query, name, etc.).
|
|
88
|
+
function validateStringArgs(values) {
|
|
89
|
+
for (const v of values) {
|
|
90
|
+
if (Array.isArray(v)) {
|
|
91
|
+
validateStringArgs(v);
|
|
92
|
+
}
|
|
93
|
+
else if (v && typeof v === 'object') {
|
|
94
|
+
const o = v;
|
|
95
|
+
for (const key of ['project', 'projectUuid', 'projectUuids', 'projects', 'slug']) {
|
|
96
|
+
const val = o[key];
|
|
97
|
+
if (typeof val === 'string')
|
|
98
|
+
(0, common_1.validateResourceId)(val);
|
|
99
|
+
else if (Array.isArray(val))
|
|
100
|
+
for (const item of val)
|
|
101
|
+
if (typeof item === 'string')
|
|
102
|
+
(0, common_1.validateResourceId)(item);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
validateStringArgs(args);
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
(0, common_1.logAuditEntry)({
|
|
112
|
+
timestamp: new Date().toISOString(),
|
|
113
|
+
sessionId: (0, common_1.getSessionId)(),
|
|
114
|
+
tool: commandPath,
|
|
115
|
+
projectUuids: targetProjects,
|
|
116
|
+
status: 'blocked',
|
|
117
|
+
durationMs: Date.now() - start,
|
|
118
|
+
});
|
|
119
|
+
console.error(`Error: Invalid resource ID: ${err instanceof Error ? err.message : String(err)}`);
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
70
122
|
// ── Safety Mode Enforcement ──────────────────────────────────────────────
|
|
71
123
|
if (!(0, common_1.isAllowed)(mode, annotations)) {
|
|
72
124
|
(0, common_1.logAuditEntry)({
|
|
@@ -77,9 +129,23 @@ function wrapAction(annotations, action) {
|
|
|
77
129
|
status: 'blocked',
|
|
78
130
|
durationMs: Date.now() - start,
|
|
79
131
|
});
|
|
80
|
-
console.error(`Error: This command is disabled in ${mode} mode. To enable it, use --safety-mode or set
|
|
132
|
+
console.error(`Error: This command is disabled in ${mode} mode. To enable it, use --safety-mode or set LIGHTDASH_TOOLS_SAFETY_MODE.`);
|
|
81
133
|
process.exit(1);
|
|
82
134
|
}
|
|
135
|
+
// ── Dry-Run (Write Commands Only) ────────────────────────────────────────
|
|
136
|
+
const isReadOnly = !!annotations.readOnlyHint;
|
|
137
|
+
if (!isReadOnly && isDryRun(this)) {
|
|
138
|
+
(0, common_1.logAuditEntry)({
|
|
139
|
+
timestamp: new Date().toISOString(),
|
|
140
|
+
sessionId: (0, common_1.getSessionId)(),
|
|
141
|
+
tool: commandPath,
|
|
142
|
+
projectUuids: targetProjects.length > 0 ? targetProjects : undefined,
|
|
143
|
+
status: 'blocked',
|
|
144
|
+
durationMs: Date.now() - start,
|
|
145
|
+
});
|
|
146
|
+
console.log(`[DRY-RUN] Would execute: ${commandPath} with args: ${JSON.stringify(args)}. No changes were made.`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
83
149
|
// ── Project Guardrail Enforcement ────────────────────────────────────────
|
|
84
150
|
if (allowedProjects.length > 0) {
|
|
85
151
|
const deniedProjects = targetProjects.filter((p) => !(0, common_1.areAllProjectsAllowed)(allowedProjects, [p]));
|
|
@@ -16,11 +16,16 @@ const commander_1 = require("commander");
|
|
|
16
16
|
(0, vitest_1.describe)('CLI wrapAction', () => {
|
|
17
17
|
const mockAction = vitest_1.vi.fn().mockResolvedValue(undefined);
|
|
18
18
|
const consoleErrorSpy = vitest_1.vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
19
|
+
const consoleLogSpy = vitest_1.vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
19
20
|
const processExitSpy = vitest_1.vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
20
21
|
throw new Error('exit');
|
|
21
22
|
});
|
|
22
23
|
(0, vitest_1.beforeEach)(() => {
|
|
23
24
|
vitest_1.vi.clearAllMocks();
|
|
25
|
+
delete process.env.LIGHTDASH_TOOLS_DRY_RUN;
|
|
26
|
+
});
|
|
27
|
+
(0, vitest_1.afterEach)(() => {
|
|
28
|
+
delete process.env.LIGHTDASH_TOOLS_DRY_RUN;
|
|
24
29
|
});
|
|
25
30
|
(0, vitest_1.it)('should allow read-only action in read-only mode', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
31
|
const cmd = new commander_1.Command();
|
|
@@ -47,4 +52,44 @@ const commander_1 = require("commander");
|
|
|
47
52
|
(0, vitest_1.expect)(mockAction).toHaveBeenCalledWith('arg1');
|
|
48
53
|
(0, vitest_1.expect)(processExitSpy).not.toHaveBeenCalled();
|
|
49
54
|
}));
|
|
55
|
+
(0, vitest_1.it)('should reject invalid projectUuid in options before calling handler', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
+
const cmd = new commander_1.Command();
|
|
57
|
+
cmd.setOptionValueWithSource('safetyMode', common_1.SafetyMode.READ_ONLY, 'cli');
|
|
58
|
+
const wrapped = (0, safety_1.wrapAction)(common_1.READ_ONLY_DEFAULT, mockAction);
|
|
59
|
+
yield (0, vitest_1.expect)(wrapped.call(cmd, 'arg1', { projectUuid: 'uuid?fields=name' })).rejects.toThrow('exit');
|
|
60
|
+
(0, vitest_1.expect)(mockAction).not.toHaveBeenCalled();
|
|
61
|
+
(0, vitest_1.expect)(consoleErrorSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining('Invalid resource ID'));
|
|
62
|
+
(0, vitest_1.expect)(processExitSpy).toHaveBeenCalledWith(1);
|
|
63
|
+
}));
|
|
64
|
+
(0, vitest_1.it)('should allow free-form query strings in positionals', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
65
|
+
const cmd = new commander_1.Command();
|
|
66
|
+
cmd.setOptionValueWithSource('safetyMode', common_1.SafetyMode.READ_ONLY, 'cli');
|
|
67
|
+
const wrapped = (0, safety_1.wrapAction)(common_1.READ_ONLY_DEFAULT, mockAction);
|
|
68
|
+
yield wrapped.call(cmd, 'what?');
|
|
69
|
+
(0, vitest_1.expect)(mockAction).toHaveBeenCalledWith('what?');
|
|
70
|
+
(0, vitest_1.expect)(processExitSpy).not.toHaveBeenCalled();
|
|
71
|
+
mockAction.mockClear();
|
|
72
|
+
yield wrapped.call(cmd, 'growth%');
|
|
73
|
+
(0, vitest_1.expect)(mockAction).toHaveBeenCalledWith('growth%');
|
|
74
|
+
(0, vitest_1.expect)(processExitSpy).not.toHaveBeenCalled();
|
|
75
|
+
}));
|
|
76
|
+
(0, vitest_1.it)('should simulate write command when LIGHTDASH_TOOLS_DRY_RUN=1', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
|
+
process.env.LIGHTDASH_TOOLS_DRY_RUN = '1';
|
|
78
|
+
const cmd = new commander_1.Command();
|
|
79
|
+
cmd.setOptionValueWithSource('safetyMode', common_1.SafetyMode.WRITE_IDEMPOTENT, 'cli');
|
|
80
|
+
const wrapped = (0, safety_1.wrapAction)(common_1.WRITE_IDEMPOTENT, mockAction);
|
|
81
|
+
yield wrapped.call(cmd, 'project-uuid-123');
|
|
82
|
+
(0, vitest_1.expect)(mockAction).not.toHaveBeenCalled();
|
|
83
|
+
(0, vitest_1.expect)(consoleLogSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining('[DRY-RUN] Would execute'));
|
|
84
|
+
(0, vitest_1.expect)(consoleLogSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining('No changes were made'));
|
|
85
|
+
}));
|
|
86
|
+
(0, vitest_1.it)('should execute read command even when LIGHTDASH_TOOLS_DRY_RUN=1', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
87
|
+
process.env.LIGHTDASH_TOOLS_DRY_RUN = '1';
|
|
88
|
+
const cmd = new commander_1.Command();
|
|
89
|
+
cmd.setOptionValueWithSource('safetyMode', common_1.SafetyMode.READ_ONLY, 'cli');
|
|
90
|
+
const wrapped = (0, safety_1.wrapAction)(common_1.READ_ONLY_DEFAULT, mockAction);
|
|
91
|
+
yield wrapped.call(cmd, 'arg1');
|
|
92
|
+
(0, vitest_1.expect)(mockAction).toHaveBeenCalledWith('arg1');
|
|
93
|
+
(0, vitest_1.expect)(consoleLogSpy).not.toHaveBeenCalledWith(vitest_1.expect.stringContaining('[DRY-RUN]'));
|
|
94
|
+
}));
|
|
50
95
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightdash-tools/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "CLI for Lightdash AI.",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"commander": "^14.0.3",
|
|
15
|
-
"@lightdash-tools/
|
|
16
|
-
"@lightdash-tools/
|
|
15
|
+
"@lightdash-tools/common": "0.6.0",
|
|
16
|
+
"@lightdash-tools/client": "0.6.0"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@types/node": "^25.
|
|
19
|
+
"@types/node": "^25.3.4"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc"
|
package/src/commands/groups.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { wrapAction } from '../utils/safety';
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Registers the groups command and its subcommands.
|
|
12
|
-
* Uses client.v1.groups (typed client) per ADR-0010
|
|
12
|
+
* Uses client.v1.groups (typed client) per ADR-0010.
|
|
13
13
|
*/
|
|
14
14
|
export function registerGroupsCommand(program: Command): void {
|
|
15
15
|
const groupsCmd = program.command('groups').description('Manage groups');
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { getSchema, listResources } from './schema';
|
|
3
|
+
|
|
4
|
+
describe('schema command', () => {
|
|
5
|
+
describe('getSchema', () => {
|
|
6
|
+
it('returns JSON with path, method, params for charts.list', () => {
|
|
7
|
+
const schema = getSchema('charts.list');
|
|
8
|
+
expect(schema).not.toBeNull();
|
|
9
|
+
expect(schema).toHaveProperty('path', '/api/v1/projects/{projectUuid}/charts');
|
|
10
|
+
expect(schema).toHaveProperty('method', 'GET');
|
|
11
|
+
expect(schema).toHaveProperty('params');
|
|
12
|
+
expect(schema?.params).toEqual(['projectUuid']);
|
|
13
|
+
expect(schema).toHaveProperty('description');
|
|
14
|
+
expect(schema).toHaveProperty('resource', 'charts.list');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('returns schema for ai-agents.settings.update', () => {
|
|
18
|
+
const schema = getSchema('ai-agents.settings.update');
|
|
19
|
+
expect(schema).not.toBeNull();
|
|
20
|
+
expect(schema).toHaveProperty('path', '/api/v1/aiAgents/admin/settings');
|
|
21
|
+
expect(schema).toHaveProperty('method', 'PATCH');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('returns null for unknown resource', () => {
|
|
25
|
+
const schema = getSchema('unknown.resource');
|
|
26
|
+
expect(schema).toBeNull();
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('listResources', () => {
|
|
31
|
+
it('returns sorted list of resource identifiers', () => {
|
|
32
|
+
const resources = listResources();
|
|
33
|
+
expect(resources).toContain('charts.list');
|
|
34
|
+
expect(resources).toContain('ai-agents.settings.get');
|
|
35
|
+
expect(resources).toEqual([...resources].sort());
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('includes charts.list', () => {
|
|
39
|
+
expect(listResources()).toContain('charts.list');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema introspection command for agent discoverability.
|
|
3
|
+
* Outputs machine-readable JSON describing API paths, methods, and parameters.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Command } from 'commander';
|
|
7
|
+
import { READ_ONLY_DEFAULT } from '@lightdash-tools/common';
|
|
8
|
+
import { wrapAction } from '../utils/safety';
|
|
9
|
+
|
|
10
|
+
/** Maps resource identifiers to API path + method + description. */
|
|
11
|
+
const SCHEMA_REGISTRY: Record<
|
|
12
|
+
string,
|
|
13
|
+
{ path: string; method: string; description: string; params?: string[] }
|
|
14
|
+
> = {
|
|
15
|
+
'organization.get': {
|
|
16
|
+
path: '/api/v1/org',
|
|
17
|
+
method: 'GET',
|
|
18
|
+
description: 'Get current organization details',
|
|
19
|
+
},
|
|
20
|
+
'projects.list': {
|
|
21
|
+
path: '/api/v1/org/projects',
|
|
22
|
+
method: 'GET',
|
|
23
|
+
description: 'List all projects in the organization',
|
|
24
|
+
},
|
|
25
|
+
'projects.get': {
|
|
26
|
+
path: '/api/v1/projects/{projectUuid}',
|
|
27
|
+
method: 'GET',
|
|
28
|
+
description: 'Get project by UUID',
|
|
29
|
+
params: ['projectUuid'],
|
|
30
|
+
},
|
|
31
|
+
'charts.list': {
|
|
32
|
+
path: '/api/v1/projects/{projectUuid}/charts',
|
|
33
|
+
method: 'GET',
|
|
34
|
+
description: 'List charts in a project',
|
|
35
|
+
params: ['projectUuid'],
|
|
36
|
+
},
|
|
37
|
+
'charts.code.list': {
|
|
38
|
+
path: '/api/v1/projects/{projectUuid}/charts/code',
|
|
39
|
+
method: 'GET',
|
|
40
|
+
description: 'Get charts in code representation',
|
|
41
|
+
params: ['projectUuid'],
|
|
42
|
+
},
|
|
43
|
+
'charts.code.upsert': {
|
|
44
|
+
path: '/api/v1/projects/{projectUuid}/charts/{slug}/code',
|
|
45
|
+
method: 'POST',
|
|
46
|
+
description: 'Create or update chart by slug',
|
|
47
|
+
params: ['projectUuid', 'slug'],
|
|
48
|
+
},
|
|
49
|
+
'dashboards.list': {
|
|
50
|
+
path: '/api/v1/projects/{projectUuid}/dashboards',
|
|
51
|
+
method: 'GET',
|
|
52
|
+
description: 'List dashboards in a project',
|
|
53
|
+
params: ['projectUuid'],
|
|
54
|
+
},
|
|
55
|
+
'spaces.list': {
|
|
56
|
+
path: '/api/v1/projects/{projectUuid}/spaces',
|
|
57
|
+
method: 'GET',
|
|
58
|
+
description: 'List spaces in a project',
|
|
59
|
+
params: ['projectUuid'],
|
|
60
|
+
},
|
|
61
|
+
'ai-agents.list': {
|
|
62
|
+
path: '/api/v1/aiAgents/admin/agents',
|
|
63
|
+
method: 'GET',
|
|
64
|
+
description: 'List AI agents (admin)',
|
|
65
|
+
},
|
|
66
|
+
'ai-agents.threads': {
|
|
67
|
+
path: '/api/v1/aiAgents/admin/threads',
|
|
68
|
+
method: 'GET',
|
|
69
|
+
description: 'List AI agent threads (admin)',
|
|
70
|
+
},
|
|
71
|
+
'ai-agents.settings.get': {
|
|
72
|
+
path: '/api/v1/aiAgents/admin/settings',
|
|
73
|
+
method: 'GET',
|
|
74
|
+
description: 'Get AI organization settings',
|
|
75
|
+
},
|
|
76
|
+
'ai-agents.settings.update': {
|
|
77
|
+
path: '/api/v1/aiAgents/admin/settings',
|
|
78
|
+
method: 'PATCH',
|
|
79
|
+
description: 'Update AI organization settings',
|
|
80
|
+
},
|
|
81
|
+
'projects.validate.run': {
|
|
82
|
+
path: '/api/v1/projects/{projectUuid}/validate',
|
|
83
|
+
method: 'POST',
|
|
84
|
+
description: 'Start project validation job',
|
|
85
|
+
params: ['projectUuid'],
|
|
86
|
+
},
|
|
87
|
+
'projects.validate.results': {
|
|
88
|
+
path: '/api/v1/projects/{projectUuid}/validate',
|
|
89
|
+
method: 'GET',
|
|
90
|
+
description: 'Get latest validation results',
|
|
91
|
+
params: ['projectUuid'],
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export function getSchema(resource: string): Record<string, unknown> | null {
|
|
96
|
+
const entry = SCHEMA_REGISTRY[resource];
|
|
97
|
+
if (!entry) return null;
|
|
98
|
+
return {
|
|
99
|
+
resource,
|
|
100
|
+
path: entry.path,
|
|
101
|
+
method: entry.method,
|
|
102
|
+
description: entry.description,
|
|
103
|
+
params: entry.params ?? [],
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function listResources(): string[] {
|
|
108
|
+
return Object.keys(SCHEMA_REGISTRY).sort();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Registers the schema introspection command.
|
|
113
|
+
*/
|
|
114
|
+
export function registerSchemaCommand(program: Command): void {
|
|
115
|
+
const schemaCmd = program
|
|
116
|
+
.command('schema')
|
|
117
|
+
.description('Introspect API schema for agent discoverability');
|
|
118
|
+
|
|
119
|
+
schemaCmd
|
|
120
|
+
.command('list')
|
|
121
|
+
.description('List all introspectable resources')
|
|
122
|
+
.action(
|
|
123
|
+
wrapAction(READ_ONLY_DEFAULT, () => {
|
|
124
|
+
const resources = listResources();
|
|
125
|
+
console.log(JSON.stringify({ resources }, null, 2));
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
schemaCmd
|
|
130
|
+
.command('get <resource>')
|
|
131
|
+
.description('Get schema for a resource (e.g. charts.list, ai-agents.settings.get)')
|
|
132
|
+
.action(
|
|
133
|
+
wrapAction(READ_ONLY_DEFAULT, (resource: string) => {
|
|
134
|
+
const schema = getSchema(resource);
|
|
135
|
+
if (!schema) {
|
|
136
|
+
const available = listResources().join(', ');
|
|
137
|
+
console.error(`Error: Unknown resource '${resource}'. Available: ${available}`);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
}
|
|
140
|
+
console.log(JSON.stringify(schema, null, 2));
|
|
141
|
+
}),
|
|
142
|
+
);
|
|
143
|
+
}
|
package/src/commands/users.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { wrapAction } from '../utils/safety';
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Registers the users command and its subcommands.
|
|
12
|
-
* Uses client.v1.users (typed client) per ADR-0010
|
|
12
|
+
* Uses client.v1.users (typed client) per ADR-0010.
|
|
13
13
|
*/
|
|
14
14
|
export function registerUsersCommand(program: Command): void {
|
|
15
15
|
const usersCmd = program.command('users').description('Manage users');
|
package/src/index.ts
CHANGED
|
@@ -24,16 +24,17 @@ import { registerMetricsCommand } from './commands/metrics';
|
|
|
24
24
|
import { registerSchedulersCommand } from './commands/schedulers';
|
|
25
25
|
import { registerTagsCommand } from './commands/tags';
|
|
26
26
|
import { registerContentCommand } from './commands/content';
|
|
27
|
+
import { registerSchemaCommand } from './commands/schema';
|
|
27
28
|
|
|
28
|
-
// Initialise audit log before any command runs (uses
|
|
29
|
-
initAuditLog(process.env.
|
|
29
|
+
// Initialise audit log before any command runs (uses LIGHTDASH_TOOLS_AUDIT_LOG env var).
|
|
30
|
+
initAuditLog(process.env.LIGHTDASH_TOOLS_AUDIT_LOG);
|
|
30
31
|
|
|
31
32
|
const program = new Command();
|
|
32
33
|
|
|
33
34
|
program
|
|
34
35
|
.name('lightdash-ai')
|
|
35
36
|
.description('CLI for Lightdash AI')
|
|
36
|
-
.version('0.
|
|
37
|
+
.version('0.6.0')
|
|
37
38
|
.option(
|
|
38
39
|
'--safety-mode <mode>',
|
|
39
40
|
'Safety mode (read-only, write-idempotent, write-destructive)',
|
|
@@ -42,6 +43,10 @@ program
|
|
|
42
43
|
.option(
|
|
43
44
|
'--projects <uuids>',
|
|
44
45
|
'Comma-separated list of allowed project UUIDs (security guardrail)',
|
|
46
|
+
)
|
|
47
|
+
.option(
|
|
48
|
+
'--dry-run',
|
|
49
|
+
'Simulate mutating operations without executing (env: LIGHTDASH_TOOLS_DRY_RUN=1)',
|
|
45
50
|
);
|
|
46
51
|
|
|
47
52
|
// Register all commands (organization and projects first so subcommands can attach)
|
|
@@ -64,6 +69,7 @@ registerMetricsCommand(program);
|
|
|
64
69
|
registerSchedulersCommand(program);
|
|
65
70
|
registerTagsCommand(program);
|
|
66
71
|
registerContentCommand(program);
|
|
72
|
+
registerSchemaCommand(program);
|
|
67
73
|
|
|
68
74
|
// Parse command line arguments
|
|
69
75
|
program.parse(process.argv);
|
package/src/utils/safety.test.ts
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
2
|
import { wrapAction } from './safety';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
READ_ONLY_DEFAULT,
|
|
5
|
+
WRITE_DESTRUCTIVE,
|
|
6
|
+
WRITE_IDEMPOTENT,
|
|
7
|
+
SafetyMode,
|
|
8
|
+
} from '@lightdash-tools/common';
|
|
4
9
|
import { Command } from 'commander';
|
|
5
10
|
|
|
6
11
|
describe('CLI wrapAction', () => {
|
|
7
12
|
const mockAction = vi.fn().mockResolvedValue(undefined);
|
|
8
13
|
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
14
|
+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
9
15
|
const processExitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
10
16
|
throw new Error('exit');
|
|
11
17
|
});
|
|
12
18
|
|
|
13
19
|
beforeEach(() => {
|
|
14
20
|
vi.clearAllMocks();
|
|
21
|
+
delete process.env.LIGHTDASH_TOOLS_DRY_RUN;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
delete process.env.LIGHTDASH_TOOLS_DRY_RUN;
|
|
15
26
|
});
|
|
16
27
|
|
|
17
28
|
it('should allow read-only action in read-only mode', async () => {
|
|
@@ -50,4 +61,60 @@ describe('CLI wrapAction', () => {
|
|
|
50
61
|
expect(mockAction).toHaveBeenCalledWith('arg1');
|
|
51
62
|
expect(processExitSpy).not.toHaveBeenCalled();
|
|
52
63
|
});
|
|
64
|
+
|
|
65
|
+
it('should reject invalid projectUuid in options before calling handler', async () => {
|
|
66
|
+
const cmd = new Command();
|
|
67
|
+
cmd.setOptionValueWithSource('safetyMode', SafetyMode.READ_ONLY, 'cli');
|
|
68
|
+
|
|
69
|
+
const wrapped = wrapAction(READ_ONLY_DEFAULT, mockAction);
|
|
70
|
+
|
|
71
|
+
await expect(wrapped.call(cmd, 'arg1', { projectUuid: 'uuid?fields=name' })).rejects.toThrow(
|
|
72
|
+
'exit',
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(mockAction).not.toHaveBeenCalled();
|
|
76
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid resource ID'));
|
|
77
|
+
expect(processExitSpy).toHaveBeenCalledWith(1);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should allow free-form query strings in positionals', async () => {
|
|
81
|
+
const cmd = new Command();
|
|
82
|
+
cmd.setOptionValueWithSource('safetyMode', SafetyMode.READ_ONLY, 'cli');
|
|
83
|
+
|
|
84
|
+
const wrapped = wrapAction(READ_ONLY_DEFAULT, mockAction);
|
|
85
|
+
|
|
86
|
+
await wrapped.call(cmd, 'what?');
|
|
87
|
+
expect(mockAction).toHaveBeenCalledWith('what?');
|
|
88
|
+
expect(processExitSpy).not.toHaveBeenCalled();
|
|
89
|
+
|
|
90
|
+
mockAction.mockClear();
|
|
91
|
+
await wrapped.call(cmd, 'growth%');
|
|
92
|
+
expect(mockAction).toHaveBeenCalledWith('growth%');
|
|
93
|
+
expect(processExitSpy).not.toHaveBeenCalled();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should simulate write command when LIGHTDASH_TOOLS_DRY_RUN=1', async () => {
|
|
97
|
+
process.env.LIGHTDASH_TOOLS_DRY_RUN = '1';
|
|
98
|
+
const cmd = new Command();
|
|
99
|
+
cmd.setOptionValueWithSource('safetyMode', SafetyMode.WRITE_IDEMPOTENT, 'cli');
|
|
100
|
+
|
|
101
|
+
const wrapped = wrapAction(WRITE_IDEMPOTENT, mockAction);
|
|
102
|
+
await wrapped.call(cmd, 'project-uuid-123');
|
|
103
|
+
|
|
104
|
+
expect(mockAction).not.toHaveBeenCalled();
|
|
105
|
+
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('[DRY-RUN] Would execute'));
|
|
106
|
+
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('No changes were made'));
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should execute read command even when LIGHTDASH_TOOLS_DRY_RUN=1', async () => {
|
|
110
|
+
process.env.LIGHTDASH_TOOLS_DRY_RUN = '1';
|
|
111
|
+
const cmd = new Command();
|
|
112
|
+
cmd.setOptionValueWithSource('safetyMode', SafetyMode.READ_ONLY, 'cli');
|
|
113
|
+
|
|
114
|
+
const wrapped = wrapAction(READ_ONLY_DEFAULT, mockAction);
|
|
115
|
+
await wrapped.call(cmd, 'arg1');
|
|
116
|
+
|
|
117
|
+
expect(mockAction).toHaveBeenCalledWith('arg1');
|
|
118
|
+
expect(consoleLogSpy).not.toHaveBeenCalledWith(expect.stringContaining('[DRY-RUN]'));
|
|
119
|
+
});
|
|
53
120
|
});
|
package/src/utils/safety.ts
CHANGED
|
@@ -7,13 +7,14 @@ import {
|
|
|
7
7
|
areAllProjectsAllowed,
|
|
8
8
|
getAllowedProjectUuidsFromEnv,
|
|
9
9
|
extractProjectUuids,
|
|
10
|
+
validateResourceId,
|
|
10
11
|
} from '@lightdash-tools/common';
|
|
11
12
|
import type { ToolAnnotations } from '@lightdash-tools/common';
|
|
12
13
|
import type { Command } from 'commander';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* Resolves the safety mode from the command line options or environment variables.
|
|
16
|
-
* CLI --safety-mode takes priority over
|
|
17
|
+
* CLI --safety-mode takes priority over LIGHTDASH_TOOLS_SAFETY_MODE.
|
|
17
18
|
*/
|
|
18
19
|
export function getSafetyMode(cmd: Command): SafetyMode {
|
|
19
20
|
const options = cmd.optsWithGlobals() as { safetyMode?: string };
|
|
@@ -23,10 +24,24 @@ export function getSafetyMode(cmd: Command): SafetyMode {
|
|
|
23
24
|
return getSafetyModeFromEnv();
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Resolves whether dry-run mode is active from CLI or environment.
|
|
29
|
+
*/
|
|
30
|
+
export function isDryRun(cmd: Command): boolean {
|
|
31
|
+
let root = cmd;
|
|
32
|
+
while (root.parent) {
|
|
33
|
+
root = root.parent;
|
|
34
|
+
}
|
|
35
|
+
const options = root.opts() as { dryRun?: boolean };
|
|
36
|
+
if (options.dryRun === true) return true;
|
|
37
|
+
const v = process.env.LIGHTDASH_TOOLS_DRY_RUN;
|
|
38
|
+
return v === '1' || v === 'true' || v === 'yes';
|
|
39
|
+
}
|
|
40
|
+
|
|
26
41
|
/**
|
|
27
42
|
* Resolves the allowed project UUIDs from the command line options or environment variables.
|
|
28
43
|
* We specifically want the root --projects flag as the security guardrail.
|
|
29
|
-
* CLI
|
|
44
|
+
* CLI --projects takes priority over LIGHTDASH_TOOLS_ALLOWED_PROJECTS.
|
|
30
45
|
*/
|
|
31
46
|
export function getAllowedProjects(cmd: Command): string[] {
|
|
32
47
|
let root = cmd;
|
|
@@ -70,6 +85,41 @@ export function wrapAction<T extends unknown[]>(
|
|
|
70
85
|
const allowedProjects = getAllowedProjects(this);
|
|
71
86
|
const targetProjects = extractProjectUuids(args);
|
|
72
87
|
|
|
88
|
+
// ── Input Validation ─────────────────────────────────────────────────────
|
|
89
|
+
// Validate only known identifier fields (projectUuid, slug, etc.) in objects.
|
|
90
|
+
// Do NOT validate bare positional strings—they may be free-form (query, name, etc.).
|
|
91
|
+
function validateStringArgs(values: unknown[]): void {
|
|
92
|
+
for (const v of values) {
|
|
93
|
+
if (Array.isArray(v)) {
|
|
94
|
+
validateStringArgs(v);
|
|
95
|
+
} else if (v && typeof v === 'object') {
|
|
96
|
+
const o = v as Record<string, unknown>;
|
|
97
|
+
for (const key of ['project', 'projectUuid', 'projectUuids', 'projects', 'slug']) {
|
|
98
|
+
const val = o[key];
|
|
99
|
+
if (typeof val === 'string') validateResourceId(val);
|
|
100
|
+
else if (Array.isArray(val))
|
|
101
|
+
for (const item of val) if (typeof item === 'string') validateResourceId(item);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
validateStringArgs(args as unknown[]);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
logAuditEntry({
|
|
110
|
+
timestamp: new Date().toISOString(),
|
|
111
|
+
sessionId: getSessionId(),
|
|
112
|
+
tool: commandPath,
|
|
113
|
+
projectUuids: targetProjects,
|
|
114
|
+
status: 'blocked',
|
|
115
|
+
durationMs: Date.now() - start,
|
|
116
|
+
});
|
|
117
|
+
console.error(
|
|
118
|
+
`Error: Invalid resource ID: ${err instanceof Error ? err.message : String(err)}`,
|
|
119
|
+
);
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
|
|
73
123
|
// ── Safety Mode Enforcement ──────────────────────────────────────────────
|
|
74
124
|
if (!isAllowed(mode, annotations)) {
|
|
75
125
|
logAuditEntry({
|
|
@@ -81,11 +131,28 @@ export function wrapAction<T extends unknown[]>(
|
|
|
81
131
|
durationMs: Date.now() - start,
|
|
82
132
|
});
|
|
83
133
|
console.error(
|
|
84
|
-
`Error: This command is disabled in ${mode} mode. To enable it, use --safety-mode or set
|
|
134
|
+
`Error: This command is disabled in ${mode} mode. To enable it, use --safety-mode or set LIGHTDASH_TOOLS_SAFETY_MODE.`,
|
|
85
135
|
);
|
|
86
136
|
process.exit(1);
|
|
87
137
|
}
|
|
88
138
|
|
|
139
|
+
// ── Dry-Run (Write Commands Only) ────────────────────────────────────────
|
|
140
|
+
const isReadOnly = !!annotations.readOnlyHint;
|
|
141
|
+
if (!isReadOnly && isDryRun(this)) {
|
|
142
|
+
logAuditEntry({
|
|
143
|
+
timestamp: new Date().toISOString(),
|
|
144
|
+
sessionId: getSessionId(),
|
|
145
|
+
tool: commandPath,
|
|
146
|
+
projectUuids: targetProjects.length > 0 ? targetProjects : undefined,
|
|
147
|
+
status: 'blocked',
|
|
148
|
+
durationMs: Date.now() - start,
|
|
149
|
+
});
|
|
150
|
+
console.log(
|
|
151
|
+
`[DRY-RUN] Would execute: ${commandPath} with args: ${JSON.stringify(args)}. No changes were made.`,
|
|
152
|
+
);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
89
156
|
// ── Project Guardrail Enforcement ────────────────────────────────────────
|
|
90
157
|
if (allowedProjects.length > 0) {
|
|
91
158
|
const deniedProjects = targetProjects.filter(
|