stages: add org.osbuild.ostree.passwd

This stage takes /usr/lib/passwd and /usr/etc/passwd from an OSTree
checkout, merges them into one file, and store it as /etc/passwd in the
buildroot.

It does the same for /etc/group.

The reason for doing this is that there is an issue with unstable UIDs
and GIDs when creating OSTree commits from scratch. When there is a
package that creates a system user or a system group, it can change the
UID and GID of users and groups that are created later.

This is not a problem in traditional deployments because already created
users and groups never change their UIDs and GIDs, but with OSTree we
recreate the files from scratch and then replace the previous one so it
can actually change.

By copying the files to the build root before doing any other
operations, we can make sure that the UIDs and GIDs of already existing
users and groups won't change.

Co-author: Christian Kellner <christian@kellner.me>
This commit is contained in:
Martin Sehnoutka 2021-08-17 10:34:33 +02:00 committed by Christian Kellner
parent 3695e22369
commit 8b0ea15817
3 changed files with 203 additions and 1 deletions

View file

@ -3,8 +3,10 @@
#
import json
import unittest
import os
import subprocess
import tempfile
import unittest
from osbuild.util import ostree
@ -84,3 +86,64 @@ class TestObjectStore(unittest.TestCase):
for p, v in params.items():
self.assertEqual(v, js[p])
class TestPasswdLike(unittest.TestCase):
def test_merge_passwd(self):
with tempfile.TemporaryDirectory() as tmpdir:
primary_file_lines = [
"root:x:0:0:root:/root:/bin/bash\n",
"bin:x:1:1:bin:/bin:/sbin/nologin\n",
"daemon:x:2:2:daemon:/sbin:/sbin/nologin\n"
]
secondary_file_lines = [
"daemon:x:9:9:daemon:/sbin:/sbin/nologin\n"
"lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n",
"sync:x:5:0:sync:/sbin:/bin/sync\n"
]
result_file_lines = [
"root:x:0:0:root:/root:/bin/bash\n",
"bin:x:1:1:bin:/bin:/sbin/nologin\n",
"daemon:x:2:2:daemon:/sbin:/sbin/nologin\n",
"lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n",
"sync:x:5:0:sync:/sbin:/bin/sync\n"
]
with open(os.path.join(tmpdir, "primary"), "w") as f:
f.writelines(primary_file_lines)
with open(os.path.join(tmpdir, "secondary"), "w") as f:
f.writelines(secondary_file_lines)
passwd = ostree.PasswdLike.from_file(os.path.join(tmpdir, "primary"))
passwd.merge_with_file(os.path.join(tmpdir, "secondary"))
passwd.dump_to_file(os.path.join(tmpdir, "result"))
with open(os.path.join(tmpdir, "result"), "r") as f:
self.assertEqual(sorted(f.readlines()), sorted(result_file_lines))
def test_merge_group(self):
with tempfile.TemporaryDirectory() as tmpdir:
primary_file_lines = [
"root:x:0:\n",
"bin:x:1:\n"
]
secondary_file_lines = [
"bin:x:4:\n",
"daemon:x:2:\n"
]
result_file_lines = [
"root:x:0:\n",
"bin:x:1:\n",
"daemon:x:2:\n"
]
with open(os.path.join(tmpdir, "primary"), "w") as f:
f.writelines(primary_file_lines)
with open(os.path.join(tmpdir, "secondary"), "w") as f:
f.writelines(secondary_file_lines)
passwd = ostree.PasswdLike.from_file(os.path.join(tmpdir, "primary"))
passwd.merge_with_file(os.path.join(tmpdir, "secondary"))
passwd.dump_to_file(os.path.join(tmpdir, "result"))
with open(os.path.join(tmpdir, "result"), "r") as f:
self.assertEqual(sorted(f.readlines()), sorted(result_file_lines))