2019-06-20 18:36:40 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-12-02 12:45:26 -05:00
|
|
|
"context"
|
2019-06-20 18:36:40 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-01-21 15:30:09 -05:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2021-12-02 12:45:26 -05:00
|
|
|
goSync "sync"
|
2022-01-21 15:30:09 -05:00
|
|
|
"syscall"
|
2021-04-22 14:18:33 -05:00
|
|
|
"time"
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
"github.com/docker/distribution/registry/storage/driver/factory"
|
2019-11-25 17:33:58 -05:00
|
|
|
"github.com/gorilla/handlers"
|
2019-07-10 00:23:59 -05:00
|
|
|
"github.com/gorilla/mux"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
ext "zotregistry.io/zot/pkg/extensions"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
|
|
|
"zotregistry.io/zot/pkg/storage/s3"
|
2019-06-20 18:36:40 -05:00
|
|
|
)
|
|
|
|
|
2021-04-22 14:18:33 -05:00
|
|
|
const (
|
|
|
|
idleTimeout = 120 * time.Second
|
|
|
|
)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
type Controller struct {
|
2021-06-08 15:11:18 -05:00
|
|
|
Config *config.Config
|
2021-04-05 19:40:33 -05:00
|
|
|
Router *mux.Router
|
|
|
|
StoreController storage.StoreController
|
|
|
|
Log log.Logger
|
2021-05-25 03:38:21 -05:00
|
|
|
Audit *log.Logger
|
2021-04-05 19:40:33 -05:00
|
|
|
Server *http.Server
|
2021-10-15 10:05:00 -05:00
|
|
|
Metrics monitoring.MetricServer
|
2021-12-02 12:45:26 -05:00
|
|
|
wgShutDown *goSync.WaitGroup // use it to gracefully shutdown goroutines
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
func NewController(config *config.Config) *Controller {
|
2021-05-25 03:38:21 -05:00
|
|
|
var controller Controller
|
|
|
|
|
|
|
|
logger := log.NewLogger(config.Log.Level, config.Log.Output)
|
|
|
|
controller.Config = config
|
|
|
|
controller.Log = logger
|
2021-12-02 12:45:26 -05:00
|
|
|
controller.wgShutDown = new(goSync.WaitGroup)
|
2021-05-25 03:38:21 -05:00
|
|
|
|
|
|
|
if config.Log.Audit != "" {
|
|
|
|
audit := log.NewAuditLogger(config.Log.Level, config.Log.Audit)
|
|
|
|
controller.Audit = audit
|
|
|
|
}
|
|
|
|
|
|
|
|
return &controller
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:15:13 -05:00
|
|
|
func (c *Controller) CORSHeaders() mux.MiddlewareFunc {
|
2021-05-05 17:31:56 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
2021-12-13 14:23:31 -05:00
|
|
|
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
2021-05-05 17:31:56 -05:00
|
|
|
// CORS
|
2022-02-15 20:15:13 -05:00
|
|
|
c.CORSHandler(response, request)
|
2021-05-05 17:31:56 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
next.ServeHTTP(response, request)
|
2021-05-05 17:31:56 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:15:13 -05:00
|
|
|
func (c *Controller) CORSHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
// allow origin as specified in config if not accept request from anywhere.
|
|
|
|
if c.Config.HTTP.AllowOrigin == "" {
|
|
|
|
response.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
} else {
|
|
|
|
response.Header().Set("Access-Control-Allow-Origin", c.Config.HTTP.AllowOrigin)
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Header().Set("Access-Control-Allow-Methods", "HEAD,GET,POST,OPTIONS")
|
|
|
|
response.Header().Set("Access-Control-Allow-Headers", "Authorization")
|
|
|
|
}
|
|
|
|
|
2022-01-21 15:30:09 -05:00
|
|
|
func DumpRuntimeParams(log log.Logger) {
|
|
|
|
var rLimit syscall.Rlimit
|
|
|
|
|
|
|
|
evt := log.Info().Int("cpus", runtime.NumCPU())
|
|
|
|
|
|
|
|
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err == nil {
|
|
|
|
evt = evt.Uint64("max. open files", rLimit.Cur)
|
|
|
|
}
|
|
|
|
|
|
|
|
if content, err := ioutil.ReadFile("/proc/sys/net/core/somaxconn"); err == nil {
|
|
|
|
evt = evt.Str("listen backlog", strings.TrimSuffix(string(content), "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if content, err := ioutil.ReadFile("/proc/sys/user/max_inotify_watches"); err == nil {
|
|
|
|
evt = evt.Str("max. inotify watches", strings.TrimSuffix(string(content), "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.Msg("runtime params")
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
func (c *Controller) Run() error {
|
2019-08-15 11:34:54 -05:00
|
|
|
// validate configuration
|
|
|
|
if err := c.Config.Validate(c.Log); err != nil {
|
|
|
|
c.Log.Error().Err(err).Msg("configuration validation failed")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the current configuration, but strip secrets
|
|
|
|
c.Log.Info().Interface("params", c.Config.Sanitize()).Msg("configuration settings")
|
|
|
|
|
2022-01-21 15:30:09 -05:00
|
|
|
// print the current runtime environment
|
|
|
|
DumpRuntimeParams(c.Log)
|
|
|
|
|
|
|
|
// setup HTTP API router
|
2019-07-10 00:23:59 -05:00
|
|
|
engine := mux.NewRouter()
|
2022-01-21 15:30:09 -05:00
|
|
|
|
|
|
|
// rate-limit HTTP requests if enabled
|
|
|
|
if c.Config.HTTP.Ratelimit != nil {
|
|
|
|
if c.Config.HTTP.Ratelimit.Rate != nil {
|
|
|
|
engine.Use(RateLimiter(c, *c.Config.HTTP.Ratelimit.Rate))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mrlim := range c.Config.HTTP.Ratelimit.Methods {
|
|
|
|
engine.Use(MethodRateLimiter(c, mrlim.Method, mrlim.Rate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.Use(
|
2022-02-15 20:15:13 -05:00
|
|
|
c.CORSHeaders(),
|
2021-10-15 10:05:00 -05:00
|
|
|
SessionLogger(c),
|
2021-05-05 17:31:56 -05:00
|
|
|
handlers.RecoveryHandler(handlers.RecoveryLogger(c.Log),
|
|
|
|
handlers.PrintRecoveryStack(false)))
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-05-25 03:38:21 -05:00
|
|
|
if c.Audit != nil {
|
2021-10-15 10:05:00 -05:00
|
|
|
engine.Use(SessionAuditLogger(c.Audit))
|
2021-05-25 03:38:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
c.Router = engine
|
|
|
|
c.Router.UseEncodedPath()
|
|
|
|
|
2021-10-15 10:05:00 -05:00
|
|
|
var enabled bool
|
|
|
|
if c.Config != nil &&
|
|
|
|
c.Config.Extensions != nil &&
|
|
|
|
c.Config.Extensions.Metrics != nil &&
|
2021-12-28 08:29:30 -05:00
|
|
|
*c.Config.Extensions.Metrics.Enable {
|
2021-10-15 10:05:00 -05:00
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Metrics = monitoring.NewMetricsServer(enabled, c.Log)
|
2021-10-05 04:12:22 -05:00
|
|
|
|
|
|
|
if err := c.InitImageStore(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
monitoring.SetServerInfo(c.Metrics, c.Config.Commit, c.Config.BinaryType, c.Config.GoVersion, c.Config.Version)
|
|
|
|
_ = NewRouteHandler(c)
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%s", c.Config.HTTP.Address, c.Config.HTTP.Port)
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: c.Router,
|
|
|
|
IdleTimeout: idleTimeout,
|
|
|
|
}
|
|
|
|
c.Server = server
|
|
|
|
|
|
|
|
// Create the listener
|
2021-12-13 14:23:31 -05:00
|
|
|
listener, err := net.Listen("tcp", addr)
|
2021-10-05 04:12:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Config.HTTP.TLS != nil && c.Config.HTTP.TLS.Key != "" && c.Config.HTTP.TLS.Cert != "" {
|
2022-03-01 15:57:56 -05:00
|
|
|
server.TLSConfig = &tls.Config{
|
|
|
|
CipherSuites: []uint16{
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
},
|
|
|
|
CurvePreferences: []tls.CurveID{
|
|
|
|
tls.CurveP256,
|
|
|
|
tls.X25519,
|
|
|
|
},
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
|
2021-10-05 04:12:22 -05:00
|
|
|
if c.Config.HTTP.TLS.CACert != "" {
|
|
|
|
clientAuth := tls.VerifyClientCertIfGiven
|
|
|
|
if (c.Config.HTTP.Auth == nil || c.Config.HTTP.Auth.HTPasswd.Path == "") && !c.Config.HTTP.AllowReadAccess {
|
|
|
|
clientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert, err := ioutil.ReadFile(c.Config.HTTP.TLS.CACert)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
|
|
|
|
if !caCertPool.AppendCertsFromPEM(caCert) {
|
|
|
|
panic(errors.ErrBadCACert)
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:57:56 -05:00
|
|
|
server.TLSConfig.ClientAuth = clientAuth
|
|
|
|
server.TLSConfig.ClientCAs = caCertPool
|
2021-10-05 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return server.ServeTLS(listener, c.Config.HTTP.TLS.Cert, c.Config.HTTP.TLS.Key)
|
2021-10-05 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return server.Serve(listener)
|
2021-10-05 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) InitImageStore() error {
|
2021-04-05 19:40:33 -05:00
|
|
|
c.StoreController = storage.StoreController{}
|
|
|
|
|
|
|
|
if c.Config.Storage.RootDirectory != "" {
|
|
|
|
if c.Config.Storage.Dedupe {
|
|
|
|
err := storage.ValidateHardLink(c.Config.Storage.RootDirectory)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Warn().Msg("input storage root directory filesystem does not supports hardlinking," +
|
|
|
|
"disabling dedupe functionality")
|
|
|
|
|
|
|
|
c.Config.Storage.Dedupe = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
var defaultStore storage.ImageStore
|
|
|
|
if len(c.Config.Storage.StorageDriver) == 0 {
|
|
|
|
defaultStore = storage.NewImageStore(c.Config.Storage.RootDirectory,
|
2022-02-09 19:51:35 -05:00
|
|
|
c.Config.Storage.GC, c.Config.Storage.GCDelay, c.Config.Storage.Dedupe, c.Config.Storage.Commit, c.Log, c.Metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
} else {
|
|
|
|
storeName := fmt.Sprintf("%v", c.Config.Storage.StorageDriver["name"])
|
|
|
|
if storeName != storage.S3StorageDriverName {
|
|
|
|
c.Log.Fatal().Err(errors.ErrBadConfig).Msgf("unsupported storage driver: %s",
|
|
|
|
c.Config.Storage.StorageDriver["name"])
|
|
|
|
}
|
|
|
|
// Init a Storager from connection string.
|
|
|
|
store, err := factory.Create(storeName, c.Config.Storage.StorageDriver)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Error().Err(err).Str("rootDir", c.Config.Storage.RootDirectory).Msg("unable to create s3 service")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultStore = s3.NewImageStore(c.Config.Storage.RootDirectory,
|
2022-02-09 19:51:35 -05:00
|
|
|
c.Config.Storage.GC, c.Config.Storage.GCDelay, c.Config.Storage.Dedupe,
|
|
|
|
c.Config.Storage.Commit, c.Log, c.Metrics, store)
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
c.StoreController.DefaultStore = defaultStore
|
|
|
|
|
|
|
|
// Enable extensions if extension config is provided
|
|
|
|
if c.Config != nil && c.Config.Extensions != nil {
|
2021-06-08 15:11:18 -05:00
|
|
|
ext.EnableExtensions(c.Config, c.Log, c.Config.Storage.RootDirectory)
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we can't proceed without global storage
|
|
|
|
c.Log.Error().Err(errors.ErrImgStoreNotFound).Msg("controller: no storage config provided")
|
|
|
|
|
|
|
|
return errors.ErrImgStoreNotFound
|
2020-02-17 16:57:15 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
if c.Config.Storage.SubPaths != nil {
|
|
|
|
if len(c.Config.Storage.SubPaths) > 0 {
|
|
|
|
subPaths := c.Config.Storage.SubPaths
|
|
|
|
|
2021-09-30 08:27:13 -05:00
|
|
|
subImageStore := make(map[string]storage.ImageStore)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
// creating image store per subpaths
|
|
|
|
for route, storageConfig := range subPaths {
|
|
|
|
if storageConfig.Dedupe {
|
|
|
|
err := storage.ValidateHardLink(storageConfig.RootDirectory)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Warn().Msg("input storage root directory filesystem does not supports hardlinking, " +
|
|
|
|
"disabling dedupe functionality")
|
|
|
|
|
|
|
|
storageConfig.Dedupe = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
if len(storageConfig.StorageDriver) == 0 {
|
|
|
|
subImageStore[route] = storage.NewImageStore(storageConfig.RootDirectory,
|
2022-02-09 19:51:35 -05:00
|
|
|
storageConfig.GC, storageConfig.GCDelay, storageConfig.Dedupe, storageConfig.Commit, c.Log, c.Metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
} else {
|
|
|
|
storeName := fmt.Sprintf("%v", storageConfig.StorageDriver["name"])
|
|
|
|
if storeName != storage.S3StorageDriverName {
|
|
|
|
c.Log.Fatal().Err(errors.ErrBadConfig).Msgf("unsupported storage driver: %s", storageConfig.StorageDriver["name"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init a Storager from connection string.
|
|
|
|
store, err := factory.Create(storeName, storageConfig.StorageDriver)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Error().Err(err).Str("rootDir", storageConfig.RootDirectory).Msg("Unable to create s3 service")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
subImageStore[route] = s3.NewImageStore(storageConfig.RootDirectory,
|
2022-02-09 19:51:35 -05:00
|
|
|
storageConfig.GC, storageConfig.GCDelay, storageConfig.Dedupe, storageConfig.Commit, c.Log, c.Metrics, store)
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
// Enable extensions if extension config is provided
|
|
|
|
if c.Config != nil && c.Config.Extensions != nil {
|
2021-06-08 15:11:18 -05:00
|
|
|
ext.EnableExtensions(c.Config, c.Log, storageConfig.RootDirectory)
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.StoreController.SubStore = subImageStore
|
|
|
|
}
|
2020-06-25 03:21:47 -05:00
|
|
|
}
|
|
|
|
|
2021-12-06 09:41:04 -05:00
|
|
|
// Enable extensions if extension config is provided
|
2021-12-28 08:29:30 -05:00
|
|
|
if c.Config.Extensions != nil && c.Config.Extensions.Sync != nil && *c.Config.Extensions.Sync.Enable {
|
2021-12-02 12:45:26 -05:00
|
|
|
ext.EnableSyncExtension(c.Config, c.wgShutDown, c.StoreController, c.Log)
|
2021-12-06 09:41:04 -05:00
|
|
|
}
|
|
|
|
|
2021-10-05 04:12:22 -05:00
|
|
|
return nil
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2021-12-02 12:45:26 -05:00
|
|
|
|
|
|
|
func (c *Controller) Shutdown() {
|
|
|
|
// wait gracefully
|
|
|
|
c.wgShutDown.Wait()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
_ = c.Server.Shutdown(ctx)
|
|
|
|
}
|