Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_security_config.py

raw
1 #!/usr/bin/python
2
3 # (c) 2021-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
8 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: na_ontap_security_config
12 short_description: NetApp ONTAP modify security config for SSL.
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap
15 version_added: '21.3.0'
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Modifies the security configuration for SSL.
19 options:
20 name:
21 description:
22 - The type of FIPS compliant interface.
23 type: str
24 default: ssl
25
26 is_fips_enabled:
27 description:
28 - Enables or disables FIPS-compliant mode for the cluster.
29 - For REST, it requires ontap version 9.8.
30 type: bool
31
32 supported_ciphers:
33 description:
34 - Selects the supported cipher suites for the selected interface.
35 - This option is supported only in ZAPI.
36 type: str
37
38 supported_protocols:
39 description:
40 - Selects the supported protocols for the selected interface. Supported_ciphers should not be specified if operating in FIPS-compliant mode.
41 - For REST, it requires ontap version 9.10.1 or later.
42 - Protocol versions can be removed only from lower versions.
43 - To remove protocol TLSv1 has to be removed first.
44 choices: ['TLSv1.3', 'TLSv1.2', 'TLSv1.1', 'TLSv1']
45 type: list
46 elements: str
47
48 supported_cipher_suites:
49 description:
50 - Names a cipher suite that the system can select during TLS handshakes.
51 - A list of available options can be found on the Internet Assigned Number Authority (IANA) website.
52 - To achieve idempotency all similar cipher_suites must be set.
53 - This option requires ontap version 9.10.1 or later.
54 type: list
55 elements: str
56 version_added: 22.4.0
57 """
58
59 EXAMPLES = """
60 - name: Modify SSL Security Config - ZAPI
61 netapp.ontap.na_ontap_security_config:
62 name: ssl
63 is_fips_enabled: false
64 supported_ciphers: 'ALL:!LOW:!aNULL:!EXP:!eNULL:!3DES:!RC4:!SHA1'
65 supported_protocols: ['TLSv1.2', 'TLSv1.1', 'TLSv1']
66 hostname: "{{ netapp_hostname }}"
67 username: "{{ netapp_username }}"
68 password: "{{ netapp_password }}"
69 ontapi: "{{ ontap_info.ontap_info.ontap_version }}"
70 https: true
71 validate_certs: false
72
73 - name: Modify SSL Security Config - REST
74 netapp.ontap.na_ontap_security_config:
75 is_fips_enabled: false
76 supported_protocols: ['TLSv1.2', 'TLSv1.1', 'TLSv1']
77 supported_cipher_suites: ['TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384']
78 hostname: "{{ netapp_hostname }}"
79 username: "{{ netapp_username }}"
80 password: "{{ netapp_password }}"
81 ontapi: "{{ ontap_info.ontap_info.ontap_version }}"
82 https: true
83 validate_certs: false
84 """
85
86 RETURN = """
87
88 """
89
90 import traceback
91
92 from ansible.module_utils.basic import AnsibleModule
93 from ansible.module_utils._text import to_native
94 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
95 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
96 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
97
98
99 class NetAppOntapSecurityConfig:
100 """
101 Modifies SSL Security Config
102 """
103 def __init__(self):
104 """
105 Initialize the ONTAP Security Config class
106 """
107 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
108 self.argument_spec.update(dict(
109 name=dict(required=False, type='str', default='ssl'),
110 is_fips_enabled=dict(required=False, type='bool'),
111 supported_ciphers=dict(required=False, type='str'),
112 supported_protocols=dict(required=False, type='list', elements='str', choices=['TLSv1.3', 'TLSv1.2', 'TLSv1.1', 'TLSv1']),
113 supported_cipher_suites=dict(required=False, type='list', elements='str')
114 ))
115
116 self.module = AnsibleModule(
117 argument_spec=self.argument_spec,
118 supports_check_mode=True
119 )
120
121 self.na_helper = NetAppModule()
122 self.parameters = self.na_helper.set_parameters(self.module.params)
123
124 self.rest_api = netapp_utils.OntapRestAPI(self.module)
125 unsupported_rest_properties = ['supported_ciphers']
126 partially_supported_rest_properties = [['supported_cipher_suites', (9, 10, 1)], ['supported_protocols', (9, 10, 1)]]
127 self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties, partially_supported_rest_properties)
128 if self.use_rest and self.rest_api.fail_if_not_rest_minimum_version('na_ontap_security_config', 9, 8, 0):
129 msg = 'REST requires ONTAP 9.8 or later.'
130 self.use_rest = self.na_helper.fall_back_to_zapi(self.module, msg, self.parameters)
131
132 if not self.use_rest:
133 if self.parameters.get('supported_cipher_suites'):
134 self.module.fail_json(msg="Error: The option supported_cipher_suites is supported only with REST.")
135 if not netapp_utils.has_netapp_lib():
136 self.module.fail_json(msg='The python NetApp-Lib module is required')
137 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)
138
139 # Supported_ciphers is supported in ZAPI only.
140 if 'is_fips_enabled' in self.parameters and 'supported_ciphers' in self.parameters:
141 # if fips is enabled, supported ciphers should not be specified.
142 if self.parameters['is_fips_enabled']:
143 self.module.fail_json(
144 msg='is_fips_enabled was specified as true and supported_ciphers was specified. \
145 If fips is enabled then supported ciphers should not be specified')
146
147 if 'supported_ciphers' in self.parameters:
148 self.parameters['supported_ciphers'] = self.parameters['supported_ciphers'].replace('\\', '')
149
150 if 'is_fips_enabled' in self.parameters and 'supported_protocols' in self.parameters:
151 # if fips is enabled, TLSv1 is not a supported protocol.
152 if self.parameters['is_fips_enabled'] and 'TLSv1' in self.parameters['supported_protocols']:
153 self.module.fail_json(
154 msg='is_fips_enabled was specified as true and TLSv1 was specified as a supported protocol. \
155 If fips is enabled then TLSv1 is not a supported protocol')
156 # if fips is enabled, TLSv1.1 is not a supported protocol.
157 if self.parameters['is_fips_enabled'] and 'TLSv1.1' in self.parameters['supported_protocols']:
158 self.module.fail_json(
159 msg='is_fips_enabled was specified as true and TLSv1.1 was specified as a supported protocol. \
160 If fips is enabled then TLSv1.1 is not a supported protocol')
161
162 def get_security_config(self):
163 """
164 Get the current security configuration
165 """
166 if self.use_rest:
167 return self.get_security_config_rest()
168
169 return_value = None
170
171 security_config_get_iter = netapp_utils.zapi.NaElement('security-config-get')
172 security_config_info = netapp_utils.zapi.NaElement('desired-attributes')
173 if 'is_fips_enabled' in self.parameters:
174 security_config_info.add_new_child(
175 'is-fips-enabled', self.na_helper.get_value_for_bool(from_zapi=False, value=self.parameters['is_fips_enabled'])
176 )
177 if 'supported_ciphers' in self.parameters:
178 security_config_info.add_new_child('supported-ciphers', self.parameters['supported_ciphers'])
179 if 'supported_protocols' in self.parameters:
180 security_config_info.add_new_child('supported-protocols', ','.join(self.parameters['supported_protocols']))
181
182 security_config_get_iter.add_child_elem(security_config_info)
183 security_config_get_iter.add_new_child('interface', self.parameters['name'])
184 try:
185 result = self.server.invoke_successfully(security_config_get_iter, True)
186 security_supported_protocols = []
187 if result.get_child_by_name('attributes'):
188 attributes = result.get_child_by_name('attributes')
189 security_config_attributes = attributes.get_child_by_name('security-config-info')
190 supported_protocols = security_config_attributes.get_child_by_name('supported-protocols')
191 for supported_protocol in supported_protocols.get_children():
192 security_supported_protocols.append(supported_protocol.get_content())
193 return_value = {
194 'name': security_config_attributes['interface'],
195 'is_fips_enabled': self.na_helper.get_value_for_bool(from_zapi=True, value=security_config_attributes['is-fips-enabled']),
196 'supported_ciphers': security_config_attributes['supported-ciphers'],
197 'supported_protocols': security_supported_protocols,
198 }
199
200 except netapp_utils.zapi.NaApiError as error:
201 self.module.fail_json(
202 msg='Error getting security config for interface %s: %s' % (self.parameters['name'], to_native(error)),
203 exception=traceback.format_exc())
204
205 return return_value
206
207 def modify_security_config(self, modify):
208 """
209 Modifies the security configuration.
210 """
211 if self.use_rest:
212 return self.modify_security_config_rest(modify)
213
214 security_config_obj = netapp_utils.zapi.NaElement("security-config-modify")
215 security_config_obj.add_new_child("interface", self.parameters['name'])
216 if 'is_fips_enabled' in self.parameters:
217 self.parameters['is_fips_enabled'] = self.na_helper.get_value_for_bool(from_zapi=False, value=self.parameters['is_fips_enabled'])
218 security_config_obj.add_new_child('is-fips-enabled', self.parameters['is_fips_enabled'])
219 if 'supported_ciphers' in self.parameters:
220 security_config_obj.add_new_child('supported-ciphers', self.parameters['supported_ciphers'])
221 if 'supported_protocols' in self.parameters:
222 supported_protocol_obj = netapp_utils.zapi.NaElement("supported-protocols")
223 for protocol in self.parameters['supported_protocols']:
224 supported_protocol_obj.add_new_child('string', protocol)
225 security_config_obj.add_child_elem(supported_protocol_obj)
226 try:
227 self.server.invoke_successfully(security_config_obj, True)
228 except netapp_utils.zapi.NaApiError as error:
229 self.module.fail_json(
230 msg='Error modifying security config for interface %s: %s' % (self.parameters['name'], to_native(error)),
231 exception=traceback.format_exc()
232 )
233
234 def get_security_config_rest(self):
235 """
236 Get the current security configuration
237 """
238 fields = 'fips.enabled,'
239 if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 10, 1):
240 fields += 'tls.cipher_suites,tls.protocol_versions'
241 record, error = rest_generic.get_one_record(self.rest_api, '/security', None, fields)
242 if error:
243 self.module.fail_json(msg="Error on getting security config: %s" % error)
244 if record:
245 return {
246 'is_fips_enabled': self.na_helper.safe_get(record, ['fips', 'enabled']),
247 'supported_cipher_suites': self.na_helper.safe_get(record, ['tls', 'cipher_suites']),
248 'supported_protocols': self.na_helper.safe_get(record, ['tls', 'protocol_versions'])
249 }
250 return record
251
252 def modify_security_config_rest(self, modify):
253 """
254 Modify the current security configuration
255 """
256 body = {}
257 if 'is_fips_enabled' in modify:
258 body['fips.enabled'] = modify['is_fips_enabled']
259 if 'supported_cipher_suites' in modify:
260 body['tls.cipher_suites'] = modify['supported_cipher_suites']
261 if 'supported_protocols' in modify:
262 body['tls.protocol_versions'] = modify['supported_protocols']
263 record, error = rest_generic.patch_async(self.rest_api, '/security', None, body)
264 if error:
265 self.module.fail_json(msg="Error on modifying security config: %s" % error)
266
267 def cipher_suites_warning_rest(self, modify):
268 current = self.get_security_config()
269 suites_for_idempotency = []
270 for current_suite in list(current.get('supported_cipher_suites')):
271 if current_suite not in self.parameters.get('supported_cipher_suites'):
272 suites_for_idempotency.append(current_suite)
273 if len(suites_for_idempotency) > 0:
274 self.module.warn("To achieve idempotency the mentioned cipher_suites must be included.")
275 self.module.warn(", ".join(suites_for_idempotency))
276
277 def apply(self):
278 current = self.get_security_config()
279 modify = self.na_helper.get_modified_attributes(current, self.parameters)
280 if self.na_helper.changed and not self.module.check_mode:
281 self.modify_security_config(modify)
282 if 'supported_cipher_suites' in modify:
283 self.cipher_suites_warning_rest(modify)
284 result = netapp_utils.generate_result(self.na_helper.changed, modify=modify)
285 self.module.exit_json(**result)
286
287
288 def main():
289 """
290 Creates the NetApp ONTAP security config object and runs the correct play task
291 """
292 obj = NetAppOntapSecurityConfig()
293 obj.apply()
294
295
296 if __name__ == '__main__':
297 main()