From 836a04b9b79e5adc47da436a0008ed400159cea3 Mon Sep 17 00:00:00 2001 From: thesayyn Date: Wed, 6 Dec 2023 12:51:04 -0800 Subject: [PATCH] nits --- distroless/private/home.bzl | 2 +- distroless/private/tar.bzl | 18 ++++++++++++------ docs/rules.md | 2 +- examples/cacerts/BUILD.bazel | 2 +- examples/home/BUILD.bazel | 4 ++-- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/distroless/private/home.bzl b/distroless/private/home.bzl index e28ce83..ab8f645 100644 --- a/distroless/private/home.bzl +++ b/distroless/private/home.bzl @@ -9,7 +9,7 @@ def home(name, dirs, **kwargs): Args: name: name of the target - dirs: an array of dicts + 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 = [] diff --git a/distroless/private/tar.bzl b/distroless/private/tar.bzl index 1fc3ce0..514461f 100644 --- a/distroless/private/tar.bzl +++ b/distroless/private/tar.bzl @@ -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, @@ -21,21 +27,21 @@ def _add_parents(path, uid = "0", gid = "0", time = "1672560000", mode = "0755") segments.pop() for i in range(0, len(segments)): parent = "/".join(segments[:i + 1]) - if not parent or parent == ".": + if not parent: continue lines.append( - _mtree_line(parent.lstrip("/"), "dir", uid = uid, gid = gid, time = time, mode = mode), + _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.lstrip("/"), "dir", **kwargs)) + lines.append(_mtree_line(path, "dir", **kwargs)) return lines def _build_tar(ctx, mtree, output, inputs = [], compression = "gzip", mnemonic = "Tar"): diff --git a/docs/rules.md b/docs/rules.md index f253e52..6ae8ba4 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -154,7 +154,7 @@ home(name, dirs, name | name of the target | none | -| dirs | an array of dicts | none | +| dirs | array of home directory dicts. | none | | kwargs | other named arguments to that is passed to tar. see [common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes). | none | diff --git a/examples/cacerts/BUILD.bazel b/examples/cacerts/BUILD.bazel index b3d3ce3..03bdd0d 100644 --- a/examples/cacerts/BUILD.bazel +++ b/examples/cacerts/BUILD.bazel @@ -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 diff --git a/examples/home/BUILD.bazel b/examples/home/BUILD.bazel index 459b03d..1919573 100644 --- a/examples/home/BUILD.bazel +++ b/examples/home/BUILD.bazel @@ -5,12 +5,12 @@ home( name = "home", dirs = [ { - "home": "./root", + "home": "/root", "uid": 0, "gid": 0, }, { - "home": "./home/nonroot", + "home": "/home/nonroot", "uid": 666, "gid": 666, },