]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Documentation updates for TLS support
authorMenno Smits <menno@freshfoo.com>
Tue, 28 Jul 2015 10:48:01 +0000 (22:48 +1200)
committerMenno Smits <menno@freshfoo.com>
Tue, 28 Jul 2015 10:48:01 +0000 (22:48 +1200)
NEWS.rst
doc/src/index.rst
imapclient/examples/tls_cacert.py [new file with mode: 0644]
imapclient/examples/tls_no_checks.py [new file with mode: 0644]
imapclient/imapclient.py
imapclient/tls.py

index 9830000596889767ec622e22d3866bfc85847dc9..8961337e70cb507c915e9a7f832dfecd54b100e3 100644 (file)
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -2,9 +2,26 @@
  1.0.0
 =======
 
-XXX TLS support
-XXX Removed kwargs support from constructor - replaced by ssl_context
-XXX livetest config file change re DEFAULT
+Enhanced TLS support
+--------------------
+The way that IMAPClient establishes TLS/SSL connections has been
+completed reworked. By default IMAPClient will attempt certificate
+verification, certificate hostname checking, and will not use
+known-insecure TLS settings and protocols. In addition, TLS parameters
+are now highly configurable.
+
+XXX new dependencies
+XXX compatibility: dropped Python 3.2 support
+XXX compatibility: Removed kwargs support from constructor - replaced by ssl_context
+XXX compatibility: default is to do cert checks (pointer to docs on how to disable)
+
+XXX Thank people who contributed
+
+STARTTLS support
+----------------
+XXX
+
+XXX Thank people who contributed (Chris?)
 
 Performance optimsation for parsing message id lists
 ----------------------------------------------------
@@ -15,8 +32,6 @@ Other
 -----
   * Perform quoting of Gmail labels. Thanks to Pawel Sz for the fix.
 
-
-
 ======
  0.13
 ======
index c6e4fc47adf79f5713773b949504484f83765ad4..c4c8178f18a220d5a925e8c1882843e40c9b0af7 100644 (file)
@@ -107,12 +107,12 @@ clients to keep track of data related to a message.
 
 The IMAPClient package has constants for a number of commmonly used flags::
 
-    DELETED = r'\Deleted'
-    SEEN = r'\Seen'
-    ANSWERED = r'\Answered'
-    FLAGGED = r'\Flagged'
-    DRAFT = r'\Draft'
-    RECENT = r'\Recent'         # This flag is read-only
+    DELETED = br'\Deleted'
+    SEEN = br'\Seen'
+    ANSWERED = br'\Answered'
+    FLAGGED = br'\Flagged'
+    DRAFT = br'\Draft'
+    RECENT = br'\Recent'         # This flag is read-only
 
 Any method that accepts message flags takes either a sequence
 containing message flags (eg. ``[DELETED, 'foo', 'Bar']``) or a single
@@ -148,6 +148,44 @@ The IMAP related exceptions that will be raised by this class are:
 These are aliases for the imaplib.IMAP4 exceptions of the same name. Socket
 errors may also be raised in the case of network errors.
 
+TLS/SSL
+~~~~~~~
+IMAPClient uses sensible TLS parameter defaults for encrypted
+connections and also allows for a high level of control of TLS
+parameters if required. To provide a consistent API and capabilities
+across Python versions the `backports.ssl <https://github.com/alekstorm/backports.ssl>`_
+library is used instead of the standard library ssl
+package. backports.ssl provides an API that aims to mimic the Python
+3.4 ssl package so it should be familiar to developers that have used
+the ssl package in recent versions of Python.
+
+TLS parameters are controlled by passing a ``backports.ssl.SSLContext``
+when creating an IMAPClient instance. When ``ssl=True`` is used
+without passing a SSLContext, a default context is used. The default
+context avoids the use of known insecure ciphers and SSL protocol
+versions, with certificate verification and hostname verification
+turned on. The default context will use system installed certificate
+authority trust chains, if available.
+
+:py:func:`IMAPClient.tls.create_default_context` returns IMAPClient's
+default context. When constructing a custom context it is usually best
+to start with the default context and modify it to suit your needs.
+
+The following example shows how to to disable certification
+verification and certificate host name checks if required.
+
+.. literalinclude:: ../../imapclient/examples/tls_no_checks.py
+
+The next example shows how to create a context that will use custom CA
+certificate. This is required to perform verification of a self-signed
+certificate used by the IMAP server.
+
+.. literalinclude:: ../../imapclient/examples/tls_cacert.py
+
+The above examples show some of the most common TLS parameter
+customisations but there are many other tweaks are possible. Consult
+the Python 3 :py:mod:`ssl` package documentation for further options.
+
 API Reference
 -------------
 
