Skip to content

Commit 2f82461

Browse files
committed
Allow insecure localhost connections
Before this change, the `docker.MatchLocalhost` function was applied to hosts retrieved from labels on snapshots, but not in the artifact fetcher. This meant that data could be lazily loaded from an insecure localhost, but we couldn't fetch the SOCI index/ztocs from an insecure localhost. This change adds the matcher to the artifact fetcher so that images can be lazily loaded from an insecure localhost. Signed-off-by: Kern Walster <[email protected]>
1 parent 0e1e73b commit 2f82461

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

fs/artifact_fetcher.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/awslabs/soci-snapshotter/soci/store"
3030
"github.com/awslabs/soci-snapshotter/util/ioutils"
3131
"github.com/containerd/containerd/reference"
32+
"github.com/containerd/containerd/remotes/docker"
3233
"github.com/containerd/log"
3334
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3435
"golang.org/x/sync/errgroup"
@@ -73,6 +74,11 @@ func newRemoteStore(refspec reference.Spec, client *http.Client) (*remote.Reposi
7374
return nil, fmt.Errorf("cannot create repository %s: %w", refspec.Locator, err)
7475
}
7576
repo.Client = client
77+
repo.PlainHTTP, err = docker.MatchLocalhost(refspec.Hostname())
78+
if err != nil {
79+
return nil, fmt.Errorf("cannot create repository %s: %w", refspec.Locator, err)
80+
}
81+
7682
return repo, nil
7783
}
7884

fs/artifact_fetcher_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"io"
24+
"net/http"
2425
"testing"
2526

2627
"github.com/containerd/containerd/reference"
@@ -213,6 +214,47 @@ func TestArtifactFetcherFetchOnlyOnce(t *testing.T) {
213214
}
214215
}
215216

217+
func TestNewRemoteStore(t *testing.T) {
218+
client := http.Client{}
219+
testCases := []struct {
220+
name string
221+
ref string
222+
shouldBePlainHTTP bool
223+
expectedError error
224+
}{
225+
{
226+
name: "ECR public is not plain http",
227+
ref: "public.ecr.aws/ref:tag",
228+
shouldBePlainHTTP: false,
229+
},
230+
{
231+
name: "localhost is plain http",
232+
ref: "localhost:5000/ref:tag",
233+
shouldBePlainHTTP: true,
234+
},
235+
}
236+
237+
for _, tc := range testCases {
238+
tc := tc
239+
t.Run(tc.name, func(t *testing.T) {
240+
refspec, err := reference.Parse(tc.ref)
241+
if err != nil {
242+
t.Fatalf("unexpected failure parsing reference: %v", err)
243+
}
244+
r, err := newRemoteStore(refspec, &client)
245+
if err != nil {
246+
t.Fatalf("unexpected error, got %v", err)
247+
}
248+
if r.Client != &client {
249+
t.Fatalf("unexpected http client, expected %v, got %v", &client, r.Client)
250+
}
251+
if r.PlainHTTP != tc.shouldBePlainHTTP {
252+
t.Fatalf("unepected plain http, expected: %v, got %v", tc.shouldBePlainHTTP, r.PlainHTTP)
253+
}
254+
})
255+
}
256+
}
257+
216258
func newFakeArtifactFetcher(ref string, contents []byte) (*artifactFetcher, error) {
217259
refspec, err := reference.Parse(ref)
218260
if err != nil {

0 commit comments

Comments
 (0)