Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic AuthPlugin support #28

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Conversation

sundy-li
Copy link
Contributor

@sundy-li sundy-li commented Sep 14, 2021

Changes:

  1. Support AuthPlugin, defaults to use mysql_native_password
  2. Bump some crate (continue Bump deps #22)
  3. Some minor bug/Lint fixes (EOF -> Eof, etc)

PsiACE and others added 20 commits July 19, 2021 10:29
Signed-off-by: Chojan Shang <psiace@outlook.com>
Signed-off-by: Chojan Shang <psiace@outlook.com>
Signed-off-by: Chojan Shang <psiace@outlook.com>
Signed-off-by: Chojan Shang <psiace@outlook.com>
Signed-off-by: Chojan Shang <psiace@outlook.com>
Signed-off-by: Chojan Shang <psiace@outlook.com>
Signed-off-by: Chojan Shang <psiace@outlook.com>
@jonhoo
Copy link
Owner

jonhoo commented Sep 17, 2021

It's pretty difficult to review this given that it also includes #22, so I'd like to hold off on this until we can land #22. I'm definitely in favor of landing something along these lines though! I'll leave a few inline comments.

src/commands.rs Outdated Show resolved Hide resolved
examples/serve_one.rs Outdated Show resolved Hide resolved
src/commands.rs Outdated Show resolved Hide resolved
@@ -82,6 +85,7 @@
//! ```
#![deny(missing_docs)]
#![deny(rust_2018_idioms)]
#![allow(clippy::from_over_into)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this?

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@sundy-li
Copy link
Contributor Author

Thanks for the detailed review.

Seems there are lots of styles/documents to improve. I'll try to apply the review suggestions.

@codecov
Copy link

codecov bot commented Sep 20, 2021

Codecov Report

Merging #28 (2387bc8) into master (e77fd27) will decrease coverage by 0.55%.
The diff coverage is 72.06%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #28      +/-   ##
==========================================
- Coverage   81.19%   80.63%   -0.56%     
==========================================
  Files          10       11       +1     
  Lines        2159     2391     +232     
==========================================
+ Hits         1753     1928     +175     
- Misses        406      463      +57     
Impacted Files Coverage Δ
src/packet.rs 88.88% <ø> (+1.58%) ⬆️
src/lib.rs 64.46% <57.42%> (-0.42%) ⬇️
src/value/encode.rs 62.13% <66.66%> (+0.20%) ⬆️
src/commands.rs 81.11% <68.75%> (-7.60%) ⬇️
src/value/utils.rs 81.42% <81.42%> (ø)
tests/async.rs 91.83% <85.71%> (-0.14%) ⬇️
src/params.rs 97.61% <100.00%> (ø)
src/resultset.rs 79.67% <100.00%> (ø)
src/value/decode.rs 78.94% <100.00%> (-0.37%) ⬇️
tests/main.rs 85.63% <100.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e77fd27...2387bc8. Read the comment docs.

Copy link
Owner

@jonhoo jonhoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left some more comments inline.

@@ -35,7 +37,9 @@ pub fn client_handshake(i: &[u8]) -> nom::IResult<&[u8], ClientHandshake> {

let (i, auth_response) =
if capabilities.contains(CapabilityFlags::CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA) {
let (i, size) = read_length_encoded_number(i)?;
let mut i = i;
let size = i.read_lenenc_int().unwrap_or(0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If read_lenenc_int fails, should we return an error here instead? Assuming it is 0 doesn't seem right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I don't know how to initialize which kind of nom Error can be raised.

BTW I will continue to finish the pr #22 , after that is merged, then you can review this pr.

@@ -155,32 +159,38 @@ pub trait MysqlShim<W: Write> {
"5.1.10-alpha-msql-proxy"
}

/// Connection id
fn connect_id(&self) -> u32 {
/// Default connection id
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still like to see some more documentation here about this value is. Ideally with a link to some MySQL/MariaDB documentation about what the connection ID is used for. Otherwise, as an implementor, I don't really know what I'm supposed to return here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also document what the default value is.

Comment on lines +167 to +168
/// default plugin for authentication
/// Plugin methods: https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_authentication_methods.html
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too I'd like to actually spell out what this is, what it's used for, when you might want to override it, and what the default value is (and why). I would copy-paste some of the documentation from that linked page, and include links to relevant subsections.

Comment on lines +173 to +174
/// Called when reading the handshake response by client's auth_plugin
/// See: https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase.html#sect_protocol_connection_phase_auth_method_mismatch
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here — let's give consumers of this API a bit more to go on. At the very least this should say something like

Allows you to determine the authentication plugin to use for a particular user, independently of what the server default is (see [MysqlShim::default_auth_plugin]).

"mysql_native_password"
}

/// Default salt(scramble) for auth plugin
fn salt(&self) -> [u8; SCRAMBLE_SIZE] {
/// Called when initializing the handshake
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually tell someone looking to implement this trait what this is used for, or what kind of value they should return. Again, copying from the MySQL documentation may be the way to go here.

)
})?;
self.writer.set_seq(seq + 1);
auth_response = auth_response_data.to_vec();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can avoid this .to_vec() and the .to_vec() on auth_response before drop(handshake) if you restructure the code above a little bit to:

let auth_ok = if user_custom_auth {
    drop(handshake);
    // the rest of the contents of the `if` until this line, then:
    self.shim.authenticate(user_auth_plugin, &username, &salt, &auth_response)
} else {
    self.shim.authenticate(&handshake.auth_plugin, &handshake.username, &salt, &handshake.auth_response)
};

drop(handshake);

let user_auth_plugin = self.shim.auth_plugin_for_user(&username);
// Start SwitchAuthRequest
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this comment into the if, since that's what it applies to.

.authenticate(user_auth_plugin, &username, &salt, &auth_response)
{
let err_msg = format!(
"Access denied for user '{}'@'{}' (using password: YES)",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "using password: YES" here conditional on something?

self.socket_addr.ip()
);
writers::write_err(
ErrorKind::ER_ACCESS_DENIED_ERROR,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we perhaps allow MysqlShim::authenticate to return Result<(), ErrorKind> so that the implementor can indicate the error type? Or perhaps even Result<(), (String, ErrorKind)> so they can also dictate the message?

| CapabilityFlags::CLIENT_SECURE_CONNECTION // this is required for `Secure Password Authentication`
| CapabilityFlags::CLIENT_PLUGIN_AUTH
| CapabilityFlags::CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
| CapabilityFlags::CLIENT_CONNECT_WITH_DB)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually support CLIENT_CONNECT_WITH_DB?

ovr pushed a commit to ovr/msql-srv that referenced this pull request Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants