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

Remove Case Sensitivity for Enum Settings #18

Merged
merged 2 commits into from
Mar 31, 2023
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
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