mlflow / mlflow-export-import

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Model export: {http_status_code": 401} Authentication issue

lisanaberberi opened this issue · comments

When trying to run export-model command to export a specified model, there is this error:

export-model --model <model_name> --output-dir /tmp/export --versions 1,2

mlflow_export_import.common.MlflowExportImportException: {"message": "{"http_status_code": 401, "uri": "<mlflow_tracking_uri>/api/2.0/mlflow/registered-models/get", "params": {"name": "model_name"}, "response": "You are not authenticated.

already exported the env vars:
export MLFLOW_TRACKING_URI
export MLFLOW_TRACKING_USERNAME
export MLFLOW_TRACKING_PASSWORD

Of course when you use curl and add --user params it's working correctly but not without it:

curl -X GET $MLFLOW_TRACKING_URI/api/2.0/mlflow/registered-models/get?name=$MODEL_NAME --user "$MLFLOW_TRACKING_USERNAME:$MLFLOW_TRACKING_PASSWORD"

This applies for the scenario: MLFlow OS --> MLFlow OS

I believe mlflow's basic auth use case is not considered when the headers are made in HttpClient class

def _mk_headers(self):
headers = { "User-Agent": USER_AGENT }
if self.token:
headers["Authorization"] = f"Bearer {self.token}"
return headers

By changing the way the get request is sent from:

rsp = requests.get(uri, headers=self._mk_headers(), json=params, timeout=_TIMEOUT)

to rsp = requests.get(uri, auth=(os.environ["MLFLOW_TRACKING_USERNAME"], os.environ["MLFLOW_TRACKING_PASSWORD"]), params=params) the 401 error is no longer present. Of course this breaks the previously implemented auth methods.

I would be willing to contribute a fix for this bug if some guidance on how to best integrate it with the rest is provided.

Same problem, I fixed it manually in source code

def _mutator(self, method, resource, data=None):

  def _mutator(self, method, resource, data=None):
      uri = self._mk_uri(resource)
      user = os.environ.get('MLFLOW_TRACKING_USERNAME')
      pwd = os.environ.get('MLFLOW_TRACKING_PASSWORD')
      rsp = method(uri, headers=self._mk_headers(), data=data, timeout=_TIMEOUT, auth=(user, pwd))
      return self._check_response(rsp)

Same problem if the tracking server enables authentication. I solved it by using @lasados's answer.