Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: use * wildcard for matching secret path (#6189)

Myles Scolnicke177977ddede
diff --git a/marimo/_config/secrets.py b/marimo/_config/secrets.py
index 813642e87e..d23530c8d6 100644
--- a/marimo/_config/secrets.py
+++ b/marimo/_config/secrets.py
@@ -17,7 +17,22 @@ def mask_secrets_partial(config: PartialMarimoConfig) -> PartialMarimoConfig:
def mask_secrets(config: MarimoConfig) -> MarimoConfig:
def deep_remove_from_path(path: list[str], obj: dict[str, Any]) -> None:
+ if not path:
+ return
+
key = path[0]
+ remaining_path = path[1:]
+
+ if key == "*":
+ for v in obj.values():
+ if isinstance(v, dict):
+ deep_remove_from_path(remaining_path, v)
+ elif isinstance(v, list) and remaining_path:
+ # Only first layer recursion
+ for item in v:
+ if isinstance(item, dict):
+ deep_remove_from_path(remaining_path, item)
+ return
if key not in obj:
return
if len(path) == 1:
@@ -26,23 +41,20 @@ def mask_secrets(config: MarimoConfig) -> MarimoConfig:
elif obj[key]:
obj[key] = SECRET_PLACEHOLDER
else:
- deep_remove_from_path(path[1:], cast(dict[str, Any], obj[key]))
-
- secrets = [
- ["ai", "open_ai", "api_key"],
- ["ai", "open_ai_compatible", "api_key"],
- ["ai", "azure", "api_key"],
- ["ai", "ollama", "api_key"],
- ["ai", "anthropic", "api_key"],
- ["ai", "google", "api_key"],
- ["ai", "bedrock", "aws_access_key_id"],
- ["ai", "bedrock", "aws_secret_access_key"],
- ["runtime", "dotenv"],
- ]
+ deep_remove_from_path(remaining_path, obj[key])
+
+ secrets = (
+ ("ai", "*", "api_key"),
+ ("ai", "bedrock", "aws_access_key_id"),
+ ("ai", "bedrock", "aws_secret_access_key"),
+ ("runtime", "dotenv"),
+ )
new_config = deep_copy(config)
+ config_dict = cast(dict[str, Any], new_config)
+
for secret in secrets:
- deep_remove_from_path(secret, cast(dict[str, Any], new_config))
+ deep_remove_from_path(list(secret), config_dict)
return new_config # type: ignore
diff --git a/tests/_config/test_secrets_config.py b/tests/_config/test_secrets_config.py
index 6e90cafcb3..5c7f1c1a33 100644
--- a/tests/_config/test_secrets_config.py
+++ b/tests/_config/test_secrets_config.py
@@ -1,7 +1,11 @@
from __future__ import annotations
from marimo._config.config import PartialMarimoConfig
-from marimo._config.secrets import mask_secrets, remove_secret_placeholders
+from marimo._config.secrets import (
+ SECRET_PLACEHOLDER,
+ mask_secrets,
+ remove_secret_placeholders,
+)
def test_mask_secrets() -> None:
@@ -10,6 +14,11 @@ def test_mask_secrets() -> None:
"open_ai": {"api_key": "super_secret"},
"anthropic": {"api_key": "anthropic_secret"},
"google": {"api_key": "google_secret"},
+ "github": {"api_key": "github_secret"},
+ "bedrock": {
+ "aws_access_key_id": "bedrock_access_key_id",
+ "aws_secret_access_key": "bedrock_secret_access_key",
+ },
},
runtime={
"dotenv": [".env"],
@@ -18,16 +27,52 @@ def test_mask_secrets() -> None:
assert config["ai"]["open_ai"]["api_key"] == "super_secret"
assert config["ai"]["anthropic"]["api_key"] == "anthropic_secret"
assert config["ai"]["google"]["api_key"] == "google_secret"
+ assert config["ai"]["github"]["api_key"] == "github_secret"
+ assert (
+ config["ai"]["bedrock"]["aws_access_key_id"] == "bedrock_access_key_id"
+ )
+ assert (
+ config["ai"]["bedrock"]["aws_secret_access_key"]
+ == "bedrock_secret_access_key"
+ )
new_config = mask_secrets(config)
- assert new_config["ai"]["open_ai"]["api_key"] == "********"
- assert new_config["ai"]["anthropic"]["api_key"] == "********"
- assert new_config["ai"]["google"]["api_key"] == "********"
+ assert new_config["ai"]["open_ai"]["api_key"] == SECRET_PLACEHOLDER
+ assert new_config["ai"]["anthropic"]["api_key"] == SECRET_PLACEHOLDER
+ assert new_config["ai"]["google"]["api_key"] == SECRET_PLACEHOLDER
+ assert new_config["ai"]["github"]["api_key"] == SECRET_PLACEHOLDER
+ assert (
+ new_config["ai"]["bedrock"]["aws_access_key_id"] == SECRET_PLACEHOLDER
+ )
+ assert (
+ new_config["ai"]["bedrock"]["aws_secret_access_key"]
+ == SECRET_PLACEHOLDER
+ )
# Ensure the original config is not modified
assert config["ai"]["open_ai"]["api_key"] == "super_secret"
assert config["ai"]["anthropic"]["api_key"] == "anthropic_secret"
assert config["ai"]["google"]["api_key"] == "google_secret"
+ assert config["ai"]["github"]["api_key"] == "github_secret"
+ assert (
+ config["ai"]["bedrock"]["aws_access_key_id"] == "bedrock_access_key_id"
+ )
+ assert (
+ config["ai"]["bedrock"]["aws_secret_access_key"]
+ == "bedrock_secret_access_key"
+ )
+
+
+def test_mask_secrets_list() -> None:
+ config = PartialMarimoConfig(
+ ai={"open_ai": [{"api_key": "super_secret"}]},
+ )
+ assert config["ai"]["open_ai"][0]["api_key"] == "super_secret"
+ new_config = mask_secrets(config)
+ assert new_config["ai"]["open_ai"][0]["api_key"] == SECRET_PLACEHOLDER
+
+ # Ensure the original config is not modified
+ assert config["ai"]["open_ai"][0]["api_key"] == "super_secret"
def test_mask_secrets_empty() -> None:
@@ -69,19 +114,19 @@ def test_mask_secrets_empty() -> None:
def test_remove_secret_placeholders() -> None:
config = PartialMarimoConfig(
ai={
- "open_ai": {"api_key": "********"},
- "google": {"api_key": "********"},
- "anthropic": {"api_key": "********"},
+ "open_ai": {"api_key": SECRET_PLACEHOLDER},
+ "google": {"api_key": SECRET_PLACEHOLDER},
+ "anthropic": {"api_key": SECRET_PLACEHOLDER},
},
)
- assert config["ai"]["open_ai"]["api_key"] == "********"
- assert config["ai"]["google"]["api_key"] == "********"
- assert config["ai"]["anthropic"]["api_key"] == "********"
+ assert config["ai"]["open_ai"]["api_key"] == SECRET_PLACEHOLDER
+ assert config["ai"]["google"]["api_key"] == SECRET_PLACEHOLDER
+ assert config["ai"]["anthropic"]["api_key"] == SECRET_PLACEHOLDER
new_config = remove_secret_placeholders(config)
assert "api_key" not in new_config["ai"]["open_ai"]
assert "api_key" not in new_config["ai"]["google"]
assert "api_key" not in new_config["ai"]["anthropic"]
# Ensure the original config is not modified
- assert config["ai"]["open_ai"]["api_key"] == "********"
- assert config["ai"]["google"]["api_key"] == "********"
- assert config["ai"]["anthropic"]["api_key"] == "********"
+ assert config["ai"]["open_ai"]["api_key"] == SECRET_PLACEHOLDER
+ assert config["ai"]["google"]["api_key"] == SECRET_PLACEHOLDER
+ assert config["ai"]["anthropic"]["api_key"] == SECRET_PLACEHOLDER