Fix and simplify Gateway store interfaces#19346
Conversation
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
|
Documentation preview for aee88ed is available at: More info
|
There was a problem hiding this comment.
Pull request overview
This PR simplifies and fixes the Gateway store interfaces for secret management by standardizing secret values to use only dict[str, str] format instead of str | dict[str, str]. This change enables proper support for multi-key secrets (e.g., AWS Bedrock credentials requiring both aws_access_key_id and aws_secret_access_key).
Key changes:
- Updated all store interfaces to accept only
dict[str, str]for secret values - Removed automatic string-to-dict conversion logic that used a default "api_key" key
- Enhanced masking logic to display multiple secret field names for compound credentials
- Added comprehensive test coverage for multi-key secret lifecycle operations
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| mlflow/store/tracking/gateway/abstract_mixin.py | Updated interface signatures to require dict[str, str] for secret_value parameters and improved documentation |
| mlflow/store/tracking/gateway/sqlalchemy_mixin.py | Removed string-to-dict conversion logic, enhanced masking for multi-key secrets, simplified create/update implementations |
| mlflow/store/tracking/gateway/rest_mixin.py | Updated REST store to serialize dict values to JSON strings before API calls |
| mlflow/server/handlers.py | Added _parse_secret_value function to convert JSON strings to dicts with validation |
| tests/tracking/test_rest_tracking.py | Updated all test cases to use dict format and added comprehensive multi-key secret tests |
| tests/store/tracking/test_secret_cache.py | Updated secret cache tests to use dict format for secret values |
| tests/store/tracking/test_rest_store.py | Updated REST store tests to use dict format and added multi-key secret test cases |
| tests/store/tracking/test_gateway_sql_store.py | Updated all SQL store tests to use dict format for secret values |
| tests/server/test_handlers.py | Added unit tests for the new _parse_secret_value function |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mlflow/server/handlers.py
Outdated
| if isinstance(parsed, dict): | ||
| return parsed | ||
| raise MlflowException( | ||
| "secret_value must be a JSON object (dict)", | ||
| error_code=INVALID_PARAMETER_VALUE, | ||
| ) | ||
| except json.JSONDecodeError as e: | ||
| raise MlflowException( | ||
| f"secret_value must be valid JSON: {e}", | ||
| error_code=INVALID_PARAMETER_VALUE, | ||
| ) from e | ||
|
|
||
|
|
There was a problem hiding this comment.
The _parse_secret_value function should validate that all dictionary values are strings and that the dictionary is non-empty. Currently, it only checks that the parsed JSON is a dict, but doesn't validate the dict's content matches the expected dict[str, str] type signature. This could allow invalid inputs like empty dicts, dicts with non-string values (numbers, booleans, nested objects, None), which would cause errors later in the encryption/storage logic.
| if isinstance(parsed, dict): | |
| return parsed | |
| raise MlflowException( | |
| "secret_value must be a JSON object (dict)", | |
| error_code=INVALID_PARAMETER_VALUE, | |
| ) | |
| except json.JSONDecodeError as e: | |
| raise MlflowException( | |
| f"secret_value must be valid JSON: {e}", | |
| error_code=INVALID_PARAMETER_VALUE, | |
| ) from e | |
| if not isinstance(parsed, dict): | |
| raise MlflowException( | |
| "secret_value must be a JSON object (dict)", | |
| error_code=INVALID_PARAMETER_VALUE, | |
| ) | |
| if not parsed: | |
| raise MlflowException( | |
| "secret_value must be a non-empty JSON object (dict)", | |
| error_code=INVALID_PARAMETER_VALUE, | |
| ) | |
| for k, v in parsed.items(): | |
| if not isinstance(k, str) or not isinstance(v, str): | |
| raise MlflowException( | |
| "All keys and values in secret_value must be strings.", | |
| error_code=INVALID_PARAMETER_VALUE, | |
| ) | |
| return parsed | |
| except json.JSONDecodeError as e: | |
| raise MlflowException( | |
| f"secret_value must be valid JSON: {e}", | |
| error_code=INVALID_PARAMETER_VALUE, | |
| ) from e |
| The created GatewaySecretInfo object with masked value. | ||
| """ | ||
| secret_value_str = json.dumps(secret_value) | ||
| auth_config_json = json.dumps(auth_config) if auth_config else None |
There was a problem hiding this comment.
The condition if auth_config on line 110 will evaluate to False for an empty dict ({}), which is inconsistent with the SQLAlchemy implementation. In sqlalchemy_mixin.py, an empty dict for auth_config is explicitly used to clear the configuration (see line 253-254 in sqlalchemy_mixin.py where the comment states "Empty dict {} explicitly clears auth_config"). Using if auth_config is not None would make this consistent with the SQL implementation and allow empty dicts to be serialized as "{}".
| auth_config_json = json.dumps(auth_config) if auth_config else None | |
| auth_config_json = json.dumps(auth_config) if auth_config is not None else None |
| The updated GatewaySecretInfo object with masked value. | ||
| """ | ||
| secret_value_str = json.dumps(secret_value) if secret_value else "" | ||
| auth_config_json = json.dumps(auth_config) if auth_config else None |
There was a problem hiding this comment.
The condition if auth_config on line 166 will evaluate to False for an empty dict ({}), which is inconsistent with the SQLAlchemy implementation. In sqlalchemy_mixin.py, an empty dict for auth_config is explicitly used to clear the configuration (see line 253-254 in sqlalchemy_mixin.py where the comment states "Empty dict {} explicitly clears auth_config"). Using if auth_config is not None would make this consistent with the SQL implementation and allow empty dicts to be serialized as "{}".
| auth_config_json = json.dumps(auth_config) if auth_config else None | |
| auth_config_json = json.dumps(auth_config) if auth_config is not None else None |
|
Shouldn't we update the proto for CreateGatewaySecret and UpdateGatewaySecret to support key-value pairs in REST API? |
I was weighing that earlier about whether we want to leave it 'open' as JSON serialized string but the I should have just gone with my first instinct and just passed JSON. |
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
🛠 DevTools 🛠
Install mlflow from this PR
For Databricks, use the following command:
Related Issues/PRs
#xxxWhat changes are proposed in this pull request?
Fix an issue with update_secret store interfaces to support multiple key secrets (i.e., Bedrock, Azure).
Simplify the create and update secrets on all stores to only support
dict[str, str]inputs.Add store coverage for changes.
Add e2e multi-secret lifecycle test to verify proper create and update handling for compound key providers.
How is this PR tested?
Does this PR require documentation update?
Release Notes
Is this a user-facing change?
What component(s), interfaces, languages, and integrations does this PR affect?
Components
area/tracking: Tracking Service, tracking client APIs, autologgingarea/models: MLmodel format, model serialization/deserialization, flavorsarea/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registryarea/scoring: MLflow Model server, model deployment tools, Spark UDFsarea/evaluation: MLflow model evaluation features, evaluation metrics, and evaluation workflowsarea/gateway: MLflow AI Gateway client APIs, server, and third-party integrationsarea/prompts: MLflow prompt engineering features, prompt templates, and prompt managementarea/tracing: MLflow Tracing features, tracing APIs, and LLM tracing functionalityarea/projects: MLproject format, project running backendsarea/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev serverarea/build: Build and test infrastructure for MLflowarea/docs: MLflow documentation pagesHow should the PR be classified in the release notes? Choose one:
rn/none- No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" sectionrn/breaking-change- The PR will be mentioned in the "Breaking Changes" sectionrn/feature- A new user-facing feature worth mentioning in the release notesrn/bug-fix- A user-facing bug fix worth mentioning in the release notesrn/documentation- A user-facing documentation change worth mentioning in the release notesShould this PR be included in the next patch release?
Yesshould be selected for bug fixes, documentation updates, and other small changes.Noshould be selected for new features and larger changes. If you're unsure about the release classification of this PR, leave this unchecked to let the maintainers decide.What is a minor/patch release?
Bug fixes, doc updates and new features usually go into minor releases.
Bug fixes and doc updates usually go into patch releases.