ob-metaflow-extensions 1.1.45rc3__py2.py3-none-any.whl → 1.5.1__py2.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.

Potentially problematic release.


This version of ob-metaflow-extensions might be problematic. Click here for more details.

Files changed (128) hide show
  1. metaflow_extensions/outerbounds/__init__.py +1 -7
  2. metaflow_extensions/outerbounds/config/__init__.py +35 -0
  3. metaflow_extensions/outerbounds/plugins/__init__.py +186 -57
  4. metaflow_extensions/outerbounds/plugins/apps/__init__.py +0 -0
  5. metaflow_extensions/outerbounds/plugins/apps/app_cli.py +0 -0
  6. metaflow_extensions/outerbounds/plugins/apps/app_utils.py +187 -0
  7. metaflow_extensions/outerbounds/plugins/apps/consts.py +3 -0
  8. metaflow_extensions/outerbounds/plugins/apps/core/__init__.py +15 -0
  9. metaflow_extensions/outerbounds/plugins/apps/core/_state_machine.py +506 -0
  10. metaflow_extensions/outerbounds/plugins/apps/core/_vendor/__init__.py +0 -0
  11. metaflow_extensions/outerbounds/plugins/apps/core/_vendor/spinner/__init__.py +4 -0
  12. metaflow_extensions/outerbounds/plugins/apps/core/_vendor/spinner/spinners.py +478 -0
  13. metaflow_extensions/outerbounds/plugins/apps/core/app_config.py +128 -0
  14. metaflow_extensions/outerbounds/plugins/apps/core/app_deploy_decorator.py +330 -0
  15. metaflow_extensions/outerbounds/plugins/apps/core/artifacts.py +0 -0
  16. metaflow_extensions/outerbounds/plugins/apps/core/capsule.py +958 -0
  17. metaflow_extensions/outerbounds/plugins/apps/core/click_importer.py +24 -0
  18. metaflow_extensions/outerbounds/plugins/apps/core/code_package/__init__.py +3 -0
  19. metaflow_extensions/outerbounds/plugins/apps/core/code_package/code_packager.py +618 -0
  20. metaflow_extensions/outerbounds/plugins/apps/core/code_package/examples.py +125 -0
  21. metaflow_extensions/outerbounds/plugins/apps/core/config/__init__.py +15 -0
  22. metaflow_extensions/outerbounds/plugins/apps/core/config/cli_generator.py +165 -0
  23. metaflow_extensions/outerbounds/plugins/apps/core/config/config_utils.py +966 -0
  24. metaflow_extensions/outerbounds/plugins/apps/core/config/schema_export.py +299 -0
  25. metaflow_extensions/outerbounds/plugins/apps/core/config/typed_configs.py +233 -0
  26. metaflow_extensions/outerbounds/plugins/apps/core/config/typed_init_generator.py +537 -0
  27. metaflow_extensions/outerbounds/plugins/apps/core/config/unified_config.py +1125 -0
  28. metaflow_extensions/outerbounds/plugins/apps/core/config_schema.yaml +337 -0
  29. metaflow_extensions/outerbounds/plugins/apps/core/dependencies.py +115 -0
  30. metaflow_extensions/outerbounds/plugins/apps/core/deployer.py +959 -0
  31. metaflow_extensions/outerbounds/plugins/apps/core/experimental/__init__.py +89 -0
  32. metaflow_extensions/outerbounds/plugins/apps/core/perimeters.py +87 -0
  33. metaflow_extensions/outerbounds/plugins/apps/core/secrets.py +164 -0
  34. metaflow_extensions/outerbounds/plugins/apps/core/utils.py +233 -0
  35. metaflow_extensions/outerbounds/plugins/apps/core/validations.py +17 -0
  36. metaflow_extensions/outerbounds/plugins/apps/deploy_decorator.py +201 -0
  37. metaflow_extensions/outerbounds/plugins/apps/supervisord_utils.py +243 -0
  38. metaflow_extensions/outerbounds/plugins/auth_server.py +28 -8
  39. metaflow_extensions/outerbounds/plugins/aws/__init__.py +4 -0
  40. metaflow_extensions/outerbounds/plugins/aws/assume_role.py +3 -0
  41. metaflow_extensions/outerbounds/plugins/aws/assume_role_decorator.py +118 -0
  42. metaflow_extensions/outerbounds/plugins/card_utilities/__init__.py +0 -0
  43. metaflow_extensions/outerbounds/plugins/card_utilities/async_cards.py +142 -0
  44. metaflow_extensions/outerbounds/plugins/card_utilities/extra_components.py +545 -0
  45. metaflow_extensions/outerbounds/plugins/card_utilities/injector.py +70 -0
  46. metaflow_extensions/outerbounds/plugins/checkpoint_datastores/__init__.py +2 -0
  47. metaflow_extensions/outerbounds/plugins/checkpoint_datastores/coreweave.py +71 -0
  48. metaflow_extensions/outerbounds/plugins/checkpoint_datastores/external_chckpt.py +85 -0
  49. metaflow_extensions/outerbounds/plugins/checkpoint_datastores/nebius.py +73 -0
  50. metaflow_extensions/outerbounds/plugins/fast_bakery/__init__.py +0 -0
  51. metaflow_extensions/outerbounds/plugins/fast_bakery/baker.py +110 -0
  52. metaflow_extensions/outerbounds/plugins/fast_bakery/docker_environment.py +391 -0
  53. metaflow_extensions/outerbounds/plugins/fast_bakery/fast_bakery.py +188 -0
  54. metaflow_extensions/outerbounds/plugins/fast_bakery/fast_bakery_cli.py +54 -0
  55. metaflow_extensions/outerbounds/plugins/fast_bakery/fast_bakery_decorator.py +50 -0
  56. metaflow_extensions/outerbounds/plugins/kubernetes/kubernetes_client.py +79 -0
  57. metaflow_extensions/outerbounds/plugins/kubernetes/pod_killer.py +374 -0
  58. metaflow_extensions/outerbounds/plugins/nim/card.py +140 -0
  59. metaflow_extensions/outerbounds/plugins/nim/nim_decorator.py +101 -0
  60. metaflow_extensions/outerbounds/plugins/nim/nim_manager.py +379 -0
  61. metaflow_extensions/outerbounds/plugins/nim/utils.py +36 -0
  62. metaflow_extensions/outerbounds/plugins/nvcf/__init__.py +0 -0
  63. metaflow_extensions/outerbounds/plugins/nvcf/constants.py +3 -0
  64. metaflow_extensions/outerbounds/plugins/nvcf/exceptions.py +94 -0
  65. metaflow_extensions/outerbounds/plugins/nvcf/heartbeat_store.py +178 -0
  66. metaflow_extensions/outerbounds/plugins/nvcf/nvcf.py +417 -0
  67. metaflow_extensions/outerbounds/plugins/nvcf/nvcf_cli.py +280 -0
  68. metaflow_extensions/outerbounds/plugins/nvcf/nvcf_decorator.py +242 -0
  69. metaflow_extensions/outerbounds/plugins/nvcf/utils.py +6 -0
  70. metaflow_extensions/outerbounds/plugins/nvct/__init__.py +0 -0
  71. metaflow_extensions/outerbounds/plugins/nvct/exceptions.py +71 -0
  72. metaflow_extensions/outerbounds/plugins/nvct/nvct.py +131 -0
  73. metaflow_extensions/outerbounds/plugins/nvct/nvct_cli.py +289 -0
  74. metaflow_extensions/outerbounds/plugins/nvct/nvct_decorator.py +286 -0
  75. metaflow_extensions/outerbounds/plugins/nvct/nvct_runner.py +218 -0
  76. metaflow_extensions/outerbounds/plugins/nvct/utils.py +29 -0
  77. metaflow_extensions/outerbounds/plugins/ollama/__init__.py +225 -0
  78. metaflow_extensions/outerbounds/plugins/ollama/constants.py +1 -0
  79. metaflow_extensions/outerbounds/plugins/ollama/exceptions.py +22 -0
  80. metaflow_extensions/outerbounds/plugins/ollama/ollama.py +1924 -0
  81. metaflow_extensions/outerbounds/plugins/ollama/status_card.py +292 -0
  82. metaflow_extensions/outerbounds/plugins/optuna/__init__.py +48 -0
  83. metaflow_extensions/outerbounds/plugins/perimeters.py +19 -5
  84. metaflow_extensions/outerbounds/plugins/profilers/deco_injector.py +70 -0
  85. metaflow_extensions/outerbounds/plugins/profilers/gpu_profile_decorator.py +88 -0
  86. metaflow_extensions/outerbounds/plugins/profilers/simple_card_decorator.py +96 -0
  87. metaflow_extensions/outerbounds/plugins/s3_proxy/__init__.py +7 -0
  88. metaflow_extensions/outerbounds/plugins/s3_proxy/binary_caller.py +132 -0
  89. metaflow_extensions/outerbounds/plugins/s3_proxy/constants.py +11 -0
  90. metaflow_extensions/outerbounds/plugins/s3_proxy/exceptions.py +13 -0
  91. metaflow_extensions/outerbounds/plugins/s3_proxy/proxy_bootstrap.py +59 -0
  92. metaflow_extensions/outerbounds/plugins/s3_proxy/s3_proxy_api.py +93 -0
  93. metaflow_extensions/outerbounds/plugins/s3_proxy/s3_proxy_decorator.py +250 -0
  94. metaflow_extensions/outerbounds/plugins/s3_proxy/s3_proxy_manager.py +225 -0
  95. metaflow_extensions/outerbounds/plugins/secrets/__init__.py +0 -0
  96. metaflow_extensions/outerbounds/plugins/secrets/secrets.py +204 -0
  97. metaflow_extensions/outerbounds/plugins/snowflake/__init__.py +3 -0
  98. metaflow_extensions/outerbounds/plugins/snowflake/snowflake.py +378 -0
  99. metaflow_extensions/outerbounds/plugins/snowpark/__init__.py +0 -0
  100. metaflow_extensions/outerbounds/plugins/snowpark/snowpark.py +309 -0
  101. metaflow_extensions/outerbounds/plugins/snowpark/snowpark_cli.py +277 -0
  102. metaflow_extensions/outerbounds/plugins/snowpark/snowpark_client.py +150 -0
  103. metaflow_extensions/outerbounds/plugins/snowpark/snowpark_decorator.py +273 -0
  104. metaflow_extensions/outerbounds/plugins/snowpark/snowpark_exceptions.py +13 -0
  105. metaflow_extensions/outerbounds/plugins/snowpark/snowpark_job.py +241 -0
  106. metaflow_extensions/outerbounds/plugins/snowpark/snowpark_service_spec.py +259 -0
  107. metaflow_extensions/outerbounds/plugins/tensorboard/__init__.py +50 -0
  108. metaflow_extensions/outerbounds/plugins/torchtune/__init__.py +163 -0
  109. metaflow_extensions/outerbounds/plugins/vllm/__init__.py +255 -0
  110. metaflow_extensions/outerbounds/plugins/vllm/constants.py +1 -0
  111. metaflow_extensions/outerbounds/plugins/vllm/exceptions.py +1 -0
  112. metaflow_extensions/outerbounds/plugins/vllm/status_card.py +352 -0
  113. metaflow_extensions/outerbounds/plugins/vllm/vllm_manager.py +621 -0
  114. metaflow_extensions/outerbounds/profilers/gpu.py +131 -47
  115. metaflow_extensions/outerbounds/remote_config.py +53 -16
  116. metaflow_extensions/outerbounds/toplevel/global_aliases_for_metaflow_package.py +138 -2
  117. metaflow_extensions/outerbounds/toplevel/ob_internal.py +4 -0
  118. metaflow_extensions/outerbounds/toplevel/plugins/ollama/__init__.py +1 -0
  119. metaflow_extensions/outerbounds/toplevel/plugins/optuna/__init__.py +1 -0
  120. metaflow_extensions/outerbounds/toplevel/plugins/snowflake/__init__.py +1 -0
  121. metaflow_extensions/outerbounds/toplevel/plugins/torchtune/__init__.py +1 -0
  122. metaflow_extensions/outerbounds/toplevel/plugins/vllm/__init__.py +1 -0
  123. metaflow_extensions/outerbounds/toplevel/s3_proxy.py +88 -0
  124. {ob_metaflow_extensions-1.1.45rc3.dist-info → ob_metaflow_extensions-1.5.1.dist-info}/METADATA +2 -2
  125. ob_metaflow_extensions-1.5.1.dist-info/RECORD +133 -0
  126. ob_metaflow_extensions-1.1.45rc3.dist-info/RECORD +0 -19
  127. {ob_metaflow_extensions-1.1.45rc3.dist-info → ob_metaflow_extensions-1.5.1.dist-info}/WHEEL +0 -0
  128. {ob_metaflow_extensions-1.1.45rc3.dist-info → ob_metaflow_extensions-1.5.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,225 @@
1
+ import os
2
+ import json
3
+ import gzip
4
+ import sys
5
+ import time
6
+ import threading
7
+ import subprocess
8
+ from pathlib import Path
9
+ from typing import Optional, Tuple
10
+
11
+ import requests
12
+
13
+ from .constants import (
14
+ S3_PROXY_BINARY_URLS,
15
+ DEFAULT_PROXY_PORT,
16
+ DEFAULT_PROXY_HOST,
17
+ )
18
+ from metaflow.metaflow_config import AWS_SECRETS_MANAGER_DEFAULT_REGION
19
+ from .s3_proxy_api import S3ProxyApiClient
20
+ from .exceptions import S3ProxyException
21
+
22
+
23
+ class S3ProxyManager:
24
+ def __init__(
25
+ self,
26
+ integration_name: Optional[str] = None,
27
+ write_mode: Optional[str] = None,
28
+ debug: bool = False,
29
+ ):
30
+ self.integration_name = integration_name
31
+ self.write_mode = write_mode
32
+ self.debug = debug
33
+ self.process = None
34
+ self.binary_path = None
35
+ self.config_path = None
36
+ self.api_client = S3ProxyApiClient()
37
+ self.proxy_config = None
38
+
39
+ def setup_proxy(self) -> Tuple[dict, int, str, str]:
40
+ try:
41
+ if self._is_running_in_kubernetes():
42
+ config_data = self.api_client.fetch_s3_proxy_config(
43
+ self.integration_name
44
+ )
45
+ self.binary_path = self._download_binary()
46
+ self.config_path = self._write_config_file(config_data)
47
+ # In the new world where the binary is being called
48
+ # before even the metaflow code exection starts,
49
+ # so this implies a few important things:
50
+ # 1, We start the actual proxy process via another python file that safely ships logs to mflog.
51
+ # 2. We passback the right values to the metaflow step process via env vars.
52
+ # 3. Metaflow step code relies on env vars to decide if clients need to have s3 proxy in them.
53
+ self.process = self._start_proxy_process()
54
+
55
+ user_code_proxy_config = self._setup_proxy_config(config_data)
56
+
57
+ return_tuple = (
58
+ user_code_proxy_config, # this is the config that will be used within the metaflow `step` code.
59
+ self.process.pid, # This is the pid of the process that will jumpstart, monitor and ship logs to MFLOG for the proxy process
60
+ self.config_path, # This is the path to the config that is derived from the integration. It contains the actual bucket path and name where external objects are stored.
61
+ self.binary_path, # This is the path to the binary for the proxy.
62
+ )
63
+ # We return a tuple because these values need to be passed down to the metaflow step process where
64
+ # it will handle thier removal gracefully after the step is finished.
65
+ return return_tuple
66
+
67
+ print(
68
+ "[@s3_proxy] skipping s3-proxy set up because metaflow has not detected a Kubernetes environment"
69
+ )
70
+ raise S3ProxyException(
71
+ "S3 proxy setup failed because metaflow has not detected a Kubernetes environment"
72
+ )
73
+ except Exception as e:
74
+ if self.debug:
75
+ print(f"[@s3_proxy] Setup failed: {e}")
76
+ self.cleanup()
77
+ raise
78
+
79
+ def _is_running_in_kubernetes(self) -> bool:
80
+ """Check if running inside a Kubernetes pod by checking for Kubernetes service account token."""
81
+ return (
82
+ os.path.exists("/var/run/secrets/kubernetes.io/serviceaccount/token")
83
+ and os.environ.get("KUBERNETES_SERVICE_HOST") is not None
84
+ )
85
+
86
+ def _download_binary(self) -> str:
87
+ binary_path = Path("/tmp/s3-proxy")
88
+ if binary_path.exists():
89
+ if self.debug:
90
+ print("[@s3_proxy] Binary already exists, skipping download")
91
+ return str(binary_path.absolute())
92
+
93
+ try:
94
+ if self.debug:
95
+ print("[@s3_proxy] Downloading binary...")
96
+
97
+ from platform import machine
98
+
99
+ arch = machine()
100
+ if arch not in S3_PROXY_BINARY_URLS:
101
+ raise S3ProxyException(
102
+ f"unsupported platform architecture: {arch}. Please reach out to your Outerbounds Support team for more help."
103
+ )
104
+
105
+ response = requests.get(S3_PROXY_BINARY_URLS[arch], stream=True, timeout=60)
106
+ response.raise_for_status()
107
+
108
+ with open(binary_path, "wb") as f:
109
+ with gzip.GzipFile(fileobj=response.raw) as gz:
110
+ f.write(gz.read())
111
+
112
+ binary_path.chmod(0o755)
113
+
114
+ if self.debug:
115
+ print("[@s3_proxy] Binary downloaded successfully")
116
+
117
+ return str(binary_path.absolute())
118
+
119
+ except Exception as e:
120
+ if self.debug:
121
+ print(f"[@s3_proxy] Binary download failed: {e}")
122
+ raise S3ProxyException(f"Failed to download S3 proxy binary: {e}")
123
+
124
+ def _write_config_file(self, config_data) -> str:
125
+ config_path = Path("/tmp/s3-proxy-config.json")
126
+
127
+ proxy_config = {
128
+ "bucketName": config_data.bucket_name,
129
+ "endpointUrl": config_data.endpoint_url,
130
+ "accessKeyId": config_data.access_key_id,
131
+ "accessKeySecret": config_data.secret_access_key,
132
+ "region": config_data.region,
133
+ }
134
+
135
+ config_path.write_text(json.dumps(proxy_config, indent=2))
136
+
137
+ if self.debug:
138
+ print(f"[@s3_proxy] Config written to {config_path}")
139
+
140
+ return str(config_path.absolute())
141
+
142
+ def _start_proxy_process(self) -> subprocess.Popen:
143
+ # This command will jump start a process that will then call the proxy binary
144
+ # The reason we do something like this is because we need to run all of this before
145
+ # even the `step` command is called. So we need a python process that will ship the logs
146
+ # of the proxy process to MFLOG instead of setting print statements. We need this process
147
+ # to run independently since the S3ProxyManager gets called in the boostrap_proxy which will
148
+ # exit after jump starting the proxy process.
149
+ cmd = [self.binary_path, "--bucket-config", self.config_path, "serve"]
150
+ _env = os.environ.copy()
151
+ _env["S3_PROXY_BINARY_COMMAND"] = " ".join(cmd)
152
+ if self.debug:
153
+ _env["S3_PROXY_BINARY_DEBUG"] = "True"
154
+ _cmd = [
155
+ sys.executable,
156
+ "-m",
157
+ "metaflow_extensions.outerbounds.plugins.s3_proxy.binary_caller",
158
+ ]
159
+ devnull = subprocess.DEVNULL
160
+ process = subprocess.Popen(
161
+ _cmd,
162
+ stdout=devnull,
163
+ stderr=devnull,
164
+ text=True,
165
+ start_new_session=True,
166
+ env=_env,
167
+ )
168
+ time.sleep(3)
169
+
170
+ if process.poll() is None:
171
+ if self.debug:
172
+ print(f"[@s3_proxy] Proxy started successfully (pid: {process.pid})")
173
+
174
+ return process
175
+ else:
176
+ stdout_data, stderr_data = process.communicate()
177
+ if self.debug:
178
+ print(f"[@s3_proxy] Proxy failed to start - output: {stdout_data}")
179
+ raise S3ProxyException(f"S3 proxy failed to start: {stdout_data}")
180
+
181
+ def _setup_proxy_config(self, config_data):
182
+ from metaflow.metaflow_config import AWS_SECRETS_MANAGER_DEFAULT_REGION
183
+
184
+ region = os.environ.get(
185
+ "METAFLOW_AWS_SECRETS_MANAGER_DEFAULT_REGION",
186
+ AWS_SECRETS_MANAGER_DEFAULT_REGION,
187
+ )
188
+
189
+ proxy_config = {
190
+ "endpoint_url": f"http://{DEFAULT_PROXY_HOST}:{DEFAULT_PROXY_PORT}",
191
+ "region": region,
192
+ "bucket_name": config_data.bucket_name,
193
+ "active": True,
194
+ }
195
+
196
+ if self.write_mode:
197
+ proxy_config["write_mode"] = self.write_mode
198
+
199
+ self.proxy_config = proxy_config
200
+ return proxy_config
201
+
202
+ def cleanup(self):
203
+ try:
204
+ from metaflow_extensions.outerbounds.toplevel.global_aliases_for_metaflow_package import (
205
+ clear_s3_proxy_config,
206
+ )
207
+
208
+ clear_s3_proxy_config()
209
+
210
+ if self.process and self.process.poll() is None:
211
+ self.process.terminate()
212
+ self.process.wait(timeout=5)
213
+ if self.debug:
214
+ print("[@s3_proxy] Proxy process stopped")
215
+
216
+ from os import remove
217
+
218
+ remove(self.config_path)
219
+ remove(self.binary_path)
220
+
221
+ except Exception as e:
222
+ if self.debug:
223
+ print(f"[@s3_proxy] Cleanup error: {e}")
224
+ finally:
225
+ self.proxy_config = None
@@ -0,0 +1,204 @@
1
+ from metaflow.plugins.secrets import SecretsProvider
2
+ from typing import Dict
3
+
4
+ import base64
5
+ import json
6
+ import requests
7
+ import random
8
+ import time
9
+ import sys
10
+
11
+
12
+ class OuterboundsSecretsException(Exception):
13
+ pass
14
+
15
+
16
+ def _api_server_get(*args, conn_error_retries=2, **kwargs):
17
+ """
18
+ There are two categories of errors that we need to handle when dealing with any API server.
19
+ 1. HTTP errors. These are are errors that are returned from the API server.
20
+ - How to handle retries for this case will be application specific.
21
+ 2. Errors when the API server may not be reachable (DNS resolution / network issues)
22
+ - In this scenario, we know that something external to the API server is going wrong causing the issue.
23
+ - Failing pre-maturely in the case might not be the best course of action since critical user jobs might crash on intermittent issues.
24
+ - So in this case, we can just planely retry the request.
25
+
26
+ This function handles the second case. It's a simple wrapper to handle the retry logic for connection errors.
27
+ If this function is provided a `conn_error_retries` of 5, then the last retry will have waited 32 seconds.
28
+ Generally this is a safe enough number of retries after which we can assume that something is really broken. Until then,
29
+ there can be intermittent issues that would resolve themselves if we retry gracefully.
30
+ """
31
+ _num_retries = 0
32
+ noise = random.uniform(-0.5, 0.5)
33
+ while _num_retries < conn_error_retries:
34
+ try:
35
+ return requests.get(*args, **kwargs)
36
+ except requests.exceptions.ConnectionError:
37
+ if _num_retries <= conn_error_retries - 1:
38
+ # Exponential backoff with 2^(_num_retries+1) seconds
39
+ time.sleep((2 ** (_num_retries + 1)) + noise)
40
+ _num_retries += 1
41
+ else:
42
+ print(
43
+ "[@secrets] Failed to connect to the API server. ",
44
+ file=sys.stderr,
45
+ )
46
+ raise
47
+
48
+
49
+ class OuterboundsSecretsApiResponse:
50
+ def __init__(self, response):
51
+ self.response = response
52
+
53
+ @property
54
+ def secret_resource_id(self):
55
+ return self.response["secret_resource_id"]
56
+
57
+ @property
58
+ def secret_backend_type(self):
59
+ return self.response["secret_backend_type"]
60
+
61
+
62
+ class OuterboundsSecretsProvider(SecretsProvider):
63
+ TYPE = "outerbounds"
64
+
65
+ def get_secret_as_dict(self, secret_id, options={}, role=None):
66
+ """
67
+ Supports a special way of specifying secrets sources in outerbounds using the format:
68
+ @secrets(sources=["outerbounds.<integrations_name>"])
69
+
70
+ When invoked it makes a requests to the integrations secrets metadata endpoint on the
71
+ keywest server to get the cloud resource id for a secret. It then uses that to invoke
72
+ secrets manager on the core oss and returns the secrets.
73
+ """
74
+ headers = {"Content-Type": "application/json", "Connection": "keep-alive"}
75
+ perimeter, integrations_url = self._get_secret_configs()
76
+ integration_name = secret_id
77
+ request_payload = {
78
+ "perimeter_name": perimeter,
79
+ "integration_name": integration_name,
80
+ }
81
+ response = self._make_request(integrations_url, headers, request_payload)
82
+ secret_resource_id = response.secret_resource_id
83
+ secret_backend_type = response.secret_backend_type
84
+
85
+ from metaflow.plugins.secrets.secrets_decorator import (
86
+ get_secrets_backend_provider,
87
+ )
88
+
89
+ secrets_provider = get_secrets_backend_provider(secret_backend_type)
90
+ secret_dict = secrets_provider.get_secret_as_dict(
91
+ secret_resource_id, options={}, role=role
92
+ )
93
+
94
+ # Outerbounds stores secrets as binaries. Hence we expect the returned secret to be
95
+ # {<cloud-secret-name>: <base64 encoded full secret>}. We decode the secret here like:
96
+ # 1. decode the base64 encoded full secret
97
+ # 2. load the decoded secret as a json
98
+ # 3. decode the base64 encoded values in the dict
99
+ # 4. return the decoded dict
100
+ binary_secret = next(iter(secret_dict.values()))
101
+ return self._decode_secret(binary_secret)
102
+
103
+ def _is_base64_encoded(self, data):
104
+ try:
105
+ if isinstance(data, str):
106
+ # Check if the string can be base64 decoded
107
+ base64.b64decode(data).decode("utf-8")
108
+ return True
109
+ return False
110
+ except Exception:
111
+ return False
112
+
113
+ def _decode_secret(self, secret):
114
+ try:
115
+ result = {}
116
+ secret_str = secret
117
+ if self._is_base64_encoded(secret):
118
+ # we check if the secret string is base64 encoded because the returned secret from
119
+ # AWS secret manager is base64 encoded while the secret from GCP is not
120
+ secret_str = base64.b64decode(secret).decode("utf-8")
121
+
122
+ secret_dict = json.loads(secret_str)
123
+ for key, value in secret_dict.items():
124
+ result[key] = base64.b64decode(value).decode("utf-8")
125
+
126
+ return result
127
+ except Exception as e:
128
+ raise OuterboundsSecretsException(f"Error decoding secret: {e}")
129
+
130
+ def _get_secret_configs(self):
131
+ from metaflow_extensions.outerbounds.remote_config import init_config
132
+ from os import environ
133
+
134
+ conf = init_config()
135
+ if "OBP_PERIMETER" in conf:
136
+ perimeter = conf["OBP_PERIMETER"]
137
+ else:
138
+ # if the perimeter is not in metaflow config, try to get it from the environment
139
+ perimeter = environ.get("OBP_PERIMETER", "")
140
+
141
+ if "OBP_INTEGRATIONS_URL" in conf:
142
+ integrations_url = conf["OBP_INTEGRATIONS_URL"]
143
+ else:
144
+ # if the integrations is not in metaflow config, try to get it from the environment
145
+ integrations_url = environ.get("OBP_INTEGRATIONS_URL", "")
146
+
147
+ if not perimeter:
148
+ raise OuterboundsSecretsException(
149
+ "No perimeter set. Please make sure to run `outerbounds configure <...>` command which can be found on the Ourebounds UI or reach out to your Outerbounds support team."
150
+ )
151
+
152
+ if not integrations_url:
153
+ raise OuterboundsSecretsException(
154
+ "No integrations url set. Please notify your Outerbounds support team about this issue."
155
+ )
156
+
157
+ integrations_secrets_metadata_url = f"{integrations_url}/secrets/metadata"
158
+ return perimeter, integrations_secrets_metadata_url
159
+
160
+ def _make_request(self, url, headers: Dict, payload: Dict):
161
+ try:
162
+ from metaflow.metaflow_config import SERVICE_HEADERS
163
+
164
+ request_headers = {**headers, **(SERVICE_HEADERS or {})}
165
+ except ImportError:
166
+ headers = self.headers
167
+
168
+ retryable_status_codes = [409]
169
+ json_payload = json.dumps(payload)
170
+ for attempt in range(2): # 0 = initial attempt, 1-2 = retries
171
+ response = _api_server_get(
172
+ url, data=json_payload, headers=request_headers, conn_error_retries=5
173
+ )
174
+ if response.status_code not in retryable_status_codes:
175
+ break
176
+
177
+ if attempt < 2: # Don't sleep after the last attempt
178
+ sleep_time = 0.5 * (attempt + 1)
179
+ time.sleep(sleep_time)
180
+
181
+ self._handle_error_response(response)
182
+ return OuterboundsSecretsApiResponse(response.json())
183
+
184
+ @staticmethod
185
+ def _handle_error_response(response: requests.Response):
186
+ if response.status_code >= 500:
187
+ raise OuterboundsSecretsException(
188
+ f"Server error: {response.text}. Please reach out to your Outerbounds support team."
189
+ )
190
+
191
+ body = response.json()
192
+ status_code = body.get("error", {}).get("statusCode", response.status_code)
193
+ if status_code == 404:
194
+ raise OuterboundsSecretsException(f"Secret not found: {body}")
195
+
196
+ if status_code >= 400:
197
+ try:
198
+ raise OuterboundsSecretsException(
199
+ f"status_code={status_code}\t*{body['error']['details']['kind']}*\n{body['error']['details']['message']}"
200
+ )
201
+ except KeyError:
202
+ raise OuterboundsSecretsException(
203
+ f"status_code={status_code} Unexpected error: {body}"
204
+ )
@@ -0,0 +1,3 @@
1
+ from .snowflake import connect, get_snowflake_token, Snowflake
2
+
3
+ __mf_promote_submodules__ = ["plugins.snowflake"]