| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
module: na_ontap_firewall_policy |
| 11 |
short_description: NetApp ONTAP Manage a firewall policy |
| 12 |
version_added: 2.7.0 |
| 13 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 14 |
description: |
| 15 |
- Configure firewall on an ONTAP node and manage firewall policy for an ONTAP SVM |
| 16 |
extends_documentation_fragment: |
| 17 |
- netapp.ontap.netapp.na_ontap_zapi |
| 18 |
requirements: |
| 19 |
- Python package ipaddress. Install using 'pip install ipaddress' |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Whether to set up a firewall policy or not |
| 24 |
choices: ['present', 'absent'] |
| 25 |
type: str |
| 26 |
default: present |
| 27 |
allow_list: |
| 28 |
description: |
| 29 |
- A list of IPs and masks to use. |
| 30 |
- The host bits of the IP addresses used in this list must be set to 0. |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
policy: |
| 34 |
description: |
| 35 |
- A policy name for the firewall policy |
| 36 |
type: str |
| 37 |
service: |
| 38 |
description: |
| 39 |
- The service to apply the policy to |
| 40 |
- https and ssh are not supported starting with ONTAP 9.6 |
| 41 |
- portmap is supported for ONTAP 9.4, 9.5 and 9.6 |
| 42 |
- none is supported for ONTAP 9.8 onwards. |
| 43 |
choices: ['dns', 'http', 'https', 'ndmp', 'ndmps', 'ntp', 'portmap', 'rsh', 'snmp', 'ssh', 'telnet', 'none'] |
| 44 |
type: str |
| 45 |
vserver: |
| 46 |
description: |
| 47 |
- The Vserver to apply the policy to. |
| 48 |
type: str |
| 49 |
enable: |
| 50 |
description: |
| 51 |
- enable firewall on a node |
| 52 |
choices: ['enable', 'disable'] |
| 53 |
type: str |
| 54 |
logging: |
| 55 |
description: |
| 56 |
- enable logging for firewall on a node |
| 57 |
choices: ['enable', 'disable'] |
| 58 |
type: str |
| 59 |
node: |
| 60 |
description: |
| 61 |
- The node to run the firewall configuration on |
| 62 |
type: str |
| 63 |
''' |
| 64 |
|
| 65 |
EXAMPLES = """ |
| 66 |
- name: Create firewall Policy |
| 67 |
netapp.ontap.na_ontap_firewall_policy: |
| 68 |
state: present |
| 69 |
allow_list: [1.2.3.0/24, 1.3.0.0/16] |
| 70 |
policy: pizza |
| 71 |
service: http |
| 72 |
vserver: ci_dev |
| 73 |
hostname: "{{ netapp hostname }}" |
| 74 |
username: "{{ netapp username }}" |
| 75 |
password: "{{ netapp password }}" |
| 76 |
|
| 77 |
- name: Modify firewall Policy |
| 78 |
netapp.ontap.na_ontap_firewall_policy: |
| 79 |
state: present |
| 80 |
allow_list: [1.5.3.0/24] |
| 81 |
policy: pizza |
| 82 |
service: http |
| 83 |
vserver: ci_dev |
| 84 |
hostname: "{{ netapp hostname }}" |
| 85 |
username: "{{ netapp username }}" |
| 86 |
password: "{{ netapp password }}" |
| 87 |
|
| 88 |
- name: Destory firewall Policy |
| 89 |
netapp.ontap.na_ontap_firewall_policy: |
| 90 |
state: absent |
| 91 |
policy: pizza |
| 92 |
service: http |
| 93 |
vserver: ci_dev |
| 94 |
hostname: "{{ netapp hostname }}" |
| 95 |
username: "{{ netapp username }}" |
| 96 |
password: "{{ netapp password }}" |
| 97 |
|
| 98 |
- name: Enable firewall and logging on a node |
| 99 |
netapp.ontap.na_ontap_firewall_policy: |
| 100 |
node: test-vsim1 |
| 101 |
enable: enable |
| 102 |
logging: enable |
| 103 |
hostname: "{{ netapp hostname }}" |
| 104 |
username: "{{ netapp username }}" |
| 105 |
password: "{{ netapp password }}" |
| 106 |
""" |
| 107 |
|
| 108 |
RETURN = """ |
| 109 |
""" |
| 110 |
|
| 111 |
import traceback |
| 112 |
|
| 113 |
from ansible.module_utils.basic import AnsibleModule |
| 114 |
from ansible.module_utils._text import to_native |
| 115 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 116 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 117 |
from ansible_collections.netapp.ontap.plugins.module_utils import netapp_ipaddress |
| 118 |
|
| 119 |
|
| 120 |
class NetAppONTAPFirewallPolicy: |
| 121 |
def __init__(self): |
| 122 |
self.argument_spec = netapp_utils.na_ontap_zapi_only_spec() |
| 123 |
self.argument_spec.update(dict( |
| 124 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 125 |
allow_list=dict(required=False, type='list', elements='str'), |
| 126 |
policy=dict(required=False, type='str'), |
| 127 |
service=dict(required=False, type='str', choices=['dns', 'http', 'https', 'ndmp', 'ndmps', |
| 128 |
'ntp', 'portmap', 'rsh', 'snmp', 'ssh', 'telnet', 'none']), |
| 129 |
vserver=dict(required=False, type="str"), |
| 130 |
enable=dict(required=False, type="str", choices=['enable', 'disable']), |
| 131 |
logging=dict(required=False, type="str", choices=['enable', 'disable']), |
| 132 |
node=dict(required=False, type="str") |
| 133 |
)) |
| 134 |
self.module = AnsibleModule( |
| 135 |
argument_spec=self.argument_spec, |
| 136 |
required_together=(['policy', 'service', 'vserver'], |
| 137 |
['enable', 'node'] |
| 138 |
), |
| 139 |
supports_check_mode=True |
| 140 |
) |
| 141 |
|
| 142 |
self.na_helper = NetAppModule() |
| 143 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 144 |
self.module.warn('The module only supports ZAPI; refer to netapp.ontap.na_ontap_service_policy module for RESTful equivalent.') |
| 145 |
if not netapp_utils.has_netapp_lib(): |
| 146 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 147 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 148 |
|
| 149 |
def validate_ip_addresses(self): |
| 150 |
''' |
| 151 |
Validate if the given IP address is a network address (i.e. it's host bits are set to 0) |
| 152 |
ONTAP doesn't validate if the host bits are set, |
| 153 |
and hence doesn't add a new address unless the IP is from a different network. |
| 154 |
So this validation allows the module to be idempotent. |
| 155 |
:return: None |
| 156 |
''' |
| 157 |
for ip in self.parameters['allow_list']: |
| 158 |
netapp_ipaddress.validate_ip_address_is_network_address(ip, self.module) |
| 159 |
|
| 160 |
def get_firewall_policy(self): |
| 161 |
""" |
| 162 |
Get a firewall policy |
| 163 |
:return: returns a firewall policy object, or returns False if there are none |
| 164 |
""" |
| 165 |
net_firewall_policy_obj = netapp_utils.zapi.NaElement("net-firewall-policy-get-iter") |
| 166 |
attributes = { |
| 167 |
'query': { |
| 168 |
'net-firewall-policy-info': self.firewall_policy_attributes() |
| 169 |
} |
| 170 |
} |
| 171 |
net_firewall_policy_obj.translate_struct(attributes) |
| 172 |
|
| 173 |
try: |
| 174 |
result = self.server.invoke_successfully(net_firewall_policy_obj, True) |
| 175 |
except netapp_utils.zapi.NaApiError as error: |
| 176 |
self.module.fail_json(msg="Error getting firewall policy %s:%s" % (self.parameters['policy'], |
| 177 |
to_native(error)), |
| 178 |
exception=traceback.format_exc()) |
| 179 |
|
| 180 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 181 |
attributes_list = result.get_child_by_name('attributes-list') |
| 182 |
policy_info = attributes_list.get_child_by_name('net-firewall-policy-info') |
| 183 |
ips = self.na_helper.get_value_for_list(from_zapi=True, |
| 184 |
zapi_parent=policy_info.get_child_by_name('allow-list')) |
| 185 |
return { |
| 186 |
'service': policy_info['service'], |
| 187 |
'allow_list': ips} |
| 188 |
return None |
| 189 |
|
| 190 |
def create_firewall_policy(self): |
| 191 |
""" |
| 192 |
Create a firewall policy for given vserver |
| 193 |
:return: None |
| 194 |
""" |
| 195 |
net_firewall_policy_obj = netapp_utils.zapi.NaElement("net-firewall-policy-create") |
| 196 |
net_firewall_policy_obj.translate_struct(self.firewall_policy_attributes()) |
| 197 |
if self.parameters.get('allow_list'): |
| 198 |
self.validate_ip_addresses() |
| 199 |
net_firewall_policy_obj.add_child_elem(self.na_helper.get_value_for_list(from_zapi=False, |
| 200 |
zapi_parent='allow-list', |
| 201 |
zapi_child='ip-and-mask', |
| 202 |
data=self.parameters['allow_list']) |
| 203 |
) |
| 204 |
try: |
| 205 |
self.server.invoke_successfully(net_firewall_policy_obj, enable_tunneling=True) |
| 206 |
except netapp_utils.zapi.NaApiError as error: |
| 207 |
self.module.fail_json(msg="Error creating Firewall Policy: %s" % (to_native(error)), exception=traceback.format_exc()) |
| 208 |
|
| 209 |
def destroy_firewall_policy(self): |
| 210 |
""" |
| 211 |
Destroy a Firewall Policy from a vserver |
| 212 |
:return: None |
| 213 |
""" |
| 214 |
net_firewall_policy_obj = netapp_utils.zapi.NaElement("net-firewall-policy-destroy") |
| 215 |
net_firewall_policy_obj.translate_struct(self.firewall_policy_attributes()) |
| 216 |
try: |
| 217 |
self.server.invoke_successfully(net_firewall_policy_obj, enable_tunneling=True) |
| 218 |
except netapp_utils.zapi.NaApiError as error: |
| 219 |
self.module.fail_json(msg="Error destroying Firewall Policy: %s" % (to_native(error)), exception=traceback.format_exc()) |
| 220 |
|
| 221 |
def modify_firewall_policy(self, modify): |
| 222 |
""" |
| 223 |
Modify a firewall Policy on a vserver |
| 224 |
:return: none |
| 225 |
""" |
| 226 |
self.validate_ip_addresses() |
| 227 |
net_firewall_policy_obj = netapp_utils.zapi.NaElement("net-firewall-policy-modify") |
| 228 |
net_firewall_policy_obj.translate_struct(self.firewall_policy_attributes()) |
| 229 |
net_firewall_policy_obj.add_child_elem(self.na_helper.get_value_for_list(from_zapi=False, |
| 230 |
zapi_parent='allow-list', |
| 231 |
zapi_child='ip-and-mask', |
| 232 |
data=modify['allow_list'])) |
| 233 |
try: |
| 234 |
self.server.invoke_successfully(net_firewall_policy_obj, enable_tunneling=True) |
| 235 |
except netapp_utils.zapi.NaApiError as error: |
| 236 |
self.module.fail_json(msg="Error modifying Firewall Policy: %s" % (to_native(error)), exception=traceback.format_exc()) |
| 237 |
|
| 238 |
def firewall_policy_attributes(self): |
| 239 |
return { |
| 240 |
'policy': self.parameters['policy'], |
| 241 |
'service': self.parameters['service'], |
| 242 |
'vserver': self.parameters['vserver'], |
| 243 |
} |
| 244 |
|
| 245 |
def get_firewall_config_for_node(self): |
| 246 |
""" |
| 247 |
Get firewall configuration on the node |
| 248 |
:return: dict() with firewall config details |
| 249 |
""" |
| 250 |
if self.parameters.get('logging') and self.parameters.get('node') is None: |
| 251 |
self.module.fail_json(msg='Error: Missing parameter \'node\' to modify firewall logging') |
| 252 |
net_firewall_config_obj = netapp_utils.zapi.NaElement("net-firewall-config-get") |
| 253 |
net_firewall_config_obj.add_new_child('node-name', self.parameters['node']) |
| 254 |
try: |
| 255 |
result = self.server.invoke_successfully(net_firewall_config_obj, True) |
| 256 |
except netapp_utils.zapi.NaApiError as error: |
| 257 |
self.module.fail_json(msg="Error getting Firewall Configuration: %s" % (to_native(error)), |
| 258 |
exception=traceback.format_exc()) |
| 259 |
if result.get_child_by_name('attributes'): |
| 260 |
firewall_info = result['attributes'].get_child_by_name('net-firewall-config-info') |
| 261 |
return {'enable': self.change_status_to_bool(firewall_info.get_child_content('is-enabled'), to_zapi=False), |
| 262 |
'logging': self.change_status_to_bool(firewall_info.get_child_content('is-logging'), to_zapi=False)} |
| 263 |
return None |
| 264 |
|
| 265 |
def modify_firewall_config(self, modify): |
| 266 |
""" |
| 267 |
Modify the configuration of a firewall on node |
| 268 |
:return: None |
| 269 |
""" |
| 270 |
net_firewall_config_obj = netapp_utils.zapi.NaElement("net-firewall-config-modify") |
| 271 |
net_firewall_config_obj.add_new_child('node-name', self.parameters['node']) |
| 272 |
if modify.get('enable'): |
| 273 |
net_firewall_config_obj.add_new_child('is-enabled', self.change_status_to_bool(self.parameters['enable'])) |
| 274 |
if modify.get('logging'): |
| 275 |
net_firewall_config_obj.add_new_child('is-logging', self.change_status_to_bool(self.parameters['logging'])) |
| 276 |
try: |
| 277 |
self.server.invoke_successfully(net_firewall_config_obj, enable_tunneling=True) |
| 278 |
except netapp_utils.zapi.NaApiError as error: |
| 279 |
self.module.fail_json(msg="Error modifying Firewall Config: %s" % (to_native(error)), |
| 280 |
exception=traceback.format_exc()) |
| 281 |
|
| 282 |
def change_status_to_bool(self, input, to_zapi=True): |
| 283 |
if to_zapi: |
| 284 |
return 'true' if input == 'enable' else 'false' |
| 285 |
else: |
| 286 |
return 'enable' if input == 'true' else 'disable' |
| 287 |
|
| 288 |
def apply(self): |
| 289 |
cd_action, modify, modify_config = None, None, None |
| 290 |
if self.parameters.get('policy'): |
| 291 |
current = self.get_firewall_policy() |
| 292 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 293 |
if cd_action is None and self.parameters['state'] == 'present': |
| 294 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 295 |
if self.parameters.get('node'): |
| 296 |
current_config = self.get_firewall_config_for_node() |
| 297 |
# firewall config for a node is always present, we cannot create or delete a firewall on a node |
| 298 |
modify_config = self.na_helper.get_modified_attributes(current_config, self.parameters) |
| 299 |
|
| 300 |
if self.na_helper.changed and not self.module.check_mode: |
| 301 |
if cd_action == 'create': |
| 302 |
self.create_firewall_policy() |
| 303 |
elif cd_action == 'delete': |
| 304 |
self.destroy_firewall_policy() |
| 305 |
else: |
| 306 |
if modify: |
| 307 |
self.modify_firewall_policy(modify) |
| 308 |
if modify_config: |
| 309 |
self.modify_firewall_config(modify_config) |
| 310 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify, extra_responses={'modify_config': modify_config}) |
| 311 |
self.module.exit_json(**result) |
| 312 |
|
| 313 |
|
| 314 |
def main(): |
| 315 |
""" |
| 316 |
Execute action from playbook |
| 317 |
:return: nothing |
| 318 |
""" |
| 319 |
cg_obj = NetAppONTAPFirewallPolicy() |
| 320 |
cg_obj.apply() |
| 321 |
|
| 322 |
|
| 323 |
if __name__ == '__main__': |
| 324 |
main() |