postgres-aiops 0.1.0__tar.gz → 0.2.0__tar.gz

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.
Files changed (79) hide show
  1. postgres_aiops-0.2.0/.coverage +0 -0
  2. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/CHANGELOG.md +30 -0
  3. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/PKG-INFO +1 -1
  4. postgres_aiops-0.2.0/postgres_aiops/__init__.py +14 -0
  5. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/_common.py +1 -1
  6. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/init.py +38 -0
  7. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/query.py +7 -4
  8. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/remediate.py +28 -35
  9. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/config.py +2 -1
  10. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/__init__.py +1 -1
  11. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/audit.py +1 -1
  12. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/budget.py +1 -1
  13. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/decorators.py +3 -3
  14. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/policy.py +24 -5
  15. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/sanitize.py +10 -4
  16. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/_util.py +1 -1
  17. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/pyproject.toml +2 -1
  18. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/server.json +2 -2
  19. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/tests/conftest.py +9 -0
  20. postgres_aiops-0.2.0/tests/test_cli_writes.py +83 -0
  21. postgres_aiops-0.2.0/tests/test_governance_persistence.py +148 -0
  22. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/tests/test_smoke.py +9 -2
  23. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/uv.lock +160 -1
  24. postgres_aiops-0.1.0/postgres_aiops/__init__.py +0 -9
  25. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/.github/workflows/publish.yml +0 -0
  26. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/.gitignore +0 -0
  27. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/LICENSE +0 -0
  28. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/README.md +0 -0
  29. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/RELEASE_NOTES.md +0 -0
  30. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/SECURITY.md +0 -0
  31. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/__init__.py +0 -0
  32. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/_shared.py +0 -0
  33. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/server.py +0 -0
  34. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/__init__.py +0 -0
  35. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/activity.py +0 -0
  36. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/analysis.py +0 -0
  37. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/indexes.py +0 -0
  38. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/queries.py +0 -0
  39. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/remediation.py +0 -0
  40. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/replication.py +0 -0
  41. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/server.py +0 -0
  42. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/mcp_server/tools/tables.py +0 -0
  43. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/__init__.py +0 -0
  44. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/_root.py +0 -0
  45. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/activity.py +0 -0
  46. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/analyze.py +0 -0
  47. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/doctor.py +0 -0
  48. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/index.py +0 -0
  49. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/overview.py +0 -0
  50. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/replication.py +0 -0
  51. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/secret.py +0 -0
  52. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/server.py +0 -0
  53. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/cli/table.py +0 -0
  54. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/connection.py +0 -0
  55. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/doctor.py +0 -0
  56. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/paths.py +0 -0
  57. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/patterns.py +0 -0
  58. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/governance/undo.py +0 -0
  59. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/__init__.py +0 -0
  60. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/activity.py +0 -0
  61. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/analysis.py +0 -0
  62. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/indexes.py +0 -0
  63. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/overview.py +0 -0
  64. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/queries.py +0 -0
  65. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/remediation.py +0 -0
  66. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/replication.py +0 -0
  67. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/server.py +0 -0
  68. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/ops/tables.py +0 -0
  69. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/postgres_aiops/secretstore.py +0 -0
  70. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/skills/postgres-aiops/SKILL.md +0 -0
  71. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/skills/postgres-aiops/references/capabilities.md +0 -0
  72. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/skills/postgres-aiops/references/cli-reference.md +0 -0
  73. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/skills/postgres-aiops/references/setup-guide.md +0 -0
  74. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/smithery.yaml +0 -0
  75. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/tests/test_analysis.py +0 -0
  76. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/tests/test_connection.py +0 -0
  77. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/tests/test_reads.py +0 -0
  78. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/tests/test_secretstore.py +0 -0
  79. {postgres_aiops-0.1.0 → postgres_aiops-0.2.0}/tests/test_writes.py +0 -0
