From c1ec8d74ff1ea2169455777accaf4529e1600aed Mon Sep 17 00:00:00 2001 From: Caleb Herbel Date: Fri, 31 Mar 2023 12:25:19 -0700 Subject: [PATCH] Remove case sensitivity for enum settings --- .../licenses/license_plugin_settings.py | 23 +++++++++---------- nbdefense/plugins/secrets.py | 14 +++++++---- nbdefense/settings.py | 15 +++++++++++- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/nbdefense/plugins/licenses/license_plugin_settings.py b/nbdefense/plugins/licenses/license_plugin_settings.py index 78c91e1..04561a6 100644 --- a/nbdefense/plugins/licenses/license_plugin_settings.py +++ b/nbdefense/plugins/licenses/license_plugin_settings.py @@ -1,10 +1,10 @@ from enum import Enum from typing import Any, Dict, List -from nbdefense.settings import Settings, UnknownSettingsError +from nbdefense.settings import Settings, UnknownSettingsValueError -class LicensePluginSource(Enum): +class LicensePluginSource(str, Enum): HYBRID = "hybrid" # Looks for licenses in local pip environment and uses the pypi api when a package is not installed locally PYPI = "pypi" # Scans for licenses using PYPI api only LOCAL = "local" # Scans for licenses in local pip environment only @@ -24,15 +24,14 @@ def get_accepted_licenses(self) -> List[str]: if isinstance(accepted_licenses, list): return accepted_licenses else: - raise UnknownSettingsError("accepted_licenses") + raise UnknownSettingsValueError("accepted_licenses", accepted_licenses) def get_licenses_for_notebooks_source(self) -> LicensePluginSource: - source = super().get("licenses_for_notebooks_source") - if source == "local": - return LicensePluginSource.LOCAL - elif source == "hybrid": - return LicensePluginSource.HYBRID - elif source == "pypi": - return LicensePluginSource.PYPI - else: - raise UnknownSettingsError("licenses_for_notebooks_source") + source: str = str(super().get("licenses_for_notebooks_source")).lower() + if ( + source == LicensePluginSource.LOCAL + or source == LicensePluginSource.HYBRID + or source == LicensePluginSource.PYPI + ): + return LicensePluginSource(source) + raise UnknownSettingsValueError("licenses_for_notebooks_source", source) diff --git a/nbdefense/plugins/secrets.py b/nbdefense/plugins/secrets.py index deefce8..7209cbd 100644 --- a/nbdefense/plugins/secrets.py +++ b/nbdefense/plugins/secrets.py @@ -21,7 +21,7 @@ ) from nbdefense.notebook import Cell, OutputCellType from nbdefense.plugins.plugin import Plugin, ScanTarget -from nbdefense.settings import Settings +from nbdefense.settings import Settings, UnknownSettingsValueError logger = logging.getLogger(__name__) @@ -44,9 +44,9 @@ class RedactSecretEnum(str, Enum): `HASH` will replace the full secret with its hashed value. """ - PARTIAL = "PARTIAL" - ALL = "ALL" - HASH = "HASH" + PARTIAL = "partial" + ALL = "all" + HASH = "hash" def to_json(self) -> str: return self.name @@ -179,11 +179,15 @@ def _redact(issue: Issue, settings: SecretPluginSettings) -> None: issue.character_start_index : issue.character_end_index ] redacted_value = "******" - redact_secret = settings.get("redact_secret") + redact_secret = str(settings.get("redact_secret")).lower() if redact_secret == RedactSecretEnum.PARTIAL: redacted_value = f"{secret_value[:2]}..{secret_value[-2:]}" elif redact_secret == RedactSecretEnum.HASH: redacted_value = hashlib.md5(secret_value.encode()).hexdigest() # nosec + elif redact_secret == RedactSecretEnum.ALL: + pass + else: + raise UnknownSettingsValueError("redact_secret", redact_secret) issue.cell.scrubbed_lines[issue.line_index] = re.sub( re.escape(secret_value), diff --git a/nbdefense/settings.py b/nbdefense/settings.py index 7bd2586..9db3231 100644 --- a/nbdefense/settings.py +++ b/nbdefense/settings.py @@ -46,7 +46,20 @@ def __init__(self, setting_accessed: str, *args: object) -> None: super().__init__(*args) def __str__(self) -> str: - return f"Unknown setting: {self.setting_accessed} - {super().__str__()}" + return f"Unknown setting: {self.setting_accessed}" + + +class UnknownSettingsValueError(UnknownSettingsError): + setting_value: str + + def __init__( + self, setting_accessed: str, setting_value: str, *args: object + ) -> None: + self.setting_value = setting_value + super().__init__(setting_accessed, *args) + + def __str__(self) -> str: + return f"Setting '{self.setting_accessed}' has unknown value: '{self.setting_value}'" class SettingsUtils: