Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_drive_security_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
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 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_drive_security_policy
16 short_description: Drive Security Policy configuration for Cisco Intersight
17 description:
18 - Manages Drive Security Policy configuration on Cisco Intersight.
19 - A policy to configure drive security settings for Cisco Intersight managed servers.
20 - Supports both Manual key management and Remote key management (KMIP).
21 - For more information see L(Cisco Intersight,https://intersight.com/apidocs).
22 extends_documentation_fragment: intersight
23 options:
24 state:
25 description:
26 - If C(present), will verify the resource is present and will create if needed.
27 - If C(absent), will verify the resource is absent and will delete if needed.
28 type: str
29 choices: [present, absent]
30 default: present
31 organization:
32 description:
33 - The name of the Organization this resource is assigned to.
34 - Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization.
35 type: str
36 default: default
37 name:
38 description:
39 - The name assigned to the Drive Security Policy.
40 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
41 type: str
42 required: true
43 description:
44 description:
45 - The user-defined description for the Drive Security Policy.
46 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
47 type: str
48 aliases: [descr]
49 tags:
50 description:
51 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
52 type: list
53 elements: dict
54 default: []
55 manual_key:
56 description:
57 - Configuration for manual key management.
58 - Use this for local key management with manual passphrase.
59 - Either C(manual_key) or C(remote_key) must be specified, but not both.
60 type: dict
61 suboptions:
62 new_key:
63 description:
64 - New Security Key Passphrase to be configured on the server.
65 - The passphrase must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one number, and one
66 special character.
67 type: str
68 required: true
69 existing_key:
70 description:
71 - Current Security Key Passphrase which is already configured on the server.
72 - Required only if drive security is already enabled with manual key.
73 - The passphrase must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one number, and one
74 special character.
75 type: str
76 remote_key:
77 description:
78 - Configuration for remote key management using KMIP server.
79 - Use this for remote key management with KMIP protocol.
80 - Either C(manual_key) or C(remote_key) must be specified, but not both.
81 type: dict
82 suboptions:
83 primary_server:
84 description:
85 - Primary KMIP server configuration.
86 - At least one of C(primary_server) or C(secondary_server) must be enabled.
87 type: dict
88 suboptions:
89 enable_drive_security:
90 description:
91 - Enables/disables the primary KMIP server.
92 type: bool
93 required: true
94 ip_address:
95 description:
96 - The IP address or hostname of the KMIP server.
97 - Can be an IPv4 address, IPv6 address, or hostname.
98 - Hostnames are valid only when Inband is configured for the CIMC address.
99 - Required when C(enable_drive_security) is C(true).
100 type: str
101 port:
102 description:
103 - The port to which the KMIP client should connect.
104 - Valid range is 1024-65535.
105 type: int
106 default: 5696
107 timeout:
108 description:
109 - The timeout before which the KMIP client should connect.
110 - Valid range is 1-250 seconds.
111 type: int
112 default: 60
113 secondary_server:
114 description:
115 - Secondary KMIP server configuration.
116 - At least one of C(primary_server) or C(secondary_server) must be enabled.
117 type: dict
118 suboptions:
119 enable_drive_security:
120 description:
121 - Enables/disables the secondary KMIP server.
122 type: bool
123 required: true
124 ip_address:
125 description:
126 - The IP address or hostname of the KMIP server.
127 - Can be an IPv4 address, IPv6 address, or hostname.
128 - Hostnames are valid only when Inband is configured for the CIMC address.
129 - Required when C(enable_drive_security) is C(true).
130 type: str
131 port:
132 description:
133 - The port to which the KMIP client should connect.
134 - Valid range is 1024-65535.
135 type: int
136 default: 5696
137 timeout:
138 description:
139 - The timeout before which the KMIP client should connect.
140 - Valid range is 1-250 seconds.
141 type: int
142 default: 60
143 server_certificate:
144 description:
145 - Server Public Root CA Certificate in base64 encoded format.
146 - Required when using remote key management (KMIP).
147 type: str
148 required: true
149 use_authentication:
150 description:
151 - Enables/disables authentication for communicating with KMIP server.
152 - When enabled, authentication is mandatory.
153 type: bool
154 default: false
155 username:
156 description:
157 - The username for the KMIP server login.
158 - Required when C(use_authentication) is C(true).
159 type: str
160 password:
161 description:
162 - The password for the KMIP server login.
163 - Optional parameter for KMIP authentication.
164 type: str
165 existing_key:
166 description:
167 - Current Security Key Passphrase which is already configured on the server.
168 - Required only if drive security is already enabled with manual key and switching to KMIP.
169 - The passphrase must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one number, and one
170 special character.
171 type: str
172 author:
173 - Ron Gershburg (@rgershbu)
174 '''
175
176 EXAMPLES = r'''
177 - name: Create Drive Security Policy with Manual Key
178 cisco.intersight.intersight_drive_security_policy:
179 api_private_key: "{{ api_private_key }}"
180 api_key_id: "{{ api_key_id }}"
181 organization: "default"
182 name: "manual-drive-security-policy"
183 description: "Drive security policy with manual key management"
184 manual_key:
185 new_key: "MyS3cur3P@ssw0rd"
186 tags:
187 - Key: "Environment"
188 Value: "Production"
189 state: present
190
191 - name: Create Drive Security Policy with Manual Key (updating existing)
192 cisco.intersight.intersight_drive_security_policy:
193 api_private_key: "{{ api_private_key }}"
194 api_key_id: "{{ api_key_id }}"
195 organization: "default"
196 name: "manual-drive-security-policy"
197 description: "Drive security policy with manual key management"
198 manual_key:
199 new_key: "MyN3wS3cur3P@ssw0rd"
200 existing_key: "MyS3cur3P@ssw0rd"
201 state: present
202
203 - name: Create Drive Security Policy with KMIP (Primary Server Only)
204 cisco.intersight.intersight_drive_security_policy:
205 api_private_key: "{{ api_private_key }}"
206 api_key_id: "{{ api_key_id }}"
207 organization: "default"
208 name: "kmip-drive-security-policy"
209 description: "Drive security policy with KMIP"
210 remote_key:
211 primary_server:
212 enable_drive_security: true
213 ip_address: "192.168.1.100"
214 port: 5696
215 timeout: 60
216 secondary_server:
217 enable_drive_security: false
218 server_certificate: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..."
219 use_authentication: false
220 state: present
221
222 - name: Create Drive Security Policy with KMIP (Both Servers with Authentication)
223 cisco.intersight.intersight_drive_security_policy:
224 api_private_key: "{{ api_private_key }}"
225 api_key_id: "{{ api_key_id }}"
226 organization: "default"
227 name: "kmip-auth-drive-security-policy"
228 description: "Drive security policy with KMIP and authentication"
229 remote_key:
230 primary_server:
231 enable_drive_security: true
232 ip_address: "192.168.1.100"
233 port: 5696
234 timeout: 60
235 secondary_server:
236 enable_drive_security: true
237 ip_address: "192.168.1.101"
238 port: 5696
239 timeout: 60
240 server_certificate: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..."
241 use_authentication: true
242 username: "kmip_user"
243 password: "kmip_password"
244 state: present
245
246 - name: Delete Drive Security Policy
247 cisco.intersight.intersight_drive_security_policy:
248 api_private_key: "{{ api_private_key }}"
249 api_key_id: "{{ api_key_id }}"
250 organization: "default"
251 name: "manual-drive-security-policy"
252 state: absent
253 '''
254
255 RETURN = r'''
256 api_response:
257 description: The API response output returned by the specified resource.
258 returned: always
259 type: dict
260 sample:
261 "api_response": {
262 "Name": "manual-drive-security-policy",
263 "ObjectType": "storage.DriveSecurityPolicy",
264 "Description": "Drive security policy with manual key management",
265 "KeySetting": {
266 "ClassId": "storage.KeySetting",
267 "ObjectType": "storage.KeySetting",
268 "KeyType": "Manual",
269 "ManualKey": {
270 "ClassId": "storage.LocalKeySetting",
271 "ObjectType": "storage.LocalKeySetting",
272 "IsExistingKeySet": false,
273 "IsNewKeySet": true
274 },
275 "RemoteKey": {
276 "ClassId": "storage.RemoteKeySetting",
277 "ObjectType": "storage.RemoteKeySetting",
278 "AuthCredentials": {
279 "ClassId": "storage.KmipAuthCredentials",
280 "ObjectType": "storage.KmipAuthCredentials",
281 "IsPasswordSet": false,
282 "UseAuthentication": false,
283 "Username": ""
284 },
285 "IsExistingKeySet": false,
286 "PrimaryServer": {
287 "ClassId": "storage.KmipServer",
288 "ObjectType": "storage.KmipServer",
289 "EnableDriveSecurity": false,
290 "IpAddress": "",
291 "Port": 5696,
292 "Timeout": 60
293 },
294 "SecondaryServer": {
295 "ClassId": "storage.KmipServer",
296 "ObjectType": "storage.KmipServer",
297 "EnableDriveSecurity": false,
298 "IpAddress": "",
299 "Port": 5696,
300 "Timeout": 60
301 },
302 "ServerCertificate": ""
303 }
304 },
305 "Tags": [
306 {
307 "Key": "Environment",
308 "Value": "Production"
309 }
310 ]
311 }
312 '''
313
314 from ansible.module_utils.basic import AnsibleModule
315 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
316
317
318 def validate_passphrase(passphrase, field_name):
319 """
320 Validate that a passphrase meets security requirements.
321 """
322 if not passphrase:
323 return True, None
324 if len(passphrase) < 8:
325 return False, f"{field_name} must be at least 8 characters long"
326 has_upper = any(c.isupper() for c in passphrase)
327 has_lower = any(c.islower() for c in passphrase)
328 has_digit = any(c.isdigit() for c in passphrase)
329 has_special = any(not c.isalnum() for c in passphrase)
330 if not (has_upper and has_lower and has_digit and has_special):
331 return False, f"{field_name} must include at least one uppercase letter, one lowercase letter, one number, and one special character"
332 return True, None
333
334
335 def validate_drive_security_configuration(module):
336 """
337 Validate Drive Security Policy configuration parameters.
338 """
339 params = module.params
340 if params['state'] == 'present':
341 manual_key = params.get('manual_key')
342 remote_key = params.get('remote_key')
343 if not manual_key and not remote_key:
344 module.fail_json(msg="Either manual_key or remote_key must be specified")
345 if manual_key and remote_key:
346 module.fail_json(msg="Cannot specify both manual_key and remote_key - choose one key management type")
347 if manual_key:
348 if not manual_key.get('new_key'):
349 module.fail_json(msg="manual_key.new_key is required when using manual key management")
350 is_valid, error_msg = validate_passphrase(manual_key.get('new_key'), 'manual_key.new_key')
351 if not is_valid:
352 module.fail_json(msg=error_msg)
353 if manual_key.get('existing_key'):
354 is_valid, error_msg = validate_passphrase(manual_key.get('existing_key'), 'manual_key.existing_key')
355 if not is_valid:
356 module.fail_json(msg=error_msg)
357 elif remote_key:
358 if not remote_key.get('server_certificate'):
359 module.fail_json(msg="remote_key.server_certificate is required when using remote key management (KMIP)")
360 primary_server = remote_key.get('primary_server', {})
361 secondary_server = remote_key.get('secondary_server', {})
362 primary_enabled = primary_server.get('enable_drive_security', False)
363 secondary_enabled = secondary_server.get('enable_drive_security', False)
364 if not primary_enabled and not secondary_enabled:
365 module.fail_json(msg="At least one of primary_server or secondary_server must be enabled")
366 if primary_enabled:
367 if not primary_server.get('ip_address'):
368 module.fail_json(msg="primary_server.ip_address is required when primary_server is enabled")
369 port = primary_server.get('port', 5696)
370 if port < 1024 or port > 65535:
371 module.fail_json(msg="primary_server.port must be between 1024 and 65535")
372 timeout = primary_server.get('timeout', 60)
373 if timeout < 1 or timeout > 250:
374 module.fail_json(msg="primary_server.timeout must be between 1 and 250")
375 if secondary_enabled:
376 if not secondary_server.get('ip_address'):
377 module.fail_json(msg="secondary_server.ip_address is required when secondary_server is enabled")
378 port = secondary_server.get('port', 5696)
379 if port < 1024 or port > 65535:
380 module.fail_json(msg="secondary_server.port must be between 1024 and 65535")
381 timeout = secondary_server.get('timeout', 60)
382 if timeout < 1 or timeout > 250:
383 module.fail_json(msg="secondary_server.timeout must be between 1 and 250")
384 if remote_key.get('use_authentication'):
385 if not remote_key.get('username'):
386 module.fail_json(msg="remote_key.username is required when use_authentication is true")
387 if remote_key.get('existing_key'):
388 is_valid, error_msg = validate_passphrase(remote_key.get('existing_key'), 'remote_key.existing_key')
389 if not is_valid:
390 module.fail_json(msg=error_msg)
391
392
393 def build_manual_key_config(manual_key):
394 """
395 Build ManualKey configuration for API body.
396 """
397 manual_key_config = {
398 'NewKey': manual_key['new_key']
399 }
400 if manual_key.get('existing_key'):
401 manual_key_config['ExistingKey'] = manual_key['existing_key']
402 else:
403 manual_key_config['ExistingKey'] = ''
404 return manual_key_config
405
406
407 def build_remote_key_config(remote_key):
408 """
409 Build RemoteKey configuration for API body.
410 """
411 remote_key_config = {}
412 auth_credentials = {
413 'UseAuthentication': remote_key.get('use_authentication', False)
414 }
415 if remote_key.get('use_authentication'):
416 auth_credentials['Username'] = remote_key['username']
417 if remote_key.get('password'):
418 auth_credentials['Password'] = remote_key['password']
419 remote_key_config['AuthCredentials'] = auth_credentials
420 remote_key_config['ServerCertificate'] = remote_key['server_certificate']
421 primary_server = remote_key.get('primary_server', {})
422 primary_server_config = {
423 'EnableDriveSecurity': primary_server.get('enable_drive_security', False)
424 }
425 if primary_server.get('enable_drive_security'):
426 primary_server_config['IpAddress'] = primary_server['ip_address']
427 primary_server_config['Port'] = primary_server.get('port', 5696)
428 primary_server_config['Timeout'] = primary_server.get('timeout', 60)
429 else:
430 primary_server_config['IpAddress'] = ''
431 primary_server_config['Port'] = 5696
432 primary_server_config['Timeout'] = 60
433 remote_key_config['PrimaryServer'] = primary_server_config
434 secondary_server = remote_key.get('secondary_server', {})
435 secondary_server_config = {
436 'EnableDriveSecurity': secondary_server.get('enable_drive_security', False)
437 }
438 if secondary_server.get('enable_drive_security'):
439 secondary_server_config['IpAddress'] = secondary_server['ip_address']
440 secondary_server_config['Port'] = secondary_server.get('port', 5696)
441 secondary_server_config['Timeout'] = secondary_server.get('timeout', 60)
442 else:
443 secondary_server_config['IpAddress'] = ''
444 secondary_server_config['Port'] = 5696
445 secondary_server_config['Timeout'] = 60
446 remote_key_config['SecondaryServer'] = secondary_server_config
447 if remote_key.get('existing_key'):
448 remote_key_config['ExistingKey'] = remote_key['existing_key']
449 else:
450 remote_key_config['ExistingKey'] = ''
451 return remote_key_config
452
453
454 def build_api_body(intersight):
455 """
456 Build the API body for Drive Security Policy configuration.
457 """
458 params = intersight.module.params
459 if params['state'] == 'present':
460 intersight.api_body = {
461 'Organization': {
462 'Name': params['organization'],
463 },
464 'Name': params['name']
465 }
466 intersight.set_tags_and_description()
467 key_setting = {}
468 if params.get('manual_key'):
469 key_setting['KeyType'] = 'Manual'
470 key_setting['ManualKey'] = build_manual_key_config(params['manual_key'])
471 elif params.get('remote_key'):
472 key_setting['KeyType'] = 'Kmip'
473 key_setting['RemoteKey'] = build_remote_key_config(params['remote_key'])
474 intersight.api_body['KeySetting'] = key_setting
475
476
477 def main():
478 argument_spec = intersight_argument_spec.copy()
479 argument_spec.update(
480 state=dict(type='str', choices=['present', 'absent'], default='present'),
481 organization=dict(type='str', default='default'),
482 name=dict(type='str', required=True),
483 description=dict(type='str', aliases=['descr']),
484 tags=dict(type='list', elements='dict', default=[]),
485 manual_key=dict(
486 type='dict',
487 no_log=True,
488 options=dict(
489 new_key=dict(type='str', required=True, no_log=True),
490 existing_key=dict(type='str', no_log=True)
491 )
492 ),
493 remote_key=dict(
494 type='dict',
495 no_log=True,
496 options=dict(
497 primary_server=dict(
498 type='dict',
499 options=dict(
500 enable_drive_security=dict(type='bool', required=True),
501 ip_address=dict(type='str'),
502 port=dict(type='int', default=5696),
503 timeout=dict(type='int', default=60)
504 )
505 ),
506 secondary_server=dict(
507 type='dict',
508 options=dict(
509 enable_drive_security=dict(type='bool', required=True),
510 ip_address=dict(type='str'),
511 port=dict(type='int', default=5696),
512 timeout=dict(type='int', default=60)
513 )
514 ),
515 server_certificate=dict(type='str', required=True),
516 use_authentication=dict(type='bool', default=False),
517 username=dict(type='str'),
518 password=dict(type='str', no_log=True),
519 existing_key=dict(type='str', no_log=True)
520 )
521 )
522 )
523 module = AnsibleModule(
524 argument_spec,
525 supports_check_mode=True,
526 )
527 if module.params['state'] == 'present':
528 validate_drive_security_configuration(module)
529
530 intersight = IntersightModule(module)
531 intersight.result['api_response'] = {}
532 intersight.result['trace_id'] = ''
533
534 build_api_body(intersight)
535 resource_path = '/storage/DriveSecurityPolicies'
536 intersight.configure_policy_or_profile(resource_path=resource_path)
537
538 module.exit_json(**intersight.result)
539
540
541 if __name__ == '__main__':
542 main()