|
| 1 | +# LogicTest: 3node-tenant |
| 2 | + |
| 3 | +# Create a table, write a row, lock it, then switch users. |
| 4 | +statement ok |
| 5 | +CREATE TABLE t (k STRING PRIMARY KEY, v STRING, FAMILY (k,v)) |
| 6 | + |
| 7 | +statement ok |
| 8 | +GRANT ALL ON t TO testuser |
| 9 | + |
| 10 | +statement ok |
| 11 | +INSERT INTO t VALUES ('a', 'val1'), ('b', 'val2'), ('c', 'val3'), ('l', 'val4'), ('m', 'val5'), ('p', 'val6'), ('s', 'val7'), ('t', 'val8'), ('z', 'val9') |
| 12 | + |
| 13 | +# Also create an additional user with VIEWACTIVITYREDACTED, with only permissions on t |
| 14 | +statement ok |
| 15 | +CREATE USER testuser2 WITH VIEWACTIVITYREDACTED |
| 16 | + |
| 17 | +statement ok |
| 18 | +GRANT ALL ON t TO testuser2 |
| 19 | + |
| 20 | +statement ok |
| 21 | +CREATE TABLE t2 (k STRING PRIMARY KEY, v STRING, FAMILY (k,v)) |
| 22 | + |
| 23 | +statement ok |
| 24 | +INSERT INTO t2 VALUES ('a', 'val1'), ('b', 'val2') |
| 25 | + |
| 26 | +# Start txn1 where we acquire replicated locks |
| 27 | +statement ok |
| 28 | +BEGIN PRIORITY HIGH |
| 29 | + |
| 30 | +statement ok |
| 31 | +UPDATE t SET v = '_updated' WHERE k >= 'b' AND k < 'x' |
| 32 | + |
| 33 | +let $root_session |
| 34 | +SHOW session_id |
| 35 | + |
| 36 | +user testuser |
| 37 | + |
| 38 | +let $testuser_session |
| 39 | +SHOW session_id |
| 40 | + |
| 41 | +statement ok |
| 42 | +BEGIN |
| 43 | + |
| 44 | +# switch back to root, collect data needed for validation |
| 45 | +user root |
| 46 | + |
| 47 | +let $txn1 |
| 48 | +SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$root_session' |
| 49 | + |
| 50 | +let $txn2 |
| 51 | +SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$testuser_session' |
| 52 | + |
| 53 | +user testuser |
| 54 | + |
| 55 | +query TT async,rowsort readReq |
| 56 | +SELECT * FROM t |
| 57 | +---- |
| 58 | +a val1 |
| 59 | +b _updated |
| 60 | +c _updated |
| 61 | +l _updated |
| 62 | +m _updated |
| 63 | +p _updated |
| 64 | +s _updated |
| 65 | +t _updated |
| 66 | +z val9 |
| 67 | + |
| 68 | +user root |
| 69 | + |
| 70 | +query TTT colnames,retry |
| 71 | +SELECT user_name, query, phase FROM crdb_internal.cluster_queries WHERE txn_id='$txn2' |
| 72 | +---- |
| 73 | +user_name query phase |
| 74 | +testuser SELECT * FROM t executing |
| 75 | + |
| 76 | +# looking at each transaction separately, validate the expected results in the lock table |
| 77 | +query TTTTTTBB colnames,retry |
| 78 | +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1' |
| 79 | +---- |
| 80 | +database_name schema_name table_name lock_key_pretty lock_strength durability granted contended |
| 81 | +test public t /Table/106/1/"b"/0 Exclusive Replicated true true |
| 82 | +test public t /Table/106/1/"c"/0 Exclusive Replicated true false |
| 83 | +test public t /Table/106/1/"l"/0 Exclusive Replicated true false |
| 84 | +test public t /Table/106/1/"m"/0 Exclusive Replicated true false |
| 85 | +test public t /Table/106/1/"p"/0 Exclusive Replicated true false |
| 86 | +test public t /Table/106/1/"s"/0 Exclusive Replicated true false |
| 87 | +test public t /Table/106/1/"t"/0 Exclusive Replicated true false |
| 88 | + |
| 89 | +query TTTTTTBB colnames |
| 90 | +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn2' |
| 91 | +---- |
| 92 | +database_name schema_name table_name lock_key_pretty lock_strength durability granted contended |
| 93 | +test public t /Table/106/1/"b"/0 None Replicated false true |
| 94 | + |
| 95 | +# check that we can't see keys, potentially revealing PII, with VIEWACTIVITYREDACTED |
| 96 | +user testuser2 |
| 97 | + |
| 98 | +query TTTTTTBB colnames |
| 99 | +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1' |
| 100 | +---- |
| 101 | +database_name schema_name table_name lock_key_pretty lock_strength durability granted contended |
| 102 | +test public t · Exclusive Replicated true true |
| 103 | +test public t · Exclusive Replicated true false |
| 104 | +test public t · Exclusive Replicated true false |
| 105 | +test public t · Exclusive Replicated true false |
| 106 | +test public t · Exclusive Replicated true false |
| 107 | +test public t · Exclusive Replicated true false |
| 108 | +test public t · Exclusive Replicated true false |
| 109 | + |
| 110 | +user root |
| 111 | + |
| 112 | +query I |
| 113 | +SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't' |
| 114 | +---- |
| 115 | +8 |
| 116 | + |
| 117 | +statement ok |
| 118 | +COMMIT |
| 119 | + |
| 120 | +query I retry |
| 121 | +SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't' |
| 122 | +---- |
| 123 | +0 |
| 124 | + |
| 125 | +user testuser |
| 126 | + |
| 127 | +awaitquery readReq |
| 128 | + |
| 129 | +statement ok |
| 130 | +COMMIT |
| 131 | + |
| 132 | +user root |
| 133 | + |
| 134 | +# start txn3 |
| 135 | +statement ok |
| 136 | +BEGIN |
| 137 | + |
| 138 | +user testuser |
| 139 | + |
| 140 | +# start txn4 |
| 141 | +statement ok |
| 142 | +BEGIN |
| 143 | + |
| 144 | +user root |
| 145 | + |
| 146 | +query TT rowsort |
| 147 | +SELECT * FROM t FOR UPDATE |
| 148 | +---- |
| 149 | +a val1 |
| 150 | +b _updated |
| 151 | +c _updated |
| 152 | +l _updated |
| 153 | +m _updated |
| 154 | +p _updated |
| 155 | +s _updated |
| 156 | +t _updated |
| 157 | +z val9 |
| 158 | + |
| 159 | +let $txn3 |
| 160 | +SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$root_session' |
| 161 | + |
| 162 | +let $txn4 |
| 163 | +SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$testuser_session' |
| 164 | + |
| 165 | +user testuser |
| 166 | + |
| 167 | +statement async deleteReq count 7 |
| 168 | +DELETE FROM t WHERE k >= 'b' AND k < 'x' |
| 169 | + |
| 170 | +user root |
| 171 | + |
| 172 | +query TTT colnames,retry |
| 173 | +SELECT user_name, query, phase FROM crdb_internal.cluster_queries WHERE txn_id='$txn4' |
| 174 | +---- |
| 175 | +user_name query phase |
| 176 | +testuser DELETE FROM t WHERE (k >= 'b') AND (k < 'x') executing |
| 177 | + |
| 178 | +# looking at each transaction separately, validate the expected results in the lock table |
| 179 | +query TTTTTTBB colnames,retry |
| 180 | +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn3' |
| 181 | +---- |
| 182 | +database_name schema_name table_name lock_key_pretty lock_strength durability granted contended |
| 183 | +test public t /Table/106/1/"a"/0 Exclusive Unreplicated true false |
| 184 | +test public t /Table/106/1/"b"/0 Exclusive Unreplicated true true |
| 185 | +test public t /Table/106/1/"c"/0 Exclusive Unreplicated true false |
| 186 | +test public t /Table/106/1/"l"/0 Exclusive Unreplicated true false |
| 187 | +test public t /Table/106/1/"m"/0 Exclusive Unreplicated true false |
| 188 | +test public t /Table/106/1/"p"/0 Exclusive Unreplicated true false |
| 189 | +test public t /Table/106/1/"s"/0 Exclusive Unreplicated true false |
| 190 | +test public t /Table/106/1/"t"/0 Exclusive Unreplicated true false |
| 191 | +test public t /Table/106/1/"z"/0 Exclusive Unreplicated true false |
| 192 | + |
| 193 | +query TTTTTTBB colnames |
| 194 | +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn4' |
| 195 | +---- |
| 196 | +database_name schema_name table_name lock_key_pretty lock_strength durability granted contended |
| 197 | +test public t /Table/106/1/"b"/0 Exclusive Unreplicated false true |
| 198 | + |
| 199 | +query I |
| 200 | +SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't' |
| 201 | +---- |
| 202 | +10 |
| 203 | + |
| 204 | +statement ok |
| 205 | +ROLLBACK |
| 206 | + |
| 207 | +user testuser |
| 208 | + |
| 209 | +awaitstatement deleteReq |
| 210 | + |
| 211 | +statement ok |
| 212 | +COMMIT |
| 213 | + |
| 214 | +user root |
| 215 | + |
| 216 | +query I retry |
| 217 | +SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't' |
| 218 | +---- |
| 219 | +0 |
| 220 | + |
| 221 | +# validate that only locks on keys in privileged tables can be seen |
| 222 | +statement ok |
| 223 | +BEGIN |
| 224 | + |
| 225 | +query TT rowsort |
| 226 | +SELECT * FROM t FOR UPDATE |
| 227 | +---- |
| 228 | +a val1 |
| 229 | +z val9 |
| 230 | + |
| 231 | +query TT rowsort |
| 232 | +SELECT * FROM t2 FOR UPDATE |
| 233 | +---- |
| 234 | +a val1 |
| 235 | +b val2 |
| 236 | + |
| 237 | +query I retry |
| 238 | +SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name IN ('t','t2') |
| 239 | +---- |
| 240 | +4 |
| 241 | + |
| 242 | +user testuser |
| 243 | + |
| 244 | +query error pq: user testuser does not have VIEWACTIVITY or VIEWACTIVITYREDACTED privilege |
| 245 | +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks |
| 246 | + |
| 247 | +user testuser2 |
| 248 | + |
| 249 | +query TTTTTTBB colnames |
| 250 | +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name IN ('t', 't2') |
| 251 | +---- |
| 252 | +database_name schema_name table_name lock_key_pretty lock_strength durability granted contended |
| 253 | +test public t · Exclusive Unreplicated true false |
| 254 | +test public t · Exclusive Unreplicated true false |
| 255 | + |
| 256 | +user root |
| 257 | + |
| 258 | +statement ok |
| 259 | +ROLLBACK |
| 260 | + |
| 261 | +query I retry |
| 262 | +SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name IN ('t','t2') |
| 263 | +---- |
| 264 | +0 |
0 commit comments