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

Issue 786 mysql ssl support #2491

Merged
merged 7 commits into from
Feb 1, 2022

Conversation

p-alik
Copy link
Contributor

@p-alik p-alik commented Aug 28, 2020

This PR aims to add ssl connection support for MySQL backend, which is requested in the issue #786 .
At this stage it allow setting of mysql option MYSQL_OPT_SSL_MODE only.
The connection could be established with MYSQL_OPT_SSL_MODE set to SSL_MODE_DISABLED, SSL_MODE_PREFERRED and SSL_MODE_REQUIRED.

Tested with
DATABASE_URL='mysql://***?ssl_mode=(disabled|required|preferred)' cargo test --manifest-path diesel/Cargo.toml --features mysql --no-default-features mysql

https://dev.mysql.com/doc/c-api/8.0/en/mysql-options.html
https://dev.mysql.com/doc/c-api/8.0/en/c-api-encrypted-connections.html

@p-alik p-alik force-pushed the issue-786-mysql-ssl-support branch from b3b63c4 to 58882b3 Compare August 29, 2020 06:46
@weiznich weiznich requested a review from a team August 29, 2020 07:17
Copy link

@ELD ELD left a comment

Choose a reason for hiding this comment

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

This looks good to me, though the Windows and Rust 1.40.0 tests are failing. It’s looking like the mysqlclient_sys library doesn’t export the SSL mode variants on Windows, which is perplexing, but I’m not positive that’s the root cause. I haven’t had a chance to dig more into it.

I’m not comfortable giving approval on this PR until we can figure out why Windows isn’t building, but it does look good as-is right now!

@p-alik
Copy link
Contributor Author

p-alik commented Aug 29, 2020

I've to improve this PR for sake of Windows and I suppose to do that next week.

@p-alik
Copy link
Contributor Author

p-alik commented Aug 30, 2020

Due to sgrif/mysqlclient-sys#27 there is no way to set ssl-mode for MySQL connection on Windows in same way as on Linux/Unix yet.

Would it be appropriate to restrict setting of MySQL option MYSQL_OPT_SSL_MODE for #[cfg(not(windows))]? Any other suggestions?

@ELD
Copy link

ELD commented Aug 30, 2020

That would probably be a suitable alternative for now, since the corresponding Windows bindings don’t work right now.

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

I've left some comments, otherwise this is looking good 👍

(Sorry for taking some time to do a review here, it took some time to catch up with everything after holiday.)

diesel/Cargo.toml Outdated Show resolved Hide resolved
diesel/src/mysql/connection/raw.rs Outdated Show resolved Hide resolved
diesel/src/mysql/connection/raw.rs Outdated Show resolved Hide resolved
@weiznich weiznich modified the milestone: 2.0 Jan 26, 2021
@biot023
Copy link

biot023 commented May 25, 2021

Hi -- I guess nothing happened with this?
Are there any workarounds for SSL-secured access?
I'm going to talk to our DBA service about maybe SSH tunnelling but I'm not confident that will work out...

@weiznich
Copy link
Member

@biot023 Commenting on old issues/PR's just to ask for updates is something that is highly discouraged. Please stop doing that if you have nothing meaningful (like how to resolve the issues outlined above) to contribute.

@biot023
Copy link

biot023 commented May 26, 2021

@biot023 Commenting on old issues/PR's just to ask for updates is something that is highly discouraged. Please stop doing that if you have nothing meaningful (like how to resolve the issues outlined above) to contribute.

It was a genuine question. Never mind, consider me suitably discouraged.

@biot023
Copy link

biot023 commented May 26, 2021

Maybe not entirely discouraged.
I've been able to set up ProxySQL (https://proxysql.com) in a docker instance to handle the SSL bit of the connection.
I can just point any diesel services at that image.
Hope that's of some help to somebody else struggling with this.

@Neo-Zhixing
Copy link

Maybe as a stopgap measure we should fork libmysqlclient-sys into the diesel org for now and merge all pending PRs there? There are no new commits in that repo since 2019 and @sgrif has been inactive since July 2021. MySQL SSL support is a real use case, and doing that would unblock us on multiple issues.

