Skip to content

Commit 9b0022c

Browse files
feat: added scan and status methods to Inventory (#111)
* fix: modified create policy wrapper to use type bool instead of int for 'enabled' field * feat: added `scan` and `status` methods to `Inventory` * minor linting and pytest changes for inventory
1 parent d75f923 commit 9b0022c

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

examples/example_inventory.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Example script showing how to use the LaceworkClient class.
4+
"""
5+
6+
import logging
7+
8+
from dotenv import load_dotenv
9+
from laceworksdk import LaceworkClient
10+
11+
logging.basicConfig(level=logging.DEBUG)
12+
13+
load_dotenv()
14+
15+
if __name__ == "__main__":
16+
17+
# Instantiate a LaceworkClient instance
18+
lacework_client = LaceworkClient()
19+
20+
# Inventory API
21+
22+
# Scan each CSP (cloud service provider)
23+
24+
for csp in ["AWS", "GCP", "Azure"]:
25+
inventory_scan = lacework_client.inventory.scan(csp=csp)
26+
27+
inventory_scan_status = lacework_client.inventory.status(csp=csp)

examples/example_query_policy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
query_response = lacework_client.queries.create(
3030
evaluator_id="Cloudtrail",
3131
query_id=QUERY_ID,
32-
query_text=f"""{{
32+
query_text="""{{
3333
source {{CloudTrailRawEvents e}}
3434
filter {{EVENT_SOURCE = 'iam.amazonaws.com' AND
3535
EVENT:userIdentity.name::String NOT LIKE 'Terraform-Service-Acct'}}

examples/example_syscall_query_policy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# Create a Query
3030
query_response = lacework_client.queries.create(
3131
query_id=QUERY_ID,
32-
query_text=f"""{{
32+
query_text="""{{
3333
source {{
3434
LW_HA_SYSCALLS_FILE
3535
}}

laceworksdk/api/v2/inventory.py

+39
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,42 @@ def __init__(self, session):
1818
"""
1919

2020
super().__init__(session, "Inventory")
21+
22+
def scan(self,
23+
csp,
24+
**request_params):
25+
"""
26+
A method to issue Resource Inventory scans.
27+
28+
:param csp: A string representing the cloud service provider to run the scan on (i.e. AWS, Azure, GCP).
29+
:param request_params: Additional request parameters.
30+
(provides support for parameters that may be added in the future)
31+
32+
:return response json
33+
"""
34+
35+
params = self.build_dict_from_items(
36+
csp=csp
37+
)
38+
39+
response = self._session.post(self.build_url(action="scan"), params=params)
40+
41+
return response.json()
42+
43+
def status(self,
44+
csp):
45+
"""
46+
A method to get the status of a Resource Inventory scan.
47+
48+
:param csp: A string representing the cloud service provider retrieve the scan status from (i.e. AWS, Azure, GCP).
49+
50+
:return response json
51+
"""
52+
53+
params = self.build_dict_from_items(
54+
csp=csp
55+
)
56+
57+
response = self._session.get(self.build_url(action="scan"), params=params)
58+
59+
return response.json()

laceworksdk/api/v2/policies.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def create(self,
7171
return super().create(
7272
policy_type=policy_type,
7373
query_id=query_id,
74-
enabled=int(bool(enabled)),
74+
enabled=enabled,
7575
title=title,
7676
description=description,
7777
remediation=remediation,

tests/api/v2/test_inventory.py

+10
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ def test_api_search_by_date(self, api_object, csp):
3131
def test_api_search_by_date_deprecated(self, api_object, dataset):
3232

3333
return super().test_api_search_by_date(api_object=api_object, filters={"dataset": dataset})
34+
35+
def test_inventory_scan(self, api_object, request):
36+
response = api_object.scan(csp="AWS")
37+
38+
assert "data" in response.keys()
39+
40+
def test_inventory_status(self, api_object, request):
41+
response = api_object.scan(csp="AWS")
42+
43+
assert "data" in response.keys()

0 commit comments

Comments
 (0)