2019-06-20 18:36:40 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-08-15 11:34:54 -05:00
|
|
|
"github.com/anuvu/zot/errors"
|
2019-11-25 17:33:58 -05:00
|
|
|
"github.com/anuvu/zot/pkg/log"
|
2019-08-15 11:34:54 -05:00
|
|
|
"github.com/getlantern/deepcopy"
|
2019-06-20 18:36:40 -05:00
|
|
|
dspec "github.com/opencontainers/distribution-spec"
|
|
|
|
)
|
|
|
|
|
2019-09-16 13:01:59 -05:00
|
|
|
//nolint (gochecknoglobals)
|
|
|
|
var Commit string
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
type StorageConfig struct {
|
|
|
|
RootDirectory string
|
|
|
|
}
|
|
|
|
|
|
|
|
type TLSConfig struct {
|
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
CACert string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthHTPasswd struct {
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthConfig struct {
|
|
|
|
FailDelay int
|
|
|
|
HTPasswd AuthHTPasswd
|
2019-08-15 11:34:54 -05:00
|
|
|
LDAP *LDAPConfig
|
2020-01-24 16:32:38 -05:00
|
|
|
Bearer *BearerConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type BearerConfig struct {
|
|
|
|
Realm string
|
|
|
|
Service string
|
|
|
|
Cert string
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPConfig struct {
|
2019-08-28 16:05:16 -05:00
|
|
|
Address string
|
|
|
|
Port string
|
2019-08-15 11:34:54 -05:00
|
|
|
TLS *TLSConfig
|
|
|
|
Auth *AuthConfig
|
2019-08-28 16:05:16 -05:00
|
|
|
Realm string
|
|
|
|
AllowReadAccess bool `mapstructure:",omitempty"`
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
type LDAPConfig struct {
|
|
|
|
Port int
|
|
|
|
Insecure bool
|
|
|
|
StartTLS bool // if !Insecure, then StartTLS or LDAPs
|
|
|
|
SkipVerify bool
|
|
|
|
SubtreeSearch bool
|
|
|
|
Address string
|
|
|
|
BindDN string
|
|
|
|
BindPassword string
|
|
|
|
BaseDN string
|
|
|
|
UserAttribute string
|
|
|
|
CACert string
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
type LogConfig struct {
|
|
|
|
Level string
|
|
|
|
Output string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Version string
|
2019-09-16 13:01:59 -05:00
|
|
|
Commit string
|
2019-06-20 18:36:40 -05:00
|
|
|
Storage StorageConfig
|
|
|
|
HTTP HTTPConfig
|
2019-08-15 11:34:54 -05:00
|
|
|
Log *LogConfig
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
Version: dspec.Version,
|
2019-09-16 13:01:59 -05:00
|
|
|
Commit: Commit,
|
2019-06-20 18:36:40 -05:00
|
|
|
HTTP: HTTPConfig{Address: "127.0.0.1", Port: "8080"},
|
2019-08-15 11:34:54 -05:00
|
|
|
Log: &LogConfig{Level: "debug"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize makes a sanitized copy of the config removing any secrets
|
|
|
|
func (c *Config) Sanitize() *Config {
|
|
|
|
if c.HTTP.Auth != nil && c.HTTP.Auth.LDAP != nil && c.HTTP.Auth.LDAP.BindPassword != "" {
|
|
|
|
s := &Config{}
|
|
|
|
if err := deepcopy.Copy(s, c); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
s.HTTP.Auth.LDAP = &LDAPConfig{}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if err := deepcopy.Copy(s.HTTP.Auth.LDAP, c.HTTP.Auth.LDAP); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
s.HTTP.Auth.LDAP.BindPassword = "******"
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return s
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:33:58 -05:00
|
|
|
func (c *Config) Validate(log log.Logger) error {
|
2019-08-15 11:34:54 -05:00
|
|
|
// LDAP configuration
|
|
|
|
if c.HTTP.Auth != nil && c.HTTP.Auth.LDAP != nil {
|
|
|
|
l := c.HTTP.Auth.LDAP
|
|
|
|
if l.UserAttribute == "" {
|
|
|
|
log.Error().Str("userAttribute", l.UserAttribute).Msg("invalid LDAP configuration")
|
|
|
|
return errors.ErrLDAPConfig
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return nil
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|