|
| 1 | +import dataclasses |
| 2 | +from pathlib import Path |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from requests import Response |
| 6 | + |
| 7 | +from cycode.cli.cli_types import BusinessImpactOption |
| 8 | +from cycode.cli.exceptions.custom_exceptions import RequestHttpError |
| 9 | +from cycode.cyclient import models |
| 10 | +from cycode.cyclient.cycode_client_base import CycodeClientBase |
| 11 | + |
| 12 | + |
| 13 | +@dataclasses.dataclass |
| 14 | +class ImportSbomParameters: |
| 15 | + Name: str |
| 16 | + Vendor: str |
| 17 | + BusinessImpact: BusinessImpactOption |
| 18 | + Labels: Optional[list[str]] |
| 19 | + Owners: Optional[list[str]] |
| 20 | + |
| 21 | + def _owners_to_ids(self) -> list[str]: |
| 22 | + return [] |
| 23 | + |
| 24 | + def to_request_form(self) -> dict: |
| 25 | + form_data = {} |
| 26 | + for field in dataclasses.fields(self): |
| 27 | + key = field.name |
| 28 | + val = getattr(self, key) |
| 29 | + if val is None or len(val) == 0: |
| 30 | + continue |
| 31 | + if isinstance(val, list): |
| 32 | + form_data[f'{key}[]'] = val |
| 33 | + else: |
| 34 | + form_data[key] = val |
| 35 | + return form_data |
| 36 | + |
| 37 | + |
| 38 | +class ImportSbomClient: |
| 39 | + IMPORT_SBOM_REQUEST_PATH: str = 'v4/sbom/import' |
| 40 | + GET_USER_ID_REQUEST_PATH: str = 'v4/members' |
| 41 | + |
| 42 | + def __init__(self, client: CycodeClientBase) -> None: |
| 43 | + self.client = client |
| 44 | + |
| 45 | + def request_sbom_import_execution(self, params: ImportSbomParameters, file_path: Path) -> None: |
| 46 | + if params.Owners: |
| 47 | + owners_ids = self.get_owners_user_ids(params.Owners) |
| 48 | + params.Owners = owners_ids |
| 49 | + |
| 50 | + form_data = params.to_request_form() |
| 51 | + |
| 52 | + with open(file_path.absolute(), 'rb') as f: |
| 53 | + request_args = { |
| 54 | + 'url_path': self.IMPORT_SBOM_REQUEST_PATH, |
| 55 | + 'data': form_data, |
| 56 | + 'files': {'File': f}, |
| 57 | + } |
| 58 | + |
| 59 | + response = self.client.post(**request_args) |
| 60 | + |
| 61 | + if response.status_code != 201: |
| 62 | + raise RequestHttpError(response.status_code, response.text, response) |
| 63 | + |
| 64 | + def get_owners_user_ids(self, owners: list[str]) -> list[str]: |
| 65 | + return [self._get_user_id_by_email(owner) for owner in owners] |
| 66 | + |
| 67 | + def _get_user_id_by_email(self, email: str) -> str: |
| 68 | + request_args = {'url_path': self.GET_USER_ID_REQUEST_PATH, 'params': {'email': email}} |
| 69 | + |
| 70 | + response = self.client.get(**request_args) |
| 71 | + member_details = self.parse_requested_member_details_response(response) |
| 72 | + |
| 73 | + if not member_details.items: |
| 74 | + raise Exception( |
| 75 | + f"Failed to find user with email '{email}'. Verify this email is registered to Cycode platform" |
| 76 | + ) |
| 77 | + return member_details.items.pop(0).external_id |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def parse_requested_member_details_response(response: Response) -> models.MemberDetails: |
| 81 | + return models.RequestedMemberDetailsResultSchema().load(response.json()) |
0 commit comments