Changeset 1501:cc42f6ab9d3e


Ignore:
Timestamp:
Apr 22, 2014, 5:02:24 AM (5 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Encrypt data channel by default if a method `prot_p` exists.

I think most users will want to secure the data channel, so rather
be safe than sorry and turn the encryption on by default. If
needed, the data channel encryption can be suppressed by passing
`encrypt_data_channel=False`.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/session.py

    r1493 r1501  
    2323
    2424def session_factory(base_class=ftplib.FTP, port=21, use_passive_mode=None,
    25                     encrypt_data_channel=None, debug_level=None):
     25                    encrypt_data_channel=True, debug_level=None):
    2626    """
    2727    Create and return a session factory according to the keyword
     
    4141    passive mode.
    4242
    43     encrypt_data_channel: If `True`, call the `prot_p` method of the
    44     base class. If `False` or `None` (`None` is the default), don't
    45     call the method.
     43    encrypt_data_channel: If `True` (the default), call the `prot_p`
     44    method of the base class if it has the method. If `False` or
     45    `None` (`None` is the default), don't call the method.
    4646
    4747    debug_level: Debug level (integer) to be set on a session
     
    7777            if use_passive_mode is not None:
    7878                self.set_pasv(use_passive_mode)
    79             if encrypt_data_channel:
     79            if encrypt_data_channel and hasattr(base_class, "prot_p"):
    8080                self.prot_p()
    8181
  • test/test_session.py

    r1494 r1501  
    2020    def __init__(self):
    2121        self.calls = []
    22    
     22
    2323    def add_call(self, *args):
    2424        self.calls.append(args)
     
    2626    def connect(self, host, port):
    2727        self.add_call("connect", host, port)
    28 
    29     def auth_tls(self):
    30         self.add_call("auth_tls")
    3128
    3229    def _fix_socket(self):
     
    4138    def set_pasv(self, flag):
    4239        self.add_call("set_pasv", flag)
     40
     41
     42class EncryptedMockSession(MockSession):
     43
     44    def auth_tls(self):
     45        self.add_call("auth_tls")
    4346
    4447    def prot_p(self):
     
    9093    def test_encrypt_data_channel(self):
    9194        """Test request to call `prot_p` with `encrypt_data_channel`."""
    92         factory = ftputil.session.session_factory(base_class=MockSession,
    93                                                   encrypt_data_channel=True)
     95        # With encrypted data channel (default for encrypted session).
     96        factory = ftputil.session.session_factory(
     97                    base_class=EncryptedMockSession)
    9498        session = factory("host", "user", "password")
    9599        self.assertEqual(session.calls, [("connect", "host", 21),
    96100                                         ("login", "user", "password"),
    97101                                         ("prot_p",)])
     102        #
     103        factory = ftputil.session.session_factory(
     104                    base_class=EncryptedMockSession, encrypt_data_channel=True)
     105        session = factory("host", "user", "password")
     106        self.assertEqual(session.calls, [("connect", "host", 21),
     107                                         ("login", "user", "password"),
     108                                         ("prot_p",)])
     109        # Without encrypted data channel.
     110        factory = ftputil.session.session_factory(
     111                    base_class=EncryptedMockSession, encrypt_data_channel=False)
     112        session = factory("host", "user", "password")
     113        self.assertEqual(session.calls, [("connect", "host", 21),
     114                                         ("login", "user", "password")])
    98115
    99116    def test_debug_level(self):
     
    108125    def test_m2crypto_session(self):
    109126        """Test call sequence for M2Crypto session."""
    110         factory = ftputil.session.session_factory(base_class=MockSession)
     127        factory = \
     128          ftputil.session.session_factory(base_class=EncryptedMockSession)
    111129        # Return `True` to fake that this is a session deriving from
    112130        # `M2Crypto.ftpslib.FTP_TLS`.
     
    120138                                         ("auth_tls",),
    121139                                         ("_fix_socket",),
    122                                          ("login", "user", "password")])
     140                                         ("login", "user", "password"),
     141                                         ("prot_p",)])
    123142
    124143
Note: See TracChangeset for help on using the changeset viewer.