| 14 | |
| 15 | @dataclass |
| 16 | class ScanContext: |
| 17 | scan_mode: Union[ScanMode, str] |
| 18 | command_path: str |
| 19 | extra_headers: Optional[Dict[str, str]] = None |
| 20 | target_path: Optional[Path] = None |
| 21 | |
| 22 | def __post_init__(self) -> None: |
| 23 | self.command_id = str(uuid.uuid4()) |
| 24 | self.os_name, self.os_version = get_os_info() |
| 25 | self.python_version = platform.python_version() |
| 26 | |
| 27 | def _get_repository_url( |
| 28 | self, |
| 29 | ) -> Optional[str]: |
| 30 | repo_url = None |
| 31 | if self.scan_mode == ScanMode.CI: |
| 32 | repo_url = get_repository_url_from_ci() |
| 33 | if repo_url is None and self.target_path is not None: |
| 34 | repo_url = get_repository_url_from_path(self.target_path) |
| 35 | return repo_url |
| 36 | |
| 37 | def get_http_headers(self) -> Dict[str, str]: |
| 38 | """ |
| 39 | Returns the extra headers to send in HTTP requests. |
| 40 | If `command_id` is not None, a `GGShield-Command-Id` header will be sent. |
| 41 | Adds the "GGShield-" prefix to the header's names. |
| 42 | """ |
| 43 | headers = { |
| 44 | "Version": __version__, |
| 45 | "Command-Path": self.command_path, |
| 46 | "Command-Id": self.command_id, |
| 47 | "OS-Name": self.os_name, |
| 48 | "OS-Version": self.os_version, |
| 49 | "Python-Version": self.python_version, |
| 50 | } |
| 51 | repo_url = self._get_repository_url() |
| 52 | if repo_url is not None: |
| 53 | headers["Repository-URL"] = repo_url |
| 54 | if self.extra_headers: |
| 55 | headers = {**headers, **self.extra_headers} |
| 56 | |
| 57 | return { |
| 58 | **{f"GGShield-{key}": str(value) for key, value in headers.items()}, |
| 59 | "mode": ( |
| 60 | self.scan_mode.value |
| 61 | if isinstance(self.scan_mode, ScanMode) |
| 62 | else self.scan_mode |
| 63 | ), |
| 64 | } |
no outgoing calls