Skip to content

Commit 6482756

Browse files
authored
Add insecure repository support (#1403)
1 parent 88fb10b commit 6482756

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

container/go/cmd/pusher/pusher.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
skipUnchangedDigest = flag.Bool("skip-unchanged-digest", false, "If set to true, will only push images where the digest has changed.")
4747
layers utils.ArrayStringFlags
4848
stampInfoFile utils.ArrayStringFlags
49+
insecureRepository = flag.Bool("insecure-repository", false, "If set to true, the repository is assumed to be insecure (http vs https)")
4950
)
5051

5152
type dockerHeaders struct {
@@ -126,7 +127,12 @@ func main() {
126127
log.Printf("Failed to digest image: %v", err)
127128
}
128129

129-
if err := push(stamped, img); err != nil {
130+
var opts []name.Option
131+
if *insecureRepository {
132+
opts = append(opts, name.Insecure)
133+
}
134+
135+
if err := push(stamped, img, opts...); err != nil {
130136
log.Fatalf("Error pushing image to %s: %v", stamped, err)
131137
}
132138

@@ -163,9 +169,9 @@ func digestExists(dst string, img v1.Image) (bool, error) {
163169
// NOTE: This function is adapted from https://github.com/google/go-containerregistry/blob/master/pkg/crane/push.go
164170
// with modification for option to push OCI layout, legacy layout or Docker tarball format.
165171
// Push the given image to destination <dst>.
166-
func push(dst string, img v1.Image) error {
172+
func push(dst string, img v1.Image, opts ...name.Option) error {
167173
// Push the image to dst.
168-
ref, err := name.ParseReference(dst)
174+
ref, err := name.ParseReference(dst, opts...)
169175
if err != nil {
170176
return errors.Wrapf(err, "error parsing %q as an image reference", dst)
171177
}
@@ -237,4 +243,3 @@ func newTransport() http.RoundTripper {
237243

238244
return tr
239245
}
240-

container/push.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def _impl(ctx):
9292

9393
if ctx.attr.skip_unchanged_digest:
9494
pusher_args.append("-skip-unchanged-digest")
95+
if ctx.attr.insecure_repository:
96+
pusher_args.append("-insecure-repository")
9597
digester_args += ["--dst", str(ctx.outputs.digest.path), "--format", str(ctx.attr.format)]
9698
ctx.actions.run(
9799
inputs = digester_input,
@@ -156,6 +158,10 @@ container_push_ = rule(
156158
mandatory = True,
157159
doc = "The label of the image to push.",
158160
),
161+
"insecure_repository": attr.bool(
162+
default = False,
163+
doc = "Whether the repository is insecure or not (http vs https)",
164+
),
159165
"registry": attr.string(
160166
mandatory = True,
161167
doc = "The registry to which we are pushing.",

docs/container.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ please use the bazel startup flag `--loading_phase_threads=1` in your bazel invo
220220
## container_push
221221

222222
<pre>
223-
container_push(<a href="#container_push-name">name</a>, <a href="#container_push-extension">extension</a>, <a href="#container_push-extract_config">extract_config</a>, <a href="#container_push-format">format</a>, <a href="#container_push-image">image</a>, <a href="#container_push-incremental_load_template">incremental_load_template</a>, <a href="#container_push-registry">registry</a>,
224-
<a href="#container_push-repository">repository</a>, <a href="#container_push-repository_file">repository_file</a>, <a href="#container_push-skip_unchanged_digest">skip_unchanged_digest</a>, <a href="#container_push-stamp">stamp</a>, <a href="#container_push-tag">tag</a>, <a href="#container_push-tag_file">tag_file</a>, <a href="#container_push-tag_tpl">tag_tpl</a>,
225-
<a href="#container_push-windows_paths">windows_paths</a>)
223+
container_push(<a href="#container_push-name">name</a>, <a href="#container_push-extension">extension</a>, <a href="#container_push-extract_config">extract_config</a>, <a href="#container_push-format">format</a>, <a href="#container_push-image">image</a>, <a href="#container_push-incremental_load_template">incremental_load_template</a>,
224+
<a href="#container_push-insecure_repository">insecure_repository</a>, <a href="#container_push-registry">registry</a>, <a href="#container_push-repository">repository</a>, <a href="#container_push-repository_file">repository_file</a>, <a href="#container_push-skip_unchanged_digest">skip_unchanged_digest</a>,
225+
<a href="#container_push-stamp">stamp</a>, <a href="#container_push-tag">tag</a>, <a href="#container_push-tag_file">tag_file</a>, <a href="#container_push-tag_tpl">tag_tpl</a>, <a href="#container_push-windows_paths">windows_paths</a>)
226226
</pre>
227227

228228

@@ -238,6 +238,7 @@ container_push(<a href="#container_push-name">name</a>, <a href="#container_push
238238
| <a id="container_push-format"></a>format | The form to push: Docker or OCI, default to 'Docker'. | String | required | |
239239
| <a id="container_push-image"></a>image | The label of the image to push. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
240240
| <a id="container_push-incremental_load_template"></a>incremental_load_template | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //container:incremental_load_template |
241+
| <a id="container_push-insecure_repository"></a>insecure_repository | Whether the repository is insecure or not (http vs https) | Boolean | optional | False |
241242
| <a id="container_push-registry"></a>registry | The registry to which we are pushing. | String | required | |
242243
| <a id="container_push-repository"></a>repository | The name of the image. | String | required | |
243244
| <a id="container_push-repository_file"></a>repository_file | The label of the file with repository value. Overrides 'repository'. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |

testing/e2e/pusher.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ function test_pusher_client_config_errors() {
3232
echo "test_pusher_client_config_errors PASSED!"
3333
}
3434

35-
test_pusher_client_config_errors
35+
function test_pusher_insecure_repository() {
36+
# Ensure the pusher validates a given client config path is a valid directory.
37+
cd "${ROOT}"
38+
common_opts="--dst=foo:latest --format=Docker --config=foo.json"
39+
# Test that flag is accepted and the image is attempted
40+
EXPECT_CONTAINS "$(bazel run //container/go/cmd/pusher -- --insecure-repository ${common_opts} 2>&1)" "unable to load image metadata"
41+
echo "test_pusher_insecure_repository PASSED!"
42+
}
43+
44+
test_pusher_insecure_repository
45+
# test_pusher_client_config_errors

0 commit comments

Comments
 (0)