netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
580 stars 172 forks source link

`nb.version` Returns an Empty String Due to OIDC Proxy Configuration #640

Open Kani999 opened 3 months ago

Kani999 commented 3 months ago

pynetbox version

v7.4.0

NetBox version

v4.0.9

Python version

3.11

Steps to Reproduce

Description:

When using NetBox with an OIDC (OpenID Connect) proxy for authentication, the nb.version property consistently returns an empty string (''). This issue arises because the get_version method in pynetbox does not receive the API-Version header, as the request is redirected to the OIDC login page due to missing authorization.

Steps to Reproduce:

  1. Set up NetBox behind an OIDC proxy with the following Apache configuration:

    <Location /api>
       <If "-z req('authorization')">
         Require valid-user
       </If>
       <Else>
          Require ip <IP_ADDRESS_RANGE>
       </Else>
    </Location>
  2. Use pynetbox to access the API.

  3. Call the nb.version property.

  4. Observe that nb.version returns an empty string.

Expected Behavior:

The nb.version property should return the correct NetBox version string.

Actual Behavior:

The nb.version property returns an empty string.

Cause:

The OIDC proxy redirects unauthorized requests to the login page, causing the get_version method to miss the API-Version header.

Proposed Solution:

Modify the version property in pynetbox/core/api.py to ensure the authorization token is passed to the get_version method. Here's the proposed change:

@property
def version(self):
    """Gets the API version of NetBox.

    Can be used to check the NetBox API version if there are
    version-dependent features or syntaxes in the API.

    :Returns: Version number as a string.
    :Example:

    >>> import pynetbox
    >>> nb = pynetbox.api(
    ...     'http://localhost:8000',
    ...     token='your_token_here'
    ... )
    >>> nb.version
    '3.1'
    >>>
    """
    version = Request(
        base=self.base_url,
        token=self.token,  # Ensure the token is passed here
        http_session=self.http_session,
    ).get_version()
    return version

Additional Context:

The issue is specific to environments where NetBox is protected by an OIDC proxy, requiring a valid token for API access.

Expected Behavior

nb.version '4.0'

Observed Behavior

nb.version ''