Debian Codegraph @

ansible_collections/containers/podman/plugins/modules/podman_logout.py

raw
1 #!/usr/bin/python
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import absolute_import, division, print_function
5
6 __metaclass__ = type
7
8 DOCUMENTATION = r"""
9 module: podman_logout
10 author:
11 - "Clemens Lange (@clelange)"
12 short_description: Log out of a container registry using podman
13 notes: []
14 description:
15 - Log out of a container registry server using the podman logout command
16 by deleting the cached credentials stored in the `auth.json` file.
17 If the registry is not specified, the first registry under
18 `[registries.search]` from `registries.conf `will be used. The path of
19 the authentication file can be overridden by the user by setting the
20 `authfile` flag. The default path used is
21 `${XDG_RUNTIME_DIR}/containers/auth.json`.
22 All the cached credentials can be removed by setting the `all` flag.
23 Warning - podman will use credentials in `${HOME}/.docker/config.json`
24 to authenticate in case they are not found in the default `authfile`.
25 However, the logout command will only removed credentials in the
26 `authfile` specified.
27 requirements:
28 - "Podman installed on host"
29 options:
30 registry:
31 description:
32 - Registry server. If the registry is not specified,
33 the first registry under `[registries.search]` from
34 `registries.conf` will be used.
35 type: str
36 authfile:
37 description:
38 - Path of the authentication file. Default is
39 ``${XDG_RUNTIME_DIR}/containers/auth.json``
40 You can also override the default path of the authentication
41 file by setting the ``REGISTRY_AUTH_FILE`` environment
42 variable. ``export REGISTRY_AUTH_FILE=path``
43 type: path
44 all:
45 description:
46 - Remove the cached credentials for all registries in the auth file.
47 type: bool
48 ignore_docker_credentials:
49 description:
50 - Credentials created using other tools such as `docker login` are not
51 removed unless the corresponding `authfile` is explicitly specified.
52 Since podman also uses existing credentials in these files by default
53 (for docker e.g. `${HOME}/.docker/config.json`), module execution will
54 fail if a docker login exists for the registry specified in any
55 `authfile` is used by podman. This can be ignored by setting
56 `ignore_docker_credentials` to `true` - the credentials will be kept and
57 `changed` will be false.
58 This option cannot be used together with `all` since in this case
59 podman will not check for existing `authfiles` created by other tools.
60 type: bool
61 executable:
62 description:
63 - Path to C(podman) executable if it is not in the C($PATH) on the
64 machine running C(podman)
65 default: 'podman'
66 type: str
67 """
68
69 EXAMPLES = r"""
70 - name: Log out of default registry
71 podman_logout:
72
73 - name: Log out of quay.io
74 podman_logout:
75 registry: quay.io
76
77 - name: Log out of all registries in auth file
78 podman_logout:
79 all: true
80
81 - name: Log out of all registries in specified auth file
82 podman_logout:
83 authfile: $HOME/.docker/config.json
84 all: true
85 """
86 # noqa: F402
87
88 from ansible.module_utils.basic import AnsibleModule
89
90
91 def logout(module, executable, registry, authfile, all_registries, ignore_docker_credentials):
92 command = [executable, "logout"]
93 changed = False
94 # In check mode, do not execute the logout command. We cannot reliably
95 # determine current login state here without side effects, so report
96 # no change.
97 if module.check_mode:
98 return changed, "", ""
99 if authfile:
100 command.extend(["--authfile", authfile])
101 if registry:
102 command.append(registry)
103 if all_registries:
104 command.append("--all")
105 rc, out, err = module.run_command(command)
106 if rc != 0:
107 # Treat "not logged into" as a no-op (idempotent) regardless of
108 # capitalization differences across podman versions.
109 if "not logged into" not in err.lower():
110 module.fail_json(msg="Unable to gather info for %s: %s" % (registry, err))
111 else:
112 # If the command is successful, we managed to log out
113 # Mind: This also applied if --all flag is used, while in this case
114 # there is no check whether one has been logged into any registry
115 changed = True
116 if "Existing credentials were established via" in out:
117 # The command will return successfully but not log out the user if the
118 # credentials were initially created using docker. Catch this behaviour:
119 if not ignore_docker_credentials:
120 module.fail_json(msg="Unable to log out %s: %s" % (registry or "", out))
121 else:
122 changed = False
123 return changed, out, err
124
125
126 def main():
127 module = AnsibleModule(
128 argument_spec=dict(
129 executable=dict(type="str", default="podman"),
130 registry=dict(type="str"),
131 authfile=dict(type="path"),
132 all=dict(type="bool"),
133 ignore_docker_credentials=dict(type="bool"),
134 ),
135 supports_check_mode=True,
136 mutually_exclusive=(
137 ["registry", "all"],
138 ["ignore_docker_credentials", "all"],
139 ),
140 )
141
142 registry = module.params["registry"]
143 authfile = module.params["authfile"]
144 all_registries = module.params["all"]
145 ignore_docker_credentials = module.params["ignore_docker_credentials"]
146 executable = module.get_bin_path(module.params["executable"], required=True)
147
148 changed, out, err = logout(
149 module,
150 executable,
151 registry,
152 authfile,
153 all_registries,
154 ignore_docker_credentials,
155 )
156
157 results = {
158 "changed": changed,
159 "stdout": out,
160 "stderr": err,
161 }
162
163 module.exit_json(**results)
164
165
166 if __name__ == "__main__":
167 main()