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

ssl support #156

Closed
wants to merge 10 commits into from
Closed

ssl support #156

wants to merge 10 commits into from

Conversation

4tochka
Copy link

@4tochka 4tochka commented Mar 9, 2017

Added ssl support for connection

using external parameter ssl context to wrap connection
steps:

  1. wait once mysql handshake started using no ssl connection
  2. set ssl upgrade bit and send reply to server
  3. detach protocol and transport from socket
  4. start new protocol and transport using ssl context
  5. do ssl handshake

@codecov-io
Copy link

codecov-io commented Mar 9, 2017

Codecov Report

Merging #156 into master will decrease coverage by 0.98%.
The diff coverage is 20%.

@@            Coverage Diff             @@
##           master     #156      +/-   ##
==========================================
- Coverage   92.01%   91.03%   -0.99%     
==========================================
  Files          12       12              
  Lines        1804     1829      +25     
  Branches      254      258       +4     
==========================================
+ Hits         1660     1665       +5     
- Misses         89      107      +18     
- Partials       55       57       +2
Impacted Files Coverage Δ
aiomysql/connection.py 85.82% <20%> (-2.58%)

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 4eb7e66...0093aee. Read the comment docs.

@jettify
Copy link
Member

jettify commented Mar 9, 2017

Contribution in this area very appreciated! We need to figure out how to test SSL on CI, do we need specially configured MySQL?

self.write_packet(packet)
# upgrade connection to ssl
# close recent reader and write and keep socket connected
sock = self._writer._transport._sock
Copy link
Member

Choose a reason for hiding this comment

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

sock = self._writer.transport.get_extra_info('socket', default=None) is beter, since _transport._sock private attributes

self._writer._transport._call_connection_lost = \
types.MethodType(
_call_connection_lost, self._writer._transport)
self._writer._transport._force_close(None)
Copy link
Member

Choose a reason for hiding this comment

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

can we just self._writer._transport.close() ?

Copy link
Author

Choose a reason for hiding this comment

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

If we just close transport asyncio close socket

Copy link
Member

Choose a reason for hiding this comment

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

go it, but not sure we need wot close transport...

Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to pass ssl context in https://github.com/aio-libs/aiomysql/pull/156/files#diff-b4987acea49665e9f985c0da9f5604f4R487 without upgrading socket ourself and let asyncio to handle this?

Copy link
Member

Choose a reason for hiding this comment

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

it may be less robust but code simpler

# close recent reader and write and keep socket connected
sock = self._writer._transport._sock
# patch asyncion
self._writer._transport._call_connection_lost = \
Copy link
Member

Choose a reason for hiding this comment

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

could you clarify why we need this line? _call_connection_los is private... and may not work for other loop implementations.

Copy link
Author

Choose a reason for hiding this comment

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

I do this to replace asyncio method behavior, to not close socket after close transport

@4tochka
Copy link
Author

4tochka commented Mar 9, 2017

example of connection

    cert = '/Users/bitaps/.mysql/merchant/client-cert.pem'
    key = '/Users/bitaps/.mysql/merchant/client-key.pem'
    ca = "/Users/bitaps/.mysql/merchant/ca.pem"


    sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    sslcontext.load_cert_chain(cert, key)
    sslcontext.verify_mode = ssl.CERT_REQUIRED
    sslcontext.check_hostname = False
    sslcontext.load_verify_locations(cafile=ca)
    conn = await aiomysql.connect(db="sys",
                    user="root",
                    password="******",
                    host="18*******204",
                    port=3306, sslcontext = sslcontext)
    cur = await conn.cursor()
    while True:
        await cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
        r = await cur.fetchone()
        print(r)

self._writer._transport._force_close(None)
self._reader, self._writer = yield from \
asyncio.open_connection(sock=sock, ssl=self._sslcontext,
server_hostname=self._host)
Copy link
Member

Choose a reason for hiding this comment

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

loop=self._loop should be passed explicitly.

@terrisgit
Copy link

terrisgit commented Mar 11, 2018

I merged aiomysql 0.0.12 with this PR (resolved the conflicts in connection.py which is here- https://github.com/terrisgit/aiomysql/blob/master/aiomysql/connection.py) and everything is working great. However, #225 also supports AWS db auth tokens which also require SSL. Therefore, I think this PR should be denied unless the SSL implementation is better than #225 's in which case the two should be reconciled.

@jettify
Copy link
Member

jettify commented Apr 19, 2018

see #280

@jettify jettify closed this Apr 19, 2018
@jettify
Copy link
Member

jettify commented Apr 19, 2018

Thanks a lot for contribution, but other PR was finished first.

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.

None yet

5 participants