@weiznich
Copy link
Member

@Neo-Zhixing There is no need to fork mysqlclient-sys. I have all merge and publish rights for that crate. Unfortunately I do not have the time to do a lot active work there while maintaining diesel at the same time, so this is basically waiting for someone laying out a basic plan about the future of mysqlclient-sys and someone who implements it.

@thomasmost
Copy link
Contributor

I've been able to set up ProxySQL (https://proxysql.com) in a docker instance to handle the SSL bit of the connection.
I can just point any diesel services at that image.
Hope that's of some help to somebody else struggling with this.

@biot023 this is I think exactly what I'll need to do—you don't happen to have an example Dockerfile/diesel config that you'd feel comfortable sharing do you (scrubbed of anything sensitive, obviously)?

@biot023
Copy link

biot023 commented Jan 24, 2022

@biot023 this is I think exactly what I'll need to do—you don't happen to have an example Dockerfile/diesel config that you'd feel comfortable sharing do you (scrubbed of anything sensitive, obviously)?

Hi -- I really struggled with this. I got it working, as I recall, but then had an issue with tons of connections being established to the main server and never dropped -- took the whole site down in about 4 minutes, when I tested it!
It's probable that you could get it working but in our case I went past the point of sunken cost fallacy and we abandoned diesel for that particular project.
That's not throwing shade on diesel, btw -- I still use it whenever I can, it just didn't work out in this scenario for us.
Sorry I'm not more help, man.
Cheers,
Doug.

@weiznich
Copy link
Member

@biot023 and @thomasmost Just to reiterate what I've written above: I would be more than happy to merge support for mysql ssl connections to diesel, but that requires work from someone. I'm happy to provide some pointers + discuss design, but implementation needs to be done by someone else.

@thomasmost
Copy link
Contributor

@weiznich Let's do it! What is your schedule like/what forum would you prefer for such a discussion?

@weiznich
Copy link
Member

@thomasmost I had a quick look at open questions here: The interface proposed in this PR looks good to me. There are the following open points from my point of view:

  • Windows support
    • This is the largest open point
    • Requires changes to mysqlclient-sys, which may require restructuring the code there. That's the point that is quite fuzzy for me now.
  • Rebasing the PR to the current master branch
  • Tests?

@thomasmost
Copy link
Contributor

Got it! I will see if I can get in there and work this out.

@p-alik
Copy link
Contributor Author

p-alik commented Jan 26, 2022

Will rebase the feature branch.

@p-alik p-alik force-pushed the issue-786-mysql-ssl-support branch from 1ccf832 to 04c4dc4 Compare January 26, 2022 13:43
@thomasmost
Copy link
Contributor

thomasmost commented Jan 26, 2022

@p-alik how can I help get these tests passing?

@p-alik p-alik force-pushed the issue-786-mysql-ssl-support branch 2 times, most recently from 1fce5b8 to 04c4dc4 Compare January 27, 2022 12:35
@thomasmost
Copy link
Contributor

@weiznich now this is lookin pretty good

@weiznich
Copy link
Member

weiznich commented Jan 28, 2022

@thomasmost Yes the diesel side of this change looks good, beside the fact that this only supports linux/macos. I would like to solve the windows issue first before finally merging this. As already mentioned that would require updating the bindings in mysqlclient-sys, which requires access to bindgen on windows. That's unfortunately something I cannot help with other than merging PR's and cutting releases.
I image something like following here:

  • Update the generated bindings in mysqlclient-sys via a PR there (that should likely update bindings for both variants)
  • Update this PR to use the mysqlclient-sys version based on the previous PR to check everything works fine
  • I will merge the mysqlclient-sys PR + cut a release there
  • Update this PR again to use the now newly released mysqlclient-sys version
  • Merge this PR

@p-alik p-alik force-pushed the issue-786-mysql-ssl-support branch 3 times, most recently from d88faf1 to 5a4671f Compare January 29, 2022 07:18
p-alik and others added 5 commits January 29, 2022 08:23
ssl_mode is defined as Option<mysqlclient_sys::mysql_ssl_mode>
Windows binding of mysqlclient-sys doesn't provide values
for the option MYSQL_OPT_SSL_MODE
See: sgrif/mysqlclient-sys#27
0.2.0 do not provide enum mysql_ssl_mode.
The enum was introduces in
sgrif/mysqlclient-sys@6575414
* update diesel/Cargo.toml
* remove obsolete  windows checks

