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

feat: implement home #10

Merged
merged 2 commits into from
Dec 7, 2023
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
1 change: 1 addition & 0 deletions distroless/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bzl_library(
deps = [
"//distroless/private:cacerts",
"//distroless/private:group",
"//distroless/private:home",
"//distroless/private:java_keystore",
"//distroless/private:locale",
"//distroless/private:os_release",
Expand Down
2 changes: 2 additions & 0 deletions distroless/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("//distroless/private:cacerts.bzl", _cacerts = "cacerts")
load("//distroless/private:group.bzl", _group = "group")
load("//distroless/private:home.bzl", _home = "home")
load("//distroless/private:java_keystore.bzl", _java_keystore = "java_keystore")
load("//distroless/private:locale.bzl", _locale = "locale")
load("//distroless/private:os_release.bzl", _os_release = "os_release")
Expand All @@ -13,3 +14,4 @@ os_release = _os_release
group = _group
passwd = _passwd
java_keystore = _java_keystore
home = _home
10 changes: 10 additions & 0 deletions distroless/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ bzl_library(
deps = [":tar"],
)

bzl_library(
name = "home",
srcs = ["home.bzl"],
visibility = ["//distroless:__subpackages__"],
deps = [
":tar",
"@aspect_bazel_lib//lib:tar",
],
)

bzl_library(
name = "tar",
srcs = ["tar.bzl"],
Expand Down
27 changes: 27 additions & 0 deletions distroless/private/home.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"home"

load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load(":tar.bzl", "tar_lib")

def home(name, dirs, **kwargs):
"""
Create home directories with specific uid and gids.

Args:
name: name of the target
dirs: array of home directory dicts.
**kwargs: other named arguments to that is passed to tar. see [common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
mtree = []

for home in dirs:
mtree.extend(
tar_lib.add_directory_with_parents(home["home"], uid = str(home["uid"]), gid = str(home["gid"])),
)

tar(
name = name,
srcs = [],
mtree = mtree,
**kwargs
)
2 changes: 1 addition & 1 deletion distroless/private/passwd.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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=0700 time=0 type=file content={content}",
"",
],
substitutions = {
Expand Down
32 changes: 23 additions & 9 deletions distroless/private/tar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

BSDTAR_TOOLCHAIN = "@aspect_bazel_lib//lib:tar_toolchain_type"

def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "1672560000", mode = "0755"):
def _mtree_line(dest, type, content = None, uid = "0", gid = "0", time = "1672560000", mode = "0755"):
# mtree expects paths to start with ./ so normalize paths that starts with
# `/` or relative path (without / and ./)
if not dest.startswith("."):
if not dest.startswith("/"):
dest = "/" + dest
dest = "." + dest
spec = [
file,
dest,
"uid=" + uid,
"gid=" + gid,
"time=" + time,
Expand All @@ -15,19 +21,27 @@ def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "167256
spec.append("content=" + content)
return " ".join(spec)

def _add_parents(path):
def _add_parents(path, uid = "0", gid = "0", time = "1672560000", mode = "0755"):
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
lines = []
segments = path.split("/")
for i in range(1, len(segments)):
parent = "/".join(segments[:i])
if parent == "":
segments.pop()
for i in range(0, len(segments)):
parent = "/".join(segments[:i + 1])
if not parent:
continue
lines.append(_mtree_line(parent.lstrip("/"), "dir"))
lines.append(
_mtree_line(parent, "dir", uid = uid, gid = gid, time = time, mode = mode),
)
return lines

def _add_file_with_parents(path, file):
lines = _add_parents(path)
lines.append(_mtree_line(path.lstrip("/"), "file", content = file.path))
lines.append(_mtree_line(path, "file", content = file.path))
return lines

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

def _build_tar(ctx, mtree, output, inputs = [], compression = "gzip", mnemonic = "Tar"):
Expand Down Expand Up @@ -71,7 +85,7 @@ def _create_mtree(ctx):
tar_lib = struct(
create_mtree = _create_mtree,
line = _mtree_line,
add_directory_with_parents = _add_file_with_parents,
add_directory_with_parents = _add_directory_with_parents,
add_file_with_parents = _add_file_with_parents,
TOOLCHAIN_TYPE = BSDTAR_TOOLCHAIN,
)
20 changes: 20 additions & 0 deletions docs/rules.md

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

2 changes: 1 addition & 1 deletion examples/cacerts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ assert_tar_listing(
./etc/ssl time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./etc/ssl/certs time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./etc/ssl/certs/ca-certificates.crt nlink=0 time=1672560000.0 mode=755 gid=0 uid=0 type=file size=200313 cksum=3175436394 sha1digest=01b4ff230afaeeda5cddaf9a002cec9bc9a6d1b4
./etc/usr time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./usr time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./usr/share time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./usr/share/doc time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./usr/share/doc/ca-certificates time=1672560000.0 mode=755 gid=0 uid=0 type=dir
Expand Down
29 changes: 29 additions & 0 deletions examples/home/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("//distroless:defs.bzl", "home")
load("//distroless/tests:asserts.bzl", "assert_tar_listing")

home(
name = "home",
dirs = [
{
"home": "/root",
"uid": 0,
"gid": 0,
},
{
"home": "/home/nonroot",
"uid": 666,
"gid": 666,
},
],
)

assert_tar_listing(
name = "test_home",
actual = "home",
expected = """\
#mtree
./home time=1672560000.0 mode=755 gid=0 uid=0 type=dir
./home/nonroot time=1672560000.0 mode=755 gid=666 uid=666 type=dir
./root time=1672560000.0 mode=755 gid=0 uid=0 type=dir
""",
)