Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2022, 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_client_certificate
14 short_description: Manage Client Certificates on StorageGRID
15 extends_documentation_fragment:
16 - netapp.storagegrid.netapp.sg
17 version_added: '21.11.0'
18 author: NetApp Ansible Team (@joshedmonds) <[email protected]>
19 description:
20 - Create, Update, Delete Client Certificates on NetApp StorageGRID.
21 options:
22 state:
23 description:
24 - Whether the specified certificate should exist.
25 type: str
26 choices: ['present', 'absent']
27 default: present
28 certificate_id:
29 description:
30 - ID of the client certificate.
31 type: str
32 display_name:
33 description:
34 - A display name for the client certificate configuration.
35 - This parameter can be modified if I(certificate_id) is also specified.
36 type: str
37 public_key:
38 description:
39 - X.509 client certificate in PEM-encoding.
40 type: str
41 allow_prometheus:
42 description:
43 - Whether the external monitoring tool can access Prometheus metrics.
44 type: bool
45 """
46
47 EXAMPLES = """
48 - name: create client certificate
49 netapp.storagegrid.na_sg_grid_client_certificate:
50 api_url: "https://<storagegrid-endpoint-url>"
51 auth_token: "storagegrid-auth-token"
52 validate_certs: false
53 state: present
54 display_name: client-cert1
55 public_key: |
56 -----BEGIN CERTIFICATE-----
57 MIIC6DCCAdACCQC7l4WukhKD0zANBgkqhkiG9w0BAQsFADA2..swCQYDVQQGEwJB
58 BAMMHnNnYW4wMS5kZXYubWljcm9icmV3Lm5ldGFwcC5hdTCC..IwDQYJKoZIhvcN
59 AQEBBQADggEPADCCAQoCggEBAMvjm9I35lmKcC7ITVL8+QiZ..lvdkbfZCUQrfdy
60 71inP+XmPjs0rnkhICA9ItODteRcVlO+t7nDTfm7HgG0mJFk..m0ffyEYrcx24qu
61 S7gXYQjRsJmrep1awoaCa20BMGuqK2WKI3IvZ7YiT22qkBqK..+hIFffX6u3Jy+B
62 77pR6YcATtpMHW/AaOx+OX9l80dIRsRZKMDxYQ==
63 -----END CERTIFICATE-----
64 allow_prometheus: true
65
66 - name: rename client certificate
67 netapp.storagegrid.na_sg_grid_client_certificate:
68 api_url: "https://<storagegrid-endpoint-url>"
69 auth_token: "storagegrid-auth-token"
70 validate_certs: false
71 state: present
72 certificate_id: 00000000-0000-0000-0000-000000000000
73 display_name: client-cert1-rename
74 public_key: |
75 -----BEGIN CERTIFICATE-----
76 MIIC6DCCAdACCQC7l4WukhKD0zANBgkqhkiG9w0BAQsFADA2..swCQYDVQQGEwJB
77 BAMMHnNnYW4wMS5kZXYubWljcm9icmV3Lm5ldGFwcC5hdTCC..IwDQYJKoZIhvcN
78 AQEBBQADggEPADCCAQoCggEBAMvjm9I35lmKcC7ITVL8+QiZ..lvdkbfZCUQrfdy
79 71inP+XmPjs0rnkhICA9ItODteRcVlO+t7nDTfm7HgG0mJFk..m0ffyEYrcx24qu
80 S7gXYQjRsJmrep1awoaCa20BMGuqK2WKI3IvZ7YiT22qkBqK..+hIFffX6u3Jy+B
81 77pR6YcATtpMHW/AaOx+OX9l80dIRsRZKMDxYQ==
82 -----END CERTIFICATE-----
83 allow_prometheus: true
84
85 - name: delete client certificate
86 netapp.storagegrid.na_sg_grid_client_certificate:
87 api_url: "https://<storagegrid-endpoint-url>"
88 auth_token: "storagegrid-auth-token"
89 validate_certs: false
90 state: absent
91 display_name: client-cert1-rename
92 """
93
94 RETURN = """
95 resp:
96 description: Returns information about the StorageGRID server certificates.
97 returned: success
98 type: dict
99 sample: {
100 "id": "abcABC_01234-0123456789abcABCabc0123456789==",
101 "displayName": "client-cert1",
102 "expiryDate": "2024-01-01T00:00:00.000Z",
103 "publicKey": "-----BEGIN CERTIFICATE-----MIIC6DCCAdACCQC7l4WukhKD0zANBgkqhkiG9w0BAQsFADA2MQswCQYDVQQGE...-----END CERTIFICATE-----",
104 "allowPrometheus": true
105 }
106 """
107
108 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
109 from ansible.module_utils.basic import AnsibleModule
110 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
111 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
112
113
114 class SgGridClientCertificate:
115 """
116 Update StorageGRID client certificates
117 """
118
119 def __init__(self):
120 """
121 Parse arguments, setup state variables,
122 check parameters and ensure request module is installed
123 """
124 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
125 self.argument_spec.update(
126 dict(
127 state=dict(required=False, type="str", choices=["present", "absent"], default="present"),
128 certificate_id=dict(required=False, type="str"),
129 display_name=dict(required=False, type="str"),
130 public_key=dict(required=False, type="str"),
131 allow_prometheus=dict(required=False, type="bool"),
132 )
133 )
134
135 parameter_map = {
136 "display_name": "displayName",
137 "public_key": "publicKey",
138 "allow_prometheus": "allowPrometheus",
139 }
140
141 self.module = AnsibleModule(
142 argument_spec=self.argument_spec,
143 required_if=[("state", "present", ["display_name", "public_key"])],
144 required_one_of=[("display_name", "certificate_id")],
145 supports_check_mode=True,
146 )
147
148 self.na_helper = NetAppModule()
149
150 # set up state variables
151 self.parameters = self.na_helper.set_parameters(self.module.params)
152 # Calling generic SG rest_api class
153 self.rest_api = SGRestAPI(self.module)
154 # Get API version
155 self.rest_api.get_sg_product_version()
156 self.api_version = self.rest_api.get_api_version()
157
158 # Checking for the parameters passed and create new parameters list
159 self.data = {}
160
161 if self.parameters["state"] == "present":
162 for k in parameter_map.keys():
163 if self.parameters.get(k) is not None:
164 self.data[parameter_map[k]] = self.parameters[k]
165
166 self.module.fail_json
167
168 def get_grid_client_certificate_id(self):
169 # Check if certificate with name exists
170 # Return certificate ID if found, or None
171 api = "api/%s/grid/client-certificates" % self.api_version
172
173 response, error = self.rest_api.get(api)
174
175 if error:
176 self.module.fail_json(msg=error)
177
178 for cert in response.get("data"):
179 if cert["displayName"] == self.parameters["display_name"]:
180 return cert["id"]
181 return None
182
183 def get_grid_client_certificate(self, cert_id):
184 api = "api/%s/grid/client-certificates/%s" % (self.api_version, cert_id)
185 account, error = self.rest_api.get(api)
186
187 if error:
188 self.module.fail_json(msg=error)
189 else:
190 return account["data"]
191 return None
192
193 def create_grid_client_certificate(self):
194 api = "api/%s/grid/client-certificates" % self.api_version
195
196 response, error = self.rest_api.post(api, self.data)
197
198 if error:
199 self.module.fail_json(msg=error)
200
201 return response["data"]
202
203 def delete_grid_client_certificate(self, cert_id):
204 api = "api/%s/grid/client-certificates/%s" % (self.rest_api, cert_id)
205
206 self.data = None
207 response, error = self.rest_api.delete(api, self.data)
208 if error:
209 self.module.fail_json(msg=error)
210
211 def update_grid_client_certificate(self, cert_id):
212 api = "api/%s/grid/client-certificates/%s" % (self.api_version, cert_id)
213
214 response, error = self.rest_api.put(api, self.data)
215 if error:
216 self.module.fail_json(msg=error)
217
218 return response["data"]
219
220 def apply(self):
221 """
222 Perform pre-checks, call functions and exit
223 """
224
225 client_certificate = None
226
227 if self.parameters.get("certificate_id"):
228 client_certificate = self.get_grid_client_certificate(self.parameters["certificate_id"])
229
230 else:
231 client_cert_id = self.get_grid_client_certificate_id()
232 if client_cert_id:
233 client_certificate = self.get_grid_client_certificate(client_cert_id)
234
235 cd_action = self.na_helper.get_cd_action(client_certificate, self.parameters)
236
237 if cd_action is None and self.parameters["state"] == "present":
238 # let's see if we need to update parameters
239 modify = self.na_helper.get_modified_attributes(client_certificate, self.data)
240
241 result_message = ""
242 resp_data = client_certificate
243
244 if self.na_helper.changed and not self.module.check_mode:
245 if cd_action == "delete":
246 self.delete_grid_client_certificate(client_certificate["id"])
247 result_message = "Client Certificate deleted"
248 elif cd_action == "create":
249 resp_data = self.create_grid_client_certificate()
250 result_message = "Client Certificate created"
251 elif modify:
252 resp_data = self.update_grid_client_certificate(client_certificate["id"])
253 result_message = "Client Certificate updated"
254
255 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data)
256
257
258 def main():
259 """
260 Main function
261 """
262 na_sg_grid_certificate = SgGridClientCertificate()
263 na_sg_grid_certificate.apply()
264
265
266 if __name__ == "__main__":
267 main()