Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/modules/na_santricity_auth.py

raw
1 #!/usr/bin/python
2
3 # (c) 2020, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8
9 DOCUMENTATION = """
10 ---
11 module: na_santricity_auth
12 short_description: NetApp E-Series set or update the password for a storage array device or SANtricity Web Services Proxy.
13 description:
14 - Sets or updates the password for a storage array device or SANtricity Web Services Proxy.
15 author:
16 - Nathan Swartz (@ndswartz)
17 extends_documentation_fragment:
18 - netapp_eseries.santricity.santricity.santricity_doc
19 options:
20 current_admin_password:
21 description:
22 - The current admin password.
23 - When making changes to the embedded web services's login passwords, api_password will be used and current_admin_password will be ignored.
24 - When making changes to the proxy web services's login passwords, api_password will be used and current_admin_password will be ignored.
25 - Only required when the password has been set and will be ignored if not set.
26 type: str
27 required: false
28 password:
29 description:
30 - The password you would like to set.
31 - Cannot be more than 30 characters.
32 type: str
33 required: false
34 user:
35 description:
36 - The local user account password to update
37 - For systems prior to E2800, use admin to change the rw (system password).
38 - For systems prior to E2800, all choices except admin will be ignored.
39 type: str
40 choices: ["admin", "monitor", "support", "security", "storage"]
41 default: "admin"
42 required: false
43 minimum_password_length:
44 description:
45 - This option defines the minimum password length.
46 type: int
47 required: false
48 notes:
49 - Set I(ssid=="0") or I(ssid=="proxy") when attempting to change the password for SANtricity Web Services Proxy.
50 - SANtricity Web Services Proxy storage password will be updated when changing the password on a managed storage system from the proxy; This is only true
51 when the storage system has been previously contacted.
52 """
53
54 EXAMPLES = """
55 - name: Set the initial password
56 na_santricity_auth:
57 ssid: 1
58 api_url: https://192.168.1.100:8443/devmgr/v2
59 api_username: admin
60 api_password: adminpass
61 validate_certs: true
62 current_admin_password: currentadminpass
63 password: newpassword123
64 user: admin
65 """
66
67 RETURN = """
68 msg:
69 description: Success message
70 returned: success
71 type: str
72 sample: "Password Updated Successfully"
73 """
74 from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule
75 from ansible.module_utils._text import to_native
76
77
78 class NetAppESeriesAuth(NetAppESeriesModule):
79 def __init__(self):
80 version = "02.00.0000.0000"
81 ansible_options = dict(current_admin_password=dict(type="str", required=False, no_log=True),
82 password=dict(type="str", required=False, no_log=True),
83 user=dict(type="str", choices=["admin", "monitor", "support", "security", "storage"], default="admin", required=False),
84 minimum_password_length=dict(type="int", required=False, no_log=True))
85
86 super(NetAppESeriesAuth, self).__init__(ansible_options=ansible_options, web_services_version=version, supports_check_mode=True)
87 args = self.module.params
88 self.current_admin_password = args["current_admin_password"]
89 self.password = args["password"]
90 self.user = args["user"]
91 self.minimum_password_length = args["minimum_password_length"]
92
93 self.DEFAULT_HEADERS.update({"x-netapp-password-validate-method": "none"})
94
95 self.is_admin_password_set = None
96 self.current_password_length_requirement = None
97
98 def minimum_password_length_change_required(self):
99 """Retrieve the current storage array's global configuration."""
100 change_required = False
101 try:
102 if self.is_proxy():
103 if self.ssid == "0" or self.ssid.lower() == "proxy":
104 rc, system_info = self.request("local-users/info", force_basic_auth=False)
105
106 elif self.is_embedded_available():
107 rc, system_info = self.request("storage-systems/%s/forward/devmgr/v2/storage-systems/1/local-users/info" % self.ssid,
108 force_basic_auth=False)
109 else:
110 return False # legacy systems without embedded web services.
111 else:
112 rc, system_info = self.request("storage-systems/%s/local-users/info" % self.ssid, force_basic_auth=False)
113 except Exception as error:
114 self.module.fail_json(msg="Failed to determine minimum password length. Array [%s]. Error [%s]." % (self.ssid, to_native(error)))
115
116 self.is_admin_password_set = system_info["adminPasswordSet"]
117 if self.minimum_password_length is not None and self.minimum_password_length != system_info["minimumPasswordLength"]:
118 change_required = True
119
120 if (self.password is not None and ((change_required and self.minimum_password_length > len(self.password)) or
121 (not change_required and system_info["minimumPasswordLength"] > len(self.password)))):
122 self.module.fail_json(msg="Password does not meet the length requirement [%s]. Array Id [%s]." % (system_info["minimumPasswordLength"], self.ssid))
123
124 return change_required
125
126 def update_minimum_password_length(self):
127 """Update automatic load balancing state."""
128 try:
129 if self.is_proxy():
130 if self.ssid == "0" or self.ssid.lower() == "proxy":
131 try:
132 if not self.is_admin_password_set:
133 self.creds["url_password"] = "admin"
134 rc, minimum_password_length = self.request("local-users/password-length", method="POST",
135 data={"minimumPasswordLength": self.minimum_password_length})
136 except Exception as error:
137 if not self.is_admin_password_set:
138 self.creds["url_password"] = ""
139 rc, minimum_password_length = self.request("local-users/password-length", method="POST",
140 data={"minimumPasswordLength": self.minimum_password_length})
141 elif self.is_embedded_available():
142 if not self.is_admin_password_set:
143 self.creds["url_password"] = ""
144 rc, minimum_password_length = self.request("storage-systems/%s/forward/devmgr/v2/storage-systems/1/local-users/password-length" % self.ssid,
145 method="POST", data={"minimumPasswordLength": self.minimum_password_length})
146 else:
147 if not self.is_admin_password_set:
148 self.creds["url_password"] = ""
149 rc, minimum_password_length = self.request("storage-systems/%s/local-users/password-length" % self.ssid, method="POST",
150 data={"minimumPasswordLength": self.minimum_password_length})
151 except Exception as error:
152 self.module.fail_json(msg="Failed to set minimum password length. Array [%s]. Error [%s]." % (self.ssid, to_native(error)))
153
154 def logout_system(self):
155 """Ensure system is logged out. This is required because login test will always succeed if previously logged in."""
156 try:
157 if self.is_proxy():
158 if self.ssid == "0" or self.ssid.lower() == "proxy":
159 rc, system_info = self.request("utils/login", rest_api_path=self.DEFAULT_BASE_PATH, method="DELETE", force_basic_auth=False)
160 elif self.is_embedded_available():
161 rc, system_info = self.request("storage-systems/%s/forward/devmgr/utils/login" % self.ssid, method="DELETE", force_basic_auth=False)
162 else:
163 # Nothing to do for legacy systems without embedded web services.
164 pass
165 else:
166 rc, system_info = self.request("utils/login", rest_api_path=self.DEFAULT_BASE_PATH, method="DELETE", force_basic_auth=False)
167 except Exception as error:
168 self.module.fail_json(msg="Failed to log out of storage system [%s]. Error [%s]." % (self.ssid, to_native(error)))
169
170 def password_change_required(self):
171 """Verify whether the current password is expected array password. Works only against embedded systems."""
172 if self.password is None:
173 return False
174
175 change_required = False
176 system_info = None
177 try:
178 if self.is_proxy():
179 if self.ssid == "0" or self.ssid.lower() == "proxy":
180 rc, system_info = self.request("local-users/info", force_basic_auth=False)
181 elif self.is_embedded_available():
182 rc, system_info = self.request("storage-systems/%s/forward/devmgr/v2/storage-systems/1/local-users/info" % self.ssid,
183 force_basic_auth=False)
184 else:
185 rc, response = self.request("storage-systems/%s/passwords" % self.ssid, ignore_errors=True)
186 system_info = {"minimumPasswordLength": 0, "adminPasswordSet": response["adminPasswordSet"]}
187 else:
188 rc, system_info = self.request("storage-systems/%s/local-users/info" % self.ssid, force_basic_auth=False)
189 except Exception as error:
190 self.module.fail_json(msg="Failed to retrieve information about storage system [%s]. Error [%s]." % (self.ssid, to_native(error)))
191
192 self.is_admin_password_set = system_info.get("adminPasswordSet", False) \
193 if isinstance(system_info, dict) else False
194
195 if not self.is_admin_password_set:
196 if self.user == "admin" and self.password != "":
197 change_required = True
198
199 # Determine whether user's password needs to be changed
200 else:
201 utils_login_used = False
202 self.logout_system() # This ensures that login test functions correctly. The query onlycheck=true does not work.
203
204 if self.is_proxy():
205 if self.ssid == "0" or self.ssid.lower() == "proxy":
206 utils_login_used = True
207 rc, response = self.request("utils/login?uid=%s&pwd=%s&xsrf=false&onlycheck=false" % (self.user, self.password),
208 rest_api_path=self.DEFAULT_BASE_PATH, log_request=False, ignore_errors=True, force_basic_auth=False)
209 # elif self.is_embedded_available():
210 # utils_login_used = True
211 # rc, response = self.request("storage-systems/%s/forward/devmgr/utils/login?uid=%s&pwd=%s&xsrf=false&onlycheck=false"
212 # % (self.ssid, self.user, self.password), log_request=False, ignore_errors=True, force_basic_auth=False)
213 else:
214 if self.user == "admin":
215 rc, response = self.request("storage-systems/%s/stored-password/validate" % self.ssid, method="POST", log_request=False,
216 ignore_errors=True, data={"password": self.password})
217 if rc == 200:
218 change_required = not response.get("isValidPassword", False) \
219 if isinstance(response, dict) else False
220 elif rc == 404: # endpoint did not exist, old proxy version
221 if self.is_web_services_version_met("04.10.0000.0000"):
222 self.module.fail_json(msg="For platforms before E2800 use SANtricity Web Services Proxy 4.1 or later! Array Id [%s].")
223 self.module.fail_json(msg="Failed to validate stored password! Array Id [%s].")
224 else:
225 self.module.fail_json(msg="Failed to validate stored password! Array Id [%s]." % self.ssid)
226 else:
227 self.module.fail_json(msg="Role based login not available! Only storage system password can be set for storage systems prior to E2800."
228 " Array Id [%s]." % self.ssid)
229 else:
230 utils_login_used = True
231 rc, response = self.request("utils/login?uid=%s&pwd=%s&xsrf=false&onlycheck=false" % (self.user, self.password),
232 rest_api_path=self.DEFAULT_BASE_PATH, log_request=False, ignore_errors=True, force_basic_auth=False)
233
234 # Check return codes to determine whether a change is required
235 if utils_login_used:
236 if rc == 401:
237 change_required = True
238 elif rc == 422:
239 self.module.fail_json(msg="SAML enabled! SAML disables default role based login. Array [%s]" % self.ssid)
240
241 return change_required
242
243 def set_array_admin_password(self):
244 """Set the array's admin password."""
245 if self.is_proxy():
246
247 # Update proxy's local users
248 if self.ssid == "0" or self.ssid.lower() == "proxy":
249 self.creds["url_password"] = "admin"
250 try:
251 body = {"currentAdminPassword": "", "updates": {"userName": "admin", "newPassword": self.password}}
252 rc, proxy = self.request("local-users", method="POST", data=body)
253 except Exception as error:
254 self.creds["url_password"] = ""
255 try:
256 body = {"currentAdminPassword": "", "updates": {"userName": "admin", "newPassword": self.password}}
257 rc, proxy = self.request("local-users", method="POST", data=body)
258 except Exception as error:
259 self.module.fail_json(msg="Failed to set proxy's admin password. Error [%s]." % to_native(error))
260
261 self.creds["url_password"] = self.password
262
263 # Update password using the password endpoints, this will also update the storaged password
264 else:
265 try:
266 body = {"currentAdminPassword": "", "newPassword": self.password, "adminPassword": True}
267 rc, storage_system = self.request("storage-systems/%s/passwords" % self.ssid, method="POST", data=body)
268 except Exception as error:
269 self.module.fail_json(msg="Failed to set storage system's admin password. Array [%s]. Error [%s]." % (self.ssid, to_native(error)))
270
271 # Update embedded local users
272 else:
273 self.creds["url_password"] = ""
274 try:
275 body = {"currentAdminPassword": "", "updates": {"userName": "admin", "newPassword": self.password}}
276 rc, proxy = self.request("storage-systems/%s/local-users" % self.ssid, method="POST", data=body)
277 except Exception as error:
278 self.module.fail_json(msg="Failed to set embedded storage system's admin password. Array [%s]. Error [%s]." % (self.ssid, to_native(error)))
279 self.creds["url_password"] = self.password
280
281 def set_array_password(self):
282 """Set the array password."""
283 if not self.is_admin_password_set:
284 self.module.fail_json(msg="Admin password not set! Set admin password before changing non-admin user passwords. Array [%s]." % self.ssid)
285
286 if self.is_proxy():
287
288 # Update proxy's local users
289 if self.ssid == "0" or self.ssid.lower() == "proxy":
290 try:
291 body = {"currentAdminPassword": self.creds["url_password"], "updates": {"userName": self.user, "newPassword": self.password}}
292 rc, proxy = self.request("local-users", method="POST", data=body)
293 except Exception as error:
294 self.module.fail_json(msg="Failed to set proxy password. Error [%s]." % to_native(error))
295
296 # Update embedded admin password via proxy passwords endpoint to include updating proxy/unified manager
297 elif self.user == "admin":
298 try:
299 body = {"adminPassword": True, "currentAdminPassword": self.current_admin_password, "newPassword": self.password}
300 rc, proxy = self.request("storage-systems/%s/passwords" % self.ssid, method="POST", data=body)
301 except Exception as error:
302 self.module.fail_json(msg="Failed to set embedded user password. Array [%s]. Error [%s]." % (self.ssid, to_native(error)))
303
304 # Update embedded non-admin passwords via proxy forward endpoint.
305 elif self.is_embedded_available():
306 try:
307 body = {"currentAdminPassword": self.current_admin_password, "updates": {"userName": self.user, "newPassword": self.password}}
308 rc, proxy = self.request("storage-systems/%s/forward/devmgr/v2/storage-systems/1/local-users" % self.ssid, method="POST", data=body)
309 except Exception as error:
310 self.module.fail_json(msg="Failed to set embedded user password. Array [%s]. Error [%s]." % (self.ssid, to_native(error)))
311
312 # Update embedded local users
313 else:
314 try:
315 body = {"currentAdminPassword": self.creds["url_password"], "updates": {"userName": self.user, "newPassword": self.password}}
316 rc, proxy = self.request("storage-systems/%s/local-users" % self.ssid, method="POST", data=body)
317 except Exception as error:
318 self.module.fail_json(msg="Failed to set embedded user password. Array [%s]. Error [%s]." % (self.ssid, to_native(error)))
319
320 def apply(self):
321 """Apply any required changes."""
322 password_change_required = self.password_change_required()
323 minimum_password_length_change_required = self.minimum_password_length_change_required()
324 change_required = password_change_required or minimum_password_length_change_required
325
326 if change_required and not self.module.check_mode:
327 if minimum_password_length_change_required:
328 self.update_minimum_password_length()
329
330 if password_change_required:
331 if not self.is_admin_password_set:
332 self.set_array_admin_password()
333 else:
334 self.set_array_password()
335
336 if password_change_required and minimum_password_length_change_required:
337 self.module.exit_json(msg="'%s' password and required password length has been changed. Array [%s]."
338 % (self.user, self.ssid), changed=change_required)
339 elif password_change_required:
340 self.module.exit_json(msg="'%s' password has been changed. Array [%s]." % (self.user, self.ssid), changed=change_required)
341 elif minimum_password_length_change_required:
342 self.module.exit_json(msg="Required password length has been changed. Array [%s]." % self.ssid, changed=change_required)
343 self.module.exit_json(msg="No changes have been made. Array [%s]." % self.ssid, changed=change_required)
344
345
346 def main():
347 auth = NetAppESeriesAuth()
348 auth.apply()
349
350
351 if __name__ == "__main__":
352 main()