2022-10-10 07:05:55 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
2021-07-06 11:50:46 -05:00
|
|
|
|
|
|
|
package cli //nolint:testpackage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2021-11-10 09:31:03 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
2022-02-24 15:31:36 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2022-01-19 10:57:10 -05:00
|
|
|
extConf "zotregistry.io/zot/pkg/extensions/config"
|
2022-01-19 14:54:17 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-07-06 11:50:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BaseURL1 = "http://127.0.0.1:8088"
|
|
|
|
BaseSecureURL1 = "https://127.0.0.1:8088"
|
|
|
|
HOST1 = "127.0.0.1:8088"
|
|
|
|
SecurePort1 = "8088"
|
|
|
|
BaseURL2 = "http://127.0.0.1:8089"
|
|
|
|
BaseSecureURL2 = "https://127.0.0.1:8089"
|
|
|
|
SecurePort2 = "8089"
|
|
|
|
BaseURL3 = "http://127.0.0.1:8090"
|
|
|
|
BaseSecureURL3 = "https://127.0.0.1:8090"
|
|
|
|
SecurePort3 = "8090"
|
|
|
|
username = "test"
|
|
|
|
passphrase = "test"
|
|
|
|
ServerCert = "../../test/data/server.cert"
|
|
|
|
ServerKey = "../../test/data/server.key"
|
|
|
|
CACert = "../../test/data/ca.crt"
|
|
|
|
sourceCertsDir = "../../test/data"
|
|
|
|
certsDir1 = "/.config/containers/certs.d/127.0.0.1:8088/"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTLSWithAuth(t *testing.T) {
|
|
|
|
Convey("Make a new controller", t, func() {
|
2022-09-02 07:56:02 -05:00
|
|
|
caCert, err := os.ReadFile(CACert)
|
2021-07-06 11:50:46 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
resty.SetTLSClientConfig(&tls.Config{RootCAs: caCertPool, MinVersion: tls.VersionTLS12})
|
2021-07-06 11:50:46 -05:00
|
|
|
defer func() { resty.SetTLSClientConfig(nil) }()
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = SecurePort1
|
2022-01-19 14:54:17 -05:00
|
|
|
htpasswdPath := test.MakeHtpasswdFile()
|
2021-07-06 11:50:46 -05:00
|
|
|
defer os.Remove(htpasswdPath)
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
conf.HTTP.Auth = &config.AuthConfig{
|
|
|
|
HTPasswd: config.AuthHTPasswd{
|
2021-07-06 11:50:46 -05:00
|
|
|
Path: htpasswdPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
conf.HTTP.TLS = &config.TLSConfig{
|
2021-07-06 11:50:46 -05:00
|
|
|
Cert: ServerCert,
|
|
|
|
Key: ServerKey,
|
|
|
|
CACert: CACert,
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
enable := true
|
|
|
|
conf.Extensions = &extConf.ExtensionConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
Search: &extConf.SearchConfig{BaseConfig: extConf.BaseConfig{Enable: &enable}},
|
2022-01-19 10:57:10 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
ctlr := api.NewController(conf)
|
2022-03-07 03:55:12 -05:00
|
|
|
ctlr.Config.Storage.RootDirectory = t.TempDir()
|
2021-07-06 11:50:46 -05:00
|
|
|
go func() {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
2021-07-06 11:50:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait till ready
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(BaseSecureURL1)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
2021-12-13 14:23:31 -05:00
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
2021-07-06 11:50:46 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
Convey("Test with htpassw auth", func() {
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
destCertsDir := filepath.Join(home, certsDir1)
|
2022-01-19 14:54:17 -05:00
|
|
|
if err = test.CopyFiles(sourceCertsDir, destCertsDir); err != nil {
|
2021-07-06 11:50:46 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(destCertsDir)
|
|
|
|
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "--url", HOST1}
|
|
|
|
imageCmd := NewImageCommand(new(searchService))
|
|
|
|
imageBuff := bytes.NewBufferString("")
|
|
|
|
imageCmd.SetOut(imageBuff)
|
2021-05-21 15:47:28 -05:00
|
|
|
imageCmd.SetErr(imageBuff)
|
2021-07-06 11:50:46 -05:00
|
|
|
imageCmd.SetArgs(args)
|
|
|
|
err := imageCmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(imageBuff.String(), ShouldContainSubstring, "invalid URL format")
|
|
|
|
|
|
|
|
args = []string{"imagetest"}
|
|
|
|
configPath = makeConfigFile(
|
2022-02-24 15:31:36 -05:00
|
|
|
fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s%s%s","showspinner":false}]}`,
|
|
|
|
BaseSecureURL1, constants.RoutePrefix, constants.ExtCatalogPrefix))
|
2021-07-06 11:50:46 -05:00
|
|
|
defer os.Remove(configPath)
|
|
|
|
imageCmd = NewImageCommand(new(searchService))
|
|
|
|
imageBuff = bytes.NewBufferString("")
|
|
|
|
imageCmd.SetOut(imageBuff)
|
2021-05-21 15:47:28 -05:00
|
|
|
imageCmd.SetErr(imageBuff)
|
2021-07-06 11:50:46 -05:00
|
|
|
imageCmd.SetArgs(args)
|
|
|
|
err = imageCmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(imageBuff.String(), ShouldContainSubstring, "check credentials")
|
|
|
|
|
|
|
|
user := fmt.Sprintf("%s:%s", username, passphrase)
|
|
|
|
args = []string{"imagetest", "-u", user}
|
|
|
|
configPath = makeConfigFile(
|
2022-02-24 15:31:36 -05:00
|
|
|
fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s%s%s","showspinner":false}]}`,
|
|
|
|
BaseSecureURL1, constants.RoutePrefix, constants.ExtCatalogPrefix))
|
2021-07-06 11:50:46 -05:00
|
|
|
defer os.Remove(configPath)
|
|
|
|
imageCmd = NewImageCommand(new(searchService))
|
|
|
|
imageBuff = bytes.NewBufferString("")
|
|
|
|
imageCmd.SetOut(imageBuff)
|
2021-05-21 15:47:28 -05:00
|
|
|
imageCmd.SetErr(imageBuff)
|
2021-07-06 11:50:46 -05:00
|
|
|
imageCmd.SetArgs(args)
|
|
|
|
err = imageCmd.Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTLSWithoutAuth(t *testing.T) {
|
|
|
|
Convey("Home certs - Make a new controller", t, func() {
|
2022-09-02 07:56:02 -05:00
|
|
|
caCert, err := os.ReadFile(CACert)
|
2021-07-06 11:50:46 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
resty.SetTLSClientConfig(&tls.Config{RootCAs: caCertPool, MinVersion: tls.VersionTLS12})
|
2021-07-06 11:50:46 -05:00
|
|
|
defer func() { resty.SetTLSClientConfig(nil) }()
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = SecurePort1
|
|
|
|
conf.HTTP.TLS = &config.TLSConfig{
|
2021-07-06 11:50:46 -05:00
|
|
|
Cert: ServerCert,
|
|
|
|
Key: ServerKey,
|
|
|
|
CACert: CACert,
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
enable := true
|
|
|
|
conf.Extensions = &extConf.ExtensionConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
Search: &extConf.SearchConfig{BaseConfig: extConf.BaseConfig{Enable: &enable}},
|
2022-01-19 10:57:10 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
ctlr := api.NewController(conf)
|
2022-03-07 03:55:12 -05:00
|
|
|
ctlr.Config.Storage.RootDirectory = t.TempDir()
|
2021-07-06 11:50:46 -05:00
|
|
|
go func() {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
2021-07-06 11:50:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait till ready
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(BaseURL1)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
2021-12-13 14:23:31 -05:00
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
2021-07-06 11:50:46 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
Convey("Certs in user's home", func() {
|
|
|
|
configPath := makeConfigFile(
|
2022-02-24 15:31:36 -05:00
|
|
|
fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s%s%s","showspinner":false}]}`,
|
|
|
|
BaseSecureURL1, constants.RoutePrefix, constants.ExtCatalogPrefix))
|
2021-07-06 11:50:46 -05:00
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
destCertsDir := filepath.Join(home, certsDir1)
|
2022-01-19 14:54:17 -05:00
|
|
|
if err = test.CopyFiles(sourceCertsDir, destCertsDir); err != nil {
|
2021-07-06 11:50:46 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(destCertsDir)
|
|
|
|
|
|
|
|
args := []string{"imagetest"}
|
|
|
|
imageCmd := NewImageCommand(new(searchService))
|
|
|
|
imageBuff := bytes.NewBufferString("")
|
|
|
|
imageCmd.SetOut(imageBuff)
|
2021-05-21 15:47:28 -05:00
|
|
|
imageCmd.SetErr(imageBuff)
|
2021-07-06 11:50:46 -05:00
|
|
|
imageCmd.SetArgs(args)
|
|
|
|
err := imageCmd.Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTLSBadCerts(t *testing.T) {
|
|
|
|
Convey("Make a new controller", t, func() {
|
2022-09-02 07:56:02 -05:00
|
|
|
caCert, err := os.ReadFile(CACert)
|
2021-07-06 11:50:46 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
resty.SetTLSClientConfig(&tls.Config{RootCAs: caCertPool, MinVersion: tls.VersionTLS12})
|
2021-07-06 11:50:46 -05:00
|
|
|
defer func() { resty.SetTLSClientConfig(nil) }()
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = SecurePort3
|
|
|
|
conf.HTTP.TLS = &config.TLSConfig{
|
2021-07-06 11:50:46 -05:00
|
|
|
Cert: ServerCert,
|
|
|
|
Key: ServerKey,
|
|
|
|
CACert: CACert,
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
ctlr := api.NewController(conf)
|
2022-03-07 03:55:12 -05:00
|
|
|
ctlr.Config.Storage.RootDirectory = t.TempDir()
|
2021-07-06 11:50:46 -05:00
|
|
|
go func() {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
2021-07-06 11:50:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait till ready
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(BaseURL3)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
2021-12-13 14:23:31 -05:00
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
2021-07-06 11:50:46 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
Convey("Test with system certs", func() {
|
|
|
|
configPath := makeConfigFile(
|
2022-02-24 15:31:36 -05:00
|
|
|
fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s%s%s","showspinner":false}]}`,
|
|
|
|
BaseSecureURL3, constants.RoutePrefix, constants.ExtCatalogPrefix))
|
2021-07-06 11:50:46 -05:00
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
|
|
|
args := []string{"imagetest"}
|
|
|
|
imageCmd := NewImageCommand(new(searchService))
|
|
|
|
imageBuff := bytes.NewBufferString("")
|
|
|
|
imageCmd.SetOut(imageBuff)
|
2021-05-21 15:47:28 -05:00
|
|
|
imageCmd.SetErr(imageBuff)
|
2021-07-06 11:50:46 -05:00
|
|
|
imageCmd.SetArgs(args)
|
|
|
|
err := imageCmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(imageBuff.String(), ShouldContainSubstring, "certificate signed by unknown authority")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|