|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2025 NetBox Labs Inc |
| 3 | +"""Diode NetBox Plugin - Diode - Auth.""" |
| 4 | + |
| 5 | +import datetime |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import re |
| 9 | +import threading |
| 10 | +from dataclasses import dataclass |
| 11 | +from urllib.parse import urlencode |
| 12 | + |
| 13 | +import requests |
| 14 | + |
| 15 | +from netbox_diode_plugin.plugin_config import ( |
| 16 | + get_diode_auth_base_url, |
| 17 | + get_diode_credentials, |
| 18 | + get_diode_max_auth_retries, |
| 19 | +) |
| 20 | + |
| 21 | +SCOPE_DIODE_READ = "diode:read" |
| 22 | +SCOPE_DIODE_WRITE = "diode:write" |
| 23 | + |
| 24 | +logger = logging.getLogger("netbox.diode_data") |
| 25 | + |
| 26 | +valid_client_id_re = re.compile(r"^[a-zA-Z0-9_-]{1,64}$") |
| 27 | + |
| 28 | +_client = None |
| 29 | +_client_lock = threading.Lock() |
| 30 | +def get_api_client(): |
| 31 | + """Get the client API client.""" |
| 32 | + global _client |
| 33 | + global _client_lock |
| 34 | + |
| 35 | + with _client_lock: |
| 36 | + if _client is None: |
| 37 | + client_id, client_secret = get_diode_credentials() |
| 38 | + if not client_id: |
| 39 | + raise ClientAPIError( |
| 40 | + "Please update the plugin configuration to access this feature.\nMissing netbox to diode client id.", 500) |
| 41 | + if not client_secret: |
| 42 | + raise ClientAPIError( |
| 43 | + "Please update the plugin configuration to access this feature.\nMissing netbox to diode client secret.", 500) |
| 44 | + max_auth_retries = get_diode_max_auth_retries() |
| 45 | + _client = ClientAPI( |
| 46 | + base_url=get_diode_auth_base_url(), |
| 47 | + client_id=client_id, |
| 48 | + client_secret=client_secret, |
| 49 | + max_auth_retries=max_auth_retries, |
| 50 | + ) |
| 51 | + return _client |
| 52 | + |
| 53 | + |
| 54 | +class ClientAPIError(Exception): |
| 55 | + """Client API Error.""" |
| 56 | + |
| 57 | + def __init__(self, message: str, status_code: int = 500): |
| 58 | + """Initialize the ClientAPIError.""" |
| 59 | + self.message = message |
| 60 | + self.status_code = status_code |
| 61 | + super().__init__(self.message) |
| 62 | + |
| 63 | + def is_auth_error(self) -> bool: |
| 64 | + """Check if the error is an authentication error.""" |
| 65 | + return self.status_code == 401 or self.status_code == 403 |
| 66 | + |
| 67 | +class ClientAPI: |
| 68 | + """Manages Diode Clients.""" |
| 69 | + |
| 70 | + def __init__(self, base_url: str, client_id: str, client_secret: str, max_auth_retries: int = 2): |
| 71 | + """Initialize the ClientAPI.""" |
| 72 | + self.base_url = base_url |
| 73 | + self.client_id = client_id |
| 74 | + self.client_secret = client_secret |
| 75 | + |
| 76 | + self._max_auth_retries = max_auth_retries |
| 77 | + self._client_auth_token = None |
| 78 | + self._client_auth_token_lock = threading.Lock() |
| 79 | + |
| 80 | + def create_client(self, name: str, scope: str) -> dict: |
| 81 | + """Create a client.""" |
| 82 | + for attempt in range(self._max_auth_retries): |
| 83 | + token = None |
| 84 | + try: |
| 85 | + token = self._get_token() |
| 86 | + url = self.base_url + "/clients" |
| 87 | + headers = {"Authorization": f"Bearer {token}"} |
| 88 | + data = { |
| 89 | + "client_name": name, |
| 90 | + "scope": scope, |
| 91 | + } |
| 92 | + response = requests.post(url, json=data, headers=headers) |
| 93 | + if response.status_code != 201: |
| 94 | + raise ClientAPIError("Failed to create client", response.status_code) |
| 95 | + return response.json() |
| 96 | + except ClientAPIError as e: |
| 97 | + if e.is_auth_error() and attempt < self._max_auth_retries - 1: |
| 98 | + logger.info(f"Retrying create_client due to unauthenticated error, attempt {attempt + 1}") |
| 99 | + self._mark_client_auth_token_invalid(token) |
| 100 | + continue |
| 101 | + raise |
| 102 | + raise ClientAPIError("Failed to create client: unexpected state", 500) |
| 103 | + |
| 104 | + def get_client(self, client_id: str) -> dict: |
| 105 | + """Get a client.""" |
| 106 | + if not valid_client_id_re.match(client_id): |
| 107 | + raise ValueError(f"Invalid client ID: {client_id}") |
| 108 | + |
| 109 | + for attempt in range(self._max_auth_retries): |
| 110 | + token = None |
| 111 | + try: |
| 112 | + token = self._get_token() |
| 113 | + url = self.base_url + f"/clients/{client_id}" |
| 114 | + headers = {"Authorization": f"Bearer {token}"} |
| 115 | + response = requests.get(url, headers=headers) |
| 116 | + if response.status_code == 401 or response.status_code == 403: |
| 117 | + raise ClientAPIError(f"Failed to get client {client_id}", response.status_code) |
| 118 | + if response.status_code != 200: |
| 119 | + raise ClientAPIError(f"Failed to get client {client_id}", response.status_code) |
| 120 | + return response.json() |
| 121 | + except ClientAPIError as e: |
| 122 | + if e.is_auth_error() and attempt < self._max_auth_retries - 1: |
| 123 | + logger.info(f"Retrying delete_client due to unauthenticated error, attempt {attempt + 1}") |
| 124 | + self._mark_client_auth_token_invalid(token) |
| 125 | + continue |
| 126 | + raise |
| 127 | + raise ClientAPIError(f"Failed to get client {client_id}: unexpected state") |
| 128 | + |
| 129 | + def delete_client(self, client_id: str) -> None: |
| 130 | + """Delete a client.""" |
| 131 | + if not valid_client_id_re.match(client_id): |
| 132 | + raise ValueError(f"Invalid client ID: {client_id}") |
| 133 | + |
| 134 | + for attempt in range(self._max_auth_retries): |
| 135 | + token = None |
| 136 | + try: |
| 137 | + token = self._get_token() |
| 138 | + url = self.base_url + f"/clients/{client_id}" |
| 139 | + headers = {"Authorization": f"Bearer {token}"} |
| 140 | + response = requests.delete(url, headers=headers) |
| 141 | + if response.status_code != 204: |
| 142 | + raise ClientAPIError(f"Failed to delete client {client_id}", response.status_code) |
| 143 | + return |
| 144 | + except ClientAPIError as e: |
| 145 | + if e.is_auth_error() and attempt < self._max_auth_retries - 1: |
| 146 | + logger.info(f"Retrying delete_client due to unauthenticated error, attempt {attempt + 1}") |
| 147 | + self._mark_client_auth_token_invalid(token) |
| 148 | + continue |
| 149 | + raise |
| 150 | + raise ClientAPIError(f"Failed to delete client {client_id}: unexpected state") |
| 151 | + |
| 152 | + |
| 153 | + def list_clients(self, page_token: str | None = None, page_size: int | None = None) -> list[dict]: |
| 154 | + """List all clients.""" |
| 155 | + for attempt in range(self._max_auth_retries): |
| 156 | + token = None |
| 157 | + try: |
| 158 | + token = self._get_token() |
| 159 | + url = self.base_url + "/clients" |
| 160 | + headers = {"Authorization": f"Bearer {token}"} |
| 161 | + params = {} |
| 162 | + if page_token: |
| 163 | + params["page_token"] = page_token |
| 164 | + if page_size: |
| 165 | + params["page_size"] = page_size |
| 166 | + response = requests.get(url, headers=headers, params=params) |
| 167 | + if response.status_code != 200: |
| 168 | + raise ClientAPIError("Failed to get clients", response.status_code) |
| 169 | + return response.json() |
| 170 | + except ClientAPIError as e: |
| 171 | + if e.is_auth_error() and attempt < self._max_auth_retries - 1: |
| 172 | + logger.info(f"Retrying list_clients due to unauthenticated error, attempt {attempt + 1}") |
| 173 | + self._mark_client_auth_token_invalid(token) |
| 174 | + continue |
| 175 | + raise |
| 176 | + raise ClientAPIError("Failed to list clients: unexpected state") |
| 177 | + |
| 178 | + |
| 179 | + def _get_token(self) -> str: |
| 180 | + """Get a token for the Diode Auth Service.""" |
| 181 | + with self._client_auth_token_lock: |
| 182 | + if self._client_auth_token: |
| 183 | + return self._client_auth_token |
| 184 | + self._client_auth_token = self._authenticate() |
| 185 | + return self._client_auth_token |
| 186 | + |
| 187 | + def _mark_client_auth_token_invalid(self, token: str): |
| 188 | + """Mark a client auth token as invalid.""" |
| 189 | + with self._client_auth_token_lock: |
| 190 | + self._client_auth_token = None |
| 191 | + |
| 192 | + def _authenticate(self) -> str: |
| 193 | + """Get a new access token for the Diode Auth Service.""" |
| 194 | + headers = {"Content-Type": "application/x-www-form-urlencoded"} |
| 195 | + data = urlencode( |
| 196 | + { |
| 197 | + "grant_type": "client_credentials", |
| 198 | + "client_id": self.client_id, |
| 199 | + "client_secret": self.client_secret, |
| 200 | + "scope": f"{SCOPE_DIODE_READ} {SCOPE_DIODE_WRITE}", |
| 201 | + } |
| 202 | + ) |
| 203 | + url = self.base_url + "/token" |
| 204 | + try: |
| 205 | + response = requests.post(url, data=data, headers=headers) |
| 206 | + except Exception as e: |
| 207 | + raise ClientAPIError(f"Failed to obtain access token: {e}", 401) from e |
| 208 | + if response.status_code != 200: |
| 209 | + raise ClientAPIError(f"Failed to obtain access token: {response.reason}", 401) |
| 210 | + |
| 211 | + try: |
| 212 | + token_info = response.json() |
| 213 | + except Exception as e: |
| 214 | + raise ClientAPIError(f"Failed to parse access token response: {e}", 401) from e |
| 215 | + |
| 216 | + access_token = token_info.get("access_token") |
| 217 | + if not access_token: |
| 218 | + raise ClientAPIError(f"Failed to obtain access token for client {self._client_id}", 401) |
| 219 | + |
| 220 | + return access_token |
| 221 | + |
0 commit comments