blob: cb9c72833d1d5da45cc89361e4cbb62d1abf7427 [file] [log] [blame]
Alex Gaynorc72e63f2013-09-09 21:44:26 -07001Contributing
2============
3
4Process
5-------
6
7As an open source project, ``cryptography`` welcomes contributions of all
8forms. These can include:
9
10* Bug reports and feature requests
11* Pull requests for both code and documentation
12* Patch reviews
13
Alex Gaynor2c67c822013-09-09 23:44:13 -070014You can file bugs and submit pull requests on `GitHub`_. To discuss larger
Alex Gaynorc72e63f2013-09-09 21:44:26 -070015changes you can start a conversation on `our mailing list`_.
16
17Because cryptography is so complex, and the implications of getting it wrong so
18devastating, ``cryptography`` has a strict code review policy:
19
20* Patches must *never* be pushed directly to ``master``, all changes (even the
21 most trivial typo fixes!) must be submitted as a pull request.
22* A committer may *never* merge their own pull request, a second party must
Alex Gaynore6466a52013-10-18 14:53:04 -070023 merge their changes. If multiple people work on a pull request, it must be
24 merged by someone who did not work on it.
Alex Gaynorc72e63f2013-09-09 21:44:26 -070025* A patch which breaks tests, or introduces regressions by changing or removing
26 existing tests should not be merged. Tests must always be passing on
27 ``master``.
28* If somehow the tests get into a failing state on ``master`` (such as by a
29 backwards incompatible release of a dependency) no pull requests may be
30 merged until this is rectified.
Alex Gaynorf3f00182013-12-13 19:22:33 -080031* All merged patches must have 100% test coverage.
Alex Gaynorc72e63f2013-09-09 21:44:26 -070032
33The purpose of these policies is to minimize the chances we merge a change
34which jeopardizes our users' security.
35
Alex Gaynor99b69d92013-10-19 17:52:58 -070036If you believe you've identified a security issue in ``cryptography``, please
37follow the directions on the :doc:`security page </security>`.
Alex Gaynorc72e63f2013-09-09 21:44:26 -070038
39Code
40----
41
42When in doubt, refer to `PEP 8`_ for Python code.
43
44Every code file must start with the boilerplate notice of the Apache License.
45Additionally, every Python code file must contain
46
47.. code-block:: python
48
49 from __future__ import absolute_import, division, print_function
50
Alex Gaynore21b0b22013-12-12 12:39:05 -080051API Considerations
52~~~~~~~~~~~~~~~~~~
53
54Most projects' APIs are designed with a philosophy of "make easy things easy,
55and make hard things possible". One of the perils of writing cryptographic code
56is that code that is secure looks just like code that isn't, and produces
57results that are also difficult to distinguish. As a result ``cryptography``
58has, as a design philosophy: "make it hard to do insecure things". Here are a
59few strategies for API design which should be both followed, and should inspire
60other API choices:
61
62If it is incorrect to ignore the result of a method, it should raise an
63exception, and not return a boolean ``True``/``False`` flag. For example, a
64method to verify a signature should raise ``InvalidSignature``, and not return
65whether the signature was valid.
66
67.. code-block:: python
68
69 # This is bad.
70 def verify(sig):
71 # ...
72 return is_valid
73
74 # Good!
75 def verify(sig):
76 # ...
77 if not is_valid:
78 raise InvalidSignature
79
80APIs at the :doc:`/hazmat/primitives/index` layer should always take an
81explicit backend, APIs at the recipes layer should automatically use the
Alex Gaynorf8796b12013-12-13 20:28:55 -080082:func:`~cryptography.hazmat.backends.default_backend`, but optionally allow
Alex Gaynor2a5b4a82013-12-12 17:53:08 -080083specifying a different backend.
Alex Gaynore21b0b22013-12-12 12:39:05 -080084
Alex Gaynore6466a52013-10-18 14:53:04 -070085C bindings
Alex Gaynor5246e2d2013-12-12 12:23:18 -080086~~~~~~~~~~
Alex Gaynore6466a52013-10-18 14:53:04 -070087
88When binding C code with ``cffi`` we have our own style guide, it's pretty
89simple.
90
91Don't name parameters:
92
93.. code-block:: c
94
95 // Good
96 long f(long);
97 // Bad
98 long f(long x);
99
Paul Kehrer3ed80ba2013-10-19 20:00:26 -0500100...unless they're inside a struct:
101
102.. code-block:: c
103
104 struct my_struct {
105 char *name;
106 int number;
107 ...;
108 };
109
Alex Gaynore6466a52013-10-18 14:53:04 -0700110Don't include stray ``void`` parameters:
111
112.. code-block:: c
113
114 // Good
115 long f();
116 // Bad
117 long f(void);
118
119Wrap lines at 80 characters like so:
120
121.. code-block:: c
122
123 // Pretend this went to 80 characters
124 long f(long, long,
125 int *)
126
Alex Gaynor1e8744a2013-10-18 14:57:18 -0700127Include a space after commas between parameters:
128
129.. code-block:: c
130
131 // Good
132 long f(int, char *)
133 // Bad
134 long f(int,char *)
135
Alex Gaynore6466a52013-10-18 14:53:04 -0700136
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700137Documentation
138-------------
139
140All features should be documented with prose.
141
142Docstrings should be written like this:
143
144.. code-block:: python
145
146 def some_function(some_arg):
147 """
148 Does some things.
149
150 :param some_arg: Some argument.
151 """
152
153So, specifically:
154
Alex Gaynor05a190e2013-10-29 17:11:25 -0700155* Always use three double quotes.
156* Put the three double quotes on their own line.
157* No blank line at the end.
158* Use Sphinx parameter/attribute documentation `syntax`_.
159
Alex Gaynora6596882013-11-13 12:54:03 -0800160Because of the inherent challenges in implementing correct cryptographic
Alex Gaynore9d64d72013-11-13 10:28:01 -0800161systems, we want to make our documentation point people in the right directions
162as much as possible. To that end:
163
164* When documenting a generic interface, use a strong algorithm in examples.
165 (e.g. when showing a hashing example, don't use
166 :class:`cryptography.hazmat.primitives.hashes.MD5`)
Alex Gaynor5cbab0c2013-11-13 11:55:57 -0800167* When giving prescriptive advice, always provide references and supporting
Alex Gaynore9d64d72013-11-13 10:28:01 -0800168 material.
Alex Gaynord118c912013-11-13 11:56:49 -0800169* When there is real disagreement between cryptographic experts, represent both
Alex Gaynor54e04002013-11-15 16:44:41 -0800170 sides of the argument and describe the trade-offs clearly.
Alex Gaynore9d64d72013-11-13 10:28:01 -0800171
Alex Gaynor05a190e2013-10-29 17:11:25 -0700172When documenting a new module in the ``hazmat`` package, its documentation
173should begin with the "Hazardous Materials" warning:
174
175.. code-block:: rest
176
177 .. hazmat::
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700178
Alex Gaynor953ebf82013-12-08 10:28:30 -0800179When referring to a hypothetical individual (such as "a person receiving an
180encrypted message") use gender neutral pronouns (they/them/their).
181
Richard Wall40cde822013-10-01 20:20:15 +0100182Development Environment
183-----------------------
Richard Wall0d9bb142013-10-01 16:17:24 +0100184
185Working on ``cryptography`` requires the installation of a small number of
Alex Gaynor166cbd32013-10-01 13:30:29 -0700186development dependencies. These are listed in ``dev-requirements.txt`` and they
187can be installed in a `virtualenv`_ using `pip`_. Once you've installed the
188dependencies, install ``cryptography`` in ``editable`` mode. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100189
Alex Gaynorae5c9072013-10-06 11:04:08 -0700190.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100191
Alex Gaynor7587ded2013-10-06 12:14:05 -0700192 $ # Create a virtualenv and activate it
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100193 $ pip install --requirement dev-requirements.txt
194 $ pip install --editable .
Richard Wall0d9bb142013-10-01 16:17:24 +0100195
196You are now ready to run the tests and build the documentation.
Richard Wall0d9bb142013-10-01 16:17:24 +0100197
Richard Wall40cde822013-10-01 20:20:15 +0100198Running Tests
Alex Gaynor5246e2d2013-12-12 12:23:18 -0800199~~~~~~~~~~~~~
Richard Wall0d9bb142013-10-01 16:17:24 +0100200
Alex Gaynor166cbd32013-10-01 13:30:29 -0700201``cryptography`` unit tests are found in the ``tests/`` directory and are
202designed to be run using `pytest`_. `pytest`_ will discover the tests
203automatically, so all you have to do is:
Richard Wall0d9bb142013-10-01 16:17:24 +0100204
Alex Gaynorae5c9072013-10-06 11:04:08 -0700205.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100206
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100207 $ py.test
Richard Wall0d9bb142013-10-01 16:17:24 +0100208 ...
209 4294 passed in 15.24 seconds
210
211This runs the tests with the default Python interpreter.
212
213You can also verify that the tests pass on other supported Python interpreters.
Richard Wallc3d1eb52013-10-01 16:29:07 +0100214For this we use `tox`_, which will automatically create a `virtualenv`_ for
Richard Wall40cde822013-10-01 20:20:15 +0100215each supported Python version and run the tests. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100216
Alex Gaynorae5c9072013-10-06 11:04:08 -0700217.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100218
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100219 $ tox
Richard Wall0d9bb142013-10-01 16:17:24 +0100220 ...
Richard Wall40cde822013-10-01 20:20:15 +0100221 ERROR: py26: InterpreterNotFound: python2.6
222 py27: commands succeeded
223 ERROR: pypy: InterpreterNotFound: pypy
224 ERROR: py32: InterpreterNotFound: python3.2
225 py33: commands succeeded
226 docs: commands succeeded
227 pep8: commands succeeded
Richard Wall0d9bb142013-10-01 16:17:24 +0100228
Alex Gaynor166cbd32013-10-01 13:30:29 -0700229You may not have all the required Python versions installed, in which case you
230will see one or more ``InterpreterNotFound`` errors.
Richard Wall0d9bb142013-10-01 16:17:24 +0100231
232Building Documentation
Alex Gaynor5246e2d2013-12-12 12:23:18 -0800233~~~~~~~~~~~~~~~~~~~~~~
Richard Wall0d9bb142013-10-01 16:17:24 +0100234
Alex Gaynor166cbd32013-10-01 13:30:29 -0700235``cryptography`` documentation is stored in the ``docs/`` directory. It is
236written in `reStructured Text`_ and rendered using `Sphinx`_.
Richard Wall0d9bb142013-10-01 16:17:24 +0100237
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100238Use `tox`_ to build the documentation. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100239
Alex Gaynorae5c9072013-10-06 11:04:08 -0700240.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100241
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100242 $ tox -e docs
Richard Wall0d9bb142013-10-01 16:17:24 +0100243 ...
Richard Wallc3d1eb52013-10-01 16:29:07 +0100244 docs: commands succeeded
Richard Wall0d9bb142013-10-01 16:17:24 +0100245 congratulations :)
246
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100247The HTML documentation index can now be found at ``docs/_build/html/index.html``
Richard Wall40cde822013-10-01 20:20:15 +0100248
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700249
Donald Stufft85707942013-10-04 23:55:27 -0400250.. _`GitHub`: https://github.com/pyca/cryptography
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700251.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
252.. _`PEP 8`: http://www.peps.io/8/
253.. _`syntax`: http://sphinx-doc.org/domains.html#info-field-lists
Richard Wallc3d1eb52013-10-01 16:29:07 +0100254.. _`pytest`: https://pypi.python.org/pypi/pytest
255.. _`tox`: https://pypi.python.org/pypi/tox
256.. _`virtualenv`: https://pypi.python.org/pypi/virtualenv
257.. _`pip`: https://pypi.python.org/pypi/pip
258.. _`sphinx`: https://pypi.python.org/pypi/sphinx
Alex Gaynora05358d2013-11-06 11:01:22 -0800259.. _`reStructured Text`: http://sphinx-doc.org/rest.html