Skip to content

Commit

Permalink
Remove go-ozzo/ozza-validation package
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zelihacagip committed Dec 19, 2024
1 parent 75765fb commit 6ed8c0b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
22 changes: 3 additions & 19 deletions internal/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -168,30 +166,16 @@ 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))
return nil, err
}
if errLdap != nil {
Log.Error().Msgf(strings.Replace(errLdap.Error(), "; ", "\n", -1))
return nil, err
return nil, errLdap
}
return config, nil
}
Expand Down
84 changes: 84 additions & 0 deletions internal/utils/validationConfig.go
Original file line number Diff line number Diff line change
@@ -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")

Check failure on line 47 in internal/utils/validationConfig.go

View workflow job for this annotation

GitHub Actions / Build and test images (webhook, amd64)

error strings should not be capitalized (ST1005)

Check failure on line 47 in internal/utils/validationConfig.go

View workflow job for this annotation

GitHub Actions / Build and test images (api, amd64)

error strings should not be capitalized (ST1005)

Check failure on line 47 in internal/utils/validationConfig.go

View workflow job for this annotation

GitHub Actions / Build and test images (operator, amd64)

error strings should not be capitalized (ST1005)
}
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
}

0 comments on commit 6ed8c0b

Please sign in to comment.