/
cassandra.rs
539 lines (503 loc) · 21 KB
/
cassandra.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use crate::frame::cassandra::{CassandraMetadata, CassandraOperation, Tracing};
use crate::frame::{CassandraFrame, Frame, MessageType};
use crate::message::{Encodable, Message, Messages, Metadata};
use crate::server::CodecReadError;
use anyhow::{anyhow, Result};
use bytes::{Buf, BufMut, BytesMut};
use cassandra_protocol::compression::Compression;
use cassandra_protocol::frame::message_error::{ErrorBody, ErrorType};
use cassandra_protocol::frame::message_supported::BodyResSupported;
use cassandra_protocol::frame::{
CheckEnvelopeSizeError, Envelope as RawCassandraFrame, Opcode, Version,
};
use cql3_parser::cassandra_statement::CassandraStatement;
use cql3_parser::common::Identifier;
use tokio_util::codec::{Decoder, Encoder};
use tracing::info;
#[derive(Debug, Clone)]
pub struct CassandraCodec {
compression: Compression,
messages: Vec<Message>,
current_use_keyspace: Option<Identifier>,
}
impl Default for CassandraCodec {
fn default() -> Self {
CassandraCodec::new()
}
}
impl CassandraCodec {
pub fn new() -> CassandraCodec {
CassandraCodec {
compression: Compression::None,
messages: vec![],
current_use_keyspace: None,
}
}
}
impl CassandraCodec {
fn encode_raw(&mut self, item: CassandraFrame, dst: &mut BytesMut) {
let buffer = item.encode(self.compression);
if buffer.is_empty() {
info!("trying to send 0 length frame");
}
dst.put(buffer.as_slice());
}
}
impl Decoder for CassandraCodec {
type Item = Messages;
type Error = CodecReadError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, CodecReadError> {
loop {
match RawCassandraFrame::check_envelope_size(src) {
Ok(frame_len) => {
// Clear the read bytes from the FramedReader
let bytes = src.split_to(frame_len);
tracing::debug!(
"incoming cassandra message:\n{}",
pretty_hex::pretty_hex(&bytes)
);
let version = Version::try_from(bytes[0])
.expect("Gauranteed because check_envelope_size only returns Ok if the Version will parse");
if let Version::V3 | Version::V4 = version {
// Accept these protocols
} else {
// Reject protocols that cassandra-protocol supports but shotover does not yet support
return Err(reject_protocol_version(version.into()));
}
let mut message = Message::from_bytes(bytes.freeze(), MessageType::Cassandra);
// if is startup message, reject compression because shotover does not support
if let Some(Frame::Cassandra(frame)) = message.frame() {
if let CassandraOperation::Startup(startup) = &mut frame.operation {
if let Some(compression) = startup.map.get("COMPRESSION") {
return Err(reject_compression(frame.stream_id, compression));
}
}
if let CassandraOperation::Supported(BodyResSupported { data }) =
&mut frame.operation
{
if let Some(value) = data.get_mut("COMPRESSION") {
*value = vec![];
}
message.invalidate_cache();
}
}
if let Ok(Metadata::Cassandra(CassandraMetadata {
opcode: Opcode::Query | Opcode::Batch,
..
})) = message.metadata()
{
if let Some(keyspace) = get_use_keyspace(&mut message) {
self.current_use_keyspace = Some(keyspace);
}
if let Some(keyspace) = &self.current_use_keyspace {
set_default_keyspace(&mut message, keyspace);
}
}
self.messages.push(message);
}
Err(CheckEnvelopeSizeError::NotEnoughBytes) => {
if self.messages.is_empty() || src.remaining() != 0 {
return Ok(None);
} else {
return Ok(Some(std::mem::take(&mut self.messages)));
}
}
Err(CheckEnvelopeSizeError::UnsupportedVersion(version)) => {
return Err(reject_protocol_version(version));
}
err => {
return Err(CodecReadError::Parser(anyhow!(
"Failed to parse frame {:?}",
err
)))
}
}
}
}
}
fn get_use_keyspace(message: &mut Message) -> Option<Identifier> {
if let Some(Frame::Cassandra(frame)) = message.frame() {
if let CassandraOperation::Query { query, .. } = &mut frame.operation {
if let CassandraStatement::Use(keyspace) = query.as_ref() {
return Some(keyspace.clone());
}
}
}
None
}
fn set_default_keyspace(message: &mut Message, keyspace: &Identifier) {
// TODO: rewrite Operation::Prepared in the same way
if let Some(Frame::Cassandra(frame)) = message.frame() {
for query in frame.operation.queries() {
let name = match query {
CassandraStatement::AlterMaterializedView(x) => &mut x.name,
CassandraStatement::AlterTable(x) => &mut x.name,
CassandraStatement::AlterType(x) => &mut x.name,
CassandraStatement::CreateAggregate(x) => &mut x.name,
CassandraStatement::CreateFunction(x) => &mut x.name,
CassandraStatement::CreateIndex(x) => &mut x.table,
CassandraStatement::CreateMaterializedView(x) => &mut x.name,
CassandraStatement::CreateTable(x) => &mut x.name,
CassandraStatement::CreateTrigger(x) => &mut x.name,
CassandraStatement::CreateType(x) => &mut x.name,
CassandraStatement::Delete(x) => &mut x.table_name,
CassandraStatement::DropAggregate(x) => &mut x.name,
CassandraStatement::DropFunction(x) => &mut x.name,
CassandraStatement::DropIndex(x) => &mut x.name,
CassandraStatement::DropMaterializedView(x) => &mut x.name,
CassandraStatement::DropTable(x) => &mut x.name,
CassandraStatement::DropTrigger(x) => &mut x.name,
CassandraStatement::DropType(x) => &mut x.name,
CassandraStatement::Insert(x) => &mut x.table_name,
CassandraStatement::Select(x) => &mut x.table_name,
CassandraStatement::Truncate(name) => name,
CassandraStatement::Update(x) => &mut x.table_name,
CassandraStatement::AlterKeyspace(_)
| CassandraStatement::AlterRole(_)
| CassandraStatement::AlterUser(_)
| CassandraStatement::ApplyBatch
| CassandraStatement::CreateKeyspace(_)
| CassandraStatement::CreateRole(_)
| CassandraStatement::CreateUser(_)
| CassandraStatement::DropRole(_)
| CassandraStatement::DropUser(_)
| CassandraStatement::Grant(_)
| CassandraStatement::ListRoles(_)
| CassandraStatement::Revoke(_)
| CassandraStatement::DropKeyspace(_)
| CassandraStatement::ListPermissions(_)
| CassandraStatement::Use(_)
| CassandraStatement::Unknown(_) => {
return;
}
};
if name.keyspace.is_none() {
name.keyspace = Some(keyspace.clone());
}
}
}
}
/// If the client tried to use a protocol that we dont support then we need to reject it.
/// The rejection process is sending back an error and then closing the connection.
fn reject_protocol_version(version: u8) -> CodecReadError {
info!(
"Negotiating protocol version: rejecting version {} (configure the client to use a supported version by default to improve connection time)",
version
);
CodecReadError::RespondAndThenCloseConnection(vec![Message::from_frame(Frame::Cassandra(
CassandraFrame {
version: Version::V4,
stream_id: 0,
operation: CassandraOperation::Error(ErrorBody {
message: "Invalid or unsupported protocol version".into(),
ty: ErrorType::Protocol,
}),
tracing: Tracing::Response(None),
warnings: vec![],
},
))])
}
fn reject_compression(stream_id: i16, compression: &String) -> CodecReadError {
info!(
"Rejecting compression option {} (configure the client to use no compression)",
compression
);
CodecReadError::RespondAndThenCloseConnection(vec![Message::from_frame(Frame::Cassandra(
CassandraFrame {
version: Version::V4,
stream_id,
operation: CassandraOperation::Error(ErrorBody {
message: format!("Unsupported compression type {}", compression),
ty: ErrorType::Protocol,
}),
tracing: Tracing::Response(None),
warnings: vec![],
},
))])
}
impl Encoder<Messages> for CassandraCodec {
type Error = anyhow::Error;
fn encode(
&mut self,
item: Messages,
dst: &mut BytesMut,
) -> std::result::Result<(), Self::Error> {
for m in item {
let start = dst.len();
// TODO: always check if cassandra message
match m.into_encodable(MessageType::Cassandra)? {
Encodable::Bytes(bytes) => dst.extend_from_slice(&bytes),
Encodable::Frame(frame) => self.encode_raw(frame.into_cassandra().unwrap(), dst),
}
tracing::debug!(
"outgoing cassandra message:\n{}",
pretty_hex::pretty_hex(&&dst[start..])
);
}
Ok(())
}
}
#[cfg(test)]
mod cassandra_protocol_tests {
use crate::codec::cassandra::CassandraCodec;
use crate::frame::cassandra::{
parse_statement_single, CassandraFrame, CassandraOperation, CassandraResult, Tracing,
};
use crate::frame::Frame;
use crate::message::Message;
use bytes::BytesMut;
use cassandra_protocol::events::SimpleServerEvent;
use cassandra_protocol::frame::message_register::BodyReqRegister;
use cassandra_protocol::frame::message_result::{
ColSpec, ColType, ColTypeOption, ColTypeOptionValue, RowsMetadata, RowsMetadataFlags,
TableSpec,
};
use cassandra_protocol::frame::message_startup::BodyReqStartup;
use cassandra_protocol::frame::Version;
use hex_literal::hex;
use std::collections::HashMap;
use tokio_util::codec::{Decoder, Encoder};
fn test_frame_codec_roundtrip(
codec: &mut CassandraCodec,
raw_frame: &[u8],
expected_messages: Vec<Message>,
) {
// test decode
let decoded_messages = codec
.decode(&mut BytesMut::from(raw_frame))
.unwrap()
.unwrap();
// test messages parse correctly
let mut parsed_messages = decoded_messages.clone();
for message in &mut parsed_messages {
// This has the side effect of modifying the inner message to be parsed
message.frame().unwrap();
message.invalidate_cache();
}
assert_eq!(parsed_messages, expected_messages);
// test encode round trip - parsed messages
{
let mut dest = BytesMut::new();
codec.encode(parsed_messages, &mut dest).unwrap();
assert_eq!(raw_frame, &dest.to_vec());
}
// test encode round trip - raw messages
{
let mut dest = BytesMut::new();
codec.encode(decoded_messages, &mut dest).unwrap();
assert_eq!(raw_frame, &dest.to_vec());
}
}
#[test]
fn test_codec_startup() {
let mut codec = CassandraCodec::new();
let mut startup_body: HashMap<String, String> = HashMap::new();
startup_body.insert("CQL_VERSION".into(), "3.0.0".into());
let bytes = hex!("0400000001000000160001000b43514c5f56455253494f4e0005332e302e30");
let messages = vec![Message::from_frame(Frame::Cassandra(CassandraFrame {
version: Version::V4,
operation: CassandraOperation::Startup(BodyReqStartup { map: startup_body }),
stream_id: 0,
tracing: Tracing::Request(false),
warnings: vec![],
}))];
test_frame_codec_roundtrip(&mut codec, &bytes, messages);
}
#[test]
fn test_codec_options() {
let mut codec = CassandraCodec::new();
let bytes = hex!("040000000500000000");
let messages = vec![Message::from_frame(Frame::Cassandra(CassandraFrame {
version: Version::V4,
operation: CassandraOperation::Options(vec![]),
stream_id: 0,
tracing: Tracing::Request(false),
warnings: vec![],
}))];
test_frame_codec_roundtrip(&mut codec, &bytes, messages);
}
#[test]
fn test_codec_ready() {
let mut codec = CassandraCodec::new();
let bytes = hex!("840000000200000000");
let messages = vec![Message::from_frame(Frame::Cassandra(CassandraFrame {
version: Version::V4,
operation: CassandraOperation::Ready(vec![]),
stream_id: 0,
tracing: Tracing::Response(None),
warnings: vec![],
}))];
test_frame_codec_roundtrip(&mut codec, &bytes, messages);
}
#[test]
fn test_codec_register() {
let mut codec = CassandraCodec::new();
let bytes = hex!(
"040000010b000000310003000f544f504f4c4f47595f4348414e4745
000d5354415455535f4348414e4745000d534348454d415f4348414e4745"
);
let messages = vec![Message::from_frame(Frame::Cassandra(CassandraFrame {
version: Version::V4,
operation: CassandraOperation::Register(BodyReqRegister {
events: vec![
SimpleServerEvent::TopologyChange,
SimpleServerEvent::StatusChange,
SimpleServerEvent::SchemaChange,
],
}),
stream_id: 1,
tracing: Tracing::Request(false),
warnings: vec![],
}))];
test_frame_codec_roundtrip(&mut codec, &bytes, messages);
}
#[test]
fn test_codec_result() {
let mut codec = CassandraCodec::new();
let bytes = hex!(
"840000020800000099000000020000000100000009000673797374656
d000570656572730004706565720010000b646174615f63656e746572000d0007686f73745f6964000c000c70726566
65727265645f6970001000047261636b000d000f72656c656173655f76657273696f6e000d000b7270635f616464726
573730010000e736368656d615f76657273696f6e000c0006746f6b656e730022000d00000000"
);
let messages = vec![Message::from_frame(Frame::Cassandra(CassandraFrame {
version: Version::V4,
operation: CassandraOperation::Result(CassandraResult::Rows {
rows: vec![],
metadata: Box::new(RowsMetadata {
flags: RowsMetadataFlags::GLOBAL_TABLE_SPACE,
columns_count: 9,
paging_state: None,
new_metadata_id: None,
global_table_spec: Some(TableSpec {
ks_name: "system".into(),
table_name: "peers".into(),
}),
col_specs: vec![
ColSpec {
table_spec: None,
name: "peer".into(),
col_type: ColTypeOption {
id: ColType::Inet,
value: None,
},
},
ColSpec {
table_spec: None,
name: "data_center".into(),
col_type: ColTypeOption {
id: ColType::Varchar,
value: None,
},
},
ColSpec {
table_spec: None,
name: "host_id".into(),
col_type: ColTypeOption {
id: ColType::Uuid,
value: None,
},
},
ColSpec {
table_spec: None,
name: "preferred_ip".into(),
col_type: ColTypeOption {
id: ColType::Inet,
value: None,
},
},
ColSpec {
table_spec: None,
name: "rack".into(),
col_type: ColTypeOption {
id: ColType::Varchar,
value: None,
},
},
ColSpec {
table_spec: None,
name: "release_version".into(),
col_type: ColTypeOption {
id: ColType::Varchar,
value: None,
},
},
ColSpec {
table_spec: None,
name: "rpc_address".into(),
col_type: ColTypeOption {
id: ColType::Inet,
value: None,
},
},
ColSpec {
table_spec: None,
name: "schema_version".into(),
col_type: ColTypeOption {
id: ColType::Uuid,
value: None,
},
},
ColSpec {
table_spec: None,
name: "tokens".into(),
col_type: ColTypeOption {
id: ColType::Set,
value: Some(ColTypeOptionValue::CSet(Box::new(ColTypeOption {
id: ColType::Varchar,
value: None,
}))),
},
},
],
}),
}),
stream_id: 2,
tracing: Tracing::Response(None),
warnings: vec![],
}))];
test_frame_codec_roundtrip(&mut codec, &bytes, messages);
}
#[test]
fn test_codec_query_select() {
let mut codec = CassandraCodec::new();
let bytes = hex!(
"0400000307000000350000002e53454c454354202a2046524f4d20737973
74656d2e6c6f63616c205748455245206b6579203d20276c6f63616c27000100"
);
let messages = vec![Message::from_frame(Frame::Cassandra(CassandraFrame {
version: Version::V4,
stream_id: 3,
tracing: Tracing::Request(false),
warnings: vec![],
operation: CassandraOperation::Query {
query: Box::new(parse_statement_single(
"SELECT * FROM system.local WHERE key = 'local'",
)),
params: Box::default(),
},
}))];
test_frame_codec_roundtrip(&mut codec, &bytes, messages);
}
#[test]
fn test_codec_query_insert() {
let mut codec = CassandraCodec::new();
let bytes = hex!(
"0400000307000000330000002c494e5345525420494e544f207379737465
6d2e666f6f2028626172292056414c554553202827626172322729000100"
);
let messages = vec![Message::from_frame(Frame::Cassandra(CassandraFrame {
version: Version::V4,
stream_id: 3,
tracing: Tracing::Request(false),
warnings: vec![],
operation: CassandraOperation::Query {
query: Box::new(parse_statement_single(
"INSERT INTO system.foo (bar) VALUES ('bar2')",
)),
params: Box::default(),
},
}))];
test_frame_codec_roundtrip(&mut codec, &bytes, messages);
}
}