Skip to content

Commit

Permalink
Feat add possibility to use Bearer token (#1023)
Browse files Browse the repository at this point in the history
* Add possibility to use Bearer token type

Co-authored-by: Philippe Morry <[email protected]>
Co-authored-by: Sean M. Collins <[email protected]>
  • Loading branch information
3 people authored Jun 27, 2023
1 parent 776a291 commit e062ce5
Show file tree
Hide file tree
Showing 8 changed files with 3,966 additions and 9 deletions.
37 changes: 28 additions & 9 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@
token:
required: False
description:
- NetBox API token to be able to read against NetBox.
- This may not be required depending on the NetBox setup.
- NetBox API token to be able to read against NetBox.
- This may not be required depending on the NetBox setup.
- You can provide a "type" and "value" for a token if your NetBox deployment is using a more advanced authentication like OAUTH.
- If you do not provide a "type" and "value" parameter, the HTTP authorization header will be set to "Token", which is the NetBox default
env:
# in order of precedence
- name: NETBOX_TOKEN
Expand Down Expand Up @@ -336,6 +338,14 @@
# - "time_zone_utc_minus_7"
# - "time_zone_utc_plus_1"
# - "time_zone_utc_plus_10"
# Example of using a token type
plugin: netbox.netbox.nb_inventory
api_endpoint: http://localhost:8000
token:
type: Bearer
value: test123456
"""

import json
Expand Down Expand Up @@ -1975,11 +1985,7 @@ def main(self):
host=hostname,
)

def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path)
self._read_config_data(path=path)
self.use_cache = cache

def _set_authorization(self):
# NetBox access
if version.parse(ansible_version) < version.parse("2.11"):
token = self.get_option("token")
Expand All @@ -1988,6 +1994,19 @@ def parse(self, inventory, loader, path, cache=True):
token = self.templar.template(
self.get_option("token"), fail_on_undefined=False
)
if token:
# check if token is new format
if isinstance(token, dict):
self.headers.update(
{"Authorization": f"{token['type'].capitalize()} {token['value']}"}
)
else:
self.headers.update({"Authorization": "Token %s" % token})

def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path)
self._read_config_data(path=path)
self.use_cache = cache

# Handle extra "/" from api_endpoint configuration and trim if necessary, see PR#49943
self.api_endpoint = self.get_option("api_endpoint").strip("/")
Expand All @@ -2013,8 +2032,8 @@ def parse(self, inventory, loader, path, cache=True):
self.cert = self.get_option("cert")
self.key = self.get_option("key")
self.ca_path = self.get_option("ca_path")
if token:
self.headers.update({"Authorization": "Token %s" % token})

self._set_authorization()

# Filter and group_by options
self.group_by = self.get_option("group_by")
Expand Down
Loading

0 comments on commit e062ce5

Please sign in to comment.