Skip to content

Commit

Permalink
Version 0.5.0: New Filtering and General Version Bumps (#113)
Browse files Browse the repository at this point in the history
* Bumps dependencies, fixes up code styling, and migrates to the new caracara-filters library.

* Changes version to 0.5.0 and bumps dependencies
  • Loading branch information
ChristopherHammond13 authored Aug 21, 2023
1 parent 038e4a1 commit b3c257d
Show file tree
Hide file tree
Showing 45 changed files with 578 additions and 1,682 deletions.
34 changes: 0 additions & 34 deletions .github/workflows/bandit.yml

This file was deleted.

44 changes: 44 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Caracara Code Quality
on:
push:
paths:
- '**.py'
- '**.yml'
branches:
- main
- 'ver_*'
tags:
- 'v*'
pull_request:

jobs:
codequality:
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Poetry via pipx
run: pipx install poetry
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'poetry'
- name: Install dependencies
run: poetry install
- name: Lint package source with flake8
run: poetry run flake8 caracara/ --show-source --statistics
- name: Lint package source with pylint
if: success() || failure()
run: poetry run pylint caracara/
- name: Lint package docstrings and comments with pydocstyle
if: success() || failure()
run: poetry run pydocstyle caracara/
- name: Analyse code for security issues with bandit
if: success() || failure()
run: |
poetry run bandit -r caracara --configfile .bandit
poetry run bandit -r examples --configfile examples/.bandit
poetry run bandit -r tests --configfile tests/.bandit
35 changes: 0 additions & 35 deletions .github/workflows/dev-deploy.yml

This file was deleted.

34 changes: 0 additions & 34 deletions .github/workflows/flake8.yml

This file was deleted.

34 changes: 0 additions & 34 deletions .github/workflows/pydocstyle.yml

This file was deleted.

33 changes: 0 additions & 33 deletions .github/workflows/pylint.yml

This file was deleted.

12 changes: 0 additions & 12 deletions .github/workflows/spelling.yml.disabled

This file was deleted.

34 changes: 8 additions & 26 deletions caracara/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
except ImportError as no_falconpy:
raise SystemExit("The crowdstrike-falconpy library is not installed.") from no_falconpy

from caracara.common import FILTER_ATTRIBUTES as common_filter_attributes
from caracara_filters import FQLGenerator
from caracara.common.interpolation import VariableInterpolator
from caracara.common.meta import user_agent_string
from caracara.filters.falcon_filter import FalconFilter
from caracara.filters.fql import FalconFilterAttribute
from caracara.modules import (
CustomIoaApiModule,
FlightControlApiModule,
Expand All @@ -34,7 +32,6 @@
ResponsePoliciesApiModule,
RTRApiModule,
UsersApiModule,
MODULE_FILTER_ATTRIBUTES,
)


Expand All @@ -45,12 +42,11 @@ class Client:
the FalconPy library.
"""

class FalconFilter(FalconFilter):
class FalconFilter(FQLGenerator):
"""Falcon Filter wrapper class.
Create a sub-class of the FalconFilter class which we own locally within
the client. This allows us to dynamically calculate the FQL filter attributes
that we hava available and make them available at runtime.
FalconFilter has now been replaced with the externally provided FQLGenerator functionality.
This subclass allows pre-existing code to continue referencing FalconFilter.
"""

def __init__( # pylint: disable=R0913,R0914,R0915
Expand All @@ -70,16 +66,16 @@ def __init__( # pylint: disable=R0913,R0914,R0915
self.logger = logging.getLogger(__name__)

if client_id is None and client_secret is None and falconpy_authobject is None:
raise Exception(
raise ValueError(
"You must provide either a Client ID and Client Secret, "
"or a pre-created FalconPy OAuth2 object"
)

if client_id is not None and client_secret is None:
raise Exception("You cannot provide a Client ID without a Client Secret")
raise ValueError("You cannot provide a Client ID without a Client Secret")

if client_id is not None and falconpy_authobject is not None:
raise Exception(
raise ValueError(
"Please provide either a Client ID/Client Secret pair, "
"or a pre-created FalconPy OAuth2 object, but not both"
)
Expand Down Expand Up @@ -143,7 +139,7 @@ def __init__( # pylint: disable=R0913,R0914,R0915
)
self.api_authentication = falconpy_authobject
else:
raise Exception("Impossible authentication scenario")
raise TypeError("Impossible authentication scenario")

self.logger.info("Requesting API token")
self.api_authentication.token() # Need to force the authentication to resolve the base_url
Expand All @@ -165,20 +161,6 @@ def __init__( # pylint: disable=R0913,R0914,R0915
self.logger.debug("Setting up the Users module")
self.users = UsersApiModule(self.api_authentication)

self.logger.debug("Configuring FQL filters")
# Pre-configure the FQL modules for faster instantiation later
filter_attribute_classes = [
*common_filter_attributes,
*MODULE_FILTER_ATTRIBUTES,
]
available_filters = {}
for filter_attribute_class in filter_attribute_classes:
instance: FalconFilterAttribute = filter_attribute_class()
available_filters[instance.name] = filter_attribute_class

# Modify the falcon filter class to contain the available filters
self.FalconFilter.available_filters = available_filters

self.logger.info("Caracara client configured")

def __enter__(self):
Expand Down
18 changes: 9 additions & 9 deletions caracara/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Caracara: Common functions and data imports."""
from caracara.common.common_filters import FILTER_ATTRIBUTES
from caracara.common.constants import FILTER_OPERATORS, SCROLL_BATCH_SIZE, DATA_BATCH_SIZE
__all__ = [
"DATA_BATCH_SIZE",
"FalconApiModule",
"Policy",
"SCROLL_BATCH_SIZE",
"user_agent_string",
]

from caracara.common.constants import SCROLL_BATCH_SIZE, DATA_BATCH_SIZE
from caracara.common.policy_wrapper import Policy
from caracara.filters.utils import IPChecker, RelativeTimestamp, ISOTimestampChecker
from caracara.common.meta import user_agent_string
from caracara.common.module import FalconApiModule

__all__ = [
"FILTER_OPERATORS", "SCROLL_BATCH_SIZE", "DATA_BATCH_SIZE", "IPChecker",
"RelativeTimestamp", "ISOTimestampChecker", "FILTER_ATTRIBUTES",
"user_agent_string", "FalconApiModule", "Policy",
]
3 changes: 2 additions & 1 deletion caracara/common/batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ def worker(
elif 'uuid' in resource:
resources_dict[resource['uuid']] = resource
else:
raise Exception("No ID field to build the dictionary from")
raise KeyError("No ID field to build the dictionary from")

BATCH_LOGGER.debug("Returned resources")
BATCH_LOGGER.debug(resources_dict)

if errors:
# pylint: disable=W0719
raise Exception("At least one thread returned an error: " + str(errors))

return resources_dict
22 changes: 0 additions & 22 deletions caracara/common/common_filters.py

This file was deleted.

9 changes: 0 additions & 9 deletions caracara/common/constants.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
"""Common constants to be shared throughout Caracara."""
from enum import Enum, EnumMeta

FILTER_OPERATORS = {
"EQUAL": '',
"NOT": '!',
"GREATER": '>',
"GTE": '>=',
"LESS": '<',
"LTE": '<=',
}

# Batch size of data downloaded via a multi-threaded data pull
DATA_BATCH_SIZE = 500

Expand Down
Loading

0 comments on commit b3c257d

Please sign in to comment.