0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00
zot/pkg/api/config.go

112 lines
2.1 KiB
Go
Raw Normal View History

2019-06-20 18:36:40 -05:00
package api
import (
2019-08-15 11:34:54 -05:00
"github.com/anuvu/zot/errors"
"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"
)
//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
2019-06-20 18:36:40 -05:00
}
type HTTPConfig struct {
Address string
Port string
2019-08-15 11:34:54 -05:00
TLS *TLSConfig
Auth *AuthConfig
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
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,
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-08-15 11:34:54 -05:00
s.HTTP.Auth.LDAP = &LDAPConfig{}
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-08-15 11:34:54 -05:00
s.HTTP.Auth.LDAP.BindPassword = "******"
2019-08-15 11:34:54 -05:00
return s
}
2019-08-15 11:34:54 -05:00
return c
}
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-08-15 11:34:54 -05:00
return nil
2019-06-20 18:36:40 -05:00
}