Skip to content

Commit

Permalink
Remove Case Sensitivity for Enum Settings (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherbel authored Mar 31, 2023
1 parent 1e1fe6e commit d9cd661
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
23 changes: 11 additions & 12 deletions nbdefense/plugins/licenses/license_plugin_settings.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
14 changes: 9 additions & 5 deletions nbdefense/plugins/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand Down
15 changes: 14 additions & 1 deletion nbdefense/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d9cd661

Please sign in to comment.