-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathexample_query_policy.py
53 lines (43 loc) · 1.41 KB
/
example_query_policy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Example script showing how to use the LaceworkClient class.
"""
import logging
import random
import string
from dotenv import load_dotenv
from laceworksdk import LaceworkClient
logging.basicConfig(level=logging.DEBUG)
load_dotenv()
RANDOM_TEXT = "".join(random.choices(string.ascii_uppercase, k=4))
QUERY_ID = f"Custom_Query_{RANDOM_TEXT}"
POLICY_TITLE = f"Custom_Policy_{RANDOM_TEXT}"
if __name__ == "__main__":
# Instantiate a LaceworkClient instance
lacework_client = LaceworkClient()
# Queries/Policies API
# Create a Query
query_response = lacework_client.queries.create(
evaluator_id="Cloudtrail",
query_id=QUERY_ID,
query_text="""{
source {CloudTrailRawEvents e}
filter {EVENT_SOURCE = 'iam.amazonaws.com' AND
EVENT:userIdentity.name::String NOT LIKE 'Terraform-Service-Acct'}
return distinct {EVENT_NAME, EVENT}
}
"""
)
# Create a Policy
lacework_client.policies.create(
policy_type="Violation",
query_id=query_response["data"]["queryId"],
enabled=True,
title=POLICY_TITLE,
description=f"{POLICY_TITLE}_Description",
remediation="Policy remediation",
severity="high",
alert_enabled=True,
alert_profile="LW_CloudTrail_Alerts",
evaluator_id=query_response["data"]["evaluatorId"]
)