| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
''' |
| 10 |
na_ontap_security_ssh |
| 11 |
''' |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
module: na_ontap_security_ssh |
| 16 |
short_description: NetApp ONTAP security ssh |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap_rest |
| 19 |
version_added: 21.24.0 |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
description: |
| 22 |
- Modify SSH server configuration of SVM on ONTAP |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- SSH service is always enabled. |
| 27 |
choices: ['present'] |
| 28 |
type: str |
| 29 |
default: present |
| 30 |
vserver: |
| 31 |
description: |
| 32 |
- Name of the vserver to use for vserver scope. |
| 33 |
- If absent or null, cluster scope is assumed. |
| 34 |
type: str |
| 35 |
ciphers: |
| 36 |
description: |
| 37 |
- Ciphers for encrypting the data. |
| 38 |
- Example list [ aes256_ctr, aes192_ctr, aes128_ctr, aes256_cbc, aes192_cbc ] |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
key_exchange_algorithms: |
| 42 |
description: |
| 43 |
- Key exchange algorithms. |
| 44 |
- Example list [ diffie_hellman_group_exchange_sha256, diffie_hellman_group14_sha1 ] |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
mac_algorithms: |
| 48 |
description: |
| 49 |
- MAC algorithms. |
| 50 |
- Example list [ hmac_sha1, hmac_sha2_512_etm ] |
| 51 |
type: list |
| 52 |
elements: str |
| 53 |
max_authentication_retry_count: |
| 54 |
description: |
| 55 |
- Maximum authentication retries allowed before closing the connection. |
| 56 |
- Minimum value is 2 and maximum is 6. |
| 57 |
- Default value is 2. |
| 58 |
type: int |
| 59 |
host_key_algorithms: |
| 60 |
description: |
| 61 |
- Enables the specified host key algorithms for the Vserver. It replaces all existing host key algorithms with the specified settings. |
| 62 |
- The host key algorithm "ssh_ed25519" can be configured only in non-FIPS mode. |
| 63 |
- Requires ONTAP 9.16.1 and later. |
| 64 |
- Example list [ "ecdsa_sha2_nistp256", "ssh_rsa", "ssh_ed25519" ] |
| 65 |
type: list |
| 66 |
elements: str |
| 67 |
version_added: 23.2.0 |
| 68 |
|
| 69 |
notes: |
| 70 |
- Removing all SSH key exchange algorithms is not supported. SSH login would fail. |
| 71 |
- This module is only for REST. |
| 72 |
''' |
| 73 |
|
| 74 |
EXAMPLES = """ |
| 75 |
- name: Modify SSH algorithms |
| 76 |
netapp.ontap.na_ontap_security_ssh: |
| 77 |
vserver: vserverName |
| 78 |
ciphers: ["aes256_ctr", "aes192_ctr"] |
| 79 |
key_exchange_algorithms: ["diffie_hellman_group_exchange_sha256"] |
| 80 |
mac_algorithms: ["hmac_sha1"] |
| 81 |
max_authentication_retry_count: 6 |
| 82 |
hostname: "{{ netapp_hostname }}" |
| 83 |
username: "{{ netapp_username }}" |
| 84 |
password: "{{ netapp_password }}" |
| 85 |
|
| 86 |
- name: Modify SSH algorithms at cluster level |
| 87 |
netapp.ontap.na_ontap_security_ssh: |
| 88 |
vserver: |
| 89 |
ciphers: ["aes256_ctr", "aes192_ctr"] |
| 90 |
key_exchange_algorithms: ["diffie_hellman_group_exchange_sha256"] |
| 91 |
mac_algorithms: ["hmac_sha1"] |
| 92 |
max_authentication_retry_count: 6 |
| 93 |
hostname: "{{ netapp_hostname }}" |
| 94 |
username: "{{ netapp_username }}" |
| 95 |
password: "{{ netapp_password }}" |
| 96 |
|
| 97 |
- name: Modify SSH algorithms at cluster level |
| 98 |
netapp.ontap.na_ontap_security_ssh: |
| 99 |
ciphers: ["aes256_ctr", "aes192_ctr"] |
| 100 |
key_exchange_algorithms: ["diffie_hellman_group_exchange_sha256"] |
| 101 |
mac_algorithms: ["hmac_sha1"] |
| 102 |
max_authentication_retry_count: 6 |
| 103 |
hostname: "{{ netapp_hostname }}" |
| 104 |
username: "{{ netapp_username }}" |
| 105 |
password: "{{ netapp_password }}" |
| 106 |
""" |
| 107 |
|
| 108 |
RETURN = """ |
| 109 |
""" |
| 110 |
|
| 111 |
from ansible.module_utils.basic import AnsibleModule |
| 112 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 113 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 114 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 115 |
|
| 116 |
|
| 117 |
class NetAppOntapSecuritySSH: |
| 118 |
""" object initialize and class methods """ |
| 119 |
def __init__(self): |
| 120 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 121 |
self.argument_spec.update(dict( |
| 122 |
state=dict(required=False, type='str', choices=['present'], default='present'), |
| 123 |
vserver=dict(required=False, type='str'), |
| 124 |
ciphers=dict(required=False, type='list', elements='str'), |
| 125 |
key_exchange_algorithms=dict(required=False, type='list', elements='str', no_log=False), |
| 126 |
mac_algorithms=dict(required=False, type='list', elements='str'), |
| 127 |
max_authentication_retry_count=dict(required=False, type='int'), |
| 128 |
host_key_algorithms=dict(required=False, type='list', elements='str', no_log=False), |
| 129 |
)) |
| 130 |
|
| 131 |
self.module = AnsibleModule( |
| 132 |
argument_spec=self.argument_spec, |
| 133 |
supports_check_mode=True |
| 134 |
) |
| 135 |
self.na_helper = NetAppModule(self) |
| 136 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 137 |
self.svm_uuid = None |
| 138 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 139 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_security_ssh', 9, 10, 1) |
| 140 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, None, [['host_key_algorithms', (9, 16, 1)]]) |
| 141 |
self.safe_strip() |
| 142 |
|
| 143 |
def safe_strip(self): |
| 144 |
""" strip the left and right spaces of string and also removes an empty string""" |
| 145 |
for option in ('ciphers', 'key_exchange_algorithms', 'mac_algorithms'): |
| 146 |
if option in self.parameters: |
| 147 |
self.parameters[option] = [item.strip() for item in self.parameters[option] if len(item.strip())] |
| 148 |
# Validation of input parameters |
| 149 |
if self.parameters[option] == []: |
| 150 |
self.module.fail_json(msg="Removing all SSH %s is not supported. SSH login would fail. " |
| 151 |
"There must be at least one %s associated with the SSH configuration." % (option, option)) |
| 152 |
return |
| 153 |
|
| 154 |
def get_security_ssh_rest(self): |
| 155 |
''' |
| 156 |
Retrieves the SSH server configuration for the SVM or cluster. |
| 157 |
''' |
| 158 |
fields = ['key_exchange_algorithms', 'ciphers', 'mac_algorithms', 'max_authentication_retry_count'] |
| 159 |
if self.parameters.get('host_key_algorithms'): |
| 160 |
fields.append('host_key_algorithms') |
| 161 |
query = {} |
| 162 |
if self.parameters.get('vserver'): |
| 163 |
api = 'security/ssh/svms' |
| 164 |
query['svm.name'] = self.parameters['vserver'] |
| 165 |
fields.append('svm.uuid') |
| 166 |
else: |
| 167 |
api = 'security/ssh' |
| 168 |
query['fields'] = ','.join(fields) |
| 169 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 170 |
if error: |
| 171 |
self.module.fail_json(msg=error) |
| 172 |
return record |
| 173 |
|
| 174 |
def modify_security_ssh_rest(self, modify): |
| 175 |
''' |
| 176 |
Updates the SSH server configuration for the specified SVM. |
| 177 |
''' |
| 178 |
if self.parameters.get('vserver'): |
| 179 |
if self.svm_uuid is None: |
| 180 |
self.module.fail_json(msg="Error: no uuid found for the SVM") |
| 181 |
api = 'security/ssh/svms' |
| 182 |
else: |
| 183 |
api = 'security/ssh' |
| 184 |
body = {} |
| 185 |
for option in ('ciphers', 'key_exchange_algorithms', 'mac_algorithms', 'max_authentication_retry_count', 'host_key_algorithms'): |
| 186 |
if option in modify: |
| 187 |
body[option] = modify[option] |
| 188 |
if body: |
| 189 |
dummy, error = rest_generic.patch_async(self.rest_api, api, self.svm_uuid, body) |
| 190 |
if error: |
| 191 |
self.module.fail_json(msg=error) |
| 192 |
|
| 193 |
def apply(self): |
| 194 |
current = self.get_security_ssh_rest() |
| 195 |
self.svm_uuid = self.na_helper.safe_get(current, ['svm', 'uuid']) if current and self.parameters.get('vserver') else None |
| 196 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 197 |
if self.na_helper.changed and not self.module.check_mode: |
| 198 |
self.modify_security_ssh_rest(modify) |
| 199 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modify) |
| 200 |
self.module.exit_json(**result) |
| 201 |
|
| 202 |
|
| 203 |
def main(): |
| 204 |
""" Create object and call apply """ |
| 205 |
ssh_obj = NetAppOntapSecuritySSH() |
| 206 |
ssh_obj.apply() |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == '__main__': |
| 210 |
main() |