Skip to content

Commit

Permalink
chore: move common service and action models into shared models (#472)
Browse files Browse the repository at this point in the history
secureli-440
secureli-441

<!-- Include general description here -->
closes #440 #441

Moves commonly used action and service models into shared models folder

## Changes
<!-- A detailed list of changes -->
* Move action models
* Move service models
* Remove unused action+service models

## Testing
<!--
Mention updated tests and any manual testing performed.
Are aspects not yet tested or not easily testable?
Feel free to include screenshots if appropriate.
 -->
*

## Clean Code Checklist
<!-- This is here to support you. Some/most checkboxes may not apply to
your change -->
- [ ] Meets acceptance criteria for issue
- [ ] New logic is covered with automated tests
- [ ] Appropriate exception handling added
- [ ] Thoughtful logging included
- [ ] Documentation is updated
- [ ] Follow-up work is documented in TODOs
- [ ] TODOs have a ticket associated with them
- [ ] No commented-out code included


<!--
Github-flavored markdown reference:
https://docs.github.com/en/get-started/writing-on-github
-->

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Isaac Heist <[email protected]>
Co-authored-by: Jordan Heffernan <[email protected]>
Co-authored-by: github-actions <[email protected]>
Co-authored-by: Kathleen Hogan <[email protected]>
Co-authored-by: Kathleen Hogan <[email protected]>
  • Loading branch information
7 people authored Mar 7, 2024
1 parent 16e8716 commit 9bcdda5
Showing 31 changed files with 230 additions and 240 deletions.
37 changes: 7 additions & 30 deletions secureli/actions/action.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from abc import ABC
from enum import Enum
from pathlib import Path
from typing import Optional
from secureli.modules.shared.abstractions.echo import EchoAbstraction
from secureli.modules.observability.consts.logging import TELEMETRY_DEFAULT_ENDPOINT
from secureli.modules.shared.models.echo import Color
from secureli.modules.shared.models.install import VerifyOutcome, VerifyResult
from secureli.modules.shared.models.language import (
LanguageMetadata,
LanguageNotSupportedError,
)
from secureli.modules.shared.models.scan import ScanMode
from secureli.repositories.secureli_config import (
SecureliConfig,
SecureliConfigRepository,
@@ -13,43 +17,16 @@
from secureli.repositories.settings import SecureliRepository, TelemetrySettings
from secureli.modules.language_analyzer.language_analyzer_services.language_analyzer import (
LanguageAnalyzerService,
AnalyzeResult,
)
from secureli.modules.language_analyzer.language_analyzer_services.language_config import (
LanguageNotSupportedError,
)
from secureli.modules.language_analyzer.language_analyzer_services.language_support import (
LanguageMetadata,
LanguageSupportService,
)
from secureli.modules.core.core_services.scanner import ScannerService, ScanMode
from secureli.modules.core.core_services.scanner import ScannerService
from secureli.modules.core.core_services.updater import UpdaterService

import pydantic
from secureli.modules.shared.utilities.formatter import format_sentence_list


class VerifyOutcome(str, Enum):
INSTALL_CANCELED = "install-canceled"
INSTALL_FAILED = "install-failed"
INSTALL_SUCCEEDED = "install-succeeded"
UPDATE_CANCELED = "update-canceled"
UPDATE_SUCCEEDED = "update-succeeded"
UPDATE_FAILED = "update-failed"
UP_TO_DATE = "up-to-date"


class VerifyResult(pydantic.BaseModel):
"""
The outcomes of performing verification. Actions can use these results
to decide whether to proceed with their post-initialization actions or not.
"""

outcome: VerifyOutcome
config: Optional[SecureliConfig] = None
analyze_result: Optional[AnalyzeResult] = None


class ActionDependencies:
"""
Consolidates a growing set of common dependencies so Action adopters can
2 changes: 1 addition & 1 deletion secureli/actions/build.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from secureli.modules.shared.abstractions.echo import EchoAbstraction, Color
from secureli.modules.observability.observability_services.logging import (
LoggingService,
LogAction,
)
from secureli.modules.shared.models.logging import LogAction


class BuildAction:
5 changes: 3 additions & 2 deletions secureli/actions/initializer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from pathlib import Path

from secureli.actions.scan import ScanAction
from secureli.actions.action import Action, ActionDependencies, VerifyResult
from secureli.actions.action import Action, ActionDependencies
from secureli.modules.observability.observability_services.logging import (
LoggingService,
LogAction,
)
from secureli.modules.shared.models.install import VerifyResult
from secureli.modules.shared.models.logging import LogAction


class InitializerAction(Action):
7 changes: 3 additions & 4 deletions secureli/actions/scan.py
Original file line number Diff line number Diff line change
@@ -6,22 +6,21 @@

from secureli.modules.shared.abstractions.echo import EchoAbstraction
from secureli.actions.action import (
VerifyOutcome,
Action,
ActionDependencies,
VerifyResult,
)
from secureli.modules.shared.models.exit_codes import ExitCode
from secureli.modules.shared.models.install import VerifyOutcome, VerifyResult
from secureli.modules.shared.models.logging import LogAction
from secureli.modules.shared.models.publish_results import PublishResultsOption
from secureli.modules.shared.models.result import Result
from secureli.modules.observability.observability_services.logging import (
LoggingService,
LogAction,
)
from secureli.modules.core.core_services.scanner import (
ScanMode,
ScannerService,
)
from secureli.modules.shared.models.scan import ScanMode
from secureli.settings import Settings
from secureli.modules.shared.utilities.usage_stats import (
post_log,
2 changes: 1 addition & 1 deletion secureli/actions/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jinja2

from secureli.modules.language_analyzer.language_analyzer_services.language_support import (
from secureli.modules.shared.consts.language import (
supported_languages,
)

2 changes: 1 addition & 1 deletion secureli/actions/update.py
Original file line number Diff line number Diff line change
@@ -3,12 +3,12 @@
from secureli.modules.shared.abstractions.echo import EchoAbstraction
from secureli.modules.observability.observability_services.logging import (
LoggingService,
LogAction,
)
from secureli.modules.core.core_services.updater import UpdaterService
from secureli.actions.action import Action, ActionDependencies

from rich.progress import Progress
from secureli.modules.shared.models.logging import LogAction


class UpdateAction(Action):
4 changes: 2 additions & 2 deletions secureli/main.py
Original file line number Diff line number Diff line change
@@ -3,13 +3,13 @@
from typing_extensions import Annotated
import typer
from typer import Option
from secureli.actions.action import VerifyOutcome

from secureli.actions.scan import ScanMode
from secureli.actions.setup import SetupAction
from secureli.container import Container
from secureli.modules.shared.models.echo import Color
from secureli.modules.shared.models.install import VerifyOutcome
from secureli.modules.shared.models.publish_results import PublishResultsOption
from secureli.modules.shared.models.scan import ScanMode
from secureli.modules.shared.resources import read_resource
from secureli.settings import Settings
import secureli.repositories.secureli_config as SecureliConfig
34 changes: 3 additions & 31 deletions secureli/modules/core/core_services/scanner.py
Original file line number Diff line number Diff line change
@@ -6,18 +6,10 @@
import re

from secureli.modules.shared.abstractions.pre_commit import PreCommitAbstraction
from secureli.modules.shared.models.scan import ScanFailure, ScanMode, ScanResult
from secureli.repositories.settings import PreCommitSettings


class ScanMode(str, Enum):
"""
Which scan mode to run as when we perform scanning.
"""

STAGED_ONLY = "staged-only"
ALL_FILES = "all-files"


class OutputParseErrors(str, Enum):
"""
Possible errors when parsing scan output
@@ -26,32 +18,12 @@ class OutputParseErrors(str, Enum):
REPO_NOT_FOUND = "repo-not-found"


class Failure(pydantic.BaseModel):
"""
Represents the details of a failed rule from a scan
"""

repo: str
id: str
file: str


class ScanResult(pydantic.BaseModel):
"""
The results of calling scan_repo
"""

successful: bool
output: Optional[str] = None
failures: list[Failure]


class ScanOuput(pydantic.BaseModel):
"""
Represents the parsed output from a scan
"""

failures: list[Failure]
failures: list[ScanFailure]


class ScannerService:
@@ -128,7 +100,7 @@ def _parse_scan_ouput(self, folder_path: Path, output: str = "") -> ScanOuput:
files = self._find_file_names(failure_output_list=failure_output_list)

for file in files:
failures.append(Failure(id=id, file=file, repo=repo))
failures.append(ScanFailure(id=id, file=file, repo=repo))

return ScanOuput(failures=failures)

Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
from collections import defaultdict
from pathlib import Path

import pydantic

from secureli.modules.shared.abstractions.lexer_guesser import LexerGuesser
from secureli.modules.shared.models.language import AnalyzeResult, SkippedFile
from secureli.repositories.repo_files import RepoFilesRepository
from secureli.modules.language_analyzer.language_analyzer_services.language_support import (
from secureli.modules.shared.consts.language import (
supported_languages,
)


class SkippedFile(pydantic.BaseModel):
"""
A file skipped by the analysis phase.
"""

file_path: Path
error_message: str


class AnalyzeResult(pydantic.BaseModel):
"""
The result of the analysis phase.
"""

language_proportions: dict[str, float]
skipped_files: list[SkippedFile]


class LanguageAnalyzerService:
"""
Analyzes a repository's visible files to determine which language seCureLI is targeting.
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
from pathlib import Path
from typing import Callable, Any

import pydantic
from typing import Callable
import yaml

from secureli.modules.shared.models.language import (
LanguageNotSupportedError,
LanguagePreCommitResult,
LoadLinterConfigsResult,
)
from secureli.modules.shared.resources.slugify import slugify
from secureli.modules.shared.utilities.hash import hash_config
from secureli.modules.shared.utilities.patterns import combine_patterns


class LanguageNotSupportedError(Exception):
"""The given language was not supported by the PreCommitHooks abstraction"""

pass


class LoadLinterConfigsResult(pydantic.BaseModel):
"""Results from finding and loading any pre-commit configs for the language"""

successful: bool
linter_data: list[Any]


class LanguagePreCommitResult(pydantic.BaseModel):
"""
A configuration model for a supported pre-commit-configurable language.
"""

language: str
config_data: str
version: str
linter_config: LoadLinterConfigsResult


class LanguageConfigService:
def __init__(
self,
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@

import pydantic
import yaml
from secureli.modules.shared.abstractions.echo import EchoAbstraction
from secureli.modules.shared.models.config import HookConfiguration, LinterConfig, Repo
from secureli.modules.shared.models.language import LanguageMetadata

import secureli.repositories.secureli_config as SecureliConfig
from secureli.modules.shared.abstractions.pre_commit import PreCommitAbstraction
@@ -16,70 +17,6 @@
)
from secureli.modules.shared.utilities.hash import hash_config

supported_languages = [
"C#",
"Python",
"Java",
"Terraform",
"TypeScript",
"JavaScript",
"Go",
"Swift",
"Kotlin",
]


class LanguageMetadata(pydantic.BaseModel):
version: str
security_hook_id: Optional[str]
linter_config_write_errors: Optional[list[str]] = []


class ValidateConfigResult(pydantic.BaseModel):
"""
The results of calling validate_config
"""

successful: bool
output: str


class Repo(pydantic.BaseModel):
"""A repository containing pre-commit hooks"""

repo: str
revision: str
hooks: list[str]


class HookConfiguration(pydantic.BaseModel):
"""A simplified pre-commit configuration representation for logging purposes"""

repos: list[Repo]


class UnexpectedReposResult(pydantic.BaseModel):
"""
The result of checking for unexpected repos in config
"""

missing_repos: Optional[list[str]] = []
unexpected_repos: Optional[list[str]] = []


class LinterConfigData(pydantic.BaseModel):
"""
Represents the structure of a linter config file
"""

filename: str
settings: Any


class LinterConfig(pydantic.BaseModel):
language: str
linter_data: list[LinterConfigData]


class BuildConfigResult(pydantic.BaseModel):
"""Result about building config for all laguages"""
Loading

0 comments on commit 9bcdda5

Please sign in to comment.