Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /BerriAI_litellm /litellm /proxy /auth /litellm_license.py
| # What is this? | |
| ## If litellm license in env, checks if it's valid | |
| import base64 | |
| import json | |
| import os | |
| from datetime import datetime | |
| from typing import TYPE_CHECKING, Final | |
| import httpx | |
| from litellm._logging import verbose_proxy_logger | |
| from litellm.constants import NON_LLM_CONNECTION_TIMEOUT | |
| from litellm.llms.custom_httpx.http_handler import HTTPHandler | |
| if TYPE_CHECKING: | |
| from litellm.proxy._types import EnterpriseLicenseData | |
| AUTO_ROUTER_LICENSE_FEATURE: Final = "auto_router" | |
| AUTO_ROUTER_LICENSE_REMEDY: Final = "A LiteLLM license with the 'auto_router' feature lifts the limit." | |
| class LicenseCheck: | |
| """ | |
| - Check if license in env | |
| - Returns if license is valid | |
| """ | |
| base_url = "https://license.litellm.ai" | |
| def __init__(self) -> None: | |
| self.license_str = os.getenv("LITELLM_LICENSE", None) | |
| verbose_proxy_logger.debug("License Str value - %s", self.license_str) | |
| self.http_handler = HTTPHandler(timeout=NON_LLM_CONNECTION_TIMEOUT) | |
| self._premium_check_logged = False | |
| self.public_key = None | |
| self.read_public_key() | |
| self.airgapped_license_data: EnterpriseLicenseData | None = None | |
| def read_public_key(self): | |
| try: | |
| from cryptography.hazmat.primitives import serialization | |
| # current dir | |
| current_dir: Final = os.path.dirname(os.path.realpath(__file__)) | |
| # check if public_key.pem exists | |
| _path_to_public_key: Final = os.path.join(current_dir, "public_key.pem") | |
| if os.path.exists(_path_to_public_key): | |
| with open(_path_to_public_key, "rb") as key_file: | |
| self.public_key = serialization.load_pem_public_key(key_file.read()) | |
| else: | |
| self.public_key = None | |
| except Exception as e: | |
| verbose_proxy_logger.error("Error reading public key: %s", e) | |
| def _verify(self, license_str: str) -> bool: | |
| verbose_proxy_logger.debug( | |
| "litellm.proxy.auth.litellm_license.py::_verify - Checking license against %s/verify_license - %s", | |
| self.base_url, | |
| license_str, | |
| ) | |
| url: Final = f"{self.base_url}/verify_license/{license_str}" | |
| response: httpx.Response | None = None | |
| try: # don't impact user, if call fails | |
| num_retries: Final = 3 | |
| for i in range(num_retries): | |
| try: | |
| response = self.http_handler.get(url=url) | |
| if response is None: | |
| raise Exception("No response from license server") | |
| response.raise_for_status() | |
| except httpx.HTTPStatusError: | |
| if i == num_retries - 1: | |
| raise | |
| if response is None: | |
| raise Exception("No response from license server") | |
| response_json: Final = response.json() | |
| premium: Final = response_json["verify"] | |
| assert isinstance(premium, bool) | |
| verbose_proxy_logger.debug( | |
| "litellm.proxy.auth.litellm_license.py::_verify - License=%s is premium=%s", license_str, premium | |
| ) | |
| return premium | |
| except Exception as e: | |
| verbose_proxy_logger.exception( | |
| "litellm.proxy.auth.litellm_license.py::_verify - Unable to verify License=%s via api. - %s", | |
| license_str, | |
| e, | |
| ) | |
| return False | |
| def is_premium(self) -> bool: | |
| """ | |
| 1. verify_license_without_api_request: checks if license was generate using private / public key pair | |
| 2. _verify: checks if license is valid calling litellm API. This is the old way we were generating/validating license | |
| """ | |
| try: | |
| if not self._premium_check_logged: | |
| verbose_proxy_logger.debug( | |
| "litellm.proxy.auth.litellm_license.py::is_premium() - ENTERING 'IS_PREMIUM' - LiteLLM License=%s", | |
| self.license_str, | |
| ) | |
| if self.license_str is None: | |
| self.license_str = os.getenv("LITELLM_LICENSE", None) | |
| if not self._premium_check_logged: | |
| verbose_proxy_logger.debug( | |
| "litellm.proxy.auth.litellm_license.py::is_premium() - Updated 'self.license_str' - %s", | |
| self.license_str, | |
| ) | |
| self._premium_check_logged = True | |
| if self.license_str is None: | |
| return False | |
| elif ( | |
| self.verify_license_without_api_request(public_key=self.public_key, license_key=self.license_str) | |
| is True | |
| ) or self._verify(license_str=self.license_str) is True: | |
| return True | |
| return False | |
| except Exception: | |
| return False | |
| def is_over_limit(self, total_users: int) -> bool: | |
| """ | |
| Check if the license is over the limit | |
| """ | |
| if self.airgapped_license_data is None: | |
| return False | |
| if "max_users" not in self.airgapped_license_data or not isinstance( | |
| self.airgapped_license_data["max_users"], int | |
| ): | |
| return False | |
| return total_users > self.airgapped_license_data["max_users"] | |
| def is_team_count_over_limit(self, team_count: int) -> bool: | |
| """ | |
| Check if the license is over the limit | |
| """ | |
| if self.airgapped_license_data is None: | |
| return False | |
| _max_teams_in_license: Final[int | None] = self.airgapped_license_data.get("max_teams") | |
| if "max_teams" not in self.airgapped_license_data or not isinstance(_max_teams_in_license, int): | |
| return False | |
| return team_count > _max_teams_in_license | |
| def auto_router_capability_limit(self) -> int | None: | |
| """ | |
| How many auto-routers may claim each licensed capability (heuristic_v2, operator-defined | |
| tier_definitions): unlimited (None) only when the signed license lists the auto_router | |
| feature, otherwise one per capability. A license verified through the API carries no | |
| feature list, so it does not lift the limit either. | |
| """ | |
| if self.airgapped_license_data is None: | |
| return 1 | |
| allowed_features: Final = self.airgapped_license_data.get("allowed_features") | |
| if isinstance(allowed_features, list) and AUTO_ROUTER_LICENSE_FEATURE in allowed_features: | |
| return None | |
| return 1 | |
| def verify_license_without_api_request(self, public_key, license_key): | |
| try: | |
| from cryptography.hazmat.primitives import hashes | |
| from cryptography.hazmat.primitives.asymmetric import padding | |
| from litellm.proxy._types import EnterpriseLicenseData | |
| # Decode the license key - add padding if needed for base64 | |
| # Base64 strings need to be a multiple of 4 characters | |
| padding_needed: Final = len(license_key) % 4 | |
| if padding_needed: | |
| license_key += "=" * (4 - padding_needed) | |
| decoded: Final = base64.b64decode(license_key) | |
| message, signature = decoded.split(b".", 1) | |
| # Verify the signature | |
| public_key.verify( | |
| signature, | |
| message, | |
| padding.PSS( | |
| mgf=padding.MGF1(hashes.SHA256()), | |
| salt_length=padding.PSS.MAX_LENGTH, | |
| ), | |
| hashes.SHA256(), | |
| ) | |
| # Decode and parse the data | |
| license_data: Final = json.loads(message.decode()) | |
| # debug information provided in license data | |
| verbose_proxy_logger.debug("License data: %s", license_data) | |
| # Check expiration date | |
| expiration_date: Final = datetime.strptime(license_data["expiration_date"], "%Y-%m-%d") | |
| if expiration_date < datetime.now(): | |
| self.airgapped_license_data = None | |
| return False, "License has expired" | |
| self.airgapped_license_data = EnterpriseLicenseData(**license_data) | |
| return True | |
| except Exception as e: | |
| self.airgapped_license_data = None | |
| verbose_proxy_logger.debug( | |
| "litellm.proxy.auth.litellm_license.py::verify_license_without_api_request - Unable to verify License locally. - %s", | |
| e, | |
| ) | |
| return False | |
Xet Storage Details
- Size:
- 8.48 kB
- Xet hash:
- 5454c2bc0349d07f0a7fecdaea2de9469abcaf4db3660140f3adda1a0d405742
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.