Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Added validation check to impose limits on stored, free-form strings #180

Merged
merged 13 commits into from
Aug 18, 2021
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/pquerna/cachecontrol v0.0.0-20201205024021-ac21108117ac // indirect
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/common v0.19.0 // indirect
github.com/qor/validations v0.0.0-20171228122639-f364bca61b46 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/aws/amazon-sagemaker-operator-for-k8s v1.0.1-0.20210303003444-0fb33b1fd49d/go.mod h1:mZUP7GJmjiWtf8v3FD1X/QdK08BqyeH/1Ejt0qhNzCs=
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
Expand Down Expand Up @@ -741,6 +742,8 @@ github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/qor/validations v0.0.0-20171228122639-f364bca61b46 h1:dRlsVUhwD1pwrasuVbNooGQITYjKzmXK5eYoEEvBGQI=
github.com/qor/validations v0.0.0-20171228122639-f364bca61b46/go.mod h1:UJsA0AuvrKNaWtrb1UzKai10mN3ZBbQkPjUHpxwahTc=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down
2 changes: 2 additions & 0 deletions pkg/repositories/config/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres" // Required to import database driver.
"github.com/qor/validations"
)

const Postgres = "postgres"
Expand Down Expand Up @@ -77,5 +78,6 @@ func OpenDbConnection(config DbConnectionConfigProvider) *gorm.DB {
panic(err)
}
db.LogMode(config.IsDebug())
validations.RegisterCallbacks(db)
return db
}
3 changes: 3 additions & 0 deletions pkg/repositories/gormimpl/project_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ type ProjectRepo struct {
func (r *ProjectRepo) Create(ctx context.Context, project models.Project) error {
timer := r.metrics.CreateDuration.Start()
tx := r.db.Create(&project)
// TODO: Should we return all validation error here
// txErr := tx.GetErrors()
timer.Stop()
if tx.Error != nil {
//TODO: Should we return all the errors here
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/repositories/gormimpl/utils_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/qor/validations"

mocket "github.com/Selvatico/go-mocket"
"github.com/flyteorg/flyteadmin/pkg/common"
Expand All @@ -25,6 +26,7 @@ func GetDbForTest(t *testing.T) *gorm.DB {
if err != nil {
t.Fatal(fmt.Sprintf("Failed to open mock db with err %v", err))
}
validations.RegisterCallbacks(db)
return db
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/repositories/models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
type Project struct {
BaseModel
Identifier string `gorm:"primary_key"`
Name string // Human-readable name, not a unique identifier.
Name string `valid:"length(3|50)"` // Human-readable name, not a unique identifier.
yindia marked this conversation as resolved.
Show resolved Hide resolved
Description string `gorm:"type:varchar(300)"`
Labels []byte
// GORM doesn't save the zero value for ints, so we use a pointer for the State field
Expand Down