Skip to content

Commit

Permalink
Cleanup and improve reproducibility of builds (GoogleContainerTools#553)
Browse files Browse the repository at this point in the history
* Clean up package manager files
* Added dpkg_extract and tar utility for reproducible tars
* Add test case for build release tar
* Migrate java cacert extraction
* Updated to archive.py from pkg_tar
* Fix tar mode to GNU_TAR for consistency
* Use build_tar utility
* Fix /etc/os-release file to 0644 file permissions.
* Added copyright for libc-bin back to the files.
Set default mode to 0644.
  • Loading branch information
PeterMylemans authored Aug 10, 2020
1 parent f8590f2 commit 4a2e305
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 145 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/.classpath
/.factorypath
/.idea/
*.iml
/.project
/.settings
/bazel.iml
Expand Down
6 changes: 2 additions & 4 deletions base/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package(default_visibility = ["//visibility:public"])

load(":base.bzl", "NONROOT", "distro_components")
load(":distro.bzl", "DISTRO_SUFFIXES")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@io_bazel_rules_docker//contrib:group.bzl", "group_entry", "group_file")
load("@io_bazel_rules_docker//contrib:passwd.bzl", "passwd_entry", "passwd_tar")
Expand Down Expand Up @@ -113,10 +114,7 @@ go_binary(
pure = "on",
)

# Replicate the containers and tests for debian9 and debian10
distro_components("_debian9")

distro_components("_debian10")
[distro_components(suffix) for suffix in DISTRO_SUFFIXES]

# alias debian9 as the default images
alias(
Expand Down
13 changes: 1 addition & 12 deletions base/base.bzl
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
# defines a function to replicate the container images for different distributions
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@io_bazel_rules_docker//contrib:test.bzl", "container_test")
load("@package_bundle//file:packages.bzl", "packages")
load("@package_bundle_debian10//file:packages.bzl", packages_debian10 = "packages")
load(":distro.bzl", "DISTRO_PACKAGES", "DISTRO_REPOSITORY")
load("//cacerts:cacerts.bzl", "cacerts")

NONROOT = 65532

DISTRO_PACKAGES = {
"_debian9": packages,
"_debian10": packages_debian10,
}

DISTRO_REPOSITORY = {
"_debian9": "@debian_stretch",
"_debian10": "@debian10",
}

# Replicate everything for debian9 and debian10
def distro_components(distro_suffix):
cacerts(
Expand Down
14 changes: 14 additions & 0 deletions base/distro.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@package_bundle//file:packages.bzl", "packages")
load("@package_bundle_debian10//file:packages.bzl", packages_debian10 = "packages")

DISTRO_PACKAGES = {
"_debian9": packages,
"_debian10": packages_debian10,
}

DISTRO_SUFFIXES = ("_debian9", "_debian10")

DISTRO_REPOSITORY = {
"_debian9": "@debian_stretch",
"_debian10": "@debian10",
}
19 changes: 14 additions & 5 deletions cacerts/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package(default_visibility = ["//visibility:public"])
package(default_visibility = ["//:__subpackages__"])

sh_binary(
name = "extract_certs",
srcs = ["extract.sh"],
)
load("//base:distro.bzl", "DISTRO_PACKAGES", "DISTRO_SUFFIXES")
load(":cacerts.bzl", "cacerts")
load(":java.bzl", "cacerts_java")

[cacerts(
name = "cacerts" + distro_suffix,
deb = DISTRO_PACKAGES[distro_suffix]["ca-certificates"],
) for distro_suffix in DISTRO_SUFFIXES]

[cacerts_java(
name = "cacerts_java" + distro_suffix,
cacerts_tar = ":cacerts" + distro_suffix,
) for distro_suffix in DISTRO_SUFFIXES]
42 changes: 33 additions & 9 deletions cacerts/cacerts.bzl
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
"""A rule to unpack ca certificates from the debian package."""

def _impl(ctx):
ctx.actions.run(
executable = ctx.executable._extract,
ctx.actions.run_shell(
inputs = [ctx.file.deb],
outputs = [ctx.outputs.tar],
tools = [] + ctx.files._build_tar + ctx.files._dpkg_extract,
arguments = [
ctx.file.deb.path,
ctx.outputs.tar.path,
ctx.outputs.deb.path,
],
inputs = [ctx.file.deb],
outputs = [ctx.outputs.tar, ctx.outputs.deb],
env = {
"EXTRACT_DEB": ctx.executable._dpkg_extract.path,
"BUILD_TAR": ctx.executable._build_tar.path,
},
command = """
set -o errexit
set -o xtrace
$EXTRACT_DEB "$1" ./usr/share/ca-certificates ./usr/share/doc/ca-certificates/copyright
CERT_FILE=./etc/ssl/certs/ca-certificates.crt
mkdir -p $(dirname $CERT_FILE)
CERTS=$(find usr/share/ca-certificates -type f | sort)
for cert in $CERTS; do
cat $cert >> $CERT_FILE
done
$BUILD_TAR --output "$2" \
--file $CERT_FILE=$CERT_FILE \
--file ./usr/share/doc/ca-certificates/copyright=./usr/share/doc/ca-certificates/copyright
""",
)

cacerts = rule(
Expand All @@ -19,17 +40,20 @@ cacerts = rule(
mandatory = True,
),
# Implicit dependencies.
"_extract": attr.label(
default = Label("//cacerts:extract_certs"),
"_build_tar": attr.label(
default = Label("@rules_pkg//:build_tar"),
cfg = "host",
executable = True,
),
"_dpkg_extract": attr.label(
default = Label("//package_manager:dpkg_extract"),
cfg = "host",
executable = True,
allow_files = True,
),
},
executable = False,
outputs = {
"tar": "%{name}.tar",
"deb": "%{name}.deb",
},
implementation = _impl,
)
58 changes: 0 additions & 58 deletions cacerts/extract.sh

This file was deleted.

25 changes: 19 additions & 6 deletions cacerts/java.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ def _impl(ctx):
ctx.actions.run_shell(
outputs = [ctx.outputs.out],
inputs = [ctx.file.cacerts_tar],
tools = [ctx.file._jksutil],
tools = [ctx.file._jksutil] + ctx.files._build_tar,
arguments = [ctx.file.cacerts_tar.path, ctx.outputs.out.path],
env = {
"CREATE_JKS": ctx.executable._jksutil.path,
"BUILD_TAR": ctx.executable._build_tar.path,
},
command = """
mkdir -p etc/ssl/certs/java
tar -xOf "$1" etc/ssl/certs/ca-certificates.crt |
""" + ctx.file._jksutil.path + """ > etc/ssl/certs/java/cacerts
tar -cvf "$2" etc/ssl
""",
set -o errexit
set -o xtrace
mkdir -p etc/ssl/certs/java
tar -xOf "$1" ./etc/ssl/certs/ca-certificates.crt | $CREATE_JKS > etc/ssl/certs/java/cacerts
$BUILD_TAR --output "$2" \
--file ./etc/ssl/certs/java/cacerts=./etc/ssl/certs/java/cacerts
""",
)

cacerts_java = rule(
Expand All @@ -42,6 +50,11 @@ file with the JKS file at etc/ssl/certs/java/cacerts.
executable = True,
allow_single_file = True,
),
"_build_tar": attr.label(
default = Label("@rules_pkg//:build_tar"),
cfg = "host",
executable = True,
),
},
executable = False,
outputs = {
Expand Down
15 changes: 2 additions & 13 deletions java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ load("@io_bazel_rules_docker//java:image.bzl", "java_image")
load("@package_bundle//file:packages.bzl", "packages", "versions")
load("@package_bundle_debian10//file:packages.bzl", packages_debian10 = "packages", versions_debian10 = "versions")
load("//cacerts:java.bzl", "cacerts_java")
load("//locale:locale.bzl", "locale")
load("//java:jre_ver.bzl", "jre_ver")

DISTRO_SUFFIXES = ("_debian9", "_debian10")
Expand All @@ -21,16 +20,6 @@ DISTRO_VERSIONS = {
"_debian10": versions_debian10,
}

[locale(
name = "locale_java" + distro_suffix,
deb = DISTRO_PACKAGES[distro_suffix]["libc-bin"],
) for distro_suffix in DISTRO_SUFFIXES]

[cacerts_java(
name = "cacerts_java" + distro_suffix,
cacerts_tar = "//base:cacerts" + distro_suffix + ".tar",
) for distro_suffix in DISTRO_SUFFIXES]

[[container_image(
name = "java_base" + mode + distro_suffix,
base = ("//cc:cc" if (not ("debug" in mode)) else "//cc:debug") + distro_suffix,
Expand All @@ -50,8 +39,8 @@ DISTRO_VERSIONS = {
"LANG": "C.UTF-8",
},
tars = [
":cacerts_java" + distro_suffix,
":locale_java" + distro_suffix,
"//cacerts:cacerts_java" + distro_suffix,
"//locale:locale" + distro_suffix,
],
) for mode in [
"",
Expand Down
13 changes: 8 additions & 5 deletions locale/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package(default_visibility = ["//visibility:public"])
package(default_visibility = ["//:__subpackages__"])

sh_binary(
name = "extract_locale",
srcs = ["extract.sh"],
)
load("//base:distro.bzl", "DISTRO_PACKAGES", "DISTRO_SUFFIXES")
load(":locale.bzl", "locale")

[locale(
name = "locale" + distro_suffix,
deb = DISTRO_PACKAGES[distro_suffix]["libc-bin"],
) for distro_suffix in DISTRO_SUFFIXES]
20 changes: 0 additions & 20 deletions locale/extract.sh

This file was deleted.

32 changes: 24 additions & 8 deletions locale/locale.bzl
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
"""A rule to unpack c locale from the debian package."""
"""A rule to unpack minimal locales from the debian package."""

def _impl(ctx):
ctx.actions.run(
executable = ctx.executable._extract,
ctx.actions.run_shell(
inputs = [ctx.file.deb],
outputs = [ctx.outputs.tar],
tools = [] + ctx.files._build_tar + ctx.files._dpkg_extract,
arguments = [
ctx.file.deb.path,
ctx.outputs.tar.path,
],
inputs = [ctx.file.deb],
outputs = [ctx.outputs.tar],
env = {
"EXTRACT_DEB": ctx.executable._dpkg_extract.path,
"BUILD_TAR": ctx.executable._build_tar.path,
},
command = """
$EXTRACT_DEB "$1" ./usr/lib/locale/C.UTF-8 ./usr/share/doc/libc-bin/copyright
$BUILD_TAR --output "$2" \
--mode 0644 \
--file ./usr/share/doc/libc-bin/copyright=./usr/share/doc/libc-bin/copyright \
--file ./usr/lib/locale/C.UTF-8=./usr/lib/locale/C.UTF-8
""",
)

locale = rule(
Expand All @@ -18,11 +30,15 @@ locale = rule(
mandatory = True,
),
# Implicit dependencies.
"_extract": attr.label(
default = Label("//locale:extract_locale"),
"_build_tar": attr.label(
default = Label("@rules_pkg//:build_tar"),
cfg = "host",
executable = True,
),
"_dpkg_extract": attr.label(
default = Label("//package_manager:dpkg_extract"),
cfg = "host",
executable = True,
allow_files = True,
),
},
executable = False,
Expand Down
Loading

0 comments on commit 4a2e305

Please sign in to comment.