-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from lacework/datasources
Added the new DataSources API into the SDK and made changes to the Jupyter wrapper
- Loading branch information
Showing
10 changed files
with
214 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import functools | ||
|
||
import pandas as pd | ||
|
||
from . import config | ||
|
||
|
||
def dataframe_decorator(function): | ||
""" | ||
A decorator used to convert Lacework JSON API output into a dataframe. | ||
""" | ||
@functools.wraps(function) | ||
def get_output(*args, **kwargs): | ||
data = function(*args, **kwargs) | ||
|
||
if isinstance(data, dict): | ||
df = pd.DataFrame(data.get('data', [])) | ||
if 'SEVERITY' in df: | ||
df['SEVERITY'] = df.SEVERITY.apply( | ||
lambda x: config.SEVERITY_DICT.get(x, x)) | ||
return df | ||
|
||
return data | ||
|
||
return get_output | ||
|
||
|
||
def plugin_decorator(function, output_plugin): | ||
""" | ||
A decorator used to use a plugin to convert Lacework JSON API output. | ||
""" | ||
@functools.wraps(function) | ||
def get_output(*args, **kwargs): | ||
data = function(*args, **kwargs) | ||
return output_plugin(data) | ||
|
||
return get_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""A simple loading of plugins.""" | ||
|
||
from . import alert_rules | ||
from . import datasource | ||
|
||
|
||
PLUGINS = { | ||
'alert_rules.get': alert_rules.process_alert_rules, | ||
'datasource.list_data_sources': datasource.process_list_data_sources, | ||
'datasource.get_datasource_schema': datasource.process_datasource_schema, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""An output plugin for the Lacework Alert Rules API.""" | ||
|
||
|
||
import pandas as pd | ||
|
||
|
||
def process_alert_rules(data): | ||
""" | ||
Returns a Pandas DataFrame from the API call. | ||
:return: A pandas DataFrame. | ||
""" | ||
data_dicts = data.get("data", []) | ||
lines = [] | ||
for data_dict in data_dicts: | ||
filter_dict = data_dict.get("filters", {}) | ||
filter_dict["mcGuid"] = data_dict.get("mcGuid") | ||
filter_dict["intgGuidList"] = data_dict.get("intgGuidList") | ||
filter_dict["type"] = data_dict.get("type") | ||
lines.append(filter_dict) | ||
return pd.DataFrame(lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""An output plugin for the Lacework DataSource API.""" | ||
|
||
|
||
import pandas as pd | ||
|
||
|
||
def process_list_data_sources(data): | ||
""" | ||
Returns a Pandas DataFrame from the API call. | ||
:return: A pandas DataFrame. | ||
""" | ||
lines = [{'name': x, 'description': y} for x, y in data] | ||
return pd.DataFrame(lines) | ||
|
||
|
||
def process_datasource_schema(data): | ||
""" | ||
Returns a Pandas DataFrame from the output of the API call. | ||
:return: A pandas DataFrame. | ||
""" | ||
data_dict = data.get('data', {}) | ||
schemas = data_dict.get('resultSchema', []) | ||
|
||
return pd.DataFrame(schemas) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Lacework DataSource API wrapper. | ||
""" | ||
|
||
import logging | ||
|
||
import bleach | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DataSourceAPI: | ||
""" | ||
Lacework DataSource API. | ||
""" | ||
|
||
_DEFAULT_DESCRIPTION = "No description available." | ||
|
||
def __init__(self, session): | ||
""" | ||
Initializes the DataSource object. | ||
:param session: An instance of the HttpSession class | ||
:return DataSourceAPI object. | ||
""" | ||
|
||
super(DataSourceAPI, self).__init__() | ||
|
||
self._session = session | ||
|
||
def get_datasource_schema( | ||
self, data_source): | ||
""" | ||
A method to get the schema for a particular data source. | ||
:param data_source: A string representing the data source to check for. | ||
:return response json | ||
""" | ||
|
||
logger.info( | ||
"Getting the schema for a particular datasource from Lacework...") | ||
|
||
data_clean = bleach.clean(data_source) | ||
api_uri = f"/api/v2/Datasources/{data_clean}" | ||
|
||
response = self._session.get(api_uri) | ||
return response.json() | ||
|
||
|
||
def list_data_sources(self): | ||
""" | ||
A method to list the data sources that are available. | ||
:return A list of tuples with two entries, source name and description. | ||
""" | ||
logger.info("Getting list of data sources Lacework...") | ||
|
||
api_uri = "/api/v2/Datasources" | ||
response = self._session.get(api_uri) | ||
|
||
response_json = response.json() | ||
|
||
return_sources = [] | ||
data_sources = response_json.get("data", []) | ||
for data_source in data_sources: | ||
description = data_source.get( | ||
"description", self._DEFAULT_DESCRIPTION) | ||
if description == 'None': | ||
description = self._DEFAULT_DESCRIPTION | ||
|
||
return_sources.append( | ||
(data_source.get("name", "No name"), description)) | ||
|
||
return return_sources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
python-dotenv~=0.15 | ||
requests~=2.25 | ||
configparser~=5.0 | ||
bleach~=4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters