| 1 |
.. _connecting: |
| 2 |
|
| 3 |
Connecting |
| 4 |
========== |
| 5 |
|
| 6 |
The first thing you need to do is to connect with PuppetDB. |
| 7 |
|
| 8 |
From the same host as PuppetDB |
| 9 |
------------------------------ |
| 10 |
|
| 11 |
Assuming that you are running the code on the same host as the PuppetDB |
| 12 |
(configured with the default ports etc.) you can do just this: |
| 13 |
|
| 14 |
.. code-block:: python |
| 15 |
|
| 16 |
>>> from pypuppetdb import connect |
| 17 |
>>> db = connect() |
| 18 |
|
| 19 |
Once you're all done with requests to PuppetDB, you can explicitly close all |
| 20 |
HTTP connections. This can be useful if you made those connections through a |
| 21 |
tunnel, which might be tracking open connections: |
| 22 |
|
| 23 |
.. code-block:: python |
| 24 |
|
| 25 |
>>> db.disconnect() |
| 26 |
|
| 27 |
You can also use the `with` statement to enclose the work done on PuppetDB. |
| 28 |
This will ensure that all HTTP connections are closed explicitly when we're |
| 29 |
done: |
| 30 |
|
| 31 |
.. code-block:: python |
| 32 |
|
| 33 |
>>> with connect() as db: |
| 34 |
>>> # .. |
| 35 |
|
| 36 |
From a different host than PuppetDB |
| 37 |
----------------------------------- |
| 38 |
|
| 39 |
SSL / TLS |
| 40 |
^^^^^^^^^ |
| 41 |
|
| 42 |
If PuppetDB and the tool that's using pypuppetdb aren't located on the same |
| 43 |
machine you will have to connect to PuppetDB securely using client certificates |
| 44 |
according to PuppetDB's default configuration. |
| 45 |
|
| 46 |
(You could also tell PuppetDB to accept plain connections from anywhere instead |
| 47 |
of just the local machine but **you should not do that**.) |
| 48 |
|
| 49 |
Pypuppetdb can handle this easily for you. It requires two things: |
| 50 |
|
| 51 |
* Generate with your Puppet CA a key pair that you want to use |
| 52 |
* Tell pypuppetdb to use this keypair. |
| 53 |
|
| 54 |
Generate keypair |
| 55 |
"""""""""""""""" |
| 56 |
|
| 57 |
On your Puppet Server or dedicated Puppet CA Server: |
| 58 |
|
| 59 |
.. code-block:: console |
| 60 |
|
| 61 |
$ puppet cert generate <service_name> |
| 62 |
|
| 63 |
Once that's done you'll need to get the public and private keyfile |
| 64 |
and your Puppet CA root certificate. You can find those in Puppet's |
| 65 |
``$ssldir``, usually ``/var/lib/puppet/ssl``: |
| 66 |
|
| 67 |
* private key: ``$ssldir/private_keys/<service_name>.pem`` |
| 68 |
* public key: ``$ssldir/ca/signed/<service_name>.pem`` |
| 69 |
* CA root certificate: ``$ssldir/ca/ca_crt.pem`` |
| 70 |
|
| 71 |
Configure pypuppetdb for SSL |
| 72 |
"""""""""""""""""""""""""""" |
| 73 |
|
| 74 |
Once you have those you can pass them to pypuppetdb's ``connect()``: |
| 75 |
|
| 76 |
.. code-block:: python |
| 77 |
|
| 78 |
>>> db = connect(ssl_key='/path/to/private.pem', ssl_cert='/path/to/public.pem', |
| 79 |
>>> ssl_verify='/path/to/ca_crt.pem', host='puppetdb.example.com', |
| 80 |
>>> port=8081) |
| 81 |
|
| 82 |
This will make pypuppedb use HTTPS with a mutual TLS (client will verify |
| 83 |
if the server is using a certificate issued by our Puppet CA, server will |
| 84 |
verify the same for the client's certificate). |
| 85 |
|
| 86 |
If for whatever reason want to disable the verification of PuppetDB's |
| 87 |
certificate you can pass in ``ssl_verify=False``. |
| 88 |
|
| 89 |
|
| 90 |
RBAC Token Authentication |
| 91 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 92 |
|
| 93 |
If you are using Puppet Enterprise Puppetdb >= 4.0.2 - an RBAC token can be passed |
| 94 |
to pypuppetdb's ``connect()``: |
| 95 |
|
| 96 |
.. code-block:: python |
| 97 |
|
| 98 |
>>> db = connect(token='tokenstring', host='puppetdb.example.com', |
| 99 |
>>> port=8081) |
| 100 |
|
| 101 |
If this argument is passed, pypuppetdb will automatically switch over to using HTTPS. |
| 102 |
This is handled via the addition of ``X-Authentication`` to the session headers. |
| 103 |
|
| 104 |
If you need to disable validation of the certificate PuppetDB is serving, please follow the |
| 105 |
steps documented in the ``Configure pypuppetdb for SSL`` section. |
| 106 |
|
| 107 |
It should also be noted that when using RBAC token authentication, |
| 108 |
the ``ssl_key`` and ``ssl_cert`` options should not be used and are not required. |
| 109 |
|
| 110 |
|
| 111 |
Basic authentication |
| 112 |
^^^^^^^^^^^^^^^^^^^^ |
| 113 |
|
| 114 |
Instead of using Puppet's certificates for mutual TLS, you may want to use plain HTTPS |
| 115 |
with HTTP Basic Authentication. To do that you have to set up a reverse proxy in front |
| 116 |
of the PuppetDB, that will do the SSL termination and the Basic Auth. |
| 117 |
|
| 118 |
Then you can connect pypuppetdb to it with this code: |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
|
| 122 |
>>> db = connect(protocol='https', host='puppetdb.example.com', |
| 123 |
>>> port=443, username='foo', password='bar') |
| 124 |
|
| 125 |
If your proxy serves PuppetDB under a different path than ``/``, then you can also |
| 126 |
provide the ``url_path`` parameter. |