Skip to content

Commit

Permalink
audit log postgres session PID
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinFrazar committed Oct 16, 2024
1 parent d794c54 commit 3ec8394
Show file tree
Hide file tree
Showing 6 changed files with 1,038 additions and 993 deletions.
4 changes: 4 additions & 0 deletions api/proto/teleport/legacy/types/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3028,6 +3028,10 @@ message DatabaseSessionStart {
(gogoproto.embed) = true,
(gogoproto.jsontag) = ""
];
// PostgresPID is the Postgres backend PID that was created for a Postgres
// connection. This can be useful for backend process cancellation or
// termination and it is not a sensitive or secret value.
uint32 PostgresPID = 8 [(gogoproto.jsontag) = "postgres_pid,omitempty"];
}

// DatabaseSessionQuery is emitted when a user executes a database query.
Expand Down
2,017 changes: 1,025 additions & 992 deletions api/types/events/events.pb.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion lib/srv/db/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func TestAuditPostgres(t *testing.T) {
// Connect should trigger successful session start event.
psql, err := testCtx.postgresClient(ctx, "alice", "postgres", "postgres", "postgres")
require.NoError(t, err)
requireEvent(t, testCtx, libevents.DatabaseSessionStartCode)
startEvt, ok := requireEvent(t, testCtx, libevents.DatabaseSessionStartCode).(*events.DatabaseSessionStart)
require.True(t, ok)
require.NotNil(t, startEvt)
require.NotZero(t, startEvt.PostgresPID)

// Simple query should trigger the query event.
_, err = psql.Exec(ctx, "select 1").ReadAll()
Expand Down
1 change: 1 addition & 0 deletions lib/srv/db/common/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (a *audit) OnSessionStart(ctx context.Context, session *Session, sessionErr
Status: events.Status{
Success: true,
},
PostgresPID: session.PostgresPID,
}

// If the database session wasn't started successfully, emit
Expand Down
2 changes: 2 additions & 0 deletions lib/srv/db/common/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type Session struct {
LockTargets []types.LockTarget
// AuthContext is the identity context of the user.
AuthContext *authz.Context
// PostgresPID is the Postgres backend PID for the session.
PostgresPID uint32
}

// String returns string representation of the session parameters.
Expand Down
2 changes: 2 additions & 0 deletions lib/srv/db/postgres/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func (e *Engine) HandleConnection(ctx context.Context, sessionCtx *common.Sessio
cancelAutoUserLease()
return trace.Wrap(err)
}
sessionCtx.PostgresPID = hijackedConn.PID
e.Log = e.Log.With("pg_backend_pid", hijackedConn.PID)
e.rawServerConn = hijackedConn.Conn
// Release the auto-users semaphore now that we've successfully connected.
cancelAutoUserLease()
Expand Down

0 comments on commit 3ec8394

Please sign in to comment.