Skip to content

Commit fab916b

Browse files
committed
create non-root users for containers
1 parent a659bd1 commit fab916b

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/bci_build/package/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class ParseVersion(enum.StrEnum):
8282
PATCH_UPDATE = enum.auto()
8383
OFFSET = enum.auto()
8484

85-
8685
@dataclass
8786
class StableUser:
8887
"""Data class that stores information about stable user and group
@@ -97,7 +96,8 @@ class StableUser:
9796
group_name: str
9897
# id of the group
9998
group_id: int
100-
99+
# boolean flag that checks if user needs to be created
100+
user_create: bool = False
101101

102102
@dataclass
103103
class Replacement:

src/bci_build/package/git.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from bci_build.os_version import ALL_NONBASE_OS_VERSIONS
55
from bci_build.os_version import CAN_BE_LATEST_OS_VERSION
66
from bci_build.package import ApplicationStackContainer
7+
from bci_build.package import StableUser
78
from bci_build.package import ParseVersion
89
from bci_build.package import Replacement
910
from bci_build.package.helpers import generate_from_image_tag
@@ -39,13 +40,22 @@
3940
),
4041
],
4142
license="GPL-2.0-only",
43+
4244
package_list=[
4345
"git-core",
4446
"openssh-clients",
47+
"shadow"
4548
],
4649
build_stage_custom_end=generate_package_version_check(
4750
"git-core", git_version, ParseVersion.MINOR, use_target=True
4851
),
52+
user_chown=StableUser(
53+
user_id=1000,
54+
user_name="git",
55+
group_id=1000,
56+
group_name="git",
57+
user_create=True
58+
),
4959
)
5060
for os_version in ALL_NONBASE_OS_VERSIONS
5161
]

src/bci_build/templates.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,19 @@
5555
{% endif -%} zypper -n {%- if image.from_target_image %} --installroot /target --gpg-auto-import-keys {%- endif %} install {% if image.no_recommends %}--no-recommends {% endif %}{{ image.packages }}{%- if image.packages_to_delete %}; \\
5656
zypper -n {%- if image.from_target_image %} --installroot /target {%- endif %} remove {{ image.packages_to_delete }}{%- endif %}
5757
{%- endif %}
58-
{%- if image.user_chown %}
58+
{%- if image.user_chown and not image.user_chown.user_create%}
5959
# changing user id and group id created by package installation to stable values
6060
{{ DOCKERFILE_RUN }} \\
6161
{% if image.from_target_image %}chroot /target {% endif %}chown -R --from={{ image.user_chown.user_name }}:{{ image.user_chown.group_name }} {{ image.user_chown.user_id }}:{{ image.user_chown.group_id }} /; \\
6262
groupmod {% if image.from_target_image %}-R /target {% endif %}-g {{ image.user_chown.group_id }} {{ image.user_chown.group_name }}; \\
6363
usermod {% if image.from_target_image %}-R /target {% endif %}-u {{ image.user_chown.user_id }} {{ image.user_chown.user_name }}
6464
{%- endif %}
65+
{%- if image.user_chown and image.user_chown.user_create%}
66+
# create the user and group with the given ids
67+
{{ DOCKERFILE_RUN }} \\
68+
groupadd {% if image.from_target_image %}-R /target {% endif %}-g {{ image.user_chown.group_id }} -r {{ image.user_chown.group_name }}; \\
69+
useradd {% if image.from_target_image %}-R /target {% endif %}-u {{ image.user_chown.user_id }} -g {{ image.user_chown.group_id }} -m -r -s /bin/bash {{ image.user_chown.user_name }}
70+
{%- endif %}
6571
{%- if image.build_stage_custom_end %}
6672
{{ image.build_stage_custom_end }}
6773
{%- endif %}

0 commit comments

Comments
 (0)