|
| 1 | +from .element_handler import CxOneConfigElementHandler |
| 2 | +from ...exceptions import ConfigurationException, CommunicationException |
| 3 | +from ...low.misc import retrieve_preset_list |
| 4 | +from ..util import json_on_ok |
| 5 | +from typing import Any, Type, List, Awaitable, Union |
| 6 | +from .value_types import ScanLanguageMode, RestListEnum |
| 7 | +import asyncio |
| 8 | + |
| 9 | +class BaseConfigurationCategory: |
| 10 | + def __init__(self, container : CxOneConfigElementHandler): |
| 11 | + self.__config = container |
| 12 | + |
| 13 | + @property |
| 14 | + def _config(self) -> CxOneConfigElementHandler: |
| 15 | + return self.__config |
| 16 | + |
| 17 | + |
| 18 | +class ConfigurationPropertyHandler: |
| 19 | + |
| 20 | + def __init__(self, handler : CxOneConfigElementHandler, prop_key : str, value_type : Union[Type, RestListEnum]): |
| 21 | + self.__handler = handler |
| 22 | + self.__key = prop_key |
| 23 | + self.__value_type = value_type |
| 24 | + |
| 25 | + def __convert_from(self, value : Any) -> Any: |
| 26 | + if isinstance(self.__value_type, RestListEnum): |
| 27 | + return str(value) |
| 28 | + |
| 29 | + if self.__value_type is bool: |
| 30 | + if isinstance(value, str): |
| 31 | + return (not value == '') or bool(value) |
| 32 | + else: |
| 33 | + return bool(value) |
| 34 | + else: |
| 35 | + return value |
| 36 | + |
| 37 | + def __convert_to(self, value : Any) -> Any: |
| 38 | + if self.__value_type is bool: |
| 39 | + return str(bool(value)).lower() |
| 40 | + |
| 41 | + if self.__value_type is str and not isinstance(value, str): |
| 42 | + return str(value) |
| 43 | + |
| 44 | + return value |
| 45 | + |
| 46 | + |
| 47 | + async def setValue(self, value : Any) -> None: |
| 48 | + if isinstance(self.__value_type, RestListEnum): |
| 49 | + if not await self.__value_type.is_valid(value): |
| 50 | + raise ConfigurationException.not_in_enum(value, await self.__value_type.get_enum()) |
| 51 | + elif not isinstance(value, self.__value_type) and \ |
| 52 | + not isinstance(self.__value_type(value), self.__value_type): |
| 53 | + raise ConfigurationException.wrong_type(value, self.__value_type) |
| 54 | + |
| 55 | + await self.__handler._set_value(self.__key, self.__convert_to(value)) |
| 56 | + |
| 57 | + async def getValue(self) -> Any: |
| 58 | + return self.__convert_from(await self.__handler._get_value(self.__key)) |
| 59 | + |
| 60 | + async def setOverride(self, value : bool) -> None: |
| 61 | + await self.__handler._set_override(self.__key, value) |
| 62 | + |
| 63 | + async def getOverride(self) -> bool: |
| 64 | + return await self.__handler._get_override(self.__key) |
| 65 | + |
| 66 | + async def reset(self) -> None: |
| 67 | + await self.__handler._delete_config(self.__key) |
| 68 | + |
| 69 | + |
| 70 | +class SastPresetType(RestListEnum): |
| 71 | + |
| 72 | + def __init__(self, retrieve_lambda : Awaitable[List[str]]): |
| 73 | + super().__init__() |
| 74 | + self.__get_lambda = retrieve_lambda |
| 75 | + self.__lock = asyncio.Lock() |
| 76 | + self.__enum = None |
| 77 | + |
| 78 | + async def get_enum(self) -> List[str]: |
| 79 | + async with self.__lock: |
| 80 | + if self.__enum == None: |
| 81 | + self.__enum = await self.__get_lambda() |
| 82 | + |
| 83 | + return self.__enum |
| 84 | + |
| 85 | + |
| 86 | +class SastScanConfig(BaseConfigurationCategory): |
| 87 | + def __init__(self, container : CxOneConfigElementHandler): |
| 88 | + super().__init__(container) |
| 89 | + self.__fastscan = ConfigurationPropertyHandler(self._config, "scan.config.sast.fastScanMode", bool) |
| 90 | + self.__exclusions = ConfigurationPropertyHandler(self._config, "scan.config.sast.filter", str) |
| 91 | + self.__incremental = ConfigurationPropertyHandler(self._config, "scan.config.sast.incremental", bool) |
| 92 | + self.__rec = ConfigurationPropertyHandler(self._config, "scan.config.sast.recommendedExclusions", bool) |
| 93 | + self.__verbose = ConfigurationPropertyHandler(self._config, "scan.config.sast.engineVerbose", bool) |
| 94 | + self.__mode = ConfigurationPropertyHandler(self._config, "scan.config.sast.languageMode", ScanLanguageMode) |
| 95 | + |
| 96 | + async def get_presets() -> List[str]: |
| 97 | + pl_resp = await retrieve_preset_list(self._config.client) |
| 98 | + if not pl_resp.ok: |
| 99 | + raise CommunicationException(pl_resp) |
| 100 | + |
| 101 | + pl = json_on_ok(pl_resp) |
| 102 | + |
| 103 | + return [x['name'] for x in pl] |
| 104 | + |
| 105 | + self.__preset = ConfigurationPropertyHandler(self._config, "scan.config.sast.presetName", |
| 106 | + SastPresetType(get_presets)) |
| 107 | + |
| 108 | + @property |
| 109 | + def FastScan(self) -> ConfigurationPropertyHandler: |
| 110 | + return self.__fastscan |
| 111 | + |
| 112 | + @property |
| 113 | + def Exclusions(self) -> ConfigurationPropertyHandler: |
| 114 | + return self.__exclusions |
| 115 | + |
| 116 | + @property |
| 117 | + def IncrementalScan(self) -> ConfigurationPropertyHandler: |
| 118 | + return self.__incremental |
| 119 | + |
| 120 | + @property |
| 121 | + def Preset(self) -> ConfigurationPropertyHandler: |
| 122 | + return self.__preset |
| 123 | + |
| 124 | + @property |
| 125 | + def RecommendedExclusions(self) -> ConfigurationPropertyHandler: |
| 126 | + return self.__rec |
| 127 | + |
| 128 | + @property |
| 129 | + def EngineVerbose(self) -> ConfigurationPropertyHandler: |
| 130 | + return self.__verbose |
| 131 | + |
| 132 | + @property |
| 133 | + def LanguageMode(self) -> ConfigurationPropertyHandler: |
| 134 | + return self.__mode |
| 135 | + |
| 136 | +class ScaScanConfig(BaseConfigurationCategory): |
| 137 | + def __init__(self, container : CxOneConfigElementHandler): |
| 138 | + super().__init__(container) |
| 139 | + self.__exclusions = ConfigurationPropertyHandler(self._config, "scan.config.sca.filter", str) |
| 140 | + self.__obs_pattern = ConfigurationPropertyHandler(self._config, "scan.config.sca.obfuscatePackagesPattern", str) |
| 141 | + self.__exp_path = ConfigurationPropertyHandler(self._config, "scan.config.sca.ExploitablePath", bool) |
| 142 | + self.__exp_path_days = ConfigurationPropertyHandler(self._config, "scan.config.sca.LastSastScanTime", str) |
| 143 | + |
| 144 | + @property |
| 145 | + def Exclusions(self) -> ConfigurationPropertyHandler: |
| 146 | + return self.__exclusions |
| 147 | + |
| 148 | + @property |
| 149 | + def ObfuscatePackagesPattern(self) -> ConfigurationPropertyHandler: |
| 150 | + return self.__obs_pattern |
| 151 | + |
| 152 | + @property |
| 153 | + def ExploitablePath(self) -> ConfigurationPropertyHandler: |
| 154 | + return self.__exp_path |
| 155 | + |
| 156 | + @property |
| 157 | + def ExploitablePathLastScanDays(self) -> ConfigurationPropertyHandler: |
| 158 | + return self.__exp_path_days |
| 159 | + |
| 160 | +class ApiSecurityScanConfig(BaseConfigurationCategory): |
| 161 | + def __init__(self, container : CxOneConfigElementHandler): |
| 162 | + super().__init__(container) |
| 163 | + self.__exclusions = ConfigurationPropertyHandler(self._config, "scan.config.apisec.swaggerFilter", str) |
| 164 | + |
| 165 | + @property |
| 166 | + def SwaggerFilter(self) -> ConfigurationPropertyHandler: |
| 167 | + return self.__exclusions |
| 168 | + |
| 169 | +class ContainerScanConfig(BaseConfigurationCategory): |
| 170 | + def __init__(self, container : CxOneConfigElementHandler): |
| 171 | + super().__init__(container) |
| 172 | + self.__exclusions = ConfigurationPropertyHandler(self._config, "scan.config.containers.filesFilter", str) |
| 173 | + self.__package_filter = ConfigurationPropertyHandler(self._config, "scan.config.containers.packagesFilter", str) |
| 174 | + self.__img_tag_filter = ConfigurationPropertyHandler(self._config, "scan.config.containers.imagesFilter", str) |
| 175 | + self.__exclude_non_final = ConfigurationPropertyHandler(self._config, "scan.config.containers.nonFinalStagesFilter", bool) |
| 176 | + |
| 177 | + @property |
| 178 | + def Exclusions(self) -> ConfigurationPropertyHandler: |
| 179 | + return self.__exclusions |
| 180 | + |
| 181 | + @property |
| 182 | + def ImageTagFilter(self) -> ConfigurationPropertyHandler: |
| 183 | + return self.__img_tag_filter |
| 184 | + |
| 185 | + @property |
| 186 | + def PrivatePackageFilter(self) -> ConfigurationPropertyHandler: |
| 187 | + return self.__package_filter |
| 188 | + |
| 189 | + @property |
| 190 | + def ExclusionNonFinalStagesFilter(self) -> ConfigurationPropertyHandler: |
| 191 | + return self.__exclude_non_final |
0 commit comments