Skip to content

Commit

Permalink
Changed type of transport depending of the insecure flag in the registry
Browse files Browse the repository at this point in the history
Signed-off-by: Domenico Luciani <dluciani@vmware.com>
  • Loading branch information
dlion authored and Domenico Luciani committed Sep 15, 2023
1 parent b6f088d commit d727143
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
19 changes: 5 additions & 14 deletions remote/new.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package remote

import (
"crypto/tls"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -218,22 +217,14 @@ func newV1Image(keychain authn.Keychain, repoName string, platform imgutil.Platf
OSVersion: platform.OSVersion,
}

opts := []remote.Option{remote.WithAuth(auth), remote.WithPlatform(v1Platform)}
// #nosec G402
if reg.insecure {
opts = append(opts, remote.WithTransport(&http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}))
} else {
opts = append(opts, remote.WithTransport(http.DefaultTransport))
}

var image v1.Image
for i := 0; i <= maxRetries; i++ {
time.Sleep(100 * time.Duration(i) * time.Millisecond) // wait if retrying
image, err = remote.Image(ref, opts...)
image, err = remote.Image(ref,
remote.WithAuth(auth),
remote.WithPlatform(v1Platform),
remote.WithTransport(getTransport(reg.insecure)),
)
if err != nil {
if err == io.EOF && i != maxRetries {
continue // retry if EOF
Expand Down
6 changes: 3 additions & 3 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (i *Image) found() (*v1.Descriptor, error) {
if err != nil {
return nil, err
}
return remote.Head(ref, remote.WithAuth(auth), remote.WithTransport(http.DefaultTransport))
return remote.Head(ref, remote.WithAuth(auth), remote.WithTransport(getTransport(reg.insecure)))
}

func (i *Image) Valid() bool {
Expand All @@ -117,7 +117,7 @@ func (i *Image) valid() error {
if err != nil {
return err
}
desc, err := remote.Get(ref, remote.WithAuth(auth), remote.WithTransport(http.DefaultTransport))
desc, err := remote.Get(ref, remote.WithAuth(auth), remote.WithTransport(getTransport(reg.insecure)))
if err != nil {
return err
}
Expand Down Expand Up @@ -454,7 +454,7 @@ func (i *Image) Delete() error {
if err != nil {
return err
}
return remote.Delete(ref, remote.WithAuth(auth))
return remote.Delete(ref, remote.WithAuth(auth), remote.WithTransport(getTransport(reg.insecure)))
}

func (i *Image) Rebase(baseTopLayer string, newBase imgutil.Image) error {
Expand Down
4 changes: 2 additions & 2 deletions remote/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func (i *Image) doSave(imageName string) error {
return err
}

return remote.Write(ref, i.image, []remote.Option{
return remote.Write(ref, i.image,
remote.WithAuth(auth),
remote.WithTransport(getTransport(reg.insecure)),
}...)
)
}

func getTransport(insecure bool) http.RoundTripper {
Expand Down

0 comments on commit d727143

Please sign in to comment.