Skip to content

Commit

Permalink
Merge #102165
Browse files Browse the repository at this point in the history
102165: crdb_internal: finer-grained priv for viewing {cluster,node}_transactions r=maryliag a=rafiss

Previously, only admins could view these tables. Now, we use a
finer-grained permission. Both VIEWACTIVITY and VIEWACTIVITYREDACTED
allow access.

No release note since this is a crdb_internal table.

Epic: None
Release note: None

Co-authored-by: Rafi Shamim <[email protected]>
  • Loading branch information
craig[bot] and rafiss committed Apr 25, 2023
2 parents 6563de0 + e5849ab commit a7bf183
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2169,9 +2169,13 @@ var crdbInternalLocalTxnsTable = virtualSchemaTable{
comment: "running user transactions visible by the current user (RAM; local node only)",
schema: fmt.Sprintf(txnsSchemaPattern, "node_transactions"),
populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
if err := p.RequireAdminRole(ctx, "read crdb_internal.node_transactions"); err != nil {
hasViewActivityOrhasViewActivityRedacted, err := p.HasViewActivityOrViewActivityRedactedRole(ctx)
if err != nil {
return err
}
if !hasViewActivityOrhasViewActivityRedacted {
return noViewActivityOrViewActivityRedactedRoleError(p.User())
}
req, err := p.makeSessionsRequest(ctx, true /* excludeClosed */)
if err != nil {
return err
Expand All @@ -2188,9 +2192,13 @@ var crdbInternalClusterTxnsTable = virtualSchemaTable{
comment: "running user transactions visible by the current user (cluster RPC; expensive!)",
schema: fmt.Sprintf(txnsSchemaPattern, "cluster_transactions"),
populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
if err := p.RequireAdminRole(ctx, "read crdb_internal.cluster_transactions"); err != nil {
hasViewActivityOrhasViewActivityRedacted, err := p.HasViewActivityOrViewActivityRedactedRole(ctx)
if err != nil {
return err
}
if !hasViewActivityOrhasViewActivityRedacted {
return noViewActivityOrViewActivityRedactedRoleError(p.User())
}
req, err := p.makeSessionsRequest(ctx, true /* excludeClosed */)
if err != nil {
return err
Expand Down
53 changes: 53 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,59 @@ SELECT * FROM crdb_internal.cluster_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries last_auto_retry_reason

# Accessing the tables should error for a user without a privilege.
user testuser

statement error user testuser does not have VIEWACTIVITY or VIEWACTIVITYREDACTED privilege
SELECT * FROM crdb_internal.node_transactions WHERE node_id < 0

statement error user testuser does not have VIEWACTIVITY or VIEWACTIVITYREDACTED privilege
SELECT * FROM crdb_internal.cluster_transactions WHERE node_id < 0

user root

statement ok
GRANT SYSTEM VIEWACTIVITY TO testuser

# Now testuser can query transactions since it has the VIEWACTIVITY privilege.
user testuser

query TITTTTIIIT colnames
SELECT * FROM crdb_internal.node_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries last_auto_retry_reason

query TITTTTIIIT colnames
SELECT * FROM crdb_internal.cluster_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries last_auto_retry_reason

user root

statement ok
REVOKE SYSTEM VIEWACTIVITY FROM testuser

statement ok
GRANT SYSTEM VIEWACTIVITYREDACTED TO testuser

# testuser can query transactions since it has the VIEWACTIVITYREDACTED privilege.
user testuser

query TITTTTIIIT colnames
SELECT * FROM crdb_internal.node_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries last_auto_retry_reason

query TITTTTIIIT colnames
SELECT * FROM crdb_internal.cluster_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries last_auto_retry_reason

user root

statement ok
REVOKE SYSTEM VIEWACTIVITYREDACTED FROM testuser

query ITTTTTTTTTTTTTT colnames
SELECT * FROM crdb_internal.node_sessions WHERE node_id < 0
----
Expand Down

0 comments on commit a7bf183

Please sign in to comment.