Co-authored-by: Thomas Constantine Moore <tomismore@gmail.com>
@p-alik p-alik force-pushed the issue-786-mysql-ssl-support branch from 5a4671f to 1a66269 Compare January 29, 2022 07:23
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

Looks good for me if the following things are added:

  • A changelog entry here + adjusting the minimal supported mysqlclient-sys version under "REMOVED"
  • A note in the documentation of MysqlConnection::establish on the available options.

* add `ssl_mode` support to `MysqlConnection::establish`
* remove support for `mysqlclient-sys` < `0.2.5`
@p-alik
Copy link
Contributor Author

p-alik commented Jan 30, 2022

Added requested notes to the changelog.

Copy link
Contributor

@thomasmost thomasmost left a comment

Choose a reason for hiding this comment

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

LGTM!

@thomasmost
Copy link
Contributor

thomasmost commented Jan 31, 2022

@weiznich one last follow-up; just so I understand, are we talking days, weeks, or months before this gets included in a release of the crate?

Until that time, any reason I can't get my app working with an (admittedly, unstable) build using the master branch as its dependency, i.e.

[dependencies]
diesel = { git = "https://github.com/diesel-rs/diesel", features = ["mysql", "extras", "chrono"] }

?

@weiznich
Copy link
Member

weiznich commented Feb 1, 2022

one last follow-up; just so I understand, are we talking days, weeks, or months before this gets included in a release of the crate?

We generally do not give any estimates when a certain release happens other than: When it's done. Additionally as this PR is not merged yet (due to missing documentation) it's not certain if the next release will contain this feature or not.

Until that time, any reason I can't get my app working with an (admittedly, unstable) build using the master branch as its dependency, i.e.

Please open an discussion thread about that with more details about what you understand under "can't get working". A PR is definitively not the right place to discuss such issues.

@p-alik
Copy link
Contributor Author

p-alik commented Feb 1, 2022

as this PR is not merged yet (due to missing documentation) it's not certain if the next release will contain this feature or not.

9dc434e aims to add missed documentation.

@p-alik p-alik force-pushed the issue-786-mysql-ssl-support branch from 24b612d to 3ce9b48 Compare February 1, 2022 13:36
database URL may contain GET parameters
* `unix_socket`
* `ssl_mode`
@p-alik p-alik force-pushed the issue-786-mysql-ssl-support branch from 3ce9b48 to 9dc434e Compare February 1, 2022 13:37
@weiznich weiznich merged commit 41404e6 into diesel-rs:master Feb 1, 2022
@thomasmost
Copy link
Contributor

thomasmost commented Feb 1, 2022

Until that time, any reason I can't get my app working with an (admittedly, unstable) build using the master branch as its dependency, i.e.

Please open an discussion thread about that with more details about what you understand under "can't get working". A PR is definitively not the right place to discuss such issues.

I think you misunderstood me @weiznich — I wasn't asking for support — nvm

Thanks again for your work on this crate

@p-alik p-alik deleted the issue-786-mysql-ssl-support branch February 1, 2022 19:05
@yerke
Copy link

yerke commented Feb 1, 2022

@p-alik @weiznich Thank you very much for working on this!

@ToniIltanen

This comment was marked as off-topic.

@weiznich

This comment was marked as off-topic.

@ToniIltanen

This comment was marked as off-topic.

@weiznich

This comment was marked as off-topic.

@libsilverwolf
Copy link

libsilverwolf commented Jun 14, 2024

Is this feature also usable with diesel-cli ?

I tried

DATABASE_URL="mysql://***:3306/port?ssl_mode=required" diesel print-schema

but got

Could not connect to database via `mysql://***:3306/port?ssl_mode=required`: Connections using insecure transport are prohibited while --require_secure_transport=ON.

I'm using MariaDB.

$ diesel --version
diesel 
 Version: 2.2.1
 Supported Backends: mysql

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.

9 participants