From 6ed8c0b7d2d0d5f17f5e06cae244b65fe97c2981 Mon Sep 17 00:00:00 2001 From: Zeliha KONAK Date: Thu, 19 Dec 2024 23:24:28 +0100 Subject: [PATCH] Remove go-ozzo/ozza-validation package with that, we are no longer dependent on ozzo packages which is an external package and is no longer maintained without this, we are dependent on a external package that is very old --- go.mod | 2 - go.sum | 4 -- internal/utils/config.go | 22 ++------ internal/utils/validationConfig.go | 84 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 internal/utils/validationConfig.go diff --git a/go.mod b/go.mod index 4cbb1e52..802dc3f6 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.23.3 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible - github.com/go-ozzo/ozzo-validation v3.6.0+incompatible github.com/gorilla/mux v1.8.1 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.20.5 @@ -20,7 +19,6 @@ require ( ) require ( - github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index f2eb5095..fb2ba233 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= -github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= @@ -22,8 +20,6 @@ github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4= github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= -github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE= -github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= diff --git a/internal/utils/config.go b/internal/utils/config.go index 904662f0..1b08ea5c 100644 --- a/internal/utils/config.go +++ b/internal/utils/config.go @@ -11,8 +11,6 @@ import ( "strings" "github.com/ca-gip/kubi/pkg/types" - validation "github.com/go-ozzo/ozzo-validation" - "github.com/go-ozzo/ozzo-validation/is" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/client-go/rest" @@ -168,22 +166,8 @@ func MakeConfig() (*types.Config, error) { BlackWhitelistNamespace: getEnv("BLACK_WHITELIST_NAMESPACE", "default"), } - // TODO: Remove validation through ozzo-validation - err = validation.ValidateStruct(config, - validation.Field(&config.KubeToken, validation.Required), - validation.Field(&config.KubeCa, validation.Required, is.Base64), - validation.Field(&config.PublicApiServerURL, validation.Required, is.URL), - ) - // TODO: Get rid of Check method - Check(err) - - errLdap := validation.ValidateStruct(&ldapConfig, - validation.Field(&ldapConfig.UserBase, validation.Required, validation.Length(2, 200)), - validation.Field(&ldapConfig.GroupBase, validation.Required, validation.Length(2, 200)), - validation.Field(&ldapConfig.Host, validation.Required, is.URL), - validation.Field(&ldapConfig.BindDN, validation.Required, validation.Length(2, 200)), - validation.Field(&ldapConfig.BindPassword, validation.Required, validation.Length(2, 200)), - ) + err = validateConfig(config) + errLdap := validateLdapConfig(&ldapConfig) if err != nil { Log.Error().Msgf(strings.Replace(err.Error(), "; ", "\n", -1)) @@ -191,7 +175,7 @@ func MakeConfig() (*types.Config, error) { } if errLdap != nil { Log.Error().Msgf(strings.Replace(errLdap.Error(), "; ", "\n", -1)) - return nil, err + return nil, errLdap } return config, nil } diff --git a/internal/utils/validationConfig.go b/internal/utils/validationConfig.go new file mode 100644 index 00000000..321f615c --- /dev/null +++ b/internal/utils/validationConfig.go @@ -0,0 +1,84 @@ +package utils + +import ( + "encoding/base64" + "errors" + "net/url" + "strconv" + + "github.com/ca-gip/kubi/pkg/types" +) + +func validateBase64(s string) error { + if _, err := base64.StdEncoding.DecodeString(s); err != nil { + return errors.New("must be a valid base64 string") + } + return nil +} + +func validateURL(s string) error { + if _, err := url.ParseRequestURI(s); err != nil { + return errors.New("must be a valid URL") + } + return nil +} +func validateLength(field string, min int, max int) error { + length := len(field) + if length < min || length > max { + return errors.New("length must be between " + strconv.Itoa(min) + " and " + strconv.Itoa(max) + " characters") + } + return nil +} + +func validateLdapConfig(ldapConfig *types.LdapConfig) error { + if ldapConfig.UserBase == "" { + return errors.New("UserBase is required") + } + if err := validateLength(ldapConfig.UserBase, 2, 200); err != nil { + return err + } + if ldapConfig.GroupBase == "" { + return errors.New("GroupBase is required") + } + if err := validateLength(ldapConfig.GroupBase, 2, 200); err != nil { + return err + } + if ldapConfig.Host == "" { + return errors.New("Host is required") + } + if err := validateURL(ldapConfig.Host); err != nil { + return err + } + if ldapConfig.BindDN == "" { + return errors.New("BindDN is required") + } + if err := validateLength(ldapConfig.BindDN, 2, 200); err != nil { + return err + } + if ldapConfig.BindPassword == "" { + return errors.New("BindPassword is required") + } + if err := validateLength(ldapConfig.BindPassword, 2, 200); err != nil { + return err + } + return nil +} + +func validateConfig(config *types.Config) error { + if config.KubeToken == "" { + return errors.New("KubeToken is required") + } + if config.KubeCa == "" { + return errors.New("KubeCa is required") + } + if err := validateBase64(config.KubeCa); err != nil { + return err + } + if config.PublicApiServerURL == "" { + return errors.New("PublicApiServerURL is required") + } + if err := validateURL(config.PublicApiServerURL); err != nil { + return err + } + return nil +}