From: Omar Sandoval <osandov@osandov.com>
To: linux-btrfs@vger.kernel.org
Cc: kernel-team@fb.com, Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
Subject: [PATCH 05/10] libbtrfsutil: add test helpers for dropping privileges
Date: Tue, 13 Nov 2018 23:47:00 -0800 [thread overview]
Message-ID: <c3f53eb2e4cc9bae5abacf0f54ce68b6be3f0f7b.1542181521.git.osandov@fb.com> (raw)
In-Reply-To: <cover.1542181521.git.osandov@fb.com>
From: Omar Sandoval <osandov@fb.com>
These will be used for testing some upcoming changes which allow
unprivileged operations.
Signed-off-by: Omar Sandoval <osandov@fb.com>
---
libbtrfsutil/python/tests/__init__.py | 31 ++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/libbtrfsutil/python/tests/__init__.py b/libbtrfsutil/python/tests/__init__.py
index 35550e0a..4bc11990 100644
--- a/libbtrfsutil/python/tests/__init__.py
+++ b/libbtrfsutil/python/tests/__init__.py
@@ -15,14 +15,44 @@
# You should have received a copy of the GNU Lesser General Public License
# along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
+import contextlib
import os
from pathlib import PurePath
+import pwd
import subprocess
import tempfile
import unittest
HAVE_PATH_LIKE = hasattr(PurePath, '__fspath__')
+try:
+ NOBODY_UID = pwd.getpwnam('nobody').pw_uid
+ skipUnlessHaveNobody = lambda func: func
+except KeyError:
+ NOBODY_UID = None
+ skipUnlessHaveNobody = unittest.skip('must have nobody user')
+
+
+@contextlib.contextmanager
+def drop_privs():
+ try:
+ os.seteuid(NOBODY_UID)
+ yield
+ finally:
+ os.seteuid(0)
+
+
+@contextlib.contextmanager
+def regain_privs():
+ uid = os.geteuid()
+ if uid:
+ try:
+ os.seteuid(0)
+ yield
+ finally:
+ os.seteuid(uid)
+ else:
+ yield
@unittest.skipIf(os.geteuid() != 0, 'must be run as root')
@@ -67,4 +97,3 @@ class BtrfsTestCase(unittest.TestCase):
yield fd
finally:
os.close(fd)
-
--
2.19.1
next prev parent reply other threads:[~2018-11-14 7:47 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-14 7:46 [PATCH 00/10] btrfs-progs: my libbtrfsutil patch queue Omar Sandoval
2018-11-14 7:46 ` [PATCH 01/10] libbtrfsutil: use top=0 as default for SubvolumeIterator() Omar Sandoval
2018-11-14 7:46 ` [PATCH 02/10] libbtrfsutil: change async parameters to async_ in Python bindings Omar Sandoval
2018-11-14 7:46 ` [PATCH 03/10] libbtrfsutil: document qgroup_inherit parameter " Omar Sandoval
2018-11-14 7:46 ` [PATCH 04/10] libbtrfsutil: use SubvolumeIterator as context manager in tests Omar Sandoval
2018-11-14 7:47 ` Omar Sandoval [this message]
2018-11-14 7:47 ` [PATCH 06/10] libbtrfsutil: allow tests to create multiple Btrfs instances Omar Sandoval
2018-11-14 7:47 ` [PATCH 07/10] libbtrfsutil: relax the privileges of subvolume_info() Omar Sandoval
2018-11-14 7:47 ` [PATCH 08/10] libbtrfsutil: relax the privileges of subvolume iterator Omar Sandoval
2018-11-14 7:47 ` [PATCH 09/10] libbtrfsutil: bump version to 1.1.0 Omar Sandoval
2018-11-14 7:47 ` [PATCH 10/10] libbtrfsutil: document API in README Omar Sandoval
2018-11-26 16:18 ` [PATCH 00/10] btrfs-progs: my libbtrfsutil patch queue David Sterba
2018-11-26 17:15 ` Omar Sandoval
2018-11-27 2:51 ` misono.tomohiro
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c3f53eb2e4cc9bae5abacf0f54ce68b6be3f0f7b.1542181521.git.osandov@fb.com \
--to=osandov@osandov.com \
--cc=kernel-team@fb.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=misono.tomohiro@jp.fujitsu.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).