|
5 | 5 | from ..common import Object |
6 | 6 |
|
7 | 7 |
|
| 8 | +def initialize(edge): |
| 9 | + """ |
| 10 | + Conditional intialization of the Edge Filer SSL Module. |
| 11 | + """ |
| 12 | + if edge.session().version > '7.8': |
| 13 | + return SSLv78(edge) |
| 14 | + return SSLv1(edge) |
| 15 | + |
| 16 | + |
8 | 17 | class SSL(BaseCommand): |
9 | 18 | """ Edge Filer SSL APIs """ |
10 | 19 |
|
@@ -38,6 +47,10 @@ def _get_force_https(self): |
38 | 47 | def _set_force_https(self, force): |
39 | 48 | self._edge.api.put('/config/fileservices/webdav/forceHttps', force) |
40 | 49 |
|
| 50 | + |
| 51 | +class SSLv1(SSL): |
| 52 | + """ Edge Filer SSLv1 APIs """ |
| 53 | + |
41 | 54 | def remove_storage_ca(self): |
42 | 55 | """ |
43 | 56 | Remove object storage trusted CA certificate |
@@ -81,3 +94,94 @@ def import_certificate(self, private_key, *certificates): |
81 | 94 | response = self._edge.api.put('/config/certificate', f"\n{server_certificate}") |
82 | 95 | logging.getLogger('cterasdk.edge').info("Uploaded SSL certificate.") |
83 | 96 | return response |
| 97 | + |
| 98 | + |
| 99 | +class SSLv78(SSL): |
| 100 | + """ Edge Filer v7.8 SSL Certificate APIs """ |
| 101 | + |
| 102 | + def __init__(self, edge): |
| 103 | + super().__init__(edge) |
| 104 | + self.server = ServerCertificate(edge) |
| 105 | + self.ca = TrustedCAs(edge) |
| 106 | + |
| 107 | + |
| 108 | +class ServerCertificate(BaseCommand): |
| 109 | + """ Edge Filer v7.8 Server Certificate APIs """ |
| 110 | + |
| 111 | + def get(self): |
| 112 | + """ |
| 113 | + Get Server Cerificate. |
| 114 | + """ |
| 115 | + return self._edge.api.get('/proc/certificates/serverCertificate') |
| 116 | + |
| 117 | + def regenerate(self): |
| 118 | + """ |
| 119 | + Generate a Self Signed Certificate. |
| 120 | + """ |
| 121 | + logging.getLogger('cterasdk.edge').info("Generating a Self Signed Certificate.") |
| 122 | + response = self._edge.api.execute('/config/certificates', 'createSelfSign') |
| 123 | + logging.getLogger('cterasdk.edge').info("Generated a Self Signed Certificate.") |
| 124 | + return response |
| 125 | + |
| 126 | + def import_certificate(self, private_key, *certificates): |
| 127 | + """ |
| 128 | + Import the Edge Filer's server SSL certificate |
| 129 | +
|
| 130 | + :param str private_key: The PEM-encoded private key, or a path to the PEM-encoded private key file |
| 131 | + :param list[str] certificates: The PEM-encoded certificates, or a list of paths to the PEM-encoded certificates |
| 132 | + """ |
| 133 | + key_object = PrivateKey.load_private_key(private_key) |
| 134 | + certificates = [X509Certificate.load_certificate(certificate) for certificate in certificates] |
| 135 | + certificate_chain = [certificate.pem_data.decode('utf-8') for certificate in create_certificate_chain(*certificates)] |
| 136 | + server_certificate = ''.join([key_object.pem_data.decode('utf-8')] + certificate_chain) |
| 137 | + logging.getLogger('cterasdk.edge').info("Uploading SSL certificate.") |
| 138 | + response = self._edge.api.put('/config/certificates/serverCertificate', f"\n{server_certificate}") |
| 139 | + logging.getLogger('cterasdk.edge').info("Uploaded SSL certificate.") |
| 140 | + return response |
| 141 | + |
| 142 | + |
| 143 | +class TrustedCAs(BaseCommand): |
| 144 | + """ Edge Filer v7.8 Trusted CAs APIs """ |
| 145 | + |
| 146 | + def all(self): |
| 147 | + """ |
| 148 | + List Trusted CAs. |
| 149 | + """ |
| 150 | + return self._edge.api.get('/proc/certificates/trustedCACertificates') |
| 151 | + |
| 152 | + def add(self, ca): |
| 153 | + """ |
| 154 | + Add Trusted CA. |
| 155 | +
|
| 156 | + :param str certificate: The PEM-encoded certificate or a path to the PEM-encoded server certificate file |
| 157 | + """ |
| 158 | + certificate = X509Certificate.load_certificate(ca).pem_data.decode('utf-8') |
| 159 | + return self._edge.api.execute('/config/certificates', 'addTrustedCACert', certificate) |
| 160 | + |
| 161 | + def remove(self, ca): |
| 162 | + """ |
| 163 | + Remove Trusted CA. |
| 164 | +
|
| 165 | + :param object ca: CA fingerprint as `str`, or a trusted CA object. |
| 166 | + """ |
| 167 | + fingerprint = None |
| 168 | + if isinstance(ca, Object) and hasattr(ca, 'fingerprint'): |
| 169 | + fingerprint = ca.fingerprint |
| 170 | + else: |
| 171 | + fingerprint = ca |
| 172 | + |
| 173 | + if fingerprint: |
| 174 | + logging.getLogger('cterasdk.edge').info("Removing Trusted CA. %s", {'fingerprint': fingerprint}) |
| 175 | + response = self._edge.api.delete(f'/config/certificates/trustedCACertificates/{fingerprint}') |
| 176 | + logging.getLogger('cterasdk.edge').info("Removed Trusted CA. %s", {'fingerprint': fingerprint}) |
| 177 | + return response |
| 178 | + |
| 179 | + raise ValueError('Could not identify CA fingerprint.') |
| 180 | + |
| 181 | + def clear(self): |
| 182 | + """ |
| 183 | + Remove all Trusted CAs. |
| 184 | + """ |
| 185 | + logging.getLogger('cterasdk.edge').info("Removing all Trusted CAs.") |
| 186 | + for ca in self.all(): |
| 187 | + self.remove(ca) |
0 commit comments