Debian Codegraph @

src/protocol/connect/auth_switch.rs

raw symbols SCIP
1 use bytesrust-analyzer cargo bytes 1.12.1 crate/::{Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#, Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#};
2
3 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::errorrust-analyzer cargo sqlx-mysql 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#;
4 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::iorust-analyzer cargo sqlx-mysql 0.8.3 io/::ProtocolEncoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#;
5 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::iorust-analyzer cargo sqlx-mysql 0.8.3 io/::{BufExtrust-analyzer cargo sqlx-core 0.8.3 io/buf/BufExt#, ProtocolDecoderust-analyzer cargo sqlx-core 0.8.3 io/decode/ProtocolDecode#};
6 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::protocolrust-analyzer cargo sqlx-mysql 0.8.3 protocol/::authrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/::AuthPluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#;
7 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::protocolrust-analyzer cargo sqlx-mysql 0.8.3 protocol/::Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#;
8
9 // https://dev.mysql.com/doc/dev/mysql-server/8.0.12/page_protocol_connection_phase_packets_protocol_auth_switch_request.html
10
11 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
12 pub struct AuthSwitchRequestrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest# {
13 pub pluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#plugin.: AuthPluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#,
14 pub datarust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#data.: Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#,
15 }
16
17 impl ProtocolDecoderust-analyzer cargo sqlx-core 0.8.3 io/decode/ProtocolDecode#<'_, bool> for AuthSwitchRequestrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest# {
18 fn decode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchRequest][`ProtocolDecode<'_, bool>`]decode_with().(mut buf: Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#, enable_cleartext_plugin: bool) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchRequest][`ProtocolDecode<'_, bool>`], Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
19 let header = buf.get_u8rust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#get_u8().();
20 if header rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().0xfe {
21 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!(
22 "expected 0xfe (AUTH_SWITCH) but found 0x{:x}",
23 header
24 ));
25 }
26
27 let plugin = buf.get_str_nulrust-analyzer cargo sqlx-core 0.8.3 io/buf/impl#[Bytes][BufExt]get_str_nul().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
28
29 if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(plugin, AuthPluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#::MySqlClearPasswordrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#MySqlClearPassword#) && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().enable_cleartext_plugin {
30 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!("mysql_cleartext_plugin disabled"));
31 }
32
33 if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(plugin, AuthPluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#::MySqlClearPasswordrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#MySqlClearPassword#) && buf.is_emptyrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]is_empty().() {
34 // Contrary to the MySQL protocol, AWS Aurora with IAM sends
35 // no data. That is fine because the mysql_clear_password says to
36 // ignore any data sent.
37 // See: https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_authentication_methods_clear_text_password.html
38 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchRequest][`ProtocolDecode<'_, bool>`] {
39 pluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#plugin.,
40 datarust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#data.: Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().(),
41 });
42 }
43
44 // See: https://github.com/mysql/mysql-server/blob/ea7d2e2d16ac03afdd9cb72a972a95981107bf51/sql/auth/sha2_password.cc#L942
45 if buf.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().21 {
46 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!(
47 "expected 21 bytes but found {} bytes",
48 buf.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().()
49 ));
50 }
51 let data = buf.get_bytesrust-analyzer cargo sqlx-core 0.8.3 io/buf/impl#[Bytes][BufExt]get_bytes().(20);
52 buf.advancerust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes][Buf]advance().(1); // NUL-terminator
53
54 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchRequest][`ProtocolDecode<'_, bool>`] { pluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#plugin., datarust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#data. })
55 }
56 }
57
58 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
59 pub struct AuthSwitchResponserust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchResponse#(pub Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>);
60
61 impl ProtocolEncoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#<'_, Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#> for AuthSwitchResponserust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchResponse# {
62 fn encode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchResponse][`ProtocolEncode<'_, Capabilities>`]encode_with().(&self, buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, _: Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
63 buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&self.0rust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchResponse#0.);
64 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
65 }
66 }
67
68 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
69 fn test_decode_auth_switch_packet_datarust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/test_decode_auth_switch_packet_data().() {
70 const AUTH_SWITCH_NO_DATArust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AUTH_SWITCH_NO_DATA.: &[u8] = b"\xfecaching_sha2_password\x00abcdefghijabcdefghij\x00";
71
72 let p = AuthSwitchRequestrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#::decode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchRequest][`ProtocolDecode<'_, bool>`]decode_with().(AUTH_SWITCH_NO_DATArust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AUTH_SWITCH_NO_DATA..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), true).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
73
74 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(p.pluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#plugin., AuthPluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#::CachingSha2Passwordrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#CachingSha2Password#));
75 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.datarust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#data., &b"abcdefghijabcdefghij"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
76 }
77
78 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
79 fn test_decode_auth_switch_cleartext_disabledrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/test_decode_auth_switch_cleartext_disabled().() {
80 const AUTH_SWITCH_CLEARTEXTrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AUTH_SWITCH_CLEARTEXT.: &[u8] = b"\xfemysql_clear_password\x00abcdefghijabcdefghij\x00";
81
82 let e = AuthSwitchRequestrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#::decode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchRequest][`ProtocolDecode<'_, bool>`]decode_with().(AUTH_SWITCH_CLEARTEXTrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AUTH_SWITCH_CLEARTEXT..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), false).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
83
84 let e_str = e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
85
86 let expected = "encountered unexpected or invalid data: mysql_cleartext_plugin disabled";
87
88 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
89 // Don't want to assert the full string since it contains the module path now.
90 e_str.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(expected),
91 "expected error string to start with {expected:?}, got {e_str:?}"
92 );
93 }
94
95 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
96 fn test_decode_auth_switch_packet_no_datarust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/test_decode_auth_switch_packet_no_data().() {
97 const AUTH_SWITCH_NO_DATArust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AUTH_SWITCH_NO_DATA.: &[u8] = b"\xfemysql_clear_password\x00";
98
99 let p = AuthSwitchRequestrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#::decode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/impl#[AuthSwitchRequest][`ProtocolDecode<'_, bool>`]decode_with().(AUTH_SWITCH_NO_DATArust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AUTH_SWITCH_NO_DATA..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), true).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
100
101 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(p.pluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#plugin., AuthPluginrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#::MySqlClearPasswordrust-analyzer cargo sqlx-mysql 0.8.3 protocol/auth/AuthPlugin#MySqlClearPassword#));
102 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.datarust-analyzer cargo sqlx-mysql 0.8.3 protocol/connect/auth_switch/AuthSwitchRequest#data., Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().());
103 }