mirror of
https://github.com/nabbar/golib.git
synced 2025-10-10 10:10:13 +08:00
Add Validator from github.com/go-playground/validator for LDAP config
Add error function to check if there are any parent error
This commit is contained in:
@@ -92,6 +92,7 @@ type Error interface {
|
|||||||
|
|
||||||
IsError(e error) bool
|
IsError(e error) bool
|
||||||
HasError(err error) bool
|
HasError(err error) bool
|
||||||
|
HasParent() bool
|
||||||
|
|
||||||
AddParent(parent ...error)
|
AddParent(parent ...error)
|
||||||
SetParent(parent ...error)
|
SetParent(parent ...error)
|
||||||
@@ -243,6 +244,10 @@ func (e *errors) HasError(err error) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *errors) HasParent() bool {
|
||||||
|
return len(e.p) > 0
|
||||||
|
}
|
||||||
|
|
||||||
func (e *errors) SetParent(parent ...error) {
|
func (e *errors) SetParent(parent ...error) {
|
||||||
e.p = make([]Error, 0)
|
e.p = make([]Error, 0)
|
||||||
e.AddParent(parent...)
|
e.AddParent(parent...)
|
||||||
|
@@ -45,6 +45,7 @@ const (
|
|||||||
ErrorLDAPInvalidUID
|
ErrorLDAPInvalidUID
|
||||||
ErrorLDAPAttributeNotFound
|
ErrorLDAPAttributeNotFound
|
||||||
ErrorLDAPAttributeEmpty
|
ErrorLDAPAttributeEmpty
|
||||||
|
ErrorLDAPValidatorError
|
||||||
)
|
)
|
||||||
|
|
||||||
var isCodeError = false
|
var isCodeError = false
|
||||||
@@ -94,6 +95,8 @@ func getMessage(code errors.CodeError) (message string) {
|
|||||||
return "requested attribute is not found"
|
return "requested attribute is not found"
|
||||||
case ErrorLDAPAttributeEmpty:
|
case ErrorLDAPAttributeEmpty:
|
||||||
return "requested attribute is empty"
|
return "requested attribute is empty"
|
||||||
|
case ErrorLDAPValidatorError:
|
||||||
|
return "invalid validation config"
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
@@ -92,7 +92,7 @@ func (lc *HelperLDAP) ForceTLSMode(tlsMode TLSMode, tlsConfig *tls.Config) {
|
|||||||
if tlsConfig != nil {
|
if tlsConfig != nil {
|
||||||
lc.tlsConfig = tlsConfig
|
lc.tlsConfig = tlsConfig
|
||||||
} else {
|
} else {
|
||||||
//nosec nolint gosec
|
//nolint gosec /* #nosec */
|
||||||
lc.tlsConfig = &tls.Config{}
|
lc.tlsConfig = &tls.Config{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -27,6 +27,9 @@ package ldap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/nabbar/golib/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TLSMode uint8
|
type TLSMode uint8
|
||||||
@@ -61,12 +64,12 @@ func GetDefaultAttributes() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Uri string `cloud:"uri" mapstructure:"uri" json:"uri" yaml:"uri" toml:"uri"`
|
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"`
|
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"`
|
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"`
|
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"`
|
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"`
|
FilterUser string `cloud:"filter-user" mapstructure:"filter-user" json:"filter-user" yaml:"filter-user" toml:"filter-user" validate:"printascii,required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
@@ -103,3 +106,25 @@ func (cnf Config) PatternFilterGroup() string {
|
|||||||
func (cnf Config) PatternFilterUser() string {
|
func (cnf Config) PatternFilterUser() string {
|
||||||
return cnf.FilterUser
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user