Binary file
@@ -1,5 +1,35 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.2.0 — 2026-07-13
4
+
5
+ Security-hardening release from a line-wide code review.
6
+
7
+ ### Changed (behavior)
8
+ - **Secure by default**: with no `rules.yaml`, high/critical operations now require a
9
+ named approver (`POSTGRES_AUDIT_APPROVED_BY`). A fresh install no longer allows
10
+ destructive writes unattended; `init` seeds a starter `rules.yaml` you can edit,
11
+ and an operator-authored rules file is honoured as-is.
12
+ - `__version__` is now single-sourced from package metadata (the previous release
13
+ self-reported a stale version string).
14
+ - Sanitize docs no longer overstate scope: it strips control/format characters and
15
+ truncates; semantic prompt-injection resistance must come from the consuming agent.
16
+
17
+ ### Fixed
18
+ - CLI `query reset` now executes through the governed MCP twin — the last CLI write that bypassed audit/undo recording.
19
+
20
+ ### Tests
21
+ - Governance persistence is now tested against REAL `audit.db`/`undo.db` files
22
+ (write → audit row + inverse undo row with captured prior state).
23
+ - The CLI confirmed-write path (dry-run / double-confirm / governed execution) is
24
+ covered end-to-end.
25
+ - `pytest-cov` added to the dev dependencies.
26
+
27
+ ## v0.1.1
28
+
29
+ - Fix: `POSTGRES_AIOPS_HOME` now also relocates `config.yaml` (was hardcoded to `~/.postgres-aiops`).
30
+ - Fix: **CLI writes are now audited + undo-recorded** via the governance path — previously only the MCP tools recorded audit/undo; CLI `manage`/`remediate`/etc. writes now go through the same `@governed_tool` layer (they keep their dry-run + double-confirm). CLI write output is now the governed JSON result. No API/tool changes.
31
+
32
+
3
33
  All notable changes to postgres-aiops are documented here. Format loosely follows
