Skip to content
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

Harden configuration reading #2701

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/databricks/labs/ucx/assessment/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ def _get_secret_if_exists(self, secret_scope, secret_key) -> str | None:
)
return None

def _get_value_from_config_key(self, config: dict, key: str, get_secret: bool = True) -> str | None:
def _get_value_from_config_key(
self,
config: dict,
key: str,
get_secret: bool = True,
) -> str | None:
"""Get a config value based on its key, with some special handling:
If the key is prefixed with spark_conf, i.e. this is in a cluster policy, the actual value is nested
If the value is of format {{secret_scope/secret}}, we extract that as well
"""
if re.search("spark_conf", key):
value = config.get(key, {}).get("value", "")
value = config.get(key, {})
if isinstance(value, dict):
value = value.get("value", "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug: otherwise value is None. add an else statement

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could also happen in line 48 and the function signature allows return type None. It will break when get_secret is True

else:
value = config.get(key, "")
# retrieve from secret scope if used
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/assessment/test_secrets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from databricks.labs.ucx.assessment.secrets import SecretsMixin


@pytest.mark.parametrize(
"key,expected",
[
("spark_conf.spark.hadoop.javax.jdo.option.ConnectionURL", "url"),
("NonExistentKey", ""),
("spark_conf.invalid", "{'should_not': 'be_string'}"),
],
)
def test_secrets_mixin_gets_value_from_config_key(key, expected) -> None:
config = {
"spark_conf.invalid": "{'should_not': 'be_string'}",
"spark_conf.spark.hadoop.javax.jdo.option.ConnectionURL": {"value": "url"},
}
secrets_mixin = SecretsMixin()

value = secrets_mixin._get_value_from_config_key(config, key) # pylint: disable=protected-access

assert value == expected
Loading