@@ -169,6 +207,12 @@ are encountered during parsing.
 .. automodule:: imapclient.response_types
    :members:
 
+TLS Support
+~~~~~~~~~~~
+
+.. automodule:: imapclient.tls
+   :members:
+
 Interactive Sessions
 --------------------
 When developing program using IMAPClient is it sometimes useful to
diff --git a/imapclient/examples/tls_cacert.py b/imapclient/examples/tls_cacert.py
new file mode 100644 (file)
index 0000000..a67a7f5
--- /dev/null
@@ -0,0 +1,16 @@
+# Copyright (c) 2015, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from __future__ import unicode_literals
+import imapclient
+
+HOST = 'imap.host.com'
+USERNAME = 'someuser'
+PASSWORD = 'secret'
+
+context = imapclient.create_default_context(cafile="/path/to/cacert.pem")
+
+server = imapclient.IMAPClient(HOST, use_uid=True, ssl=True, ssl_context=context)
+server.login(USERNAME, PASSWORD)
+# ...
diff --git a/imapclient/examples/tls_no_checks.py b/imapclient/examples/tls_no_checks.py
new file mode 100644 (file)
index 0000000..7c2e5df
--- /dev/null
@@ -0,0 +1,23 @@
+# Copyright (c) 2015, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from __future__ import unicode_literals
+import imapclient
+from backports import ssl
+
+HOST = 'imap.host.com'
+USERNAME = 'someuser'
+PASSWORD = 'secret'
+
+context = imapclient.create_default_context()
+
+# don't check if certificate hostname doesn't match target hostname
+context.check_hostname = False
+
+# don't check if the certificate is trusted by a certificate authority
+context.verify_mode = ssl.CERT_NONE
+
+server = imapclient.IMAPClient(HOST, use_uid=True, ssl=True, ssl_context=context)
+server.login(USERNAME, PASSWORD)
+# ...
index a672ee7b26b95a440ef08f1f00b6c93f8db7310b..c3292972c71f42730b6a69fb72388a0fb4dbb7ae 100644 (file)
@@ -157,8 +157,7 @@ class IMAPClient(object):
 
         The *ssl_context* argument is optional and should be a
         :py:class:`backports.ssl.SSLContext` object. If no SSL context
-        is given, a default SSL context with reasonable default
-        settings will be created.
+        is given, a SSL context with reasonable default settings will be used.
 
         You can enable checking of the hostname in the certificate presented
         by the server  against the hostname which was used for connecting, by
index 49b065b04a421e273391116f18ca313d06215197..18e6da01c41d298232e23eedd48c97d326cc470f 100644 (file)
@@ -3,11 +3,14 @@
 # Please see http://en.wikipedia.org/wiki/BSD_licenses
 
 """
-Good TLS support for IMAPClient built on backports.ssl/pyOpenSSL.
+This module contains IMAPClient's functionality related to Transport
+Layer Security (TLS a.k.a. SSL).
 
-Works with Python 2.6+ and 3.3+.
+It uses ``backports.ssl`` to provide consistent TLS functionality
+across Python versions.
 """
 
+
 __all__ = ('create_default_context',)
 
 
@@ -39,7 +42,23 @@ _RESTRICTED_SERVER_CIPHERS = (
 
 # TODO: get this into backports.ssl
 def create_default_context(cafile=None, capath=None, cadata=None):
-    """Create a SSLContext object with sensible default settings.
+    """Return a backports.ssl.SSLContext object configured with sensible
+    default settings.
+
+    The optional *cafile* argument is path to a file of concatenated
+    CA certificates in PEM format.
+
+    The optional *capath* argument is a path to a directory containing
+    several CA certificates in PEM format, following an OpenSSL
+    specific layout.
+
+    The optional *cadata* argument is either an ASCII string of one or
+    more PEM-encoded certificates or a bytes-like object of
+    DER-encoded certificates.
+
+    If *cafile*, *capath* and *cadata* are all None then
+    system-installed CA certificates will be loaded (if available).
+
     """
     # adapted from Python 3.4's ssl.create_default_context