Debian Codegraph @

ansible_collections/containers/podman/plugins/become/podman_unshare.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2022 Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # Written by Janos Gerzson ([email protected])
5 from __future__ import absolute_import, division, print_function
6
7 __metaclass__ = type
8
9 DOCUMENTATION = """
10 name: podman_unshare
11 short_description: Run tasks using podman unshare
12 description:
13 - "This become plugins allows your remote/login user
14 to execute commands in its container user namespace.
15 Official documentation: https://docs.podman.io/en/latest/markdown/podman-unshare.1.html"
16 author:
17 - Janos Gerzson (@grzs)
18 version_added: 1.9.0
19 options:
20 become_user:
21 description: User you 'become' to execute the task ('root' is not a valid value here).
22 ini:
23 - section: privilege_escalation
24 key: become_user
25 - section: sudo_become_plugin
26 key: user
27 vars:
28 - name: ansible_become_user
29 - name: ansible_sudo_user
30 env:
31 - name: ANSIBLE_BECOME_USER
32 - name: ANSIBLE_SUDO_USER
33 become_exe:
34 description: Sudo executable
35 default: sudo
36 ini:
37 - section: privilege_escalation
38 key: become_exe
39 - section: sudo_become_plugin
40 key: executable
41 vars:
42 - name: ansible_become_exe
43 - name: ansible_sudo_exe
44 env:
45 - name: ANSIBLE_BECOME_EXE
46 - name: ANSIBLE_SUDO_EXE
47 become_pass:
48 description: Password to pass to sudo
49 required: False
50 vars:
51 - name: ansible_become_password
52 - name: ansible_become_pass
53 - name: ansible_sudo_pass
54 env:
55 - name: ANSIBLE_BECOME_PASS
56 - name: ANSIBLE_SUDO_PASS
57 ini:
58 - section: sudo_become_plugin
59 key: password
60 """
61
62 EXAMPLES = """
63 - name: checking uid of file 'foo'
64 ansible.builtin.stat:
65 path: "{{ test_dir }}/foo"
66 register: foo
67 - ansible.builtin.debug:
68 var: foo.stat.uid
69 # The output shows that it's owned by the login user
70 # ok: [test_host] => {
71 # "foo.stat.uid": "1003"
72 # }
73
74 - name: mounting the file to an unprivileged container and modifying its owner
75 containers.podman.podman_container:
76 name: chmod_foo
77 image: alpine
78 rm: true
79 volume:
80 - "{{ test_dir }}:/opt/test:z"
81 command: chown 1000 /opt/test/foo
82
83 # Now the file 'foo' is owned by the container uid 1000,
84 # which is mapped to something completaly different on the host.
85 # It creates a situation when the file is unaccessible to the host user (uid 1003)
86 # Running stat again, debug output will be like this:
87 # ok: [test_host] => {
88 # "foo.stat.uid": "328679"
89 # }
90
91 - name: running stat in modified user namespace
92 become_method: containers.podman.podman_unshare
93 become: true
94 ansible.builtin.stat:
95 path: "{{ test_dir }}/foo"
96 register: foo
97 # By gathering file stats with podman_ushare
98 # we can see the uid set in the container:
99 # ok: [test_host] => {
100 # "foo.stat.uid": "1000"
101 # }
102
103 - name: resetting file ownership with podman unshare
104 become_method: containers.podman.podman_unshare
105 become: true
106 ansible.builtin.file:
107 state: file
108 path: "{{ test_dir }}/foo"
109 owner: 0 # in a modified user namespace host uid is mapped to 0
110 # If we run stat and debug with 'become: false',
111 # we can see that the file is ours again:
112 # ok: [test_host] => {
113 # "foo.stat.uid": "1003"
114 # }
115 """
116
117
118 from ansible.plugins.become import BecomeBase
119
120
121 class BecomeModule(BecomeBase):
122
123 name = "containers.podman.podman_unshare"
124
125 def build_become_command(self, cmd, shell):
126 super(BecomeModule, self).build_become_command(cmd, shell)
127
128 if not cmd:
129 return cmd
130
131 becomecmd = "podman unshare"
132
133 user = self.get_option("become_user") or "root"
134 if user != "root":
135 cmdlist = [self.get_option("become_exe") or "sudo"]
136 # -i is required, because
137 # podman unshare should be executed in a login shell to avoid chdir permission errors
138 cmdlist.append("-iu %s" % user)
139 if self.get_option("become_pass"):
140 self.prompt = "[sudo podman unshare via ansible, key=%s] password:" % self._id
141 cmdlist.append('-p "%s"' % self.prompt)
142 cmdlist.append("-- %s" % becomecmd)
143 becomecmd = " ".join(cmdlist)
144
145 return " ".join([becomecmd, self._build_success_command(cmd, shell)])