mlflow / mlflow-export-import

Apache License 2.0
110 stars 70 forks source link

Model export: {http_status_code": 401} Authentication issue #158

Open lisanaberberi opened 7 months ago

lisanaberberi commented 7 months ago

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

export-model --model --output-dir /tmp/export --versions 1,2

mlflow_export_import.common.MlflowExportImportException: {"message": "{\"http_status_code\": 401, \"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"

lisanaberberi commented 7 months ago

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

goncalo-maia commented 7 months ago

I believe mlflow's basic auth use case is not considered when the headers are made in HttpClient class https://github.com/mlflow/mlflow-export-import/blob/5248d8818d3cfc717f3f9496fd431b9c808c010c/mlflow_export_import/client/http_client.py#L174-L178

By changing the way the get request is sent from: https://github.com/mlflow/mlflow-export-import/blob/5248d8818d3cfc717f3f9496fd431b9c808c010c/mlflow_export_import/client/http_client.py#L103 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.

lasados commented 2 months ago

Same problem, I fixed it manually in source code https://github.com/mlflow/mlflow-export-import/blob/a012089272e18430963d6fe460b71ec8851ce377/mlflow_export_import/client/http_client.py#L159

  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)
Linnore commented 3 weeks ago

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