Skip to content

Commit 5b63b5d

Browse files
authored
Merge pull request #58 from JupiterOne/KNO-611
fix for multiple action_configs for alert rule creation
2 parents 01416ee + b1ad53b commit 5b63b5d

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

examples/05_alert_rules_and_smartclasses.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def alert_rule_examples(j1):
3636
severity="HIGH",
3737
j1ql="FIND Database WITH encrypted = false"
3838
)
39-
print(f"Created basic alert rule: {basic_rule['rule']['_id']}\n")
39+
print(f"Created basic alert rule: {basic_rule['id']}\n")
4040

4141
# 2. Complex alert rule with multiple conditions
4242
print("2. Creating a complex alert rule:")
@@ -55,7 +55,7 @@ def alert_rule_examples(j1):
5555
AND u.tag.Role != 'admin'
5656
"""
5757
)
58-
print(f"Created complex alert rule: {complex_rule['rule']['_id']}\n")
58+
print(f"Created complex alert rule: {complex_rule['id']}\n")
5959

6060
return basic_rule, complex_rule
6161

@@ -135,7 +135,7 @@ def alert_rule_with_actions_examples(j1):
135135
j1ql="FIND Finding WITH severity = 'HIGH'",
136136
action_configs=webhook_action_config
137137
)
138-
print(f"Created webhook alert rule: {webhook_rule['rule']['_id']}\n")
138+
print(f"Created webhook alert rule: {webhook_rule['id']}\n")
139139

140140
# Create alert rule with multiple actions
141141
print("2. Creating alert rule with multiple actions:")
@@ -149,7 +149,7 @@ def alert_rule_with_actions_examples(j1):
149149
j1ql="FIND Finding WITH severity = ('HIGH' OR 'CRITICAL')",
150150
action_configs=multiple_actions
151151
)
152-
print(f"Created multi-action alert rule: {multi_action_rule['rule']['_id']}\n")
152+
print(f"Created multi-action alert rule: {multi_action_rule['id']}\n")
153153

154154
return webhook_rule, multi_action_rule
155155

@@ -162,16 +162,16 @@ def alert_rule_management_examples(j1, rule_id):
162162
print("1. Getting alert rule details:")
163163
try:
164164
rule_details = j1.get_alert_rule_details(rule_id=rule_id)
165-
print(f"Rule: {rule_details['rule']['name']}")
166-
print(f"Description: {rule_details['rule']['description']}")
167-
print(f"J1QL: {rule_details['rule']['j1ql']}")
168-
print(f"Severity: {rule_details['rule']['severity']}")
169-
print(f"Polling Interval: {rule_details['rule']['pollingInterval']}")
165+
print(f"Rule: {rule_details['name']}")
166+
print(f"Description: {rule_details['description']}")
167+
print(f"J1QL: {rule_details.get('j1ql', 'N/A')}")
168+
print(f"Severity: {rule_details.get('severity', 'N/A')}")
169+
print(f"Polling Interval: {rule_details.get('pollingInterval', 'N/A')}")
170170

171171
# Check action configurations
172-
if 'actionConfigs' in rule_details['rule']:
172+
if 'actionConfigs' in rule_details:
173173
print("Action Configurations:")
174-
for action in rule_details['rule']['actionConfigs']:
174+
for action in rule_details['actionConfigs']:
175175
print(f" Type: {action['type']}")
176176
if action['type'] == 'WEBHOOK':
177177
print(f" Endpoint: {action['endpoint']}")
@@ -208,7 +208,7 @@ def alert_rule_management_examples(j1, rule_id):
208208
tag_op="OVERWRITE",
209209
severity="INFO"
210210
)
211-
print(f"Updated alert rule: {updated_rule['rule']['_id']}")
211+
print(f"Updated alert rule: {updated_rule['id']}")
212212
except Exception as e:
213213
print(f"Error updating alert rule: {e}")
214214
print()
@@ -240,7 +240,7 @@ def smartclass_examples(j1):
240240
smartclass_name='ProductionServers',
241241
smartclass_description='All production servers across cloud providers'
242242
)
243-
smartclass_id = smartclass['smartclass']['_id']
243+
smartclass_id = smartclass['id']
244244
print(f"Created SmartClass: {smartclass_id}\n")
245245

246246
# 2. Add queries to SmartClass
@@ -267,8 +267,8 @@ def smartclass_examples(j1):
267267
print("3. Getting SmartClass details:")
268268
try:
269269
smartclass_details = j1.get_smartclass_details(smartclass_id=smartclass_id)
270-
print(f"SmartClass: {smartclass_details['smartclass']['name']}")
271-
print(f"Description: {smartclass_details['smartclass']['description']}")
270+
print(f"SmartClass: {smartclass_details['tagName']}")
271+
print(f"Description: {smartclass_details['description']}")
272272
print(f"Queries: {len(smartclass_details.get('queries', []))}")
273273

274274
# List all queries in the SmartClass
@@ -410,7 +410,7 @@ def main():
410410
webhook_rule, multi_action_rule = alert_rule_with_actions_examples(j1)
411411

412412
# Alert rule management (using the basic rule)
413-
alert_rule_management_examples(j1, basic_rule['rule']['_id'])
413+
alert_rule_management_examples(j1, basic_rule['id'])
414414

415415
# SmartClass examples
416416
smartclass_id = smartclass_examples(j1)
@@ -419,7 +419,7 @@ def main():
419419
natural_language_to_j1ql_examples(j1)
420420

421421
# Alert rule evaluation examples
422-
alert_rule_evaluation_examples(j1, basic_rule['rule']['_id'])
422+
alert_rule_evaluation_examples(j1, basic_rule['id'])
423423

424424
# Compliance framework examples
425425
compliance_framework_examples(j1)

jupiterone/client.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ def create_alert_rule(
10301030
polling_interval: str = None,
10311031
severity: str = None,
10321032
j1ql: str = None,
1033-
action_configs: Dict = None,
1033+
action_configs: Union[Dict, List[Dict]] = None,
10341034
resource_group_id: str = None,
10351035
):
10361036
"""Create Alert Rule Configuration in J1 account"""
@@ -1079,7 +1079,10 @@ def create_alert_rule(
10791079
}
10801080

10811081
if action_configs:
1082-
variables["instance"]["operations"][0]["actions"].append(action_configs)
1082+
if isinstance(action_configs, list):
1083+
variables["instance"]["operations"][0]["actions"].extend(action_configs)
1084+
else:
1085+
variables["instance"]["operations"][0]["actions"].append(action_configs)
10831086

10841087
response = self._execute_query(CREATE_RULE_INSTANCE, variables=variables)
10851088

@@ -1106,7 +1109,7 @@ def update_alert_rule(
11061109
tags: List[str] = None,
11071110
tag_op: str = None,
11081111
labels: List[dict] = None,
1109-
action_configs: List[dict] = None,
1112+
action_configs: Union[Dict, List[Dict]] = None,
11101113
action_configs_op: str = None,
11111114
resource_group_id: str = None,
11121115
):
@@ -1178,15 +1181,23 @@ def update_alert_rule(
11781181
alert_action_configs = []
11791182
base_action = alert_rule_config["operations"][0]["actions"][0]
11801183
alert_action_configs.append(base_action)
1181-
alert_action_configs.extend(action_configs)
1184+
1185+
# Handle both single dict and list of dicts
1186+
if isinstance(action_configs, list):
1187+
alert_action_configs.extend(action_configs)
1188+
else:
1189+
alert_action_configs.append(action_configs)
11821190

11831191
# update actions field inside operations payload
11841192
operations[0]["actions"] = alert_action_configs
11851193

11861194
elif action_configs_op == "APPEND":
11871195

1188-
# update actions field inside operations payload
1189-
operations[0]["actions"].extend(action_configs)
1196+
# Handle both single dict and list of dicts
1197+
if isinstance(action_configs, list):
1198+
operations[0]["actions"].extend(action_configs)
1199+
else:
1200+
operations[0]["actions"].append(action_configs)
11901201

11911202
# update alert severity if provided
11921203
if severity is not None:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="jupiterone",
8-
version="1.6.0",
8+
version="1.6.1",
99
description="A Python client for the JupiterOne API",
1010
license="MIT License",
1111
author="JupiterOne",

0 commit comments

Comments
 (0)