diff --git a/errors/errors.go b/errors/errors.go index 5b826a9..3642448 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -92,6 +92,7 @@ type Error interface { IsError(e error) bool HasError(err error) bool + HasParent() bool AddParent(parent ...error) SetParent(parent ...error) @@ -243,6 +244,10 @@ func (e *errors) HasError(err error) bool { return false } +func (e *errors) HasParent() bool { + return len(e.p) > 0 +} + func (e *errors) SetParent(parent ...error) { e.p = make([]Error, 0) e.AddParent(parent...) diff --git a/ldap/error.go b/ldap/error.go index 010790d..4bf7e4b 100644 --- a/ldap/error.go +++ b/ldap/error.go @@ -45,6 +45,7 @@ const ( ErrorLDAPInvalidUID ErrorLDAPAttributeNotFound ErrorLDAPAttributeEmpty + ErrorLDAPValidatorError ) var isCodeError = false @@ -94,6 +95,8 @@ func getMessage(code errors.CodeError) (message string) { return "requested attribute is not found" case ErrorLDAPAttributeEmpty: return "requested attribute is empty" + case ErrorLDAPValidatorError: + return "invalid validation config" } return "" diff --git a/ldap/ldap.go b/ldap/ldap.go index c4bae56..a1ce756 100644 --- a/ldap/ldap.go +++ b/ldap/ldap.go @@ -92,7 +92,7 @@ func (lc *HelperLDAP) ForceTLSMode(tlsMode TLSMode, tlsConfig *tls.Config) { if tlsConfig != nil { lc.tlsConfig = tlsConfig } else { - //nosec nolint gosec + //nolint gosec /* #nosec */ lc.tlsConfig = &tls.Config{} } diff --git a/ldap/model.go b/ldap/model.go index 5429f3b..3d352a2 100644 --- a/ldap/model.go +++ b/ldap/model.go @@ -27,6 +27,9 @@ package ldap import ( "fmt" + + "github.com/go-playground/validator/v10" + "github.com/nabbar/golib/errors" ) type TLSMode uint8 @@ -61,12 +64,12 @@ func GetDefaultAttributes() []string { } type Config struct { - Uri string `cloud:"uri" mapstructure:"uri" json:"uri" yaml:"uri" toml:"uri"` - PortLdap int `cloud:"port-ldap" mapstructure:"port-ldap" json:"port-ldap" yaml:"port-ldap" toml:"port-ldap"` - Portldaps int `cloud:"port-ldaps" mapstructure:"port-ldaps" json:"port-ldaps" yaml:"port-ldaps" toml:"port-ldaps"` - Basedn string `cloud:"basedn" mapstructure:"basedn" json:"basedn" yaml:"basedn" toml:"basedn"` - FilterGroup string `cloud:"filter-group" mapstructure:"filter-group" json:"filter-group" yaml:"filter-group" toml:"filter-group"` - FilterUser string `cloud:"filter-user" mapstructure:"filter-user" json:"filter-user" yaml:"filter-user" toml:"filter-user"` + Uri string `cloud:"uri" mapstructure:"uri" json:"uri" yaml:"uri" toml:"uri" validate:"url,required"` + PortLdap int `cloud:"port-ldap" mapstructure:"port-ldap" json:"port-ldap" yaml:"port-ldap" toml:"port-ldap" validate:"int,gte=0,nefield=Portldaps,required"` + Portldaps int `cloud:"port-ldaps" mapstructure:"port-ldaps" json:"port-ldaps" yaml:"port-ldaps" toml:"port-ldaps" validate:"int,nefield=Portldap,omitempty"` + Basedn string `cloud:"basedn" mapstructure:"basedn" json:"basedn" yaml:"basedn" toml:"basedn" validate:"printascii,omitempty"` + FilterGroup string `cloud:"filter-group" mapstructure:"filter-group" json:"filter-group" yaml:"filter-group" toml:"filter-group" validate:"printascii,required"` + FilterUser string `cloud:"filter-user" mapstructure:"filter-user" json:"filter-user" yaml:"filter-user" toml:"filter-user" validate:"printascii,required"` } func NewConfig() *Config { @@ -103,3 +106,25 @@ func (cnf Config) PatternFilterGroup() string { func (cnf Config) PatternFilterUser() string { return cnf.FilterUser } + +func (cnf Config) Validate() errors.Error { + val := validator.New() + err := val.Struct(cnf) + + if e, ok := err.(*validator.InvalidValidationError); ok { + return ErrorLDAPValidatorError.ErrorParent(e) + } + + out := ErrorLDAPValidatorError.Error(nil) + + for _, e := range err.(validator.ValidationErrors) { + //nolint goerr113 + out.AddParent(fmt.Errorf("config field '%s' is not validated by constraint '%s'", e.Field(), e.ActualTag())) + } + + if out.HasParent() { + return out + } + + return nil +}