-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathsession.rs
440 lines (403 loc) · 15.3 KB
/
session.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
use std::net::SocketAddr;
use std::ops::Deref;
use std::sync::Arc;
use bytes::{Buf, Bytes, BytesMut};
use rand::Rng;
use rustls::ServerConfig;
use tokio::net::TcpStream;
use tokio::sync::Mutex;
use tracing::*;
use uuid::Uuid;
use warpgate_common::auth::{AuthCredential, AuthResult, AuthSelector, CredentialKind};
use warpgate_common::helpers::rng::get_crypto_rng;
use warpgate_common::{Secret, TargetMySqlOptions, TargetOptions};
use warpgate_core::{authorize_ticket, consume_ticket, Services, WarpgateServerHandle};
use warpgate_database_protocols::io::{BufExt, Decode};
use warpgate_database_protocols::mysql::protocol::auth::AuthPlugin;
use warpgate_database_protocols::mysql::protocol::connect::{
AuthSwitchRequest, Handshake, HandshakeResponse,
};
use warpgate_database_protocols::mysql::protocol::response::{ErrPacket, OkPacket, Status};
use warpgate_database_protocols::mysql::protocol::text::Query;
use warpgate_database_protocols::mysql::protocol::Capabilities;
use crate::client::{ConnectionOptions, MySqlClient};
use crate::error::MySqlError;
use crate::stream::MySqlStream;
pub struct MySqlSession {
stream: MySqlStream<tokio_rustls::server::TlsStream<TcpStream>>,
capabilities: Capabilities,
challenge: [u8; 20],
username: Option<String>,
database: Option<String>,
tls_config: Arc<ServerConfig>,
server_handle: Arc<Mutex<WarpgateServerHandle>>,
id: Uuid,
services: Services,
remote_address: SocketAddr,
}
impl MySqlSession {
pub async fn new(
server_handle: Arc<Mutex<WarpgateServerHandle>>,
services: Services,
stream: TcpStream,
tls_config: ServerConfig,
remote_address: SocketAddr,
) -> Self {
let id = server_handle.lock().await.id();
Self {
services,
stream: MySqlStream::new(stream),
capabilities: Capabilities::PROTOCOL_41
| Capabilities::PLUGIN_AUTH
| Capabilities::FOUND_ROWS
| Capabilities::LONG_FLAG
| Capabilities::NO_SCHEMA
| Capabilities::PLUGIN_AUTH_LENENC_DATA
| Capabilities::CONNECT_WITH_DB
| Capabilities::SESSION_TRACK
| Capabilities::IGNORE_SPACE
| Capabilities::INTERACTIVE
| Capabilities::TRANSACTIONS
| Capabilities::DEPRECATE_EOF
| Capabilities::SECURE_CONNECTION
| Capabilities::SSL,
challenge: get_crypto_rng().gen(),
tls_config: Arc::new(tls_config),
username: None,
database: None,
server_handle,
id,
remote_address,
}
}
pub fn make_logging_span(&self) -> tracing::Span {
let client_ip = self.remote_address.ip().to_string();
match self.username {
Some(ref username) => {
info_span!("MySQL", session=%self.id, session_username=%username, %client_ip)
}
None => info_span!("MySQL", session=%self.id, %client_ip),
}
}
pub async fn run(mut self) -> Result<(), MySqlError> {
let mut challenge_1 = BytesMut::from(&self.challenge[..]);
let challenge_2 = challenge_1.split_off(8);
let challenge_chain = challenge_1.freeze().chain(challenge_2.freeze());
let handshake = Handshake {
protocol_version: 10,
server_version: "8.0.0-Warpgate".to_owned(),
connection_id: 1,
auth_plugin_data: challenge_chain,
server_capabilities: self.capabilities,
server_default_collation: 45,
status: Status::empty(),
auth_plugin: Some(AuthPlugin::MySqlNativePassword),
};
self.stream.push(&handshake, ())?;
self.stream.flush().await?;
let resp = loop {
let Some(payload) = self.stream.recv().await? else {
return Err(MySqlError::Eof);
};
let resp = HandshakeResponse::decode_with(payload, &mut self.capabilities)
.map_err(MySqlError::decode)?;
trace!(?resp, "Handshake response");
info!(capabilities=?self.capabilities, username=%resp.username, "User handshake");
if self.capabilities.contains(Capabilities::SSL) {
if self.stream.is_tls() {
break resp;
}
self.stream = self.stream.upgrade(self.tls_config.clone()).await?;
continue;
} else {
self.send_error(1002, "Warpgate requires TLS - please enable it in your client: add `--ssl` on the CLI or add `?sslMode=PREFERRED` to your database URI").await?;
return Err(MySqlError::TlsNotSupportedByClient);
}
};
if resp.auth_plugin == Some(AuthPlugin::MySqlClearPassword) {
if let Some(mut response) = resp.auth_response.clone() {
let password = Secret::new(response.get_str_nul()?);
return self.run_authorization(resp, password).await;
}
}
let req = AuthSwitchRequest {
plugin: AuthPlugin::MySqlClearPassword,
data: Bytes::new(),
};
self.stream.push(&req, ())?;
// self.push(&RawBytes::<
self.stream.flush().await?;
let Some(response) = &self.stream.recv().await? else {
return Err(MySqlError::Eof);
};
let password = Secret::new(response.clone().get_str_nul()?);
self.run_authorization(resp, password).await
}
async fn send_error(&mut self, code: u16, message: &str) -> Result<(), MySqlError> {
self.stream.push(
&ErrPacket {
error_code: code,
error_message: message.to_owned(),
sql_state: None,
},
(),
)?;
self.stream.flush().await?;
Ok(())
}
pub async fn run_authorization(
mut self,
handshake: HandshakeResponse,
password: Secret<String>,
) -> Result<(), MySqlError> {
let selector: AuthSelector = handshake.username.deref().into();
async fn fail(this: &mut MySqlSession) -> Result<(), MySqlError> {
this.stream.push(
&ErrPacket {
error_code: 1,
error_message: "Warpgate access denied".to_owned(),
sql_state: None,
},
(),
)?;
this.stream.flush().await?;
Ok(())
}
match selector {
AuthSelector::User {
username,
target_name,
} => {
let state_arc = self
.services
.auth_state_store
.lock()
.await
.create(
Some(&self.server_handle.lock().await.id()),
&username,
crate::common::PROTOCOL_NAME,
&[CredentialKind::Password],
)
.await?
.1;
let mut state = state_arc.lock().await;
let user_auth_result = {
let credential = AuthCredential::Password(password);
let mut cp = self.services.config_provider.lock().await;
if cp.validate_credential(&username, &credential).await? {
state.add_valid_credential(credential);
}
state.verify()
};
match user_auth_result {
AuthResult::Accepted { username } => {
self.services
.auth_state_store
.lock()
.await
.complete(state.id())
.await;
let target_auth_result = {
self.services
.config_provider
.lock()
.await
.authorize_target(&username, &target_name)
.await
.map_err(MySqlError::other)?
};
if !target_auth_result {
warn!(
"Target {} not authorized for user {}",
target_name, username
);
return fail(&mut self).await;
}
self.run_authorized(handshake, username, target_name).await
}
AuthResult::Rejected | AuthResult::Need(_) => fail(&mut self).await, // TODO SSO
}
}
AuthSelector::Ticket { secret } => {
match authorize_ticket(&self.services.db, &secret)
.await
.map_err(MySqlError::other)?
{
Some(ticket) => {
info!("Authorized for {} with a ticket", ticket.target);
consume_ticket(&self.services.db, &ticket.id)
.await
.map_err(MySqlError::other)?;
self.run_authorized(handshake, ticket.username, ticket.target)
.await
}
_ => fail(&mut self).await,
}
}
}
}
async fn run_authorized(
mut self,
handshake: HandshakeResponse,
username: String,
target_name: String,
) -> Result<(), MySqlError> {
self.stream.push(
&OkPacket {
affected_rows: 0,
last_insert_id: 0,
status: Status::empty(),
warnings: 0,
},
(),
)?;
self.stream.flush().await?;
let target = {
self.services
.config_provider
.lock()
.await
.list_targets()
.await?
.iter()
.filter_map(|t| match t.options {
TargetOptions::MySql(ref options) => Some((t, options)),
_ => None,
})
.find(|(t, _)| t.name == target_name)
.map(|(t, opt)| (t.clone(), opt.clone()))
};
let Some((target, mysql_options)) = target else {
warn!("Selected target not found");
self.stream.push(
&ErrPacket {
error_code: 1,
error_message: "Warpgate access denied".to_owned(),
sql_state: None,
},
(),
)?;
self.stream.flush().await?;
return Ok(());
};
{
let handle = self.server_handle.lock().await;
handle.set_username(username).await?;
handle.set_target(&target).await?;
}
self.run_authorized_inner(handshake, mysql_options).await
}
async fn run_authorized_inner(
mut self,
handshake: HandshakeResponse,
options: TargetMySqlOptions,
) -> Result<(), MySqlError> {
self.database = handshake.database.clone();
self.username = Some(handshake.username);
if let Some(ref database) = handshake.database {
info!("Selected database: {database}");
}
let mut client = match MySqlClient::connect(
&options,
ConnectionOptions {
collation: handshake.collation,
database: handshake.database,
max_packet_size: handshake.max_packet_size,
capabilities: self.capabilities,
},
)
.await
{
Err(error) => {
error!(%error, "Target connection failed");
self.send_error(1045, "Access denied").await?;
Err(error)
}
x => x,
}?;
loop {
self.stream.reset_sequence_id();
client.stream.reset_sequence_id();
let Some(payload) = self.stream.recv().await? else {
break;
};
trace!(?payload, "server got packet");
let com = payload.first();
// COM_QUERY
if com == Some(&0x03) {
let query = Query::decode(payload)?;
info!(query=%query.0, "SQL");
client.stream.push(&query, ())?;
client.stream.flush().await?;
let mut eof_ctr = 0;
loop {
let Some(response) = client.stream.recv().await? else {
return Err(MySqlError::Eof);
};
trace!(?response, "client got packet");
self.stream.push(&&response[..], ())?;
self.stream.flush().await?;
if let Some(com) = response.first() {
if com == &0xfe {
if self.capabilities.contains(Capabilities::DEPRECATE_EOF) {
break;
}
eof_ctr += 1;
if eof_ctr == 2 {
// todo check multiple results
break;
}
}
if com == &0 || com == &0xff {
break;
}
}
}
// COM_QUIT
} else if com == Some(&0x01) {
break;
// COM_INIT_DB
} else if com == Some(&0x02) {
let mut buf = payload.clone();
buf.advance(1);
let db = buf.get_str(buf.len())?;
self.database = Some(db.clone());
info!("Selected database: {db}");
client.stream.push(&&payload[..], ())?;
client.stream.flush().await?;
self.passthrough_until_result(&mut client).await?;
// COM_FIELD_LIST, COM_PING, COM_RESET_CONNECTION
} else if com == Some(&0x04) || com == Some(&0x0e) || com == Some(&0x1f) {
client.stream.push(&&payload[..], ())?;
client.stream.flush().await?;
self.passthrough_until_result(&mut client).await?;
} else if let Some(com) = com {
warn!("Unknown packet type {com}");
self.send_error(1047, "Not implemented").await?;
} else {
break;
}
}
Ok(())
}
async fn passthrough_until_result(
&mut self,
client: &mut MySqlClient,
) -> Result<(), MySqlError> {
loop {
let Some(response) = client.stream.recv().await? else {
return Err(MySqlError::Eof);
};
trace!(?response, "client got packet");
self.stream.push(&&response[..], ())?;
self.stream.flush().await?;
if let Some(com) = response.first() {
if com == &0 || com == &0xff || com == &0xfe {
break;
}
}
}
Ok(())
}
}