Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: match rules_docker modes #17

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion distroless/private/home.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ def home(name, dirs, **kwargs):

for home in dirs:
mtree.extend(
tar_lib.mtree.add_directory_with_parents(home["home"], uid = str(home["uid"]), gid = str(home["gid"])),
tar_lib.mtree.add_directory_with_parents(
home["home"],
uid = str(home["uid"]),
gid = str(home["gid"]),
# the default matches https://github.com/bazelbuild/rules_docker/blob/3040e1fd74659a52d1cdaff81359f57ee0e2bb41/contrib/passwd.bzl#L81C24-L81C27
mode = getattr(home, "gid", "700"),
),
)

tar(
Expand Down
8 changes: 6 additions & 2 deletions distroless/private/passwd.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load("@aspect_bazel_lib//lib:utils.bzl", "propagate_common_rule_attributes")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

def passwd(name, passwds, **kwargs):
# WARNING: the mode `0o644` is important
# See: https://github.com/bazelbuild/rules_docker/blob/3040e1fd74659a52d1cdaff81359f57ee0e2bb41/contrib/passwd.bzl#L149C54-L149C57
def passwd(name, passwds, mode = "644", **kwargs):
"""
Create a passwd file from array of dicts.

Expand All @@ -20,6 +22,7 @@ def passwd(name, passwds, **kwargs):
```
dict(gid = 0, uid = 0, home = "/root", shell = "/bin/bash", username = "root")
```
mode: the mode bits for the passwd file
**kwargs: other named arguments to expanded targets. see [common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
common_kwargs = propagate_common_rule_attributes(kwargs)
Expand Down Expand Up @@ -50,11 +53,12 @@ def passwd(name, passwds, **kwargs):
stamp = 0,
template = [
"#mtree",
"./etc/passwd uid=0 gid=0 mode=0700 time=0 type=file content={content}",
"./etc/passwd uid=0 gid=0 mode={mode} time=0 type=file content={content}",
"",
],
substitutions = {
"{content}": "$(BINDIR)/$(rootpath :%s_content)" % name,
"{mode}": mode,
},
**common_kwargs
)
Expand Down
2 changes: 1 addition & 1 deletion distroless/private/tar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _add_file_with_parents(path, file):
return lines

def _add_directory_with_parents(path, **kwargs):
lines = _add_parents(path)
lines = _add_parents(path, **kwargs)
lines.append(_mtree_line(path, "dir", **kwargs))
return lines

Expand Down
3 changes: 2 additions & 1 deletion docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions examples/flatten/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ assert_tar_listing(
actual = "flatten",
expected = """\
#mtree
./etc/passwd nlink=0 time=0.0 mode=700 gid=0 uid=0 type=file size=33 cksum=3891093834 sha1digest=94f013494b98f8ed618ce2e670d405f818ec3915
./etc/passwd nlink=0 time=0.0 mode=644 gid=0 uid=0 type=file size=33 cksum=3891093834 sha1digest=94f013494b98f8ed618ce2e670d405f818ec3915
./examples time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten/dir time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten/dir/changelog nlink=0 time=1672560000.0 mode=755 gid=0 uid=0 type=file size=0 cksum=4294967295 sha1digest=da39a3ee5e6b4b0d3255bfef95601890afd80709
./examples/flatten/dir/sub time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./examples/flatten/dir/sub/content.txt nlink=0 time=1672560000.0 mode=755 gid=0 uid=0 type=file size=0 cksum=4294967295 sha1digest=da39a3ee5e6b4b0d3255bfef95601890afd80709
./home time=0.0 mode=755 gid=0 uid=0 type=dir
./home/nonroot time=0.0 mode=755 gid=666 uid=666 type=dir
./root time=0.0 mode=755 gid=0 uid=0 type=dir
./home time=0.0 mode=700 gid=666 uid=666 type=dir
./home/nonroot time=0.0 mode=700 gid=666 uid=666 type=dir
./root time=0.0 mode=700 gid=0 uid=0 type=dir
""",
)
6 changes: 3 additions & 3 deletions examples/home/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ assert_tar_listing(
actual = "home",
expected = """\
#mtree
./home time=0.0 mode=755 gid=0 uid=0 type=dir
./home/nonroot time=0.0 mode=755 gid=666 uid=666 type=dir
./root time=0.0 mode=755 gid=0 uid=0 type=dir
./home time=0.0 mode=700 gid=666 uid=666 type=dir
./home/nonroot time=0.0 mode=700 gid=666 uid=666 type=dir
./root time=0.0 mode=700 gid=0 uid=0 type=dir
""",
)
2 changes: 1 addition & 1 deletion examples/passwd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ assert_tar_listing(
actual = "passwd",
expected = """\
#mtree
./etc/passwd nlink=0 time=0.0 mode=700 gid=0 uid=0 type=file size=35 cksum=2298809208 sha1digest=31ad675c1210fd0413dd9b2441aaaf13c18d1547
./etc/passwd nlink=0 time=0.0 mode=644 gid=0 uid=0 type=file size=35 cksum=2298809208 sha1digest=31ad675c1210fd0413dd9b2441aaaf13c18d1547
""",
)
Loading