Skip to content

Commit

Permalink
Merge pull request #1 from schubergphilis/fix-dependencies-exceptions
Browse files Browse the repository at this point in the history
Fix dependencies, exceptions and extend the default interface.
  • Loading branch information
marwinbaumannsbp authored Dec 6, 2023
2 parents 4ae7930 + c6019c8 commit 988cb26
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 42 deletions.
4 changes: 4 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ typing-extensions = ">=4.0,<5.0"


[packages]

boto3 = ">=1.19.8,<2.0"
opnieuw = ">=1.1.0,<2.0"
python-dateutil = ">=2.8.2,<3.0"
97 changes: 86 additions & 11 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 61 additions & 30 deletions awsfindingsmanagerlib/awsfindingsmanagerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
"""

import logging
from copy import deepcopy
from datetime import datetime

import boto3
import botocore.errorfactory
import botocore.exceptions
from botocore.config import Config
from dateutil.parser import parse
from opnieuw import retry

from .awsfindingsmanagerlibexceptions import (InvalidRegion,
NoRegion,
InvalidOrNoCredentials)
from .configuration import (AWS_FOUNDATIONAL_SECURITY_FRAMEWORK,
CIS_AWS_FOUNDATION_FRAMEWORK,
PCI_DSS_FRAMEWORK, DEFAULT_SECURITY_HUB_FILTER)
from .validations import validate_allowed_denied_regions, validate_allowed_denied_account_ids

__author__ = '''Marwin Baumann <[email protected]>'''
__docformat__ = '''google'''
Expand All @@ -37,25 +54,11 @@
__email__ = '''<[email protected]>,<[email protected]>,<[email protected]>'''
__status__ = '''Development''' # "Prototype", "Development", "Production".


# This is the main prefix used for logging
LOGGER_BASENAME = '''awsfindingsmanagerlib'''
LOGGER = logging.getLogger(LOGGER_BASENAME)
LOGGER.addHandler(logging.NullHandler())

class FindingsManager:

def get_findings(self):
pass

def get_findings_by_rule_id(self):
pass

def get_findings_by_control_id(self):
pass

def get_findings_by_tag(self):
pass

class Finding:
"""Models a finding."""
Expand Down Expand Up @@ -393,9 +396,7 @@ def _calculate_account_id_filter(allowed_account_ids, denied_account_ids):
@staticmethod
def calculate_query_filter(query_filter=DEFAULT_SECURITY_HUB_FILTER,
allowed_account_ids=None,
denied_account_ids=None,
#frameworks=DEFAULT_SECURITY_HUB_FRAMEWORKS
):
denied_account_ids=None):
"""Calculates a Security Hub compatible filter for retrieving findings.
Depending on arguments provided for allow list, deny list and frameworks to retrieve a query is constructed to
Expand All @@ -405,8 +406,6 @@ def calculate_query_filter(query_filter=DEFAULT_SECURITY_HUB_FILTER,
query_filter: The default filter if no filter is provided.
allowed_account_ids: The allow list of account ids to get the findings for.
denied_account_ids: The deny list of account ids to filter out findings for.
frameworks: The default frameworks if no frameworks are provided.
Returns:
query_filter (dict): The query filter calculated based on the provided arguments.
Expand All @@ -417,24 +416,56 @@ def calculate_query_filter(query_filter=DEFAULT_SECURITY_HUB_FILTER,
if aws_account_ids:
query_filter.update({'AwsAccountId': aws_account_ids})
return query_filter

def get_findings(self, query_filter):
"""Retrieves findings from security hub based on a provided query.

Args:
query_filter (dict): The query filter to execute on security hub to get the findings.
def get_findings(self):
"""Retrieves findings from security hub based on a default query.
Returns:
findings (list): A list of findings from security hub.
"""

query_filter = DEFAULT_SECURITY_HUB_FILTER
return self._get_findings(query_filter)

def get_findings_by_rule_id(self):
pass

"""Retrieves findings from security hub based on a provided query that filters by rule id.
Returns:
findings (list): A list of findings from security hub.
"""
query_filter = {} # fix for rule id
return self._get_findings(query_filter)

def get_findings_by_control_id(self):
pass

"""Retrieves findings from security hub based on a provided query that filters by control id.
Returns:
findings (list): A list of findings from security hub.
"""
query_filter = {} # fix for control id
return self._get_findings(query_filter)

def get_findings_by_tag(self):
pass
"""Retrieves findings from security hub based on a provided query that filters by tag.
Returns:
findings (list): A list of findings from security hub.
"""
query_filter = {} # fix for tag
return self._get_findings(query_filter)

def suppress_findings(self):
"""Suppresses findings from security hub based on a default query."""

def suppress_findings_by_rule_id(self):
"""Suppresses findings from security hub based on a query by rule id."""

def suppress_findings_by_control_id(self):
"""Suppresses findings from security hub based on a query by control id."""

def suppress_findings_by_tag(self):
"""Suppresses findings from security hub based on a query by tag."""
28 changes: 28 additions & 0 deletions awsfindingsmanagerlib/awsfindingsmanagerlibexceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,31 @@
__maintainer__ = '''Ben van Breukelen, Costas Tyfoxylos, Marwin Baumann'''
__email__ = '''<[email protected]>,<[email protected]>,<[email protected]>'''
__status__ = '''Development''' # "Prototype", "Development", "Production".


class InvalidAccountListProvided(Exception):
"""The list of accounts provided are not valid AWS accounts."""


class InvalidRegionListProvided(Exception):
"""The list of regions provided are not valid AWS regions."""


class MutuallyExclusiveArguments(Exception):
"""The arguments provided are mutually exclusive and only one of the should be provided."""


class InvalidOrNoCredentials(Exception):
"""Invalid or no credentials were provided from the environment."""


class NoRegion(Exception):
"""No region is set on the environment or provided to the library."""


class InvalidRegion(Exception):
"""The region provided is not valid."""


class UnableToRetrieveSecurityHubRegions(Exception):
"""Could not retrieve the regions security hub is active in."""
Loading

0 comments on commit 988cb26

Please sign in to comment.