Debian Codegraph @

ansible_collections/kubernetes/core/plugins/modules/helm_registry_auth.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: © Ericsson AB 2024
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10
11 DOCUMENTATION = r"""
12 ---
13 module: helm_registry_auth
14
15 short_description: Helm registry authentication module
16
17 version_added: 5.1.0
18
19 author:
20 - Yuriy Novostavskiy (@yurnov)
21
22 requirements:
23 - "helm (https://github.com/helm/helm/releases) >= 3.8.0"
24
25 description:
26 - Helm registry authentication module allows you to login C(helm registry login) and logout C(helm registry logout) from a Helm registry.
27
28 options:
29 state:
30 description:
31 - Desired state of the registry.
32 - If set to V(present) attempt to log in to the remote registry server using the URL specified in O(host).
33 - If set to V(absent) attempt to log out from the remote registry server using the URL specified in O(host).
34 - As helm >= 3.18.0 reports successful logout even if the user is not logged in, this module will report a change regardless of the current state.
35 required: false
36 default: present
37 choices: ['present', 'absent']
38 type: str
39 host:
40 description:
41 - Provide a URL for accessing the registry.
42 required: true
43 aliases: [ registry_url ]
44 type: str
45 insecure:
46 description:
47 - Allow connections to SSL sites without certs.
48 required: false
49 default: false
50 type: bool
51 username:
52 description:
53 - Username for the registry.
54 required: false
55 type: str
56 aliases: [ repo_username ]
57 password:
58 description:
59 - Password for the registry.
60 required: false
61 type: str
62 aliases: [ repo_password ]
63 key_file:
64 description:
65 - Path to the client key SSL file for identify registry client using this key file.
66 required: false
67 type: path
68 cert_file:
69 description:
70 - Path to the client certificate SSL file for identify registry client using this certificate file.
71 required: false
72 type: path
73 ca_file:
74 description:
75 - Path to the CA certificate SSL file for verify registry server certificate.
76 required: false
77 type: path
78 plain_http:
79 description:
80 - Use insecure HTTP connections for C(helm registry login).
81 - Requires Helm >= 3.18.0
82 required: false
83 type: bool
84 default: False
85 version_added: 6.4.0
86 binary_path:
87 description:
88 - The path of a helm binary to use.
89 required: false
90 type: path
91 """
92
93 EXAMPLES = r"""
94 - name: Login to remote registry
95 kubernetes.core.helm_registry_auth:
96 username: admin
97 password: "sample_password"
98 host: localhost:5000
99
100 - name: Logout from remote registry
101 kubernetes.core.helm_registry_auth:
102 state: absent
103 host: localhost:5000
104 """
105
106 RETURN = r"""
107 stdout:
108 type: str
109 description: Full C(helm) command stdout, in case you want to display it or examine the event log
110 returned: always
111 stout_lines:
112 type: list
113 description: Full C(helm) command stdout, in case you want to display it or examine the event log
114 returned: always
115 stderr:
116 type: str
117 description: >-
118 Full C(helm) command stderr, in case you want to display it or examine the event log.
119 Please be note that helm binnary may print messages to stderr even if the command is successful.
120 returned: always
121 sample: 'Login Succeeded\n'
122 stderr_lines:
123 type: list
124 description: Full C(helm) command stderr, in case you want to display it or examine the event log
125 returned: always
126 command:
127 type: str
128 description: Full C(helm) command executed
129 returned: always
130 sample: '/usr/local/bin/helm registry login oci-registry.domain.example --username=admin --password-stdin --insecure'
131 failed:
132 type: bool
133 description: Indicate if the C(helm) command failed
134 returned: always
135 sample: false
136 """
137
138 from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
139 AnsibleHelmModule,
140 )
141 from ansible_collections.kubernetes.core.plugins.module_utils.version import (
142 LooseVersion,
143 )
144
145
146 def arg_spec():
147 return dict(
148 binary_path=dict(type="path", required=False),
149 host=dict(type="str", aliases=["registry_url"], required=True),
150 state=dict(default="present", choices=["present", "absent"], required=False),
151 insecure=dict(type="bool", default=False, required=False),
152 username=dict(type="str", aliases=["repo_username"], required=False),
153 password=dict(
154 type="str", aliases=["repo_password"], no_log=True, required=False
155 ),
156 key_file=dict(type="path", required=False),
157 cert_file=dict(type="path", required=False),
158 ca_file=dict(type="path", required=False),
159 plain_http=dict(type="bool", default=False),
160 )
161
162
163 def login(
164 command,
165 host,
166 insecure,
167 username,
168 password,
169 key_file,
170 cert_file,
171 ca_file,
172 plain_http,
173 ):
174 login_command = command + " registry login " + host
175
176 if username is not None and password is not None:
177 login_command += " --username=" + username + " --password-stdin"
178
179 if insecure:
180 login_command += " --insecure"
181
182 if key_file is not None:
183 login_command += " --key-file=" + key_file
184
185 if cert_file is not None:
186 login_command += " --cert-file=" + cert_file
187
188 if ca_file is not None:
189 login_command += " --ca-file=" + ca_file
190 if plain_http:
191 login_command += " --plain-http"
192
193 return login_command
194
195
196 def logout(command, host):
197 return command + " registry logout " + host
198
199
200 def main():
201 global module
202
203 module = AnsibleHelmModule(
204 argument_spec=arg_spec(),
205 required_together=[["username", "password"]],
206 supports_check_mode=True,
207 )
208
209 # Validate Helm version >=3.8.0
210 module.validate_helm_version(version="3.8.0")
211
212 changed = False
213
214 host = module.params.get("host")
215 state = module.params.get("state")
216 insecure = module.params.get("insecure")
217 username = module.params.get("username")
218 password = module.params.get("password")
219 key_file = module.params.get("key_file")
220 cert_file = module.params.get("cert_file")
221 ca_file = module.params.get("ca_file")
222 plain_http = module.params.get("plain_http")
223
224 helm_version = module.get_helm_version()
225
226 if plain_http:
227 if LooseVersion(helm_version) < LooseVersion("3.18.0"):
228 module.warn(
229 "plain_http option requires helm >= 3.18.0, current version is {0}".format(
230 helm_version
231 )
232 )
233 # reset option
234 plain_http = False
235
236 helm_cmd = module.get_helm_binary()
237
238 if state == "absent":
239 helm_cmd = logout(helm_cmd, host)
240 changed = True
241 elif state == "present":
242 helm_cmd = login(
243 helm_cmd,
244 host,
245 insecure,
246 username,
247 password,
248 key_file,
249 cert_file,
250 ca_file,
251 plain_http,
252 )
253 changed = True
254
255 if module.check_mode:
256 module.exit_json(changed=changed, command=helm_cmd)
257
258 rc, out, err = module.run_helm_command(
259 helm_cmd, data=password, fails_on_error=False
260 )
261
262 if rc != 0:
263 if state == "absent" and "Error: not logged in" in err:
264 changed = False
265 else:
266 module.fail_json(
267 msg="Failure when executing Helm command. Exited {0}.\nstdout: {1}\nstderr: {2}".format(
268 rc, out, err
269 ),
270 stderr=err,
271 command=helm_cmd,
272 )
273
274 if LooseVersion(helm_version) >= LooseVersion("3.18.0") and state == "absent":
275 # https://github.com/ansible-collections/kubernetes.core/issues/944
276 module.warn(
277 "The helm_registry_auth is not idempotent with helm >= 3.18.0, always report a change."
278 )
279
280 module.exit_json(changed=changed, stdout=out, stderr=err, command=helm_cmd)
281
282
283 if __name__ == "__main__":
284 main()