Debian Codegraph @

ansible_collections/community/hashi_vault/docs/docsite/rst/contributor_guide.rst

raw
1 .. _ansible_collections.community.hashi_vault.docsite.contributor_guide:
2
3 *****************
4 Contributor guide
5 *****************
6
7 This guide aims to help anyone who wishes to contribute to the ``community.hashi_vault`` collection.
8
9 .. note::
10
11 This guide can be improved with your help! Open a `GitHub issue in the repository <https://github.com/ansible-collections/community.hashi_vault/issues>`_ or contribute directly by following the instructions below.
12
13
14 .. contents::
15 :local:
16 :depth: 3
17
18
19 Quick start
20 ===========
21
22 #. Log into your GitHub account.
23 #. Fork the `ansible-collections/community.hashi_vault repository <https://github.com/ansible-collections/community.hashi_vault>`_ by clicking the **Fork** button in the upper right corner. This will create a fork in your own account.
24 #. Clone the repository locally, following :ref:`the example instructions here <hacking_collections>` (but replace ``general`` with ``hashi_vault``). **Pay special attention to the local path structure** of the cloned repository as described in those instructions (for example ``ansible_collections/community/hashi_vault``).
25 #. As mentioned on that page, commit your changes to a branch, push them to your fork, and create a pull request (GitHub will automatically prompt you to do so when you look at your repository).
26 #. :ref:`See the guidance on Changelogs <community_changelogs>` and include a :ref:`changelog fragment <changelogs_how_to>` if appropriate.
27
28 Contributing documentation
29 ==========================
30
31 Additions to the collection documentation are very welcome! We have three primary types of documentation, each with their own syntax and rules.
32
33 README and other markdown files
34 -------------------------------
35
36 Markdown files (those with the extension ``.md``) can be found in several directories within the repository. These files are primarily aimed at developers and those browsing the repository, to explain or give context to the other files nearby.
37
38 The main exception to the above is the ``README.md`` in the repository root. This file is more important because it provides introductory information and links for anyone browsing the repository, both on GitHub and on the collection's `Ansible Galaxy page <https://galaxy.ansible.com/community/hashi_vault>`_.
39
40 Markdown files can be previewed natively in GitHub, so they are easy to validate by reviewers, and there are no specific tests that need to run against them.
41
42 Your IDE or code editor may also be able to preview these files. For example `Visual Studio Code has built-in markdown preview <https://code.visualstudio.com/docs/languages/markdown#_markdown-preview>`_.
43
44 Module and plugin documentation
45 -------------------------------
46
47 This type of documentation gets generated from structured YAML, inside of a Python string. It is included in the same code that it's documenting, or in a separate Python file, such as a doc fragment. Please see the :ref:`module format and documentation guidance <developing_modules_documenting>` for more information.
48
49 This type of documentation is highly structured and tested with ``ansible-test sanity``. Full instructions are available on the :ref:`testing module documentation <testing_module_documentation>` page.
50
51 Additionally, the docsite build on pull requests (or built locally) will include module and plugin documentation as well. See the next section for details.
52
53 Collection docsite
54 ------------------
55
56 The collection docsite is what you are reading now. It is written in reStructuredText (RST) format and published on the :ref:`ansible_documentation` site. This is where we have long-form documentation that doesn't fit into the other two categories.
57
58 If you are considering adding an entirely new document here it may be best to open a GitHub issue first to discuss the idea and how best to organize it.
59
60 Refer to the :ref:`Ansible style guide <style_guide>` for all submissions to the collection docsite.
61
62 RST files for the docsite are in the ``docs/docsite/rst/`` directory. Some submissions may also require edits to ``docs/docsite/extra-docs.yml``.
63
64 When a pull request is submitted which changes the collection's documentation, a new docsite will be generated and published to a temporary site, and a bot will post a comment on the PR with a link. This will let you see the rendered docs to help with spotting formatting errors.
65
66 It's also possible to build the docs locally, by installing some extra Python requirements and running the build script:
67
68 .. code-block:: shell-session
69
70 $ pushd docs/preview
71 $ pip install -r requirements.txt
72 $ ./build.sh
73
74 You can then find the generated HTML in ``docs/preview/build/html`` and can open them locally in your browser.
75
76 Running tests locally
77 =====================
78
79 If you're making anything more than very small or one-time changes, run the tests locally to avoid having to push a commit for each thing, and waiting for the CI to run tests.
80
81 First, :ref:`review the guidance on testing collections <testing_collections>`, as it applies to this collection as well.
82
83 Integration Tests
84 -----------------
85
86 Unlike other collections, we require an `integration_config.yml <https://docs.ansible.com/ansible/latest/dev_guide/testing_integration.html#integration-config-yml>`_ file for properly running integration tests, as the tests require external dependencies (like a Vault server) and they need to know where to find those dependencies.
87
88 If you have contributed to this collection or to the ``hashi_vault`` lookup plugin in the past, you might remember that the integration tests used to download, extract, and run a Vault server during the course of the tests, by default. This *legacy mode* is **no longer available**.
89
90
91 .. _ansible_collections.community.hashi_vault.docsite.contributor_guide.localenv_docker:
92
93 Docker Compose localenv
94 ^^^^^^^^^^^^^^^^^^^^^^^
95
96 The recommended way to run the tests has Vault and other dependencies running in their own containers, set up via docker-compose, and the integration tests run in their own container separately.
97
98 We have a pre-defined "localenv" setup role for this purpose.
99
100 Usage
101 """""
102
103 For ease of typing / length of commands, we'll enter the role directory first:
104
105 .. code-block:: shell-session
106
107 $ pushd tests/integration/targets/setup_localenv_docker
108
109 This localenv has both Ansible collection and Python requirements, so let's get those out of the way:
110
111 .. code-block:: shell-session
112
113 $ pip install -r files/requirements/requirements.txt -c files/requirements/constraints.txt
114 $ ansible-galaxy collection install -r files/requirements/requirements.yml
115
116 To set up your docker-compose environment with all the defaults:
117
118 .. code-block:: shell-session
119
120 $ ./setup.sh
121
122 The setup script does the following:
123
124 #. Template a ``docker-compose.yml`` for the project.
125 #. Generate a private key and self-signed certificate for Vault.
126 #. Template a Vault config file.
127 #. Bring down the existing compose project.
128 #. Bring up the compose project as defined by the vars (specified or defaults).
129 #. Template an ``integration_config.yml`` file that has all the right settings for integration tests to connect.
130 #. Copy the integration config to the correct location *if there isn't already one there* (it won't overwrite, in case you had customized changes).
131
132 With your containers running, you can now run the tests in docker (after returning back to the collection root):
133
134 .. code-block:: shell-session
135
136 $ popd
137 $ ansible-test integration --docker default --docker-network hashi_vault_default -v
138
139 The ``--docker-network`` part is important, because it ensures that the Ansible test container is in the same network as the dependency containers, that way the test container can reach them by their container names. The network name, ``hashi_vault_default`` comes from the default docker-compose project name used by this role (``hashi_vault``). See the :ref:`customization section <ansible_collections.community.hashi_vault.docsite.contributor_guide.localenv_docker_customization>` for more information.
140
141 Running ``setup.sh`` again can be used to re-deploy the containers, or if you prefer you can use the generated ``files/.output/<project_name>/docker-compose.yml`` directly with local tools.
142
143 If running again, remember to manually copy the contents of newly generated ``files/.output/integration_config.yml`` to the integration root, or delete the file in the root before re-running setup so that it copies the file automatically.
144
145 .. _ansible_collections.community.hashi_vault.docsite.contributor_guide.localenv_docker_customization:
146
147 Customization
148 """""""""""""
149
150 ``setup.sh`` passes any additional params you send it to the ``ansible-playbook`` command it calls, so you can customize variables with the standard ``--extra-vars`` (or ``-e``) option. There are many advanced scenarios possible, but a few things you might want to override:
151
152 * ``vault_version`` -- can target any version of Vault for which a docker container exists (this is the container's tag), defaults to ``latest``
153 * ``docker_compose`` (defaults to ``clean`` but could be set to ``up``, ``down``, or ``none``)
154 * ``up`` -- similar to running ``docker-compose up`` (no op if the project is running as it should)
155 * ``down`` -- similar to ``docker-compose down`` (destroys the project)
156 * ``clean`` -- (default) similar to ``docker-compose down`` followed by ``docker-compose up``
157 * ``none`` -- does the other tasks, including templating, but does not bring the project up or down. With this option, the ``community.docker`` collection is not required.
158 * ``vault_crypto_force`` -- by default this is ``false`` so if the cert and key exist they won't be regenerated. Setting to ``true`` will overwrite them.
159 * ``vault_port_http``, ``vault_port_https``, ``proxy_port`` -- all of the ports are exposed to the host, so if you already have any of the default ports in use on your host, you may need to override these.
160 * ``vault_container_name``, ``proxy_container_name`` -- these are the names for their respective containers, which will also be the DNS names used within the container network. In case you have the default names in use you may need to override these.
161 * ``docker_compose_project_name`` -- unlikely to need to be changed, but it affects the name of the docker network which will be needed for your ``ansible-test`` invocation, so it's worth mentioning. For example, if you set this to ``ansible_hashi_vault`` then the docker network name will be ``ansible_hashi_vault_default``.
162
163 .. _ansible_collections.community.hashi_vault.docsite.contributor_guide.contributing_auth_methods:
164
165 Contributing auth methods
166 =========================
167
168 In this collection, auth methods are shared among all plugins and modules rather than being re-implemented in each one. This saves the effort of re-inventing the wheel, prevents test bloat by having to test functionality across auth methods, and provides a consistent experience.
169
170 File location & scope
171 ---------------------
172
173 Auth methods are implemented as classes in ``module_utils``, in a file named ``plugins/module_utils/_auth_method_<method_name>.py``. The leading underscore indicates that the module util is private to the collection and that it is not intended to be used outside the collection; this lets us make changes as needed without needing to release a new major version, and clearly indicates to would-be downstream users that they should not rely on these utils outside content within the collection.
174
175 In addition, all auth method module utils within the collection must contain a comment explaining this, such as:
176
177 .. code-block:: python
178
179 # FOR INTERNAL COLLECTION USE ONLY
180 # The interfaces in this file are meant for use within the community.hashi_vault collection
181 # and may not remain stable to outside uses. Changes may be made in ANY release, even a bugfix release.
182 # See also: https://github.com/ansible/community/issues/539#issuecomment-780839686
183 # Please open an issue if you have questions about this.
184
185 It is best to look at `existing auth methods <https://github.com/ansible-collections/community.hashi_vault/tree/main/plugins/module_utils>`_ to get a feel for how they are implemented.
186
187 Class anatomy
188 -------------
189
190 Each auth method class should be named ``HashiVaultAuthMethod<MethodName>`` and inherit from ``HashiVaultAuthMethodBase``.
191
192 The base class provides some common functionality, like standardizing a way to emit warnings and providing a common function for validating required options.
193
194 An auth method must run the base class's ``__init__`` function.
195
196 It must implement two methods:
197
198 * ``validate()`` -- this method does everything it can to ensure that the requirements are met for performing authentication with this particular auth method. This may include checking for required options, validating the values of those options, pulling in additional information and context from the environment, preparing that information for use by ``authenticate()``, etc. Generally speaking, it should not contact Vault, and should minimize reliance on external sources and services, but that is a guideline and the details will depend on the specifics of the auth method in question. ``validate()`` raises an exception if validation fails. If it succeeds, nothing is returned.
199 * ``authenticate(client, use_token=False)`` -- this method performs the actual authentication, and it returns the API result of the authentication (which will include the token, lease information, etc.). The HVAC client object is passed in, as well an optional parameter ``use_token`` which specifies whether the client should have its token field set to the result of authentication (typically this is desired).
200
201 The auth method class should also contain two fields:
202
203 * ``NAME`` -- the name of the auth method.
204 * ``OPTIONS`` -- a list containing the name of every option that may be used by the auth method, including optional options; this list should not include the ``auth_method`` option.
205
206 Raising exceptions and warnings
207 -------------------------------
208
209 Because auth methods are shared among both Ansible modules and Ansible plugins, any exceptions raised must be applicable to both. Standard Python exceptions like ``KeyError`` can be raised if they appropriate.
210
211 In situations where you would normally raise ``AnsibleError`` (in plugins), or call ``module.fail_json()`` (in modules), you may raise ``HashiVaultValueError`` with your error message. Plugins and modules in this collection should expect this type and act accordingly.
212
213 Similarly for warnings, because plugins and modules implement warnings differently, module util code that needs to warn takes a warning callback, and this is true for auth methods as well.
214
215 The base class provides a ``warn()`` method that handles calling the callback specified at class init, so a simple ``self.warn()`` can be used in auth method code.
216
217 Accessing options
218 -----------------
219
220 Because auth methods are shared among both Ansible modules and Ansible plugins, which do not access options in the same way, this collection implements a class called ``HashiVaultOptionAdapter``. This class provides a standard interface for accessing option values in code that must work in both plugins and modules.
221
222 It implements the following methods:
223
224 * ``get_option(key)`` -- gets the option with the specified name. Raises ``KeyError`` if the option is not present.
225 * ``get_option_default(key, default=None)`` -- gets the option with the specified name. If it's not present, returns the value of ``default``.
226 * ``set_option(key, value)`` -- sets the value of the specified option ``key`` to ``value``.
227 * ``set_option_default(key, default=None)`` -- returns the value of the option ``key``. If the key is not present, sets its value to ``default`` and returns that value.
228 * ``has_option(key)`` -- returns ``True`` if the specified option *is present* (``None`` value counts as present).
229 * ``set_options(**kwargs)`` -- sets options to the key/value pairs specified in ``kwargs``.
230 * ``get_options(*args)`` -- returns a dict of the option names specified in ``args``.
231 * ``get_filtered_options(filter, *args)`` -- returns a dict of the option names specified in ``args``, if the callable ``filter`` (which has ``key`` and ``value`` passed into it) returns ``True`` for the given key/value pair.
232 * ``get_filled_options(*args)`` -- returns a dict of the option names specified in ``*args`` that are not ``None``.
233
234 The authenticator
235 -----------------
236
237 The ``HashiVaultAuthenticator`` class is how most of the content in the collection will handle authentication, rather than having to directly references each individual auth method. As a result, ``_authenticator.py`` needs to be modified for every new auth method, because it imports and directly references each class. See `the implementation of this class <https://github.com/ansible-collections/community.hashi_vault/blob/main/plugins/module_utils/_authenticator.py>`_ to find the places that need to be modified.
238
239 Auth method options and documentation
240 -------------------------------------
241
242 Because auth methods are shared among collection content, their options are documented in doc_fragment plugins. Because many options end up being shared among many auth methods (for example ``role_id``, ``username``, ``password``), we do not have a separate doc fragment for each auth method, as this would end up with duplicated option documentation.
243
244 Instead, all of the options for auth methods are in ``plugins/doc_fragments/auth.py``.
245
246 This contains the standard ``DOCUMENTATION`` field, as well as a ``PLUGINS`` field. The reason for this split is that there are certain parts of the documentation that are only applicable to plugins; namely the ``env``, ``ini``, and ``vars`` entries.
247
248 ``DOCUMENTATION`` should contains all fields common to both, like ``description``, ``type``, ``version_added``, ``required``, etc., while anything plugin-specific goes in ``PLUGINS``.
249
250 Since plugins and modules will reference the doc fragments, it's not usually required to modify the docstrings in the content directly; if it seems necessary, please raise an issue to discuss.
251
252 Wherever possible, we should provide ``env``, ``ini``, and ``vars`` alternatives for specifying options, to give maximum flexibility for plugins. Occasionally, these won't make sense, like providing ``token`` (a sensitive value) in ``ini``.
253
254 When deciding to implement new options for an auth method, consider whether existing options can or should be reused. If a new option is needed, consider scoping its name to the auth method, in order to differentiate it from current or future option names that could be confusing in another context.
255
256 For example ``cert_auth_public_key`` and ``cert_auth_private_key`` were named that way to prevent them being confused with other certificate options that relate to the Vault connection, or other contexts where specific plugins or modules might need key pairs.
257
258 Testing auth methods
259 --------------------
260
261 Because auth methods are shared across the collection, we want them to be very well tested. Auth methods have both unit and integration tests, and the combination of those should give us high confidence that the methods work as intended.
262
263 Unit tests
264 ^^^^^^^^^^
265
266 Unit tests allow us to check some of the functionality that is difficult to test effectively in integration tests, like checking that every possible combination of options behaves as it should, or simulating conditions that we can't easily reproduce. The coverage of various scenarios should be extensive, and the details of which, or how complex they are, will depend on the intricacies of the auth method itself. Looking at existing examples is highly recommended.
267
268 A pytest fixture is provided to load fixtures files that contain sample Vault API responses. Using these allows for mocking of the HVAC authentication calls within the unit tests.
269
270 Integration tests
271 ^^^^^^^^^^^^^^^^^
272
273 Our integration tests provide a running Vault server, and with that we can set up any auth methods we want (in theory). In practice, auth methods often require external services to integrate with. When possible, we should consider setting up such external services so that we can create a meaningful, real life integration and test it.
274
275 Often however, this is not possible, or difficult. We must consider that tests are not only run in CI, but should be able to be run locally as well.
276
277 Mocking integrations
278 """"""""""""""""""""
279
280 We have implemented `MMock (Monster Mock) <https://github.com/jmartin82/mmock>`_ in our integration test setup to help with this. This server is setup to proxy its requests to the test Vault server, but you can write configurations that allow it to return different data for specific requests. By carefully constructing these responses, we can simulate the Vault API's response to login requests for specific auth methods, and also simulate its failures. With that, we can then run integration tests that hopefully provide us some assurance that we are implementing it correctly.
281
282 Testing plugin and module Usage
283 """""""""""""""""""""""""""""""
284
285 Auth methods are usable from modules and plugins, so integration tests for an auth method must test it via both plugins and modules.
286
287 We provide custom modules and plugins specifically for testing auth methods within the integration tests. These are simplified implementations but they use the common code that should be used by all content, and they can be set to return some useful information about the login process. See the existing tests for details.
288
289 Test coverage
290 ^^^^^^^^^^^^^
291
292 In CI, we use CodeCov to track coverage. We also set some specific "tags" in coverage, and one of those is to tag individual auth methods as targets for integration tests. This happens automatically in CI, however new auth methods need an entry into ``codecov.yml`` that maps the coverage flag to the file where the auth method is implemented. For example:
293
294 .. code:: yaml
295
296 flags:
297 target_auth_aws_iam:
298 paths:
299 - plugins/module_utils/_auth_method_aws_iam.py