From cdf7329dd6da9e5753b286a07525ab91c7cd0cd2 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 9 Dec 2024 15:39:20 +0000 Subject: [PATCH] feat: optimized location setting --- plugins/gcp/fix_plugin_gcp/resources/base.py | 27 ++++++++++---------- plugins/gcp/fix_plugin_gcp/resources/scc.py | 4 +-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/plugins/gcp/fix_plugin_gcp/resources/base.py b/plugins/gcp/fix_plugin_gcp/resources/base.py index f4274f619..4f0ebe9a2 100644 --- a/plugins/gcp/fix_plugin_gcp/resources/base.py +++ b/plugins/gcp/fix_plugin_gcp/resources/base.py @@ -213,8 +213,8 @@ def add_region_to_node(self, node: GcpResourceType, source: Optional[Json] = Non def set_zone_or_region(location_name: str) -> bool: return set_zone(location_name) or set_region(location_name) - def set_zone(location_name: str) -> bool: - if zone := self.zone_by_name.get(location_name): + def set_zone(zone_name: str) -> bool: + if zone := self.zone_by_name.get(zone_name): node._zone = zone node._region = self.region_by_zone_name.get(zone.id) self.add_edge(zone, node=node) @@ -222,29 +222,22 @@ def set_zone(location_name: str) -> bool: else: log.debug( "Zone property '%s' found in the source but no corresponding region object is available to associate with the node.", - location_name, + zone_name, ) return False - def set_region(location_name: str) -> bool: - if region := self.region_by_name.get(location_name): + def set_region(region_name: str) -> bool: + if region := self.region_by_name.get(region_name): node._region = region self.add_edge(node, node=region, reverse=True) return True else: log.debug( "Region property '%s' found in the source but no corresponding region object is available to associate with the node.", - location_name, + region_name, ) return False - parts = node.id.split("/", maxsplit=4) - if len(parts) > 3 and parts[0] == "projects": - if parts[2] in ["locations", "zones", "regions"]: - location_name = parts[3].lower() - if set_zone_or_region(location_name): - return - if source is not None: if ZoneProp in source: zone_name = source[ZoneProp].lower().rsplit("/", 1)[-1] @@ -260,10 +253,18 @@ def set_region(location_name: str) -> bool: region_name = source[RegionProp].lower().rsplit("/", 1)[-1] if set_region(region_name): return + # location property can be a zone or region if LocationProp in source: location_name = source[LocationProp].lower().rsplit("/", 1)[-1] if set_zone_or_region(location_name): return + + parts = node.id.split("/", maxsplit=4) + if len(parts) > 3 and parts[0] == "projects": + if parts[2] in ["locations", "zones", "regions"]: + location_name = parts[3].lower() + if set_zone_or_region(location_name): + return # Fallback to GraphBuilder region, i.e. regional collection if self.region is not None: diff --git a/plugins/gcp/fix_plugin_gcp/resources/scc.py b/plugins/gcp/fix_plugin_gcp/resources/scc.py index 7ea27a21d..1721851ef 100644 --- a/plugins/gcp/fix_plugin_gcp/resources/scc.py +++ b/plugins/gcp/fix_plugin_gcp/resources/scc.py @@ -14,7 +14,7 @@ GcpProject, GcpExpectedErrorCodes, ) -from fixlib.baseresources import SEVERITY_MAPPING, Finding, Severity, PhantomBaseResource +from fixlib.baseresources import SEVERITY_MAPPING, Finding, Severity from fixlib.json_bender import Bender, S, Bend from fixlib.types import Json @@ -122,7 +122,7 @@ class GcpFindingResource: @define(eq=False, slots=False) -class GcpSccFinding(GcpResource, PhantomBaseResource): +class GcpSccFinding(GcpResource): kind: ClassVar[str] = "gcp_scc_finding" _model_export: ClassVar[bool] = False api_spec: ClassVar[GcpApiSpec] = GcpApiSpec(