Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v16] audit log postgres session PID #47643

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3041,6 +3041,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
1,513 changes: 773 additions & 740 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 @@ -61,7 +61,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 @@ -145,6 +145,7 @@ func (a *audit) OnSessionStart(ctx context.Context, session *Session, sessionErr
Status: events.Status{
Success: true,
},
PostgresPID: session.PostgresPID,
}
event.SetTime(session.StartTime)

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 @@ -66,6 +66,8 @@ type Session struct {
AuthContext *authz.Context
// StartTime is the time the session started.
StartTime time.Time
// 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 @@ -147,6 +147,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
Loading