Skip to content

Commit 2985724

Browse files
authored
Merge pull request #534 from rokernel/custom_certificate_support
Added custom certificate support
2 parents fb9b06c + c959009 commit 2985724

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
- Allows connection when SSL certificates are not valid. Set to C(false) when certificates are not trusted.
3636
default: True
3737
type: boolean
38+
cert:
39+
description:
40+
- Certificate path
41+
default: False
42+
key:
43+
description:
44+
- Certificate key path
45+
default: False
46+
ca_path:
47+
description:
48+
- CA path
49+
default: False
3850
follow_redirects:
3951
description:
4052
- Determine how redirects are followed.
@@ -299,6 +311,9 @@ def _fetch_information(self, url):
299311
timeout=self.timeout,
300312
validate_certs=self.validate_certs,
301313
follow_redirects=self.follow_redirects,
314+
client_cert=self.cert,
315+
client_key=self.key,
316+
ca_path=self.ca_path,
302317
)
303318
except urllib_error.HTTPError as e:
304319
"""This will return the response body when we encounter an error.
@@ -1623,6 +1638,9 @@ def parse(self, inventory, loader, path, cache=True):
16231638
% (ansible_version, python_version.split(" ")[0]),
16241639
"Content-type": "application/json",
16251640
}
1641+
self.cert = self.get_option("cert")
1642+
self.key = self.get_option("key")
1643+
self.ca_path = self.get_option("ca_path")
16261644
if token:
16271645
self.headers.update({"Authorization": "Token %s" % token})
16281646

plugins/module_utils/netbox_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@
463463
state=dict(required=False, default="present", choices=["present", "absent"]),
464464
query_params=dict(required=False, type="list", elements="str"),
465465
validate_certs=dict(type="raw", default=True),
466+
cert=dict(type="raw", required=False),
466467
)
467468

468469

@@ -490,10 +491,11 @@ def __init__(self, module, endpoint, nb_client=None):
490491
url = self.module.params["netbox_url"]
491492
token = self.module.params["netbox_token"]
492493
ssl_verify = self.module.params["validate_certs"]
494+
cert = self.module.params["cert"]
493495

494496
# Attempt to initiate connection to Netbox
495497
if nb_client is None:
496-
self.nb = self._connect_netbox_api(url, token, ssl_verify)
498+
self.nb = self._connect_netbox_api(url, token, ssl_verify, cert)
497499
else:
498500
self.nb = nb_client
499501
try:
@@ -536,10 +538,11 @@ def _version_check_greater(self, greater, lesser, greater_or_equal=False):
536538
elif g_major == l_major and g_minor > l_minor:
537539
return True
538540

539-
def _connect_netbox_api(self, url, token, ssl_verify):
541+
def _connect_netbox_api(self, url, token, ssl_verify, cert):
540542
try:
541543
session = requests.Session()
542544
session.verify = ssl_verify
545+
session.cert = tuple(i for i in cert)
543546
nb = pynetbox.api(url, token=token)
544547
nb.http_session = session
545548
try:

0 commit comments

Comments
 (0)