| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
module: na_santricity_client_certificate |
| 11 |
short_description: NetApp E-Series manage remote server certificates. |
| 12 |
description: Manage NetApp E-Series storage array's remote server certificates. |
| 13 |
author: |
| 14 |
- Nathan Swartz (@swartzn) |
| 15 |
- Vu Tran (@VuTran007) |
| 16 |
extends_documentation_fragment: |
| 17 |
- netapp_eseries.santricity.santricity.santricity_doc |
| 18 |
options: |
| 19 |
certificates: |
| 20 |
description: |
| 21 |
- List of certificate files |
| 22 |
- Each item must include the path to the file |
| 23 |
type: list |
| 24 |
elements: str |
| 25 |
required: false |
| 26 |
remove_unspecified_user_certificates: |
| 27 |
description: |
| 28 |
- Whether to remove user install client certificates that are not specified in I(certificates). |
| 29 |
type: bool |
| 30 |
default: false |
| 31 |
required: false |
| 32 |
reload_certificates: |
| 33 |
description: |
| 34 |
- Whether to reload certificates when certificates have been added or removed. |
| 35 |
- Certificates will not be available or removed until the servers have been reloaded. |
| 36 |
type: bool |
| 37 |
default: true |
| 38 |
required: false |
| 39 |
notes: |
| 40 |
- Set I(ssid=="0") or I(ssid=="proxy") to specifically reference SANtricity Web Services Proxy. |
| 41 |
requirements: |
| 42 |
- cryptography |
| 43 |
""" |
| 44 |
EXAMPLES = """ |
| 45 |
- name: Upload certificates |
| 46 |
na_santricity_client_certificate: |
| 47 |
ssid: 1 |
| 48 |
api_url: https://192.168.1.100:8443/devmgr/v2 |
| 49 |
api_username: admin |
| 50 |
api_password: adminpass |
| 51 |
certificates: ["/path/to/certificates.crt", "/path/to/another_certificate.crt"] |
| 52 |
- name: Remove all certificates |
| 53 |
na_santricity_client_certificate: |
| 54 |
ssid: 1 |
| 55 |
api_url: https://192.168.1.100:8443/devmgr/v2 |
| 56 |
api_username: admin |
| 57 |
api_password: adminpass |
| 58 |
""" |
| 59 |
RETURN = """ |
| 60 |
changed: |
| 61 |
description: Whether changes have been made. |
| 62 |
type: bool |
| 63 |
returned: always |
| 64 |
sample: true |
| 65 |
add_certificates: |
| 66 |
description: Any SSL certificates that were added. |
| 67 |
type: list |
| 68 |
returned: always |
| 69 |
sample: ["added_cerificiate.crt"] |
| 70 |
removed_certificates: |
| 71 |
description: Any SSL certificates that were removed. |
| 72 |
type: list |
| 73 |
returned: always |
| 74 |
sample: ["removed_cerificiate.crt"] |
| 75 |
""" |
| 76 |
|
| 77 |
import binascii |
| 78 |
import os |
| 79 |
import re |
| 80 |
from time import sleep |
| 81 |
|
| 82 |
from datetime import datetime |
| 83 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule, create_multipart_formdata |
| 84 |
from ansible.module_utils._text import to_native |
| 85 |
|
| 86 |
try: |
| 87 |
from cryptography import x509 |
| 88 |
from cryptography.hazmat.backends import default_backend |
| 89 |
except ImportError: |
| 90 |
HAS_CRYPTOGRAPHY = False |
| 91 |
else: |
| 92 |
HAS_CRYPTOGRAPHY = True |
| 93 |
|
| 94 |
|
| 95 |
class NetAppESeriesClientCertificate(NetAppESeriesModule): |
| 96 |
RELOAD_TIMEOUT_SEC = 3 * 60 |
| 97 |
|
| 98 |
def __init__(self): |
| 99 |
ansible_options = dict(certificates=dict(type="list", elements="str", required=False), |
| 100 |
remove_unspecified_user_certificates=dict(type="bool", default=False, required=False), |
| 101 |
reload_certificates=dict(type="bool", default=True, required=False)) |
| 102 |
|
| 103 |
super(NetAppESeriesClientCertificate, self).__init__(ansible_options=ansible_options, |
| 104 |
web_services_version="02.00.0000.0000", |
| 105 |
supports_check_mode=True) |
| 106 |
|
| 107 |
args = self.module.params |
| 108 |
self.certificates = args["certificates"] if args["certificates"] else [] |
| 109 |
self.remove_unspecified_user_certificates = args["remove_unspecified_user_certificates"] |
| 110 |
self.apply_reload_certificates = args["reload_certificates"] |
| 111 |
|
| 112 |
|
| 113 |
self.url_path_prefix = "" |
| 114 |
if self.is_proxy() and self.ssid != "0" and self.ssid.lower() != "proxy": |
| 115 |
self.url_path_prefix = "storage-systems/%s/forward/devmgr/v2/" % self.ssid |
| 116 |
|
| 117 |
self.remove_certificates = list() |
| 118 |
self.add_certificates = list() |
| 119 |
self.certificate_fingerprint_cache = None |
| 120 |
self.certificate_info_cache = None |
| 121 |
|
| 122 |
def certificate_info(self, path): |
| 123 |
"""Determine the pertinent certificate information: alias, subjectDN, issuerDN, start and expire. |
| 124 |
|
| 125 |
Note: Use only when certificate/remote-server endpoints do not exist. Used to identify certificates through |
| 126 |
the sslconfig/ca endpoint. |
| 127 |
""" |
| 128 |
certificate = None |
| 129 |
with open(path, "rb") as fh: |
| 130 |
data = fh.read() |
| 131 |
try: |
| 132 |
certificate = x509.load_pem_x509_certificate(data, default_backend()) |
| 133 |
except Exception as error: |
| 134 |
try: |
| 135 |
certificate = x509.load_der_x509_certificate(data, default_backend()) |
| 136 |
except Exception as error: |
| 137 |
self.module.fail_json(msg="Failed to load certificate. Array [%s]. Error [%s]." % (self.ssid, to_native(error))) |
| 138 |
|
| 139 |
if not isinstance(certificate, x509.Certificate): |
| 140 |
self.module.fail_json(msg="Failed to open certificate file or invalid certificate object type. Array [%s]." % self.ssid) |
| 141 |
|
| 142 |
return dict(start_date=certificate.not_valid_before_utc, |
| 143 |
expire_date=certificate.not_valid_after_utc, |
| 144 |
subject_dn=[attr.value for attr in certificate.subject], |
| 145 |
issuer_dn=[attr.value for attr in certificate.issuer]) |
| 146 |
|
| 147 |
def certificate_fingerprint(self, path): |
| 148 |
"""Load x509 certificate that is either encoded DER or PEM encoding and return the certificate fingerprint.""" |
| 149 |
certificate = None |
| 150 |
with open(path, "rb") as fh: |
| 151 |
data = fh.read() |
| 152 |
try: |
| 153 |
certificate = x509.load_pem_x509_certificate(data, default_backend()) |
| 154 |
except Exception as error: |
| 155 |
try: |
| 156 |
certificate = x509.load_der_x509_certificate(data, default_backend()) |
| 157 |
except Exception as error: |
| 158 |
self.module.fail_json(msg="Failed to determine certificate fingerprint. File [%s]. Array [%s]. Error [%s]." |
| 159 |
% (path, self.ssid, to_native(error))) |
| 160 |
|
| 161 |
return binascii.hexlify(certificate.fingerprint(certificate.signature_hash_algorithm)).decode("utf-8") |
| 162 |
|
| 163 |
def determine_changes(self): |
| 164 |
"""Search for remote server certificate that goes by the alias or has a matching fingerprint.""" |
| 165 |
rc, current_certificates = self.request(self.url_path_prefix + "certificates/remote-server", ignore_errors=True) |
| 166 |
|
| 167 |
if rc == 404: |
| 168 |
rc, current_certificates = self.request(self.url_path_prefix + "sslconfig/ca?useTruststore=true", ignore_errors=True) |
| 169 |
|
| 170 |
if rc > 299: |
| 171 |
self.module.fail_json(msg="Failed to retrieve remote server certificates. Array [%s]." % self.ssid) |
| 172 |
|
| 173 |
user_installed_certificates = [certificate for certificate in current_certificates if certificate["isUserInstalled"]] |
| 174 |
existing_certificates = [] |
| 175 |
|
| 176 |
for path in self.certificates: |
| 177 |
for current_certificate in user_installed_certificates: |
| 178 |
info = self.certificate_info(path) |
| 179 |
tmp = dict(subject_dn=[re.sub(r".*=", "", item) for item in current_certificate["subjectDN"].split(", ")], |
| 180 |
issuer_dn=[re.sub(r".*=", "", item) for item in current_certificate["issuerDN"].split(", ")], |
| 181 |
start_date=datetime.strptime(current_certificate["start"].split(".")[0], "%Y-%m-%dT%H:%M:%S"), |
| 182 |
expire_date=datetime.strptime(current_certificate["expire"].split(".")[0], "%Y-%m-%dT%H:%M:%S")) |
| 183 |
if (all((attr in info["subject_dn"] for attr in tmp["subject_dn"])) and |
| 184 |
all((attr in info["issuer_dn"] for attr in tmp["issuer_dn"])) and |
| 185 |
tmp["start_date"] == info["start_date"] and |
| 186 |
tmp["expire_date"] == info["expire_date"]): |
| 187 |
existing_certificates.append(current_certificate) |
| 188 |
break |
| 189 |
else: |
| 190 |
self.add_certificates.append(path) |
| 191 |
if self.remove_unspecified_user_certificates: |
| 192 |
self.remove_certificates = [certificate for certificate in user_installed_certificates if certificate not in existing_certificates] |
| 193 |
|
| 194 |
elif rc > 299: |
| 195 |
self.module.fail_json(msg="Failed to retrieve remote server certificates. Array [%s]." % self.ssid) |
| 196 |
|
| 197 |
else: |
| 198 |
user_installed_certificates = [certificate for certificate in current_certificates if certificate["isUserInstalled"]] |
| 199 |
existing_certificates = [] |
| 200 |
for path in self.certificates: |
| 201 |
fingerprint = self.certificate_fingerprint(path) |
| 202 |
for current_certificate in user_installed_certificates: |
| 203 |
if current_certificate["sha256Fingerprint"] == fingerprint or current_certificate["shaFingerprint"] == fingerprint: |
| 204 |
existing_certificates.append(current_certificate) |
| 205 |
break |
| 206 |
else: |
| 207 |
self.add_certificates.append(path) |
| 208 |
if self.remove_unspecified_user_certificates: |
| 209 |
self.remove_certificates = [certificate for certificate in user_installed_certificates if certificate not in existing_certificates] |
| 210 |
|
| 211 |
def upload_certificate(self, path): |
| 212 |
"""Add or update remote server certificate to the storage array.""" |
| 213 |
file_name = os.path.basename(path) |
| 214 |
headers, data = create_multipart_formdata(files=[("file", file_name, path)]) |
| 215 |
|
| 216 |
rc, resp = self.request(self.url_path_prefix + "certificates/remote-server", method="POST", headers=headers, data=data, ignore_errors=True) |
| 217 |
if rc == 404: |
| 218 |
rc, resp = self.request(self.url_path_prefix + "sslconfig/ca?useTruststore=true", method="POST", headers=headers, data=data, ignore_errors=True) |
| 219 |
|
| 220 |
if rc > 299: |
| 221 |
self.module.fail_json(msg="Failed to upload certificate. Array [%s]. Error [%s, %s]." % (self.ssid, rc, resp)) |
| 222 |
|
| 223 |
def delete_certificate(self, info): |
| 224 |
"""Delete existing remote server certificate in the storage array truststore.""" |
| 225 |
rc, resp = self.request(self.url_path_prefix + "certificates/remote-server/%s" % info["alias"], method="DELETE", ignore_errors=True) |
| 226 |
if rc == 404: |
| 227 |
rc, resp = self.request(self.url_path_prefix + "sslconfig/ca/%s?useTruststore=true" % info["alias"], method="DELETE", ignore_errors=True) |
| 228 |
|
| 229 |
if rc > 204: |
| 230 |
self.module.fail_json(msg="Failed to delete certificate. Alias [%s]. Array [%s]. Error [%s, %s]." % (info["alias"], self.ssid, rc, resp)) |
| 231 |
|
| 232 |
def reload_certificates(self): |
| 233 |
"""Reload certificates on both controllers.""" |
| 234 |
rc, resp = self.request(self.url_path_prefix + "certificates/reload?reloadBoth=true", method="POST", ignore_errors=True) |
| 235 |
if rc == 404: |
| 236 |
rc, resp = self.request(self.url_path_prefix + "sslconfig/reload?reloadBoth=true", method="POST", ignore_errors=True) |
| 237 |
|
| 238 |
if rc > 202: |
| 239 |
self.module.fail_json(msg="Failed to initiate certificate reload on both controllers! Array [%s]." % self.ssid) |
| 240 |
|
| 241 |
|
| 242 |
for retry in range(int(self.RELOAD_TIMEOUT_SEC / 3)): |
| 243 |
rc, current_certificates = self.request(self.url_path_prefix + "certificates/remote-server", ignore_errors=True) |
| 244 |
|
| 245 |
if rc == 404: |
| 246 |
rc, current_certificates = self.request(self.url_path_prefix + "sslconfig/ca?useTruststore=true", ignore_errors=True) |
| 247 |
|
| 248 |
if rc < 300: |
| 249 |
break |
| 250 |
sleep(3) |
| 251 |
else: |
| 252 |
self.module.fail_json(msg="Failed to retrieve server certificates. Array [%s]." % self.ssid) |
| 253 |
|
| 254 |
def apply(self): |
| 255 |
"""Apply state changes to the storage array's truststore.""" |
| 256 |
changed = False |
| 257 |
|
| 258 |
self.determine_changes() |
| 259 |
if self.remove_certificates or self.add_certificates: |
| 260 |
changed = True |
| 261 |
|
| 262 |
if changed and not self.module.check_mode: |
| 263 |
for info in self.remove_certificates: |
| 264 |
self.delete_certificate(info) |
| 265 |
|
| 266 |
for path in self.add_certificates: |
| 267 |
self.upload_certificate(path) |
| 268 |
|
| 269 |
if self.apply_reload_certificates: |
| 270 |
self.reload_certificates() |
| 271 |
|
| 272 |
self.module.exit_json(changed=changed, removed_certificates=self.remove_certificates, add_certificates=self.add_certificates) |
| 273 |
|
| 274 |
|
| 275 |
def main(): |
| 276 |
client_certs = NetAppESeriesClientCertificate() |
| 277 |
client_certs.apply() |
| 278 |
|
| 279 |
|
| 280 |
if __name__ == "__main__": |
| 281 |
main() |