4
34
  [Keep a Changelog](https://keepachangelog.com/); this project uses semantic
5
35
  versioning (currently 0.x preview — the API may change).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: postgres-aiops
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Governed AI-ops for PostgreSQL DBA operations: slow-query RCA, bloat/vacuum & blocking-lock analysis with a built-in governance harness (audit, budget, undo, risk tiers)
5
5
  Author-email: wei <zhouwei008@gmail.com>
6
6
  License-Expression: MIT
@@ -0,0 +1,14 @@
1
+ """postgres-aiops — governed PostgreSQL DBA operations for AI agents.
2
+
3
+ Standalone and self-contained: the governance harness (audit, token budget,
4
+ undo-token recording, graduated risk tiers, output sanitize) is bundled under
5
+ ``postgres_aiops.governance`` — this package has no external skill-family
6
+ dependency. Preview: not yet full-coverage.
7
+ """
8
+
9
+ from importlib.metadata import PackageNotFoundError, version
10
+
11
+ try:
12
+ __version__ = version("postgres-aiops")
13
+ except PackageNotFoundError: # running from an uninstalled source tree
14
+ __version__ = "0.0.0+unknown"
@@ -48,7 +48,7 @@ def cli_errors(fn: Callable) -> Callable:
48
48
  return wrapper
49
49
 
50
50
 
51
- def get_connection(target: str | None, config_path: Path | None = None):
51
+ def get_connection(target: str | None, config_path: Path | None = None) -> tuple[Any, Any]:
52
52
  """Return a (conn, config) tuple for the given target."""
53
53
  from postgres_aiops.config import load_config
54
54
  from postgres_aiops.connection import ConnectionManager
@@ -22,8 +22,45 @@ from postgres_aiops.config import (
22
22
  DEFAULT_SSLMODE,
23
23
  DEFAULT_USER,
24
24
  )
25
+ from postgres_aiops.governance.paths import ops_path
25
26
  from postgres_aiops.secretstore import SecretStore, resolve_master_password
26
27
 
28
+ # Starter policy: keeps the secure-by-default gate (high/critical writes need a
29
+ # named approver) explicit and editable, and shows the other rule kinds.
30
+ DEFAULT_RULES_YAML = """\
31
+ # postgres-aiops policy rules — hot-reloaded on change (no restart needed).
32
+ # Kinds: deny rules, maintenance_window, risk_tiers (graduated autonomy).
33
+
34
+ risk_tiers:
35
+ - name: high-risk-requires-approver
36
+ tier: dual
37
+ min_risk_level: high
38
+ reason: >-
39
+ High/critical writes need a named human approver — set
40
+ POSTGRES_AUDIT_APPROVED_BY (and POSTGRES_AUDIT_RATIONALE) before the call.
41
+
42
+ # deny:
43
+ # - name: no-prod-drops
44
+ # operations: ["drop_*"]
45
+ # environments: ["production"]
46
+ # reason: "Drops in production go through change management."
47
+
48
+ # maintenance_window:
49
+ # start: "22:00"
50
+ # end: "06:00"
51
+ """
52
+
53
+
54
+ def _write_default_rules() -> None:
55
+ """Seed a starter rules.yaml (only when none exists) so the policy layer
56
+ is explicit from day one; never overwrites an operator-authored file."""
57
+ rules_path = ops_path("rules.yaml")
58
+ if rules_path.exists():
59
+ return
60
+ rules_path.parent.mkdir(parents=True, exist_ok=True)
61
+ rules_path.write_text(DEFAULT_RULES_YAML, "utf-8")
62
+ console.print(f"[green]✓ Wrote default policy rules:[/] {rules_path}")
63
+
27
64
 
28
65
  def _load_existing_targets() -> list[dict]:
29
66
  if not CONFIG_FILE.exists():
@@ -101,6 +138,7 @@ def init_cmd() -> None:
101
138
  if not typer.confirm("\nAdd another target?", default=False):
102
139
  break
103
140
 
141
+ _write_default_rules()
104
142
  console.print(f"\n[green]✓ Setup complete.[/] Config: {CONFIG_FILE}")
105
143
  console.print(
106
144
  "[dim]Tip: export POSTGRES_AIOPS_MASTER_PASSWORD=... in your shell profile "
@@ -59,12 +59,15 @@ def query_explain(
59
59
  @query_app.command("reset")
60
60
  @cli_errors
61
61
  def query_reset(target: TargetOption = None, dry_run: DryRunOption = False) -> None:
62
- """Reset pg_stat_statements accumulators (irreversible; dry-run + confirm)."""
63
- from postgres_aiops.ops import queries as ops
62
+ """Reset pg_stat_statements accumulators (irreversible; dry-run + confirm).
63
+
64
+ Real execution is delegated to the ``@governed_tool``-wrapped MCP function
65
+ so the reset is audited on the same governance path as MCP calls.
66
+ """
67
+ from mcp_server.tools import queries as gov
64
68
 
65
69
  if dry_run:
66
70
  dry_run_print(operation="reset_query_stats", api_call="SELECT pg_stat_statements_reset()")
67
71
  return
68
72
  double_confirm("reset pg_stat_statements on", "this target")
69
- conn, _ = get_connection(target)
70
- console.print_json(json.dumps(ops.reset_query_stats(conn)))
73
+ console.print_json(json.dumps(gov.reset_query_stats(target=target)))
@@ -14,7 +14,6 @@ from postgres_aiops.cli._common import (
14
14
  console,
15
15
  double_confirm,
16
16
  dry_run_print,
17
- get_connection,
18
17
  )
19
18
 
20
19
  remediate_app = typer.Typer(
@@ -32,15 +31,14 @@ def remediate_terminate(
32
31
  dry_run: DryRunOption = False,
33
32
  ) -> None:
34
33
  """Terminate a backend (no undo; dry-run + confirm)."""
35
- from postgres_aiops.ops import remediation as ops
36
-
37
34
  if dry_run:
38
35
  dry_run_print(operation="terminate_backend",
39
36
  api_call="SELECT pg_terminate_backend(pid)", parameters={"pid": pid})
40
37
  return
41
38
  double_confirm("terminate backend", str(pid))
42
- conn, _ = get_connection(target)
43
- console.print_json(json.dumps(ops.terminate_backend(conn, pid)))
39
+ from mcp_server.tools import remediation as gov
40
+
41
+ console.print_json(json.dumps(gov.terminate_backend(pid=pid, target=target)))
44
42
 
45
43
 
46
44
  @remediate_app.command("cancel")
@@ -51,15 +49,14 @@ def remediate_cancel(
51
49
  dry_run: DryRunOption = False,
52
50
  ) -> None:
53
51
  """Cancel a backend's running query (no undo; dry-run + confirm)."""
54
- from postgres_aiops.ops import remediation as ops
55
-
56
52
  if dry_run:
57
53
  dry_run_print(operation="cancel_query",
58
54
  api_call="SELECT pg_cancel_backend(pid)", parameters={"pid": pid})
59
55
  return
60
56
  double_confirm("cancel query on backend", str(pid))
61
- conn, _ = get_connection(target)
62
- console.print_json(json.dumps(ops.cancel_query(conn, pid)))
57
+ from mcp_server.tools import remediation as gov
58
+
59
+ console.print_json(json.dumps(gov.cancel_query(pid=pid, target=target)))
63
60
 
64
61
 
65
62
  @remediate_app.command("vacuum")
@@ -72,15 +69,15 @@ def remediate_vacuum(
72
69
  dry_run: DryRunOption = False,
73
70
  ) -> None:
74
71
  """VACUUM a table (dry-run + confirm)."""
75
- from postgres_aiops.ops import remediation as ops
76
-
77
72
  if dry_run:
78
73
  dry_run_print(operation="run_vacuum", api_call=f"VACUUM {table}",
79
74
  parameters={"full": full, "analyze": analyze})
80
75
  return
81
76
  double_confirm("VACUUM", table)
82
- conn, _ = get_connection(target)
83
- console.print_json(json.dumps(ops.run_vacuum(conn, table, full=full, analyze=analyze)))
77
+ from mcp_server.tools import remediation as gov
78
+
79
+ console.print_json(
80
+ json.dumps(gov.run_vacuum(table=table, full=full, analyze=analyze, target=target)))
84
81
 
85
82
 
86
83
  @remediate_app.command("analyze-table")
@@ -91,14 +88,13 @@ def remediate_analyze(
91
88
  dry_run: DryRunOption = False,
92
89
  ) -> None:
93
90
  """ANALYZE a table (dry-run + confirm)."""
94
- from postgres_aiops.ops import remediation as ops
95
-
96
91
  if dry_run:
97
92
  dry_run_print(operation="run_analyze", api_call=f"ANALYZE {table}")
98
93
  return
99
94
  double_confirm("ANALYZE", table)
100
- conn, _ = get_connection(target)
101
- console.print_json(json.dumps(ops.run_analyze(conn, table)))
95
+ from mcp_server.tools import remediation as gov
96
+
97
+ console.print_json(json.dumps(gov.run_analyze(table=table, target=target)))
102
98
 
103
99
 
104
100
  @remediate_app.command("create-index")
@@ -113,16 +109,15 @@ def remediate_create_index(
113
109
  dry_run: DryRunOption = False,
114
110
  ) -> None:
115
111
  """Create an index (reversible; dry-run + confirm)."""
116
- from postgres_aiops.ops import remediation as ops
117
-
118
112
  if dry_run:
119
113
  dry_run_print(operation="create_index", api_call=f"CREATE INDEX ON {table}",
120
114
  parameters={"columns": columns, "name": name, "unique": unique})
121
115
  return
122
116
  double_confirm("create index on", table)
123
- conn, _ = get_connection(target)
124
- result = ops.create_index(conn, table, columns, name=name, unique=unique,
125
- concurrently=concurrently)
117
+ from mcp_server.tools import remediation as gov
118
+
119
+ result = gov.create_index(table=table, columns=columns, name=name, unique=unique,
120
+ concurrently=concurrently, target=target)
126
121
  console.print_json(json.dumps(result))
127
122
 
128
123
 
@@ -135,14 +130,14 @@ def remediate_drop_index(
135
130
  dry_run: DryRunOption = False,
136
131
  ) -> None:
137
132
  """Drop an index (reversible; captures the definition first; dry-run + confirm)."""
138
- from postgres_aiops.ops import remediation as ops
139
-
140
133
  if dry_run:
141
134
  dry_run_print(operation="drop_index", api_call=f"DROP INDEX {name}")
142
135
  return
143
136
  double_confirm("drop index", name)
144
- conn, _ = get_connection(target)
145
- console.print_json(json.dumps(ops.drop_index(conn, name, concurrently=concurrently)))
137
+ from mcp_server.tools import remediation as gov
138
+
139
+ console.print_json(
140
+ json.dumps(gov.drop_index(name=name, concurrently=concurrently, target=target)))
146
141
 
147
142
 
148
143
  @remediate_app.command("reindex")
@@ -155,15 +150,14 @@ def remediate_reindex(
155
150
  dry_run: DryRunOption = False,
156
151
  ) -> None:
157
152
  """REINDEX an index/table/schema (dry-run + confirm)."""
158
- from postgres_aiops.ops import remediation as ops
159
-
160
153
  if dry_run:
161
154
  dry_run_print(operation="reindex", api_call=f"REINDEX {kind} {target_name}")
162
155
  return
163
156
  double_confirm(f"REINDEX {kind}", target_name)
164
- conn, _ = get_connection(target)
165
- console.print_json(json.dumps(ops.reindex(conn, target_name, kind=kind,
166
- concurrently=concurrently)))
157
+ from mcp_server.tools import remediation as gov
158
+
159
+ console.print_json(json.dumps(gov.reindex(target_name=target_name, kind=kind,
160
+ concurrently=concurrently, target=target)))
167
161
 
168
162
 
169
163
  @remediate_app.command("set")
@@ -175,12 +169,11 @@ def remediate_set(
175
169
  dry_run: DryRunOption = False,
176
170
  ) -> None:
177
171
  """ALTER SYSTEM SET a parameter (reversible; dry-run + confirm)."""
178
- from postgres_aiops.ops import remediation as ops
179
-
180
172
  if dry_run:
181
173
  dry_run_print(operation="update_setting",
182
174
  api_call=f"ALTER SYSTEM SET {name} = ...", parameters={"value": value})
183
175
  return
184
176
  double_confirm(f"ALTER SYSTEM SET {name} =", value)
185
- conn, _ = get_connection(target)
186
- console.print_json(json.dumps(ops.update_setting(conn, name, value)))
177
+ from mcp_server.tools import remediation as gov
178
+
179
+ console.print_json(json.dumps(gov.update_setting(name=name, value=value, target=target)))
@@ -17,9 +17,10 @@ from pathlib import Path
17
17
 
18
18
  import yaml
19
19
 
20
+ from postgres_aiops.governance.paths import ops_home
20
21
  from postgres_aiops.secretstore import SecretStoreError, get_secret, has_store
21
22
 
22
- CONFIG_DIR = Path.home() / ".postgres-aiops"
23
+ CONFIG_DIR = ops_home()
23
24
  CONFIG_FILE = CONFIG_DIR / "config.yaml"
24
25
  ENV_FILE = CONFIG_DIR / ".env"
25
26
 
@@ -8,7 +8,7 @@ any external skill family — this package is its own copy of the harness:
8
8
  and undo-token recording.
9
9
  - unified SQLite audit log under ``~/.postgres-aiops/`` (override with
10
10
  ``POSTGRES_AIOPS_HOME``).
11
- - ``sanitize`` — prompt-injection defense for API-returned text.
11
+ - ``sanitize`` — control-character stripping + truncation for API-returned text.
12
12
 
13
13
  State lives under ``ops_home()`` (default ``~/.postgres-aiops``).
14
14
  """
@@ -1,4 +1,4 @@
1
- """Unified audit logging engine — all Endpoint skills write to a single SQLite database.
1
+ """Unified audit logging engine — all PostgreSQL skills write to a single SQLite database.
2
2
 
3
3
  Replaces 7 per-skill JSON Lines audit loggers with one shared ``~/.postgres-aiops/audit.db``.
4
4
  Framework-agnostic: works with Claude, Codex, local agents, or any MCP client.
@@ -43,7 +43,7 @@ _DEFAULT_RUNAWAY_WINDOW_SEC = 120
43
43
  class BudgetExceeded(Exception):
44
44
  """Raised when a tool call would exceed a budget / runaway limit.
45
45
 
46
- Subclasses nothing Endpoint-specific; the decorator wraps it into a
46
+ Subclasses nothing PostgreSQL-specific; the decorator wraps it into a
47
47
  PolicyResult so it shares the existing denial audit path. Kept as its own
48
48
  type (not raw PolicyDenied) so callers can catch budget stops distinctly.
49
49
  """
@@ -1,4 +1,4 @@
1
- """The ``@governed_tool`` decorator — mandatory wrapper for all Endpoint MCP tool functions.
1
+ """The ``@governed_tool`` decorator — mandatory wrapper for all PostgreSQL MCP tool functions.
2
2
 
3
3
  Responsibilities:
4
4
  1. Pre-check: evaluate policy rules (deny, maintenance window)
@@ -58,7 +58,7 @@ def governed_tool(
58
58
  sensitive_params: list[str] | None = None,
59
59
  undo: Any = None,
60
60
  ) -> Any:
61
- """Decorator for all Endpoint MCP tool functions.
61
+ """Decorator for all PostgreSQL MCP tool functions.
62
62
 
63
63
  Can be used with or without arguments::
64
64
 
@@ -365,7 +365,7 @@ def _finalize(state: _CallState) -> None:
365
365
  pass
366
366
 
367
367
  # timeout_seconds is advisory: exceeding it logs a warning, no hard
368
- # cancellation (cancelling mid-flight Endpoint calls is worse).
368
+ # cancellation (cancelling mid-flight PostgreSQL calls is worse).
369
369
  if state.timeout_seconds and duration > state.timeout_seconds * 1000:
370
370
  _log.warning(
371
371
  "%s.%s took %dms — exceeded timeout_seconds=%d (advisory, not cancelled)",
@@ -1,4 +1,4 @@
1
- """Policy engine — rule-based access control for Endpoint MCP tools.
1
+ """Policy engine — rule-based access control for PostgreSQL MCP tools.
2
2
 
3
3
  Rules are loaded from ``~/.postgres-aiops/rules.yaml`` with hot-reload on file change.
4
4
  """
@@ -93,7 +93,8 @@ class PolicyEngine:
93
93
  self._load_rules()
94
94
 
95
95
  def _load_rules(self) -> None:
96
- """Load rules from YAML file. Missing file → empty rules (allow all)."""
96
+ """Load rules from YAML file. Missing file → empty rules (deny rules off;
97
+ high/critical still require an approver — see required_approval_tier)."""
97
98
  if not self._path.exists():
98
99
  self._rules = {}
99
100
  self._mtime = 0.0
@@ -113,7 +114,7 @@ class PolicyEngine:
113
114
  """Hot-reload if file changed."""
114
115
  if not self._path.exists():
115
116
  if self._rules:
116
- _log.warning("Policy rules file deleted: %s — clearing rules (allow all)", self._path)
117
+ _log.warning("Policy rules file deleted: %s — clearing rules (high/critical fall back to the default approver gate)", self._path)
117
118
  self._rules = {}
118
119
  self._mtime = 0.0
119
120
  return
@@ -156,7 +157,8 @@ class PolicyEngine:
156
157
 
157
158
  self._maybe_reload()
158
159
 
159
- # No rules file → allow everything
160
+ # No rules file → no deny rules (destructive ops are still gated by
161
+ # required_approval_tier's secure-by-default dual tier)
160
162
  if not self._rules:
161
163
  return PolicyResult(allowed=True, rule="no_rules")
162
164
 
@@ -225,11 +227,28 @@ class PolicyEngine:
225
227
  on operation glob / environment / resource tag / minimum risk and maps
226
228
  to a tier (none/confirm/dual/review). The FIRST matching, HIGHEST tier
227
229
  wins so a prod-tagged destructive op can't be down-graded by a looser
228
- rule listed earlier. No config → tier ``none`` (backward compatible).
230
+ rule listed earlier.
231
+
232
+ Secure by default: with NO rules file at all, high/critical operations
233
+ require a named approver (``dual`` tier) — a fresh install must not
234
+ allow destructive writes unattended. An operator-authored rules file
235
+ (even one without a ``risk_tiers`` block) is an explicit choice and is
236
+ honoured as-is (tier ``none``).
229
237
  """
238
+ if os.environ.get("POSTGRES_POLICY_DISABLED") == "1":
239
+ return TierDecision(tier="none", rule="policy_disabled")
230
240
  self._maybe_reload()
231
241
  tiers = self._rules.get("risk_tiers") if self._rules else None
232
242
  if not tiers:
243
+ if not self._rules and risk_level in ("high", "critical"):
244
+ return TierDecision(
245
+ tier="dual",
246
+ rule="default_high_risk",
247
+ reason=(
248
+ f"No rules file at {self._path} — high/critical operations require a named approver (POSTGRES_AUDIT_APPROVED_BY) by default. Author rules.yaml "
249
+ "(run the init wizard) to customize this."
250
+ ),
251
+ )
233
252
  return TierDecision(tier="none", rule="no_tiers")
234
253
 
235
254
  tags = _extract_tags(params)
@@ -1,6 +1,12 @@
1
- """Prompt-injection defense: strip control characters and truncate untrusted text.
1
+ """Output hygiene: strip control/format characters and truncate untrusted text.
2
2
 
3
- Consolidated from 22 duplicate ``_sanitize()`` implementations across 7 Endpoint skills.
3
+ Defense-in-depth against *encoding-level* tricks: control characters,
4
+ zero-width / bidi format characters, and payload-pushing padding. It does NOT
5
+ neutralize natural-language prompt injection — "ignore previous instructions"
6
+ passes through unchanged; semantic injection resistance must come from the
7
+ consuming agent's own prompt boundaries.
8
+
9
+ Consolidated from 22 duplicate ``_sanitize()`` implementations across the tool line.
4
10
  All skills should import from here instead of defining their own copy.
5
11
  """
6
12
 
@@ -19,14 +25,14 @@ def sanitize(text: str | None, max_len: int = 500) -> str:
19
25
  Removes:
20
26
  - C0/C1 control characters (except newline/tab)
21
27
  - Unicode Format characters (Cf): zero-width spaces, bidi overrides,
22
- zero-width joiners — used in prompt injection attacks
28
+ zero-width joiners — used to smuggle or disguise injected text
23
29
 
24
30
  Stripping happens BEFORE truncation so an attacker cannot push the real
25
31
  payload past the cut-off by padding with junk control characters.
26
32
  ``None`` sanitizes to ``""``.
27
33
 
28
34
  Args:
29
- text: Untrusted text from Endpoint SCALE API responses.
35
+ text: Untrusted text from PostgreSQL server responses.
30
36
  max_len: Maximum length after truncation. Default 500.
31
37
 
32
38
  Returns:
@@ -3,7 +3,7 @@
3
3
  Two jobs:
4
4
 
5
5
  * ``s`` — pass catalog/query text through the governance ``sanitize`` (bounded
6
- length, prompt-injection defense) before it reaches an agent.
6
+ length, control-character stripping) before it reaches an agent.
7
7
  * ``qualify`` / ``quote_ident`` — the ONLY sanctioned way to place an
8
8
  identifier (schema/table/index/column) into a statement that cannot be
9
9
  parameterised (DDL, ``VACUUM``, ``ANALYZE``, ``REINDEX``). Every part is
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "postgres-aiops"
7
- version = "0.1.0"
7
+ version = "0.2.0"
8
8
  description = "Governed AI-ops for PostgreSQL DBA operations: slow-query RCA, bloat/vacuum & blocking-lock analysis with a built-in governance harness (audit, budget, undo, risk tiers)"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -22,6 +22,7 @@ dependencies = [
22
22
  [dependency-groups]
23
23
  dev = [
24
24
  "pytest>=8.0,<10.0",
25
+ "pytest-cov>=5.0,<8.0",
25
26
  "ruff>=0.5,<1.0",
26
27
  ]
27
28
 
@@ -7,12 +7,12 @@
7
7
  "url": "https://github.com/AIops-tools/Postgres-AIops",
8
8
  "source": "github"
9
9
  },
10
- "version": "0.1.0",
10
+ "version": "0.2.0",
11
11
  "packages": [
12
12
  {
13
13
  "registryType": "pypi",
14
14
  "identifier": "postgres-aiops",
15
- "version": "0.1.0",
15
+ "version": "0.2.0",
16
16
  "transport": {
17
17
  "type": "stdio"
18
18
  }
@@ -13,6 +13,15 @@ from typing import Any
13
13
  import pytest
14
14
 
15
15
 
16
+ @pytest.fixture(autouse=True)
17
+ def _default_approver(monkeypatch):
18
+ """The policy layer is secure-by-default: with no rules.yaml, high/critical
19
+ governed calls require a named approver. Tests exercising tool behavior
20
+ are not about that gate, so record a synthetic approver globally; the
21
+ governance-persistence tests remove it to test the gate itself."""
22
+ monkeypatch.setenv("POSTGRES_AUDIT_APPROVED_BY", "pytest")
23
+
24
+
16
25
  class FakePg:
17
26
  def __init__(
18
27
  self,
@@ -0,0 +1,83 @@
1
+ """CLI confirmed-write path — past dry-run, through governance, onto disk.
2
+
3
+ The CLI write commands delegate real execution to the ``@governed_tool``
4
+ functions in ``mcp_server.tools``. These tests drive a write command PAST the
5
+ dry-run branch and the double-confirm prompts and assert the call really went
6
+ through the governed path (audit row on disk) — the regression test for the
7
+ "CLI writes were unaudited" line-wide fix.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import sqlite3
13
+
14
+ import pytest
15
+ from typer.testing import CliRunner
16
+
17
+ import postgres_aiops.governance.audit as audit_mod
18
+ import postgres_aiops.governance.policy as policy_mod
19
+ import postgres_aiops.governance.undo as undo_mod
20
+
21
+
22
+ @pytest.fixture
23
+ def gov_home(tmp_path, monkeypatch):
24
+ monkeypatch.setenv("POSTGRES_AIOPS_HOME", str(tmp_path))
25
+ audit_mod.reset_engine()
26
+ policy_mod.reset_policy_engine()
27
+ undo_mod.reset_undo_store()
28
+ yield tmp_path
29
+ audit_mod.reset_engine()
30
+ policy_mod.reset_policy_engine()
31
+ undo_mod.reset_undo_store()
32
+
33
+
34
+ def _audit_tools(db_path) -> list[str]:
35
+ conn = sqlite3.connect(db_path)
36
+ try:
37
+ return [r[0] for r in conn.execute("SELECT tool FROM audit_log ORDER BY id")]
38
+ finally:
39
+ conn.close()
40
+
41
+
42
+ @pytest.mark.unit
43
+ def test_cli_query_reset_dry_run_makes_no_call_and_no_audit(gov_home, monkeypatch, fake_pg):
44
+ from postgres_aiops.cli import app
45
+
46
+ fake = fake_pg()
47
+ import mcp_server.tools.queries as gov_queries
48
+
49
+ monkeypatch.setattr(gov_queries, "_get_connection", lambda target=None: fake)
50
+ result = CliRunner().invoke(app, ["query", "reset", "--dry-run"])
51
+ assert result.exit_code == 0
52
+ assert "DRY-RUN" in result.output
53
+ assert fake.executed == [] and fake.queried == []
54
+ assert not (gov_home / "audit.db").exists()
55
+
56
+
57
+ @pytest.mark.unit
58
+ def test_cli_query_reset_confirmed_goes_through_governance(gov_home, monkeypatch, fake_pg):
59
+ """Confirmed CLI write must execute via the governed twin: the SQL runs
60
+ AND an audit row lands in audit.db (this is what the reroute fix bought)."""
61
+ from postgres_aiops.cli import app
62
+
63
+ fake = fake_pg(responses={"pg_stat_statements_reset": [{"reset": True}]})
64
+ import mcp_server.tools.queries as gov_queries
65
+
66
+ monkeypatch.setattr(gov_queries, "_get_connection", lambda target=None: fake)
67
+ result = CliRunner().invoke(app, ["query", "reset"], input="y\ny\n")
68
+ assert result.exit_code == 0, result.output
69
+ assert _audit_tools(gov_home / "audit.db") == ["reset_query_stats"]
70
+
71
+
72
+ @pytest.mark.unit
73
+ def test_cli_query_reset_aborts_without_double_confirm(gov_home, monkeypatch, fake_pg):
74
+ from postgres_aiops.cli import app
75
+
76
+ fake = fake_pg()
77
+ import mcp_server.tools.queries as gov_queries
78
+
79
+ monkeypatch.setattr(gov_queries, "_get_connection", lambda target=None: fake)
80
+ result = CliRunner().invoke(app, ["query", "reset"], input="y\nn\n")
81
+ assert result.exit_code != 0
82
+ assert fake.executed == [] and fake.queried == []
83
+ assert not (gov_home / "audit.db").exists()