Skip to content

Commit

Permalink
Merge branch 'main' into dev-image-index
Browse files Browse the repository at this point in the history
  • Loading branch information
jjbustamante authored Sep 20, 2023
2 parents 2fcc6b5 + 4ec9360 commit 47b3689
Show file tree
Hide file tree
Showing 5 changed files with 45 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: 5 additions & 1 deletion remote/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ func WithPreviousImage(imageName string) ImageOption {
// insecure parameter allows image references to be fetched without TLS.
func WithRegistrySetting(repository string, insecure bool) ImageOption {
return func(opts *options) error {
opts.registrySettings = make(map[string]registrySetting)
if len(opts.registrySettings) == 0 {
opts.registrySettings = make(map[string]registrySetting)
}

opts.registrySettings[repository] = registrySetting{
insecure: insecure,
}

return nil
}
}
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
12 changes: 12 additions & 0 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertError(t, err, "http://")
})

it("tries to pull the image from an insecure registry if WithRegistrySettings insecure has been set, it works with multiple registries", func() {
_, err := remote.NewImage(
repoName,
authn.DefaultKeychain,
remote.FromBaseImage("myother-insecure-registry.com/repo/superbase"),
remote.WithRegistrySetting("myregistry.domain.com", true),
remote.WithRegistrySetting("myother-insecure-registry.com", true),
)

h.AssertError(t, err, "http://myother-insecure-registry.com")
})

it("sets the initial state from a windows/amd64 base image", func() {
baseImageName := "mcr.microsoft.com/windows/nanoserver@sha256:06281772b6a561411d4b338820d94ab1028fdeb076c85350bbc01e80c4bfa2b4"
existingLayerSha := "sha256:26fd2d9d4c64a4f965bbc77939a454a31b607470f430b5d69fc21ded301fa55e"
Expand Down
21 changes: 20 additions & 1 deletion remote/save.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package remote

import (
"crypto/tls"
"fmt"
"net/http"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
Expand Down Expand Up @@ -89,5 +91,22 @@ func (i *Image) doSave(imageName string) error {
if err != nil {
return err
}
return remote.Write(ref, i.image, remote.WithAuth(auth))

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

func getTransport(insecure bool) http.RoundTripper {
// #nosec G402
if insecure {
return &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}

return http.DefaultTransport
}

0 comments on commit 47b3689

Please sign in to comment.