@matimo/postgres 0.1.0-alpha.7.1 → 0.1.0-alpha.8
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 +20 -11
- package/package.json +2 -2
- package/tools/execute-sql/execute-sql.ts +6 -36
package/README.md
CHANGED
|
@@ -144,41 +144,50 @@ The tool automatically detects destructive operations:
|
|
|
144
144
|
For interactive terminal environments:
|
|
145
145
|
|
|
146
146
|
```typescript
|
|
147
|
-
import {
|
|
147
|
+
import { MatimoInstance, getGlobalApprovalHandler } from '@matimo/core';
|
|
148
148
|
import * as readline from 'readline';
|
|
149
149
|
|
|
150
|
-
const
|
|
150
|
+
const matimo = await MatimoInstance.init({ autoDiscover: true });
|
|
151
|
+
const handler = getGlobalApprovalHandler();
|
|
151
152
|
|
|
152
153
|
// Set interactive callback
|
|
153
|
-
|
|
154
|
+
handler.setApprovalCallback(async (request) => {
|
|
154
155
|
const rl = readline.createInterface({
|
|
155
156
|
input: process.stdin,
|
|
156
157
|
output: process.stdout
|
|
157
158
|
});
|
|
158
159
|
|
|
159
160
|
return new Promise(resolve => {
|
|
160
|
-
rl.question(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
rl.question(
|
|
162
|
+
`\nApprove ${request.toolName}?\nSQL: ${request.params.sql}\n(yes/no): `,
|
|
163
|
+
answer => {
|
|
164
|
+
rl.close();
|
|
165
|
+
resolve(answer.toLowerCase() === 'yes');
|
|
166
|
+
}
|
|
167
|
+
);
|
|
164
168
|
});
|
|
165
169
|
});
|
|
170
|
+
|
|
171
|
+
// Approve write operations
|
|
172
|
+
await matimo.execute('postgres-execute-sql', {
|
|
173
|
+
sql: 'UPDATE users SET active = true'
|
|
174
|
+
});
|
|
166
175
|
```
|
|
167
176
|
|
|
168
177
|
#### 2. **Automatic Approval (CI/CD)**
|
|
169
178
|
For automated environments:
|
|
170
179
|
|
|
171
180
|
```bash
|
|
172
|
-
# Enable auto-approval for all
|
|
173
|
-
export
|
|
181
|
+
# Enable auto-approval for all operations requiring approval
|
|
182
|
+
export MATIMO_AUTO_APPROVE=true
|
|
174
183
|
```
|
|
175
184
|
|
|
176
185
|
#### 3. **Pattern-Based Approval**
|
|
177
186
|
Pre-approve specific patterns:
|
|
178
187
|
|
|
179
188
|
```bash
|
|
180
|
-
# Approve
|
|
181
|
-
export
|
|
189
|
+
# Approve only postgres-execute-sql tool
|
|
190
|
+
export MATIMO_APPROVED_PATTERNS="postgres-*"
|
|
182
191
|
```
|
|
183
192
|
|
|
184
193
|
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matimo/postgres",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.8",
|
|
4
4
|
"description": "Postgres tools for Matimo",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"pg": "^8.18.0",
|
|
13
|
-
"@matimo/core": "0.1.0-alpha.
|
|
13
|
+
"@matimo/core": "0.1.0-alpha.8"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/pg": "^8.6.6"
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Client } from 'pg';
|
|
2
|
-
import { MatimoError, ErrorCode
|
|
2
|
+
import { MatimoError, ErrorCode } from '@matimo/core';
|
|
3
3
|
|
|
4
4
|
export default async function (input: Record<string, unknown>) {
|
|
5
5
|
const sql = (input.sql as string) || '';
|
|
@@ -15,31 +15,6 @@ export default async function (input: Record<string, unknown>) {
|
|
|
15
15
|
const envUrl = process.env.MATIMO_POSTGRES_URL;
|
|
16
16
|
let connectionString: string | undefined = envUrl;
|
|
17
17
|
|
|
18
|
-
// Detect destructive SQL and require approval
|
|
19
|
-
const destructiveRegex = /^\s*(CREATE|DROP|ALTER|TRUNCATE|DELETE|UPDATE)\b/i;
|
|
20
|
-
const isDestructive = destructiveRegex.test(sql);
|
|
21
|
-
if (isDestructive) {
|
|
22
|
-
const manager = getSQLApprovalManager();
|
|
23
|
-
try {
|
|
24
|
-
const ok = await manager.isApproved(sql, 'write');
|
|
25
|
-
if (!ok) {
|
|
26
|
-
throw new MatimoError('Destructive SQL not approved', ErrorCode.EXECUTION_FAILED, {
|
|
27
|
-
toolName: 'postgres-execute-sql',
|
|
28
|
-
hint:
|
|
29
|
-
'Destructive SQL requires approval. Use getSQLApprovalManager().setApprovalCallback() or set MATIMO_SQL_APPROVED_PATTERNS / MATIMO_SQL_AUTO_APPROVE=true',
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
-
} catch (e: any) {
|
|
34
|
-
// Re-throw MatimoError or wrap
|
|
35
|
-
if (e instanceof MatimoError) throw e;
|
|
36
|
-
throw new MatimoError('SQL approval check failed', ErrorCode.EXECUTION_FAILED, {
|
|
37
|
-
toolName: 'postgres-execute-sql',
|
|
38
|
-
details: { message: e?.message || String(e) },
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
18
|
if (!connectionString) {
|
|
44
19
|
const host = process.env.MATIMO_POSTGRES_HOST;
|
|
45
20
|
const port = process.env.MATIMO_POSTGRES_PORT || '5432';
|
|
@@ -66,13 +41,11 @@ export default async function (input: Record<string, unknown>) {
|
|
|
66
41
|
const client = new Client({ connectionString });
|
|
67
42
|
try {
|
|
68
43
|
await client.connect();
|
|
69
|
-
|
|
70
|
-
const result = await client.query(sql, (params ?? []) as any);
|
|
44
|
+
const result = await client.query(sql, (params ?? []) as unknown[]);
|
|
71
45
|
return { rows: result.rows, rowCount: result.rowCount };
|
|
72
46
|
} catch (err) {
|
|
73
47
|
// Extract meaningful error message
|
|
74
|
-
|
|
75
|
-
const originalError = (err as any)?.message || String(err);
|
|
48
|
+
const originalError = String((err as Record<string, unknown>).message || err);
|
|
76
49
|
const details: Record<string, unknown> = {
|
|
77
50
|
originalMessage: originalError,
|
|
78
51
|
};
|
|
@@ -90,11 +63,8 @@ export default async function (input: Record<string, unknown>) {
|
|
|
90
63
|
details,
|
|
91
64
|
});
|
|
92
65
|
} finally {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
} catch (_e) {
|
|
97
|
-
// ignore
|
|
98
|
-
}
|
|
66
|
+
await client.end().catch(() => {
|
|
67
|
+
// Ignore connection close errors
|
|
68
|
+
});
|
|
99
69
|
}
|
|
100
70
|
}
|