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

remove dependency on external python for sha256 #1993

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions container/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ bzl_library(
":layer_tools",
":providers",
"//skylib:filetype",
"//skylib:hash",
"//skylib:label",
"//skylib:path",
"@bazel_skylib//lib:dicts",
"@bazel_tools//tools/build_defs/hash:hash.bzl",
],
)

Expand All @@ -113,9 +113,9 @@ bzl_library(
":layer_tools",
":providers",
"//skylib:filetype",
"//skylib:hash",
"//skylib:path",
"//skylib:zip",
"@bazel_tools//tools/build_defs/hash:hash.bzl",
],
)

Expand All @@ -126,9 +126,9 @@ bzl_library(
":layer_tools",
":providers",
"//skylib:filetype",
"//skylib:hash",
"//skylib:path",
"//skylib:zip",
"@bazel_tools//tools/build_defs/hash:hash.bzl",
],
)

Expand Down
16 changes: 16 additions & 0 deletions container/go/cmd/sha256/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

# Build file for sha256 binary.

go_library(
name = "go_default_library",
srcs = ["sha256.go"],
importpath = "github.com/bazelbuild/rules_docker/container/go/cmd/sha256",
visibility = ["//visibility:private"],
)

go_binary(
name = "sha256",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
39 changes: 39 additions & 0 deletions container/go/cmd/sha256/sha256.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Portable SHA256 tool.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a comment here about the sha256.py you mentioned below so we have a chance of finding it later.

package main

import (
"crypto/sha256"
"encoding/hex"
"flag"
"io"
"io/ioutil"
"log"
"os"
)

func main() {
flag.Parse()
if len(os.Args) != 3 {
log.Fatalf("Usage: %s input output", os.Args[0])
}

inputfile, err := os.Open(os.Args[1])
if err != nil {
log.Fatalf("error reading %s: %s", os.Args[1], err)
}

h := sha256.New()
if _, err := io.Copy(h, inputfile); err != nil {
log.Fatalf("error reading %s: %s", os.Args[1], err)
}

if err := inputfile.Close(); err != nil {
log.Fatalf("error reading %s: %s", os.Args[1], err)
}
sum := h.Sum(nil)
hexSum := hex.EncodeToString(sum)

if err := ioutil.WriteFile(os.Args[2], []byte(hexSum), 0666); err != nil {
log.Fatalf("error writing %s: %s", os.Args[2], err)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 changes: 1 addition & 1 deletion container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ more specialized build leveraging the same implementation.

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"@bazel_tools//tools/build_defs/hash:hash.bzl",
"//skylib:hash.bzl",
_hash_tools = "tools",
_sha256 = "sha256",
)
Expand Down
2 changes: 1 addition & 1 deletion container/import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"@bazel_tools//tools/build_defs/hash:hash.bzl",
"//skylib:hash.bzl",
_hash_tools = "tools",
_sha256 = "sha256",
)
Expand Down
2 changes: 1 addition & 1 deletion container/layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"@bazel_tools//tools/build_defs/hash:hash.bzl",
"//skylib:hash.bzl",
_hash_tools = "tools",
_sha256 = "sha256",
)
Expand Down
2 changes: 1 addition & 1 deletion docker/util/run.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ the host machine.

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"@bazel_tools//tools/build_defs/hash:hash.bzl",
"//skylib:hash.bzl",
_hash_tools = "tools",
)
load("@io_bazel_rules_docker//container:layer.bzl", "zip_layer")
Expand Down
4 changes: 2 additions & 2 deletions docs/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ A rule that imports a docker image into our intermediate form.
| <a id="container_import-layers"></a>layers | The list of layer .tar.gz files in the order they appear in the config.json's layer section, or in the order that they appear in the <code>Layers</code> field of the docker save tarballs' <code>manifest.json</code> (these may or may not be gzipped).<br><br> Note that the layers should each have a different basename. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | required | |
| <a id="container_import-manifest"></a>manifest | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="container_import-repository"></a>repository | - | String | optional | "bazel" |
| <a id="container_import-sha256"></a>sha256 | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //tools/build_defs/hash:sha256 |
| <a id="container_import-sha256"></a>sha256 | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //container/go/cmd/sha256:sha256 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funny how the old doc here was wrong since it didn't include the repository name



<a id="#container_layer"></a>
Expand Down Expand Up @@ -126,7 +126,7 @@ A rule that assembles data into a tarball which can be use as in layers attr in
| <a id="container_layer-mtime"></a>mtime | - | Integer | optional | -1 |
| <a id="container_layer-operating_system"></a>operating_system | - | String | optional | "linux" |
| <a id="container_layer-portable_mtime"></a>portable_mtime | - | Boolean | optional | False |
| <a id="container_layer-sha256"></a>sha256 | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //tools/build_defs/hash:sha256 |
| <a id="container_layer-sha256"></a>sha256 | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //container/go/cmd/sha256:sha256 |
| <a id="container_layer-symlinks"></a>symlinks | Symlinks to create in the Docker image.<br><br> For example,<br><br> symlinks = { "/path/to/link": "/path/to/target", ... }, | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="container_layer-tars"></a>tars | Tar file to extract in the layer.<br><br> A list of tar files whose content should be in the Docker image. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |

Expand Down
5 changes: 5 additions & 0 deletions skylib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ bzl_library(
name = "zip",
srcs = ["zip.bzl"],
)

bzl_library(
name = "hash",
srcs = ["hash.bzl"],
)
23 changes: 23 additions & 0 deletions skylib/hash.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Functions for producing the hash of an artifact."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a comment here pointing to the upstream hash.bzl in bazel_tools and explain why we forked it? that way we have a chance of comparing this against future changes to that one.


def sha256(ctx, artifact, execution_requirements = None):
"""Create an action to compute the SHA-256 of an artifact."""
out = ctx.actions.declare_file(artifact.basename + ".sha256")
ctx.actions.run(
executable = ctx.executable.sha256,
arguments = [artifact.path, out.path],
inputs = [artifact],
outputs = [out],
mnemonic = "SHA256",
execution_requirements = execution_requirements,
)
return out

tools = {
"sha256": attr.label(
default = Label("//container/go/cmd/sha256:sha256"),
cfg = "host",
executable = True,
allow_files = True,
),
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for pointing to that, it's a strange API shape in this file, but I think it's clever how you made it a drop-in replacement.