|
| 1 | +import dataclasses |
| 2 | +from typing import List, Optional |
| 3 | +from pathlib import Path |
| 4 | +from requests import Response |
| 5 | +from urllib.parse import quote |
| 6 | + |
| 7 | +from cycode.cli.cli_types import BusinessImpactOption |
| 8 | +from cycode.cli.exceptions.custom_exceptions import CycodeError, RequestHttpError |
| 9 | +from cycode.cyclient.cycode_client_base import CycodeClientBase |
| 10 | +from cycode.cyclient import models |
| 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 | + request_args = { |
| 53 | + 'url_path': self.IMPORT_SBOM_REQUEST_PATH, |
| 54 | + 'data': form_data, |
| 55 | + 'files': { |
| 56 | + 'File': open(file_path.absolute(),'rb') |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + response = self.client.post(**request_args) |
| 61 | + |
| 62 | + if response.status_code != 201: |
| 63 | + raise RequestHttpError(response.status_code, response.text,response) |
| 64 | + |
| 65 | + def get_owners_user_ids(self,owners: List[str]) -> List[str]: |
| 66 | + return [ self._get_user_id_by_email(owner) for owner in owners] |
| 67 | + |
| 68 | + def _get_user_id_by_email(self,email: str) -> str: |
| 69 | + |
| 70 | + request_args = { |
| 71 | + 'url_path': self.GET_USER_ID_REQUEST_PATH, |
| 72 | + 'params': { |
| 73 | + 'email': email |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + response = self.client.get(**request_args) |
| 78 | + member_details = self.parse_requested_member_details_response(response) |
| 79 | + |
| 80 | + if not member_details.items: |
| 81 | + raise Exception(f"Failed to find user with email '{email}'. Verify this email is registered to Cycode platform") |
| 82 | + return member_details.items.pop(0).external_id |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def parse_requested_member_details_response(response: Response) -> models.MemberDetails: |
| 86 | + return models.RequestedMemberDetailsResultSchema().load(response.json()) |
| 87 | + |
| 88 | + |
| 89 | + |
0 commit comments