-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Add support for TLS/SSL mutual authentication #396
Closed
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2941cd0
Add support for TLS/SSL mutual authentication
bb52b92
Fix indentation errors in handlers.py
933257d
Update 'print' statements to use logger
128b61f
Remove print/log statements
c3675b9
Clean up client-certfile support
aa41ce3
Updates per comments
0928806
Flake8 fixes
099630f
Small flake8 fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright (C) 2007-2016 Giampaolo Rodola' <[email protected]>. | ||
# Use of this source code is governed by MIT license that can be | ||
# found in the LICENSE file. | ||
|
||
import contextlib | ||
import ftplib | ||
import os | ||
import socket | ||
import sys | ||
import ssl | ||
|
||
import OpenSSL # requires "pip install pyopenssl" | ||
|
||
from pyftpdlib.handlers import TLS_FTPHandler | ||
from pyftpdlib.test import configure_logging | ||
from pyftpdlib.test import PASSWD | ||
from pyftpdlib.test import remove_test_files | ||
from pyftpdlib.test import ThreadedTestFTPd | ||
from pyftpdlib.test import TIMEOUT | ||
from pyftpdlib.test import TRAVIS | ||
from pyftpdlib.test import unittest | ||
from pyftpdlib.test import USER | ||
from pyftpdlib.test import VERBOSITY | ||
from pyftpdlib.test.test_functional import TestCallbacks | ||
from pyftpdlib.test.test_functional import TestConfigurableOptions | ||
from pyftpdlib.test.test_functional import TestCornerCases | ||
from pyftpdlib.test.test_functional import TestFtpAbort | ||
from pyftpdlib.test.test_functional import TestFtpAuthentication | ||
from pyftpdlib.test.test_functional import TestFtpCmdsSemantic | ||
from pyftpdlib.test.test_functional import TestFtpDummyCmds | ||
from pyftpdlib.test.test_functional import TestFtpFsOperations | ||
from pyftpdlib.test.test_functional import TestFtpListingCmds | ||
from pyftpdlib.test.test_functional import TestFtpRetrieveData | ||
from pyftpdlib.test.test_functional import TestFtpStoreData | ||
from pyftpdlib.test.test_functional import TestIPv4Environment | ||
from pyftpdlib.test.test_functional import TestIPv6Environment | ||
from pyftpdlib.test.test_functional import TestSendfile | ||
from pyftpdlib.test.test_functional import TestTimeouts | ||
from _ssl import SSLError | ||
|
||
|
||
FTPS_SUPPORT = hasattr(ftplib, 'FTP_TLS') | ||
if sys.version_info < (2, 7): | ||
FTPS_UNSUPPORT_REASON = "requires python 2.7+" | ||
else: | ||
FTPS_UNSUPPORT_REASON = "FTPS test skipped" | ||
|
||
CERTFILE = os.path.abspath(os.path.join(os.path.dirname(__file__), | ||
'keycert.pem')) | ||
CLIENT_CERTFILE = os.path.abspath(os.path.join(os.path.dirname(__file__), | ||
'clientcert.pem')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you forgot to add this |
||
|
||
del OpenSSL | ||
|
||
|
||
if FTPS_SUPPORT: | ||
class FTPSClient(ftplib.FTP_TLS): | ||
"""A modified version of ftplib.FTP_TLS class which implicitly | ||
secure the data connection after login(). | ||
""" | ||
|
||
def login(self, *args, **kwargs): | ||
ftplib.FTP_TLS.login(self, *args, **kwargs) | ||
self.prot_p() | ||
|
||
class FTPSServerAuth(ThreadedTestFTPd): | ||
"""A threaded FTPS server that forces client certificate | ||
authentication used for functional testing. | ||
""" | ||
handler = TLS_FTPHandler | ||
handler.certfile = CERTFILE | ||
handler.client_certfile = CLIENT_CERTFILE | ||
|
||
|
||
# ===================================================================== | ||
# dedicated FTPS tests with client authentication | ||
# ===================================================================== | ||
|
||
|
||
@unittest.skipUnless(FTPS_SUPPORT, FTPS_UNSUPPORT_REASON) | ||
class TestFTPS(unittest.TestCase): | ||
"""Specific tests for TLS_FTPHandler class.""" | ||
|
||
def setUp(self): | ||
self.server = FTPSServerAuth() | ||
self.server.start() | ||
|
||
def tearDown(self): | ||
self.client.close() | ||
self.server.stop() | ||
|
||
def assertRaisesWithMsg(self, excClass, msg, callableObj, *args, **kwargs): | ||
try: | ||
callableObj(*args, **kwargs) | ||
except excClass as err: | ||
if str(err) == msg: | ||
return | ||
raise self.failureException("%s != %s" % (str(err), msg)) | ||
else: | ||
if hasattr(excClass, '__name__'): | ||
excName = excClass.__name__ | ||
else: | ||
excName = str(excClass) | ||
raise self.failureException("%s not raised" % excName) | ||
|
||
def test_auth_client_cert(self): | ||
self.client = ftplib.FTP_TLS(timeout=TIMEOUT, certfile=CLIENT_CERTFILE) | ||
self.client.connect(self.server.host, self.server.port) | ||
# secured | ||
try: | ||
self.client.login() | ||
except Exception as e: | ||
self.fail("login with certificate should work") | ||
|
||
def test_auth_client_nocert(self): | ||
self.client = ftplib.FTP_TLS(timeout=TIMEOUT) | ||
self.client.connect(self.server.host, self.server.port) | ||
try: | ||
self.client.login() | ||
except SSLError as e: | ||
# client should not be able to log in | ||
if "SSLV3_ALERT_HANDSHAKE_FAILURE" in e.reason: | ||
pass | ||
else: | ||
self.fail("Incorrect SSL error with missing client certificate") | ||
else: | ||
self.fail("Client able to log in with no certificate") | ||
|
||
def test_auth_client_badcert(self): | ||
self.client = ftplib.FTP_TLS(timeout=TIMEOUT, certfile=CERTFILE) | ||
self.client.connect(self.server.host, self.server.port) | ||
try: | ||
self.client.login() | ||
except Exception as e: | ||
# client should not be able to log in | ||
if "TLSV1_ALERT_UNKNOWN_CA" in e.reason: | ||
pass | ||
else: | ||
self.fail("Incorrect SSL error with bad client certificate") | ||
else: | ||
self.fail("Client able to log in with bad certificate") | ||
|
||
configure_logging() | ||
remove_test_files() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main(verbosity=VERBOSITY) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is called when a client connects, as I suppose, then this should be a
classmethod
.