mlflow-plugin-proxy-auth/mlflow_plugin_proxy_auth/proxy_auth_header_provider.py

40 lines
1023 B
Python
Raw Normal View History

2023-12-17 20:11:36 +00:00
"""Mlflow plugin proxy auth"""
import base64
import os
from mlflow.tracking.request_auth.abstract_request_auth_provider import (
RequestAuthProvider,
)
2023-12-13 22:25:44 +00:00
from requests.auth import AuthBase
class ProxyAuthProvider(RequestAuthProvider):
2023-12-17 20:11:36 +00:00
""""Mlflow plugin class"""
def __init__(self):
2023-12-13 22:34:58 +00:00
self.username = os.getenv("MLFLOW_PROXY_USERNAME")
self.password = os.getenv("MLFLOW_PROXY_PASSWORD")
def get_name(self):
return "proxy_auth_provider"
def get_auth(self):
2023-12-13 22:25:44 +00:00
return ProxyAuth(self.username, self.password)
class ProxyAuth(AuthBase):
2023-12-17 20:11:36 +00:00
"""Requests proxy auth class"""
2023-12-13 22:25:44 +00:00
def __init__(self, username, password):
self.username = username
self.password = password
def __call__(self, r):
credentials = f"{self.username}:{self.password}"
encoded_credentials = base64.b64encode(credentials.encode("utf-8")).decode(
"utf-8"
)
2023-12-13 22:25:44 +00:00
r.headers["Proxy-Authorization"] = f"Basic {encoded_credentials}"
return r