-
Notifications
You must be signed in to change notification settings - Fork 955
feat(server): support injecting env vars into egress container #1069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |
| from opensandbox_server.api.schema import CreateSandboxRequest | ||
| from opensandbox_server.config import AppConfig, EGRESS_MODE_DNS | ||
| from opensandbox_server.services.constants import ( | ||
| EGRESS_ENV_PREFIX, | ||
| RESERVED_EGRESS_ENV_VARS, | ||
| SANDBOX_EGRESS_AUTH_TOKEN_METADATA_KEY, | ||
| SANDBOX_SECURE_ACCESS_TOKEN_METADATA_KEY, | ||
| SANDBOX_ID_LABEL, | ||
|
|
@@ -29,6 +31,32 @@ | |
| ) | ||
| from opensandbox_server.services.validators import calculate_expiration_or_raise | ||
|
|
||
| Pair = tuple[Dict[str, Optional[str]], Dict[str, Optional[str]]] | ||
|
|
||
|
|
||
| def _split_egress_env( | ||
| env: Optional[Dict[str, Optional[str]]], | ||
| ) -> Pair: | ||
| """Split request env into (sandbox_env, egress_env) by OPENSANDBOX_EGRESS_ prefix. | ||
|
|
||
| Raises ValueError if a user-supplied key collides with a reserved internal var. | ||
| """ | ||
| if not env: | ||
| return {}, {} | ||
|
|
||
| sandbox_env: Dict[str, Optional[str]] = {} | ||
| egress_env: Dict[str, Optional[str]] = {} | ||
| for key, value in env.items(): | ||
| if key.startswith(EGRESS_ENV_PREFIX): | ||
| if key in RESERVED_EGRESS_ENV_VARS: | ||
| raise ValueError( | ||
| f"Environment variable '{key}' is reserved and cannot be overridden" | ||
| ) | ||
| egress_env[key] = value | ||
|
Pangjiping marked this conversation as resolved.
Comment on lines
+51
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For Kubernetes create requests that do not include Useful? React with 👍 / 👎. |
||
| else: | ||
| sandbox_env[key] = value | ||
| return sandbox_env, egress_env | ||
|
|
||
|
|
||
| @dataclass | ||
| class _CreateWorkloadContext: | ||
|
|
@@ -41,6 +69,8 @@ class _CreateWorkloadContext: | |
| egress_auth_token: Optional[str] | ||
| credential_proxy_enabled: bool | ||
| secure_access_token: Optional[str] | ||
| sandbox_env: Dict[str, Optional[str]] | ||
| egress_env: Dict[str, Optional[str]] | ||
|
|
||
|
|
||
| def _build_create_workload_context( | ||
|
|
@@ -84,6 +114,8 @@ def _build_create_workload_context( | |
| if request.resource_limits and request.resource_limits.root: | ||
| resource_limits = request.resource_limits.root | ||
|
|
||
| sandbox_env, egress_env = _split_egress_env(request.env) | ||
|
Pangjiping marked this conversation as resolved.
|
||
|
|
||
| return _CreateWorkloadContext( | ||
| labels=labels, | ||
| annotations=annotations, | ||
|
|
@@ -94,4 +126,6 @@ def _build_create_workload_context( | |
| egress_auth_token=egress_auth_token, | ||
| credential_proxy_enabled=credential_proxy_enabled, | ||
| secure_access_token=secure_access_token, | ||
| sandbox_env=sandbox_env, | ||
| egress_env=egress_env, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ def apply_egress_to_spec( | |
| egress_auth_token: Optional[str] = None, | ||
| egress_mode: str = EGRESS_MODE_DNS, | ||
| credential_proxy_enabled: bool = False, | ||
| extra_env: Optional[Dict[str, Optional[str]]] = None, | ||
| ) -> None: | ||
| """ | ||
| Append the egress sidecar to ``containers``. When ``egress.disable_ipv6`` is enabled, | ||
|
|
@@ -94,6 +95,9 @@ def apply_egress_to_spec( | |
| env.append({"name": OPENSANDBOX_EGRESS_MITMPROXY_TRANSPARENT, "value": "true"}) | ||
| if egress_auth_token: | ||
| env.append({"name": OPENSANDBOX_EGRESS_TOKEN, "value": egress_auth_token}) | ||
| if extra_env: | ||
| for name, value in extra_env.items(): | ||
| env.append({"name": name, "value": value or ""}) | ||
|
Pangjiping marked this conversation as resolved.
Pangjiping marked this conversation as resolved.
Comment on lines
+100
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
|
|
||
| sidecar: Dict[str, Any] = { | ||
| "name": "egress", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.