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

Allow plain HTTP for insecure-registry option #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion client/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package client
import (
"context"
"fmt"
"strings"

"github.com/containerd/containerd/remotes/docker"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/util/push"
)
Expand Down Expand Up @@ -34,5 +36,31 @@ func (c *Client) Push(ctx context.Context, image string, insecure bool) error {
if err != nil {
return err
}
return push.Push(ctx, sm, opt.ContentStore, imgObj.Target.Digest, image, insecure, opt.RegistryHosts, false)

if err := push.Push(ctx, sm, opt.ContentStore, imgObj.Target.Digest, image, insecure, opt.RegistryHosts, false); err != nil {
if !isErrHTTPResponseToHTTPSClient(err) {
return err
}

if !insecure {
return err
}

return push.Push(ctx, sm, opt.ContentStore, imgObj.Target.Digest, image, insecure, registryHostsWithPlainHTTP(), false)
}
return nil
}

func isErrHTTPResponseToHTTPSClient(err error) bool {
// The error string is unexposed as of Go 1.13, so we can't use `errors.Is`.
// https://github.com/golang/go/issues/44855

const unexposed = "server gave HTTP response to HTTPS client"
return strings.Contains(err.Error(), unexposed)
}

func registryHostsWithPlainHTTP() docker.RegistryHosts {
return docker.ConfigureDefaultRegistries(docker.WithPlainHTTP(func(_ string) (bool, error) {
return true, nil
}))
}