google-cloud-managed-mlflow 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google_cloud_managed_mlflow/__init__.py +100 -0
- google_cloud_managed_mlflow/_entrypoint_guard.py +120 -0
- google_cloud_managed_mlflow/_validation.py +161 -0
- google_cloud_managed_mlflow/auth.py +682 -0
- google_cloud_managed_mlflow/cab_credentials.py +1589 -0
- google_cloud_managed_mlflow/gcs_artifact_repo.py +1126 -0
- google_cloud_managed_mlflow/models_artifact_repo.py +308 -0
- google_cloud_managed_mlflow/py.typed +0 -0
- google_cloud_managed_mlflow/registry_store.py +1060 -0
- google_cloud_managed_mlflow/tracking_store.py +617 -0
- google_cloud_managed_mlflow/uri.py +1150 -0
- google_cloud_managed_mlflow-0.1.0.dist-info/METADATA +95 -0
- google_cloud_managed_mlflow-0.1.0.dist-info/RECORD +17 -0
- google_cloud_managed_mlflow-0.1.0.dist-info/WHEEL +5 -0
- google_cloud_managed_mlflow-0.1.0.dist-info/entry_points.txt +12 -0
- google_cloud_managed_mlflow-0.1.0.dist-info/licenses/LICENSE +202 -0
- google_cloud_managed_mlflow-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Google Cloud Managed Service for MLflow plugin."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import importlib
|
|
20
|
+
from typing import TYPE_CHECKING, Any
|
|
21
|
+
|
|
22
|
+
__version__ = "0.1.0"
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from google_cloud_managed_mlflow.auth import (
|
|
26
|
+
AUTH_PROVIDER_NAME as AUTH_PROVIDER_NAME,
|
|
27
|
+
CLOUD_PLATFORM_SCOPE as CLOUD_PLATFORM_SCOPE,
|
|
28
|
+
CredentialsProtocol as CredentialsProtocol,
|
|
29
|
+
GoogleCloudAuthProvider as GoogleCloudAuthProvider,
|
|
30
|
+
GoogleCloudBearerAuth as GoogleCloudBearerAuth,
|
|
31
|
+
create_secure_session as create_secure_session,
|
|
32
|
+
ensure_auth_provider_registered as ensure_auth_provider_registered,
|
|
33
|
+
harden_mlflow_request_session as harden_mlflow_request_session,
|
|
34
|
+
is_truthy_env as is_truthy_env,
|
|
35
|
+
validate_transport_security as validate_transport_security,
|
|
36
|
+
)
|
|
37
|
+
from google_cloud_managed_mlflow.gcs_artifact_repo import (
|
|
38
|
+
GcpGCSArtifactRepository as GcpGCSArtifactRepository,
|
|
39
|
+
ensure_artifact_repo_registered as ensure_artifact_repo_registered,
|
|
40
|
+
)
|
|
41
|
+
from google_cloud_managed_mlflow.models_artifact_repo import (
|
|
42
|
+
GcpModelsArtifactRepository as GcpModelsArtifactRepository,
|
|
43
|
+
)
|
|
44
|
+
from google_cloud_managed_mlflow.registry_store import (
|
|
45
|
+
GcpModelRegistryRestStore as GcpModelRegistryRestStore,
|
|
46
|
+
build_registry_store as build_registry_store,
|
|
47
|
+
ensure_registry_store_registered as ensure_registry_store_registered,
|
|
48
|
+
)
|
|
49
|
+
from google_cloud_managed_mlflow.tracking_store import (
|
|
50
|
+
GcpTrackingRestStore as GcpTrackingRestStore,
|
|
51
|
+
build_tracking_store as build_tracking_store,
|
|
52
|
+
ensure_tracking_store_registered as ensure_tracking_store_registered,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
__all__ = [
|
|
56
|
+
"__version__",
|
|
57
|
+
"GcpGCSArtifactRepository",
|
|
58
|
+
"GcpModelRegistryRestStore",
|
|
59
|
+
"GcpModelsArtifactRepository",
|
|
60
|
+
"GcpTrackingRestStore",
|
|
61
|
+
"build_registry_store",
|
|
62
|
+
"build_tracking_store",
|
|
63
|
+
"ensure_artifact_repo_registered",
|
|
64
|
+
"ensure_registry_store_registered",
|
|
65
|
+
"ensure_tracking_store_registered",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
_LAZY_EXPORTS: dict[str, str] = {
|
|
69
|
+
"GcpGCSArtifactRepository": "gcs_artifact_repo",
|
|
70
|
+
"ensure_artifact_repo_registered": "gcs_artifact_repo",
|
|
71
|
+
"GcpModelsArtifactRepository": "models_artifact_repo",
|
|
72
|
+
"GcpModelRegistryRestStore": "registry_store",
|
|
73
|
+
"build_registry_store": "registry_store",
|
|
74
|
+
"ensure_registry_store_registered": "registry_store",
|
|
75
|
+
"GcpTrackingRestStore": "tracking_store",
|
|
76
|
+
"build_tracking_store": "tracking_store",
|
|
77
|
+
"ensure_tracking_store_registered": "tracking_store",
|
|
78
|
+
"AUTH_PROVIDER_NAME": "auth",
|
|
79
|
+
"CLOUD_PLATFORM_SCOPE": "auth",
|
|
80
|
+
"CredentialsProtocol": "auth",
|
|
81
|
+
"GoogleCloudAuthProvider": "auth",
|
|
82
|
+
"GoogleCloudBearerAuth": "auth",
|
|
83
|
+
"create_secure_session": "auth",
|
|
84
|
+
"ensure_auth_provider_registered": "auth",
|
|
85
|
+
"harden_mlflow_request_session": "auth",
|
|
86
|
+
"is_truthy_env": "auth",
|
|
87
|
+
"validate_transport_security": "auth",
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def __getattr__(name: str) -> Any:
|
|
92
|
+
submodule = _LAZY_EXPORTS.get(name)
|
|
93
|
+
if submodule is not None:
|
|
94
|
+
mod = importlib.import_module(f"{__name__}.{submodule}")
|
|
95
|
+
return getattr(mod, name)
|
|
96
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def __dir__() -> list[str]:
|
|
100
|
+
return sorted(set(__all__) | set(_LAZY_EXPORTS))
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Private stdlib-only entry-point guard and registration repair helpers."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import contextlib
|
|
20
|
+
import importlib.metadata
|
|
21
|
+
import logging
|
|
22
|
+
import sys
|
|
23
|
+
import warnings
|
|
24
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from collections.abc import Iterator
|
|
28
|
+
|
|
29
|
+
logger = logging.getLogger(__name__)
|
|
30
|
+
|
|
31
|
+
# Matches warnings from
|
|
32
|
+
# mlflow.store.artifact.artifact_repository_registry.register_entrypoints
|
|
33
|
+
# (MLflow >= 3.17.2).
|
|
34
|
+
_REGISTRATION_WARNING_PATTERN = (
|
|
35
|
+
r'Failure attempting to register artifact repository for scheme "(?:gs|models)"'
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@contextlib.contextmanager
|
|
40
|
+
def quiet_registration_warning() -> Iterator[None]:
|
|
41
|
+
"""Context manager to suppress MLflow entry-point registration warnings.
|
|
42
|
+
|
|
43
|
+
Temporarily silences warnings emitted when MLflow discovers this package's
|
|
44
|
+
entry points while an import cycle or early-load condition is active. Filter
|
|
45
|
+
is removed afterwards. Do not use warnings.catch_warnings(): it restores the
|
|
46
|
+
whole filter list and would discard filters that mlflow and its
|
|
47
|
+
dependencies install during their first import.
|
|
48
|
+
"""
|
|
49
|
+
if "mlflow.store.artifact.artifact_repository_registry" in sys.modules:
|
|
50
|
+
yield
|
|
51
|
+
return
|
|
52
|
+
warnings.filterwarnings(
|
|
53
|
+
"ignore", message=_REGISTRATION_WARNING_PATTERN, category=UserWarning
|
|
54
|
+
)
|
|
55
|
+
# With Python 3.14 context-aware warnings inside catch_warnings(), the new
|
|
56
|
+
# filter goes to a context-local list, so warnings.filters[0] may belong to
|
|
57
|
+
# someone else. Only remember the entry if it is the one just added.
|
|
58
|
+
filter_item: Any = None
|
|
59
|
+
candidate = warnings.filters[0] if warnings.filters else None
|
|
60
|
+
cand_pat = getattr(candidate[1], "pattern", None) if candidate is not None else None
|
|
61
|
+
if (
|
|
62
|
+
candidate is not None
|
|
63
|
+
and candidate[0] == "ignore"
|
|
64
|
+
and candidate[2] is UserWarning
|
|
65
|
+
and cand_pat == _REGISTRATION_WARNING_PATTERN
|
|
66
|
+
):
|
|
67
|
+
filter_item = candidate
|
|
68
|
+
try:
|
|
69
|
+
yield
|
|
70
|
+
finally:
|
|
71
|
+
if filter_item is not None:
|
|
72
|
+
filters = cast("list[Any]", warnings.filters)
|
|
73
|
+
for index, item in enumerate(filters):
|
|
74
|
+
if item is filter_item:
|
|
75
|
+
del filters[index]
|
|
76
|
+
break
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def repair_artifact_entrypoint(
|
|
80
|
+
scheme: str,
|
|
81
|
+
repo_cls: type[Any],
|
|
82
|
+
stock_cls: type[Any],
|
|
83
|
+
*,
|
|
84
|
+
imported_before_mlflow: bool,
|
|
85
|
+
) -> None:
|
|
86
|
+
"""Repair an artifact repository scheme registration in MLflow's registry.
|
|
87
|
+
|
|
88
|
+
If MLflow loaded early before our entry points could resolve without an import
|
|
89
|
+
cycle, this function re-registers our repository class once prerequisites are met.
|
|
90
|
+
"""
|
|
91
|
+
class_name = repo_cls.__name__
|
|
92
|
+
try:
|
|
93
|
+
registry_module = sys.modules.get(
|
|
94
|
+
"mlflow.store.artifact.artifact_repository_registry"
|
|
95
|
+
)
|
|
96
|
+
registry = getattr(registry_module, "_artifact_repository_registry", None)
|
|
97
|
+
if registry is None:
|
|
98
|
+
if registry_module is not None and imported_before_mlflow:
|
|
99
|
+
logger.warning(
|
|
100
|
+
f"Could not register {class_name} for '{scheme}': "
|
|
101
|
+
"MLflow's artifact repository registry was not found."
|
|
102
|
+
)
|
|
103
|
+
return
|
|
104
|
+
current = registry.get_registered_artifact_repositories().get(scheme)
|
|
105
|
+
if current is not stock_cls:
|
|
106
|
+
return
|
|
107
|
+
target = f"{repo_cls.__module__}:{class_name}"
|
|
108
|
+
if any(
|
|
109
|
+
ep.name == scheme and ep.value.replace(" ", "") == target
|
|
110
|
+
for ep in importlib.metadata.entry_points(
|
|
111
|
+
group="mlflow.artifact_repository"
|
|
112
|
+
)
|
|
113
|
+
):
|
|
114
|
+
registry.register(scheme, repo_cls)
|
|
115
|
+
except Exception:
|
|
116
|
+
logger.log(
|
|
117
|
+
logging.WARNING if imported_before_mlflow else logging.DEBUG,
|
|
118
|
+
f"Could not repair MLflow's '{scheme}' registration.",
|
|
119
|
+
exc_info=True,
|
|
120
|
+
)
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Validation and URL parsing primitives for Google Cloud Managed MLflow."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import ipaddress
|
|
20
|
+
import os
|
|
21
|
+
import re
|
|
22
|
+
import urllib.parse
|
|
23
|
+
from typing import NamedTuple
|
|
24
|
+
|
|
25
|
+
from mlflow.exceptions import MlflowException
|
|
26
|
+
from mlflow.protos.databricks_pb2 import INVALID_PARAMETER_VALUE
|
|
27
|
+
|
|
28
|
+
_LOOPBACK_HOSTS = frozenset({"localhost", "127.0.0.1", "::1"})
|
|
29
|
+
_GOOGLEAPIS_DOMAIN_SUFFIX = ".googleapis.com"
|
|
30
|
+
_DNS_LABEL_RE = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$")
|
|
31
|
+
_UNBRACKETED_HOST_RE = re.compile(r"^[a-z0-9_-]+(?:\.[a-z0-9_-]+)*$")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class _EndpointOrigin(NamedTuple):
|
|
35
|
+
"""Normalized (scheme, hostname, effective_port) endpoint origin."""
|
|
36
|
+
|
|
37
|
+
scheme: str
|
|
38
|
+
hostname: str
|
|
39
|
+
port: int
|
|
40
|
+
|
|
41
|
+
def __str__(self) -> str:
|
|
42
|
+
host_display = f"[{self.hostname}]" if ":" in self.hostname else self.hostname
|
|
43
|
+
return f"{self.scheme}://{host_display}:{self.port}"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _contains_control_chars(s: str) -> bool:
|
|
47
|
+
"""Return True if s contains ASCII C0 or DEL control characters.
|
|
48
|
+
|
|
49
|
+
Must run before urllib.parse.urlsplit/urljoin, which silently strip
|
|
50
|
+
'\\t', '\\r', and '\\n' (bpo-43882).
|
|
51
|
+
"""
|
|
52
|
+
return any(ord(ch) < 0x20 or ord(ch) == 0x7F for ch in s)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _parse_validated_endpoint_url(raw_url: str) -> _EndpointOrigin:
|
|
56
|
+
"""Parse and validate URL scheme, host, effective port, and HTTP policy."""
|
|
57
|
+
if not raw_url:
|
|
58
|
+
raise MlflowException(
|
|
59
|
+
"Endpoint URL must be a non-empty string.",
|
|
60
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
if _contains_control_chars(raw_url):
|
|
64
|
+
raise MlflowException(
|
|
65
|
+
"Endpoint URL contains invalid control characters.",
|
|
66
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
if raw_url[0].isspace() or raw_url[-1].isspace():
|
|
70
|
+
raise MlflowException(
|
|
71
|
+
"Endpoint URL must be a non-empty string without "
|
|
72
|
+
"leading/trailing whitespace.",
|
|
73
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
parsed = urllib.parse.urlsplit(raw_url)
|
|
78
|
+
raw_port = parsed.port
|
|
79
|
+
except ValueError as exc:
|
|
80
|
+
raise MlflowException(
|
|
81
|
+
"Endpoint URL is malformed or contains an invalid port.",
|
|
82
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
83
|
+
) from exc
|
|
84
|
+
|
|
85
|
+
if raw_port == 0:
|
|
86
|
+
raise MlflowException(
|
|
87
|
+
"Endpoint URL is malformed or contains an invalid port.",
|
|
88
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
raw_hostname = (parsed.hostname or "").lower()
|
|
92
|
+
if (
|
|
93
|
+
not parsed.scheme
|
|
94
|
+
or not raw_hostname.rstrip(".")
|
|
95
|
+
or "@" in parsed.netloc
|
|
96
|
+
or "\\" in parsed.netloc
|
|
97
|
+
or any(ch.isspace() for ch in parsed.netloc)
|
|
98
|
+
):
|
|
99
|
+
raise MlflowException(
|
|
100
|
+
"Endpoint URL must include a valid scheme and host.",
|
|
101
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
if "[" in parsed.netloc or "]" in parsed.netloc:
|
|
105
|
+
try:
|
|
106
|
+
if "%" in raw_hostname:
|
|
107
|
+
raise ValueError("IPv6 zone identifiers are not supported.")
|
|
108
|
+
hostname = ipaddress.IPv6Address(raw_hostname).compressed
|
|
109
|
+
except ValueError as exc:
|
|
110
|
+
raise MlflowException(
|
|
111
|
+
"Endpoint URL contains an invalid IPv6 host literal.",
|
|
112
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
113
|
+
) from exc
|
|
114
|
+
else:
|
|
115
|
+
hostname = raw_hostname.rstrip(".")
|
|
116
|
+
if not _UNBRACKETED_HOST_RE.fullmatch(hostname):
|
|
117
|
+
raise MlflowException(
|
|
118
|
+
"Endpoint URL contains invalid characters in host.",
|
|
119
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
scheme = parsed.scheme.lower()
|
|
123
|
+
if scheme not in ("https", "http"):
|
|
124
|
+
raise MlflowException(
|
|
125
|
+
f"Invalid scheme '{scheme}': only https:// and loopback http:// "
|
|
126
|
+
"are allowed.",
|
|
127
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
if scheme == "http" and hostname not in _LOOPBACK_HOSTS:
|
|
131
|
+
raise MlflowException(
|
|
132
|
+
f"Insecure HTTP is only permitted on loopback addresses "
|
|
133
|
+
f"({', '.join(sorted(_LOOPBACK_HOSTS))}); got '{hostname}'.",
|
|
134
|
+
error_code=INVALID_PARAMETER_VALUE,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
effective_port = (
|
|
138
|
+
raw_port if raw_port is not None else (443 if scheme == "https" else 80)
|
|
139
|
+
)
|
|
140
|
+
return _EndpointOrigin(scheme, hostname, effective_port)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _is_allowed_destination(origin: _EndpointOrigin) -> bool:
|
|
144
|
+
"""Return True if origin is an allowed Google Cloud or override destination."""
|
|
145
|
+
if (
|
|
146
|
+
origin.scheme == "https"
|
|
147
|
+
and origin.port == 443
|
|
148
|
+
and origin.hostname.endswith(_GOOGLEAPIS_DOMAIN_SUFFIX)
|
|
149
|
+
and all(_DNS_LABEL_RE.fullmatch(label) for label in origin.hostname.split("."))
|
|
150
|
+
):
|
|
151
|
+
return True
|
|
152
|
+
|
|
153
|
+
override_raw = os.environ.get("MLFLOW_GCP_ENDPOINT_OVERRIDE", "").strip(" ")
|
|
154
|
+
if override_raw:
|
|
155
|
+
try:
|
|
156
|
+
override_origin = _parse_validated_endpoint_url(override_raw)
|
|
157
|
+
except MlflowException:
|
|
158
|
+
return False
|
|
159
|
+
return origin == override_origin
|
|
160
|
+
|
|
161
|
+
return False
|