|
|
|
@ -22,7 +22,7 @@ import (
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"net/url"
|
|
|
|
@ -35,6 +35,7 @@ import (
|
|
|
|
|
"github.com/distribution/distribution/v3/registry"
|
|
|
|
|
_ "github.com/distribution/distribution/v3/registry/auth/htpasswd"
|
|
|
|
|
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
|
|
|
|
|
"github.com/foxcpp/go-mockdns"
|
|
|
|
|
"github.com/phayes/freeport"
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
@ -64,6 +65,9 @@ type TestSuite struct {
|
|
|
|
|
CompromisedRegistryHost string
|
|
|
|
|
WorkspaceDir string
|
|
|
|
|
RegistryClient *Client
|
|
|
|
|
|
|
|
|
|
// A mock DNS server needed for TLS connection testing.
|
|
|
|
|
srv *mockdns.Server
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setup(suite *TestSuite, tlsEnabled bool, insecure bool) *registry.Registry {
|
|
|
|
@ -110,7 +114,7 @@ func setup(suite *TestSuite, tlsEnabled bool, insecure bool) *registry.Registry
|
|
|
|
|
pwBytes, err := bcrypt.GenerateFromPassword([]byte(testPassword), bcrypt.DefaultCost)
|
|
|
|
|
suite.Nil(err, "no error generating bcrypt password for test htpasswd file")
|
|
|
|
|
htpasswdPath := filepath.Join(suite.WorkspaceDir, testHtpasswdFileBasename)
|
|
|
|
|
err = ioutil.WriteFile(htpasswdPath, []byte(fmt.Sprintf("%s:%s\n", testUsername, string(pwBytes))), 0644)
|
|
|
|
|
err = os.WriteFile(htpasswdPath, []byte(fmt.Sprintf("%s:%s\n", testUsername, string(pwBytes))), 0644)
|
|
|
|
|
suite.Nil(err, "no error creating test htpasswd file")
|
|
|
|
|
|
|
|
|
|
// Registry config
|
|
|
|
@ -123,6 +127,15 @@ func setup(suite *TestSuite, tlsEnabled bool, insecure bool) *registry.Registry
|
|
|
|
|
// That function does not handle matching of ip addresses in octal,
|
|
|
|
|
// decimal or hex form.
|
|
|
|
|
suite.DockerRegistryHost = fmt.Sprintf("0x7f000001:%d", port)
|
|
|
|
|
|
|
|
|
|
// As of Go 1.20, Go may lookup "0x7f000001" as a DNS entry and fail.
|
|
|
|
|
// Using a mock DNS server to handle the address.
|
|
|
|
|
suite.srv, _ = mockdns.NewServer(map[string]mockdns.Zone{
|
|
|
|
|
"0x7f000001.": {
|
|
|
|
|
A: []string{"127.0.0.1"},
|
|
|
|
|
},
|
|
|
|
|
}, false)
|
|
|
|
|
suite.srv.PatchNet(net.DefaultResolver)
|
|
|
|
|
} else {
|
|
|
|
|
suite.DockerRegistryHost = fmt.Sprintf("localhost:%d", port)
|
|
|
|
|
}
|
|
|
|
@ -153,6 +166,13 @@ func setup(suite *TestSuite, tlsEnabled bool, insecure bool) *registry.Registry
|
|
|
|
|
return dockerRegistry
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func teardown(suite *TestSuite) {
|
|
|
|
|
if suite.srv != nil {
|
|
|
|
|
mockdns.UnpatchNet(net.DefaultResolver)
|
|
|
|
|
suite.srv.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initCompromisedRegistryTestServer() string {
|
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if strings.Contains(r.URL.Path, "manifests") {
|
|
|
|
@ -200,7 +220,7 @@ func testPush(suite *TestSuite) {
|
|
|
|
|
suite.NotNil(err, "error pushing non-chart bytes")
|
|
|
|
|
|
|
|
|
|
// Load a test chart
|
|
|
|
|
chartData, err := ioutil.ReadFile("../repo/repotest/testdata/examplechart-0.1.0.tgz")
|
|
|
|
|
chartData, err := os.ReadFile("../repo/repotest/testdata/examplechart-0.1.0.tgz")
|
|
|
|
|
suite.Nil(err, "no error loading test chart")
|
|
|
|
|
meta, err := extractChartMeta(chartData)
|
|
|
|
|
suite.Nil(err, "no error extracting chart meta")
|
|
|
|
@ -224,7 +244,7 @@ func testPush(suite *TestSuite) {
|
|
|
|
|
suite.Nil(err, "no error pushing non-strict ref (bad tag), with strict mode disabled")
|
|
|
|
|
|
|
|
|
|
// basic push, good ref
|
|
|
|
|
chartData, err = ioutil.ReadFile("../downloader/testdata/local-subchart-0.1.0.tgz")
|
|
|
|
|
chartData, err = os.ReadFile("../downloader/testdata/local-subchart-0.1.0.tgz")
|
|
|
|
|
suite.Nil(err, "no error loading test chart")
|
|
|
|
|
meta, err = extractChartMeta(chartData)
|
|
|
|
|
suite.Nil(err, "no error extracting chart meta")
|
|
|
|
@ -236,13 +256,13 @@ func testPush(suite *TestSuite) {
|
|
|
|
|
suite.Nil(err, "no error pulling a simple chart")
|
|
|
|
|
|
|
|
|
|
// Load another test chart
|
|
|
|
|
chartData, err = ioutil.ReadFile("../downloader/testdata/signtest-0.1.0.tgz")
|
|
|
|
|
chartData, err = os.ReadFile("../downloader/testdata/signtest-0.1.0.tgz")
|
|
|
|
|
suite.Nil(err, "no error loading test chart")
|
|
|
|
|
meta, err = extractChartMeta(chartData)
|
|
|
|
|
suite.Nil(err, "no error extracting chart meta")
|
|
|
|
|
|
|
|
|
|
// Load prov file
|
|
|
|
|
provData, err := ioutil.ReadFile("../downloader/testdata/signtest-0.1.0.tgz.prov")
|
|
|
|
|
provData, err := os.ReadFile("../downloader/testdata/signtest-0.1.0.tgz.prov")
|
|
|
|
|
suite.Nil(err, "no error loading test prov")
|
|
|
|
|
|
|
|
|
|
// push with prov
|
|
|
|
@ -284,7 +304,7 @@ func testPull(suite *TestSuite) {
|
|
|
|
|
suite.NotNil(err, "error on bad/missing ref")
|
|
|
|
|
|
|
|
|
|
// Load test chart (to build ref pushed in previous test)
|
|
|
|
|
chartData, err := ioutil.ReadFile("../downloader/testdata/local-subchart-0.1.0.tgz")
|
|
|
|
|
chartData, err := os.ReadFile("../downloader/testdata/local-subchart-0.1.0.tgz")
|
|
|
|
|
suite.Nil(err, "no error loading test chart")
|
|
|
|
|
meta, err := extractChartMeta(chartData)
|
|
|
|
|
suite.Nil(err, "no error extracting chart meta")
|
|
|
|
@ -306,14 +326,14 @@ func testPull(suite *TestSuite) {
|
|
|
|
|
"no error pulling a chart with prov when no prov exists, ignoring missing")
|
|
|
|
|
|
|
|
|
|
// Load test chart (to build ref pushed in previous test)
|
|
|
|
|
chartData, err = ioutil.ReadFile("../downloader/testdata/signtest-0.1.0.tgz")
|
|
|
|
|
chartData, err = os.ReadFile("../downloader/testdata/signtest-0.1.0.tgz")
|
|
|
|
|
suite.Nil(err, "no error loading test chart")
|
|
|
|
|
meta, err = extractChartMeta(chartData)
|
|
|
|
|
suite.Nil(err, "no error extracting chart meta")
|
|
|
|
|
ref = fmt.Sprintf("%s/testrepo/%s:%s", suite.DockerRegistryHost, meta.Name, meta.Version)
|
|
|
|
|
|
|
|
|
|
// Load prov file
|
|
|
|
|
provData, err := ioutil.ReadFile("../downloader/testdata/signtest-0.1.0.tgz.prov")
|
|
|
|
|
provData, err := os.ReadFile("../downloader/testdata/signtest-0.1.0.tgz.prov")
|
|
|
|
|
suite.Nil(err, "no error loading test prov")
|
|
|
|
|
|
|
|
|
|
// no chart and no prov causes error
|
|
|
|
@ -358,7 +378,7 @@ func testPull(suite *TestSuite) {
|
|
|
|
|
|
|
|
|
|
func testTags(suite *TestSuite) {
|
|
|
|
|
// Load test chart (to build ref pushed in previous test)
|
|
|
|
|
chartData, err := ioutil.ReadFile("../downloader/testdata/local-subchart-0.1.0.tgz")
|
|
|
|
|
chartData, err := os.ReadFile("../downloader/testdata/local-subchart-0.1.0.tgz")
|
|
|
|
|
suite.Nil(err, "no error loading test chart")
|
|
|
|
|
meta, err := extractChartMeta(chartData)
|
|
|
|
|
suite.Nil(err, "no error extracting chart meta")
|
|
|
|
|