Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fixlib][feat] Define separate assessment section #2257

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions fixlib/fixlib/baseresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
import hashlib
import weakref
from abc import ABC, abstractmethod
from collections import defaultdict
from copy import deepcopy
from datetime import datetime, timezone, timedelta
from datetime import datetime, timedelta
from enum import Enum, StrEnum, unique
from functools import wraps, cached_property
from typing import Dict, Iterator, List, ClassVar, Optional, TypedDict, Any, TypeVar, Type, Callable, Set, Tuple
from collections import defaultdict

from attr import resolve_types
from attrs import define, field, Factory, frozen, evolve
from prometheus_client import Counter, Summary

from fixlib.basecategories import Category
from fixlib.json import from_json as _from_json, to_json as _to_json, to_json_str
from fixlib.logger import log
from fixlib.types import Json
from fixlib.utils import make_valid_timestamp, utc_str, utc
from fixlib.basecategories import Category


metrics_resource_pre_cleanup_exceptions = Counter(
"resource_pre_cleanup_exceptions_total",
Expand Down Expand Up @@ -246,6 +245,32 @@ def __str__(self) -> str:
MetricNameWithUnit = str


class Severity(StrEnum):
info = "info"
low = "low"
medium = "medium"
high = "high"
critical = "critical"


@define(slots=True)
class Finding:
title: str
severity: Severity = Severity.medium
description: Optional[str] = None
remediation: Optional[str] = None
created_at: Optional[datetime] = None
details: Optional[Json] = None


@define(slots=True)
class Assessment:
# The provider of the security assessment
provider: str
# All findings of the security provider to this resource
findings: List[Finding] = field(factory=list)


@define(eq=False, slots=False, kw_only=True)
class BaseResource(ABC):
"""
Expand Down Expand Up @@ -305,6 +330,8 @@ class BaseResource(ABC):
_resource_usage: Dict[MetricNameWithUnit, Dict[str, float]] = field(factory=lambda: defaultdict(dict))
# Deep link into the cloud provider's console
_provider_link: Optional[str] = None
# Assessment details for this resource: multiple providers can append their findings
_assessments: List[Assessment] = field(factory=list)

ctime: Optional[datetime] = field(
default=None,
Expand Down
6 changes: 5 additions & 1 deletion fixlib/fixlib/core/model_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from attrs import Attribute

from fixlib.baseresources import BaseResource
from fixlib.json import from_json
from fixlib.json import from_json, to_json
from fixlib.types import Json
from fixlib.utils import type_str

Expand Down Expand Up @@ -85,6 +85,8 @@ def check(to_check: type) -> None:
for subclass in clazz.__subclasses__():
check(subclass)
for field in attrs.fields(clazz):
if field.name.startswith("_"): # ignore private properties
continue
check(field.type)
elif is_enum(clazz):
all_classes.add(clazz)
Expand Down Expand Up @@ -349,6 +351,8 @@ def node_to_dict(node: BaseResource, changes_only: bool = False, include_revisio
metadata["protected"] = True
if link := node._provider_link:
metadata["provider_link"] = link
if assessments := node._assessments:
metadata["assessments"] = to_json(assessments)

node_dict["reported"] = get_node_attributes(node)
node_dict["metadata"] = metadata
Expand Down
Loading