Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/modules/na_sg_grid_certificate.py

raw
1 #!/usr/bin/python
2
3 # (c) 2021, NetApp Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 """NetApp StorageGRID - Manage Certificates"""
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 DOCUMENTATION = """
13 module: na_sg_grid_certificate
14 short_description: Manage the Storage API and Grid Management certificates on StorageGRID.
15 extends_documentation_fragment:
16 - netapp.storagegrid.netapp.sg
17 version_added: '21.6.0'
18 author: NetApp Ansible Team (@joshedmonds) <[email protected]>
19 description:
20 - Set and update the Storage API and Grid Management certificates on NetApp StorageGRID.
21 options:
22 state:
23 description:
24 - Whether the specified certificate should be set.
25 type: str
26 choices: ['present', 'absent']
27 default: present
28 type:
29 description:
30 - Which certificate to update.
31 type: str
32 choices: ['storage-api', 'management']
33 required: true
34 server_certificate:
35 description:
36 - X.509 server certificate in PEM-encoding.
37 type: str
38 ca_bundle:
39 description:
40 - Intermediate CA certificate bundle in concatenated PEM-encoding.
41 - Omit if there is no intermediate CA.
42 type: str
43 private_key:
44 description:
45 - Certificate private key in PEM-encoding.
46 - Required if I(server_certificate) is specified.
47 type: str
48 """
49
50 EXAMPLES = """
51 - name: set storage API certificate
52 netapp.storagegrid.na_sg_grid_certificate:
53 api_url: "https://<storagegrid-endpoint-url>"
54 auth_token: "storagegrid-auth-token"
55 validate_certs: false
56 state: present
57 type: storage-api
58 server_certificate: |
59 -----BEGIN CERTIFICATE-----
60 MIIC6DCCAdACCQC7l4WukhKD0zANBgkqhkiG9w0BAQsFADA2MQswCQYDVQQGEwJB
61 BAMMHnNnYW4wMS5kZXYubWljcm9icmV3Lm5ldGFwcC5hdTCCASIwDQYJKoZIhvcN
62 AQEBBQADggEPADCCAQoCggEBAMvjm9I35lmKcC7ITVL8+QiZ/klvdkbfZCUQrfdy
63 71inP+XmPjs0rnkhICA9ItODteRcVlO+t7nDTfm7HgG0mJFkcJm0ffyEYrcx24qu
64 S7gXYQjRsJmrep1awoaCa20BMGuqK2WKI3IvZ7YiT22qkBqKJD+hIFffX6u3Jy+B
65 77pR6YcATtpMHW/AaOx+OX9l80dIRsRZKMDxYQ==
66 -----END CERTIFICATE-----
67 private_key: |
68 -----BEGIN PRIVATE KEY-----
69 MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDL45vSN+ZZinAu
70 L25W0+cz1Oi69AKkI7d9nbFics2ay5+7o+4rKqf3en2R4MSxiJvy+iDlOmATib5O
71 x8TN5pJ9AgMBAAECggEADDLM8tHXXUoUFihzv+BUwff8p8YcbHcXFcSes+xTd5li
72 po8lNsx/v2pQx4ByBkuaYLZGIEXOWS6gkp44xhIXgQKBgQD4Hq7862u5HLbmhrV3
73 vs8nC69b3QKBgQDacCD8d8JpwPbg8t2VjXM3UvdmgAaLUfU7O1DWV+W3jqzmDOoN
74 zWVgPbPNj0UmzvLDbgxLoxe77wjn2BHsAJVAfJ9VeQKBgGqFAegYO+wHR8lJUoa5
75 ZEe8Upy2oBtvND/0dnwO2ym2FGsBJN0Gr4NKdG5vkzLsthKkcwRm0ikwEUOUZQKE
76 K8J5yEVeo9K2v3wggtq8fYn6
77 -----END PRIVATE KEY-----
78 """
79
80 RETURN = """
81 resp:
82 description: Returns information about the StorageGRID server certificates.
83 returned: success
84 type: dict
85 sample: {
86 "serverCertificateEncoded": "-----BEGIN CERTIFICATE-----MIIC6DCCAdACCQC7l4WukhKD0zANBgkqhkiG9w0BAQsFADA2MQswCQYDVQQGE...-----END CERTIFICATE-----",
87 "caBundleEncoded": "-----BEGIN CERTIFICATE-----MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELM...-----END CERTIFICATE-----"
88 }
89 """
90
91 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
92 from ansible.module_utils.basic import AnsibleModule
93 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
94 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
95
96
97 class SgGridCertificate:
98 """
99 Update StorageGRID certificates
100 """
101
102 def __init__(self):
103 """
104 Parse arguments, setup state variables,
105 check parameters and ensure request module is installed
106 """
107 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
108 self.argument_spec.update(
109 dict(
110 state=dict(required=False, type="str", choices=["present", "absent"], default="present"),
111 type=dict(required=True, type="str", choices=["storage-api", "management"]),
112 server_certificate=dict(required=False, type="str"),
113 ca_bundle=dict(required=False, type="str"),
114 private_key=dict(required=False, type="str", no_log=True),
115 )
116 )
117
118 parameter_map = {
119 "server_certificate": "serverCertificateEncoded",
120 "ca_bundle": "caBundleEncoded",
121 "private_key": "privateKeyEncoded",
122 }
123
124 self.module = AnsibleModule(
125 argument_spec=self.argument_spec,
126 required_if=[("state", "present", ["server_certificate", "private_key"])],
127 supports_check_mode=True,
128 )
129
130 self.na_helper = NetAppModule()
131
132 # set up state variables
133 self.parameters = self.na_helper.set_parameters(self.module.params)
134 # Calling generic SG rest_api class
135 self.rest_api = SGRestAPI(self.module)
136 # Get API version
137 self.rest_api.get_sg_product_version()
138 self.api_version = self.rest_api.get_api_version()
139
140 # Checking for the parameters passed and create new parameters list
141 self.data = {}
142
143 if self.parameters["state"] == "present":
144 for k in parameter_map.keys():
145 if self.parameters.get(k) is not None:
146 self.data[parameter_map[k]] = self.parameters[k]
147
148 self.module.fail_json
149
150 def get_grid_certificate(self, cert_type):
151 api = "api/%s/grid/%s" % (self.api_version, cert_type)
152
153 response, error = self.rest_api.get(api)
154
155 if error:
156 self.module.fail_json(msg=error)
157
158 return response["data"]
159
160 def update_grid_certificate(self, cert_type):
161 api = "api/%s/grid/%s/update" % (self.api_version, cert_type)
162
163 response, error = self.rest_api.post(api, self.data)
164 if error:
165 self.module.fail_json(msg=error)
166
167 def apply(self):
168 """
169 Perform pre-checks, call functions and exit
170 """
171
172 cert_type = ""
173 cd_action = None
174
175 if self.parameters.get("type") == "storage-api":
176 cert_type = "storage-api-certificate"
177 elif self.parameters.get("type") == "management":
178 cert_type = "management-certificate"
179
180 cert_data = self.get_grid_certificate(cert_type)
181
182 if cert_data["serverCertificateEncoded"] is None and cert_data["caBundleEncoded"] is None:
183 cd_action = self.na_helper.get_cd_action(None, self.parameters)
184 else:
185 cd_action = self.na_helper.get_cd_action(cert_data, self.parameters)
186
187 if cd_action is None and self.parameters["state"] == "present":
188 # let's see if we need to update parameters
189 update = False
190
191 if self.data.get("serverCertificateEncoded") is not None and self.data.get("privateKeyEncoded") is not None:
192 for item in ["serverCertificateEncoded", "caBundleEncoded"]:
193 if self.data.get(item) != cert_data.get(item):
194 update = True
195
196 if update:
197 self.na_helper.changed = True
198
199 result_message = ""
200 resp_data = cert_data
201 if self.na_helper.changed:
202 if self.module.check_mode:
203 pass
204 else:
205 if cd_action == "delete":
206 self.update_grid_certificate(cert_type)
207 resp_data = self.get_grid_certificate(cert_type)
208 result_message = "Grid %s removed" % cert_type
209
210 else:
211 self.update_grid_certificate(cert_type)
212 resp_data = self.get_grid_certificate(cert_type)
213 result_message = "Grid %s updated" % cert_type
214
215 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data)
216
217
218 def main():
219 """
220 Main function
221 """
222 na_sg_grid_certificate = SgGridCertificate()
223 na_sg_grid_certificate.apply()
224
225
226 if __name__ == "__main__":
227 main()