← Back to Pull requests

[DATASEC-331] Add Error translation #56931

Closed
unknown closed this pull request without merging on Sep 26, 2026 11:17 UTC
View on GitHub
unknown unknown commented on Sep 25, 2026 10:37 UTC
Author

What does this PR do?

Stops forwarding raw postgres errors from the data security check. Errors are translated in backend/postgres/error.rs into a PostgresError:

  • Server(SqlState): SQLSTATE code only, with a readable label for common cases (e.g. postgres server error: permission denied, the user lacks the required privileges (SQLSTATE 42501)).

  • Client(String): top-level postgres::Error message only, without its cause (e.g. postgres client error: error performing TLS handshake).
  • The original error is dropped and never logged.

    Motivation

    Postgres error messages can echo scanned values (e.g. invalid input syntax for type integer: "<value>"), which were logged and sent in the payload failure_reason.

    Describe how you validated your changes

    Ran the CI-built image registry.ddbuild.io/ci/datadog-agent/agent:v140089586-83b4d5fc-7-amd64 (after) against the previous build (before), with one sub task per error case.

    <details>
    <summary>Check config: wrong password, wrong port, non-existing table, failed cast, write query, bad TLS</summary>

    init_config:
    
    instances:
      - min_collection_interval: 0
        task_id: fIk-jnc-jaz
    
        scanning_rules:
          - id: 2b9d78a5-f32d-5ecf-9d97-8fb8f0bb0392
            pattern: '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]+'
            suppressions:
              starts_with: ["alice"]
          - id: 581ae10c-56e7-51a8-afd0-091dfa4a1df7
            pattern: '(?:\d{1,3}\.){3}\d{1,3}'
    
        scan_data:
          - sub_task_id: jKb-Dpc-DwU
            connection:
              host: dbm-postgres
              port: 5432
              dbname: dbm
              username: datadog
              password: datadog
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: users
            query: "SELECT * FROM users"
            timeout_seconds: 30
          - sub_task_id: 6ND-xzm-d3M
            connection:
              host: dbm-postgres
              port: 5432
              dbname: dbm
              username: datadog
              password: datadog2  # wrong password
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: movies
            query: "SELECT * FROM movies"
            timeout_seconds: 30
          - sub_task_id: Qw7-mTv-Rb2
            connection:
              host: dbm-postgres
              port: 5432
              dbname: dbm
              username: datadog
              password: datadog
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: sample_data
            query: "SELECT * FROM sample_data"
            timeout_seconds: 30
          - sub_task_id: 6ND-xzm-d3L
            connection:
              host: dbm-postgres
              port: 5433  # wrong port
              dbname: dbm
              username: datadog
              password: datadog
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: movies
            query: "SELECT * FROM movies"
            timeout_seconds: 30
          - sub_task_id: 6ND-xzm-d3F
            connection:
              host: dbm-postgres
              port: 5432
              dbname: dbm
              username: datadog
              password: datadog
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: movies
            query: "SELECT * FROM movies2"  # table does not exist
            timeout_seconds: 30
          - sub_task_id: Zt4-Cst-Eml
            connection:
              host: dbm-postgres
              port: 5432
              dbname: dbm
              username: datadog
              password: datadog
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: users
            query: "SELECT email::uuid FROM users"  # email cannot be cast to uuid
            timeout_seconds: 30
          - sub_task_id: Yp9-Ins-Usr
            connection:
              host: dbm-postgres
              port: 5432
              dbname: dbm
              username: datadog
              password: datadog
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: users
            query: "INSERT INTO users (name, email, created_at, updated_at) VALUES ('Mallory', 'mallory@example.com', now(), now())"  # write query
            timeout_seconds: 30
          - sub_task_id: Kd3-Bad-Tls
            connection:
              host: dbm-postgres
              port: 5432
              dbname: dbm
              username: datadog
              password: datadog
              ssl: verify-full  # server cert is self-signed
            entity:
              platform: postgres
              database_cluster_name: local-dev-cluster
              database_instance_name: local-dev-instance
              database: dbm
              schema: public
              table: users
            query: "SELECT * FROM users"
            timeout_seconds: 30
    

    </details>

    [!IMPORTANT]

    Before, the failed cast (Zt4-Cst-Eml) leaked a scanned value in the log and the payload failure_reason: invalid input syntax for type uuid: "alice@example.com". Now only the SQLSTATE is reported: postgres server error: (SQLSTATE 22P02).

    Before:

    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: check started (task_id=fIk-jnc-jaz, 2 rule(s), 8 sub task(s))
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=jKb-Dpc-DwU, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: sub task succeeded (1 match(es))
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=6ND-xzm-d3M, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task 6ND-xzm-d3M failed: fetching sub task data: connecting to postgres: db error: FATAL: password authentication failed for user "datadog"
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Qw7-mTv-Rb2, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: sub task succeeded (0 match(es))
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=6ND-xzm-d3L, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task 6ND-xzm-d3L failed: fetching sub task data: connecting to postgres: error connecting to server: Connection refused (os error 111)
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=6ND-xzm-d3F, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task 6ND-xzm-d3F failed: fetching sub task data: preparing postgres query: db error: ERROR: relation "movies2" does not exist
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Zt4-Cst-Eml, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task Zt4-Cst-Eml failed: fetching sub task data: running postgres query: db error: ERROR: invalid input syntax for type uuid: "alice@example.com"
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Yp9-Ins-Usr, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task Yp9-Ins-Usr failed: fetching sub task data: running postgres query: db error: ERROR: cannot execute INSERT in a read-only transaction
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Kd3-Bad-Tls, platform=postgres)
    2026-09-25 12:28:04 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task Kd3-Bad-Tls failed: fetching sub task data: connecting to postgres: error performing TLS handshake: error:0A000086:SSL routines:tls_post_process_server_certificate:certificate verify failed::0:: self-signed certificate in certificate chain: error:0A000086:SSL routines:tls_post_process_server_certificate:certificate verify failed::0:: error:0A000086:SSL routines:tls_post_process_server_certificate:certificate verify failed::0:
    2026-09-25 12:28:04 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: check completed
    

    After:

    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: check started (task_id=fIk-jnc-jaz, 2 rule(s), 8 sub task(s))
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=jKb-Dpc-DwU, platform=postgres)
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: sub task succeeded (1 match(es))
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=6ND-xzm-d3M, platform=postgres)
    2026-09-25 12:23:20 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task 6ND-xzm-d3M failed: fetching sub task data: connecting to postgres: postgres server error: authentication failed (check username, password and pg_hba) (SQLSTATE 28P01)
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Qw7-mTv-Rb2, platform=postgres)
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: sub task succeeded (0 match(es))
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=6ND-xzm-d3L, platform=postgres)
    2026-09-25 12:23:20 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task 6ND-xzm-d3L failed: fetching sub task data: connecting to postgres: postgres client error: error connecting to server
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=6ND-xzm-d3F, platform=postgres)
    2026-09-25 12:23:20 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task 6ND-xzm-d3F failed: fetching sub task data: preparing postgres query: postgres server error: table does not exist (SQLSTATE 42P01)
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Zt4-Cst-Eml, platform=postgres)
    2026-09-25 12:23:20 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task Zt4-Cst-Eml failed: fetching sub task data: running postgres query: postgres server error:  (SQLSTATE 22P02)
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Yp9-Ins-Usr, platform=postgres)
    2026-09-25 12:23:20 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task Yp9-Ins-Usr failed: fetching sub task data: running postgres query: postgres server error: write attempted in a read-only transaction (SQLSTATE 25006)
    2026-09-25 12:23:20 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: running sub task (sub_task_id=Kd3-Bad-Tls, platform=postgres)
    2026-09-25 12:23:21 UTC | CORE | ERROR | (pkg/collector/aggregator/aggregator.go:178 in LogMsg) | datasecurity: sub task Kd3-Bad-Tls failed: fetching sub task data: connecting to postgres: postgres client error: error performing TLS handshake
    2026-09-25 12:23:21 UTC | CORE | INFO | (pkg/collector/aggregator/aggregator.go:182 in LogMsg) | datasecurity: check completed
    

    Additional Notes

    Diff stats (+139 / -1):

    | | Lines |
    |---|---|
    | Code (postgres.rs, postgres/error.rs) | +70 / -1 |
    | Tests (postgres/error.rs test module) | +63 |
    | Release note | +6 |

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: aimenebelfodil Review duration: 1479m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #56931"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%