Skip to content
This repository was archived by the owner on Aug 12, 2019. It is now read-only.

Commit 1fe1819

Browse files
committed
Added the ability to create new policies via Manager.policies.create()
1 parent 8a89c71 commit 1fe1819

File tree

1 file changed

+144
-2
lines changed

1 file changed

+144
-2
lines changed

deepsecurity/policies.py

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get(self):
2323
if response and response['status'] == 200:
2424
if not type(response['data']) == type([]): response['data'] = [response['data']]
2525
for policy in response['data']:
26-
policy_obj = Policy(self.manager, policy, self.log)
26+
policy_obj = Policy(manager=self.manager, api_response=policy, log_func=self.log)
2727
if policy_obj:
2828
try:
2929
self[policy_obj.id] = policy_obj
@@ -33,6 +33,115 @@ def get(self):
3333

3434
return len(self)
3535

36+
def create(self, name, parent_profile_id=None,
37+
enable_anti_malware=True,
38+
enable_firewall=False,
39+
enable_intrusion_prevention=True,
40+
enable_integrity_monitoring=True,
41+
enable_log_inspection=True,
42+
description=None
43+
):
44+
"""
45+
Create a new policy
46+
47+
name
48+
- the name of the new policy
49+
50+
parent_profile_id
51+
- the ID of the parent policy
52+
53+
enable_anti_malware
54+
- if True, enable the anti-malware module
55+
- if 'parent_profile_id' is set, the new policy will
56+
inherit this value from the parent
57+
58+
enable_firewall
59+
- if True, enable the firewall module
60+
- if 'parent_profile_id' is set, the new policy will
61+
inherit this value from the parent
62+
63+
enable_intrusion_prevention
64+
- if True, enable the intrusion prevention module
65+
- if 'parent_profile_id' is set, the new policy will
66+
inherit this value from the parent
67+
68+
enable_integrity_monitoring
69+
- if True, enable the integrity monitoring module
70+
- if 'parent_profile_id' is set, the new policy will
71+
inherit this value from the parent
72+
73+
enable_log_inspection
74+
- if True, enable the log inspection module
75+
- if 'parent_profile_id' is set, the new policy will
76+
inherit this value from the parent
77+
78+
description
79+
- the description of the new policy
80+
81+
Returns the ID of the new policy is successful. False if not successful in
82+
creating the new policy
83+
"""
84+
result = None
85+
86+
# set the state for each supported module
87+
anti_malware_state = 'ON' if enable_anti_malware else 'OFF'
88+
firewall_state = 'ON' if enable_firewall else 'OFF'
89+
intrusion_prevention_state = 'ON' if enable_intrusion_prevention else 'OFF'
90+
integrity_monitoring_state = 'ON' if enable_integrity_monitoring else 'OFF'
91+
log_inspection_state = 'ON' if enable_log_inspection else 'OFF'
92+
93+
# inherit all states if a parent policy is specified
94+
if parent_profile_id:
95+
anti_malware_state = 'INHERITED'
96+
firewall_state = 'INHERITED'
97+
intrusion_prevention_state = 'INHERITED'
98+
integrity_monitoring_state = 'INHERITED'
99+
log_inspection_state = 'INHERITED'
100+
101+
call = self.manager._get_request_format(call='securityProfileSave')
102+
call['data'] = { 'sp': {
103+
'DPIRuleIDs': None,
104+
'DPIState': intrusion_prevention_state,
105+
'ID': None,
106+
'antiMalwareManualID': None,
107+
'antiMalwareManualInherit': u'true',
108+
'antiMalwareRealTimeID': None,
109+
'antiMalwareRealTimeInherit': u'true',
110+
'antiMalwareRealTimeScheduleID': None,
111+
'antiMalwareScheduledID': None,
112+
'antiMalwareScheduledInherit': u'true',
113+
'antiMalwareState': anti_malware_state,
114+
'applicationTypeIDs': None,
115+
'description': description,
116+
'firewallRuleIDs': None,
117+
'firewallState': firewall_state,
118+
'integrityRuleIDs': None,
119+
'integrityState': integrity_monitoring_state,
120+
'logInspectionRuleIDs': None,
121+
'logInspectionState': log_inspection_state,
122+
'name': name,
123+
'parentSecurityProfileID': parent_profile_id if parent_profile_id else None,
124+
'recommendationState': None,
125+
'scheduleID': None,
126+
'statefulConfigurationID': None
127+
}
128+
}
129+
130+
response = self.manager._request(call)
131+
if response and response['status'] == 200:
132+
try:
133+
new_policy = Policy(api_response=response['data'], manager=self.manager, log_func=self.log)
134+
if new_policy:
135+
self[new_policy.id] = new_policy
136+
result = new_policy.id
137+
self.log("Added new policy #{}".format(new_policy.id))
138+
except Exception, err:
139+
self.log("Could not create new policy from API response", err=err)
140+
else:
141+
result = False
142+
143+
return result
144+
36145
class Rules(core.CoreDict):
37146
def __init__(self, manager=None):
38147
core.CoreDict.__init__(self)
@@ -143,6 +252,39 @@ def save(self):
143252

144253
return result
145254

255+
def get_application_control_settings(self):
256+
"""
257+
Get the details for the application control settings for this policy
258+
"""
259+
return self.manager.application_control.get_policy_settings(self.id)
260+
261+
def set_application_control_settings(self, policy_id, lockdown=None, ruleset_id=None, state=None, whitelist_mode=None):
262+
"""
263+
Set the details for the application control settings for this policy
264+
265+
lockdown:
266+
- if set to None, no changes are made
267+
- if set to True, lockdown mode is enabled and anything that's not on the whitelist will be blocked
268+
- if set to False, lockdown mode is disabled and only things on the blacklist will be blocked
269+
270+
ruleset_id:
271+
- if set to None, no changes are made
272+
- the ID of the ruleset to use for this application control policy
273+
274+
state:
275+
- if set to None, no changes are made
276+
- if set to "on", application control is turned on for this policy
277+
- if set to "off", application control is turned off for this policy
278+
- if set to "inherit", the application control state inherited from this policy's parent (if one exists)
279+
280+
whitelist_mode:
281+
- if set to None, no changes are made
282+
- if set to "local-inventory", application control is turned on for this policy
283+
- if set to "shared", application control is turned off for this policy
284+
- if set to "inherit", the application control state inherited from this policy's parent (if one exists)
285+
"""
286+
return self.manager.application_control.set_policy_settings(self.id, lockdown=lockdown, ruleset_id=ruleset_id, state=state, whitelist_mode=whitelist_mode)
287+
146288
class Rule(core.CoreObject):
147289
def __init__(self, manager=None, api_response=None, log_func=None, rule_type=None):
148290
self.manager = manager
@@ -164,4 +306,4 @@ def _split_items(self):
164306
if getattr(self, 'items') and "\n" in self.items:
165307
self.addresses = self.items.split('\n')
166308
else:
167-
self.addresses.append(self.items.strip())
309+
self.addresses.append(self.items.strip())

0 commit comments

Comments
 (0)