Skip to content

Commit

Permalink
feat: validate resource when decode or store
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 24, 2024
1 parent ec2bc04 commit ee29973
Show file tree
Hide file tree
Showing 75 changed files with 405 additions and 261 deletions.
8 changes: 4 additions & 4 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/evanw/esbuild v0.24.0 // indirect
github.com/evanw/esbuild v0.24.2 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down Expand Up @@ -81,8 +81,8 @@ require (
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241219192143-6b3ec007d9bb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241219192143-6b3ec007d9bb // indirect
google.golang.org/protobuf v1.36.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241223144023-3abc09e42ca8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
16 changes: 8 additions & 8 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd h1:QMSNEh9uQkDjyPwu/J5
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/evanw/esbuild v0.24.0 h1:GZ78naTLp7FKr+K7eNuM/SLs5maeiHYRPsTg6kmdsSE=
github.com/evanw/esbuild v0.24.0/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/evanw/esbuild v0.24.2 h1:PQExybVBrjHjN6/JJiShRGIXh1hWVm6NepVnhZhrt0A=
github.com/evanw/esbuild v0.24.2/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
Expand Down Expand Up @@ -212,12 +212,12 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20241219192143-6b3ec007d9bb h1:B7GIB7sr443wZ/EAEl7VZjmh1V6qzkt5V+RYcUYtS1U=
google.golang.org/genproto/googleapis/api v0.0.0-20241219192143-6b3ec007d9bb/go.mod h1:E5//3O5ZIG2l71Xnt+P/CYUY8Bxs8E7WMoZ9tlcMbAY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241219192143-6b3ec007d9bb h1:3oy2tynMOP1QbTC0MsNNAV+Se8M2Bd0A5+x1QHyw+pI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241219192143-6b3ec007d9bb/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/genproto/googleapis/api v0.0.0-20241223144023-3abc09e42ca8 h1:st3LcW/BPi75W4q1jJTEor/QWwbNlPlDG0JTn6XhZu0=
google.golang.org/genproto/googleapis/api v0.0.0-20241223144023-3abc09e42ca8/go.mod h1:klhJGKFyG8Tn50enBn7gizg4nXGXJ+jqEREdCWaPcV4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 h1:TqExAhdPaB60Ux47Cn0oLV07rGnxZzIsaRhQaqS666A=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
8 changes: 4 additions & 4 deletions cmd/pkg/cli/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestApplyCommand_Execute(t *testing.T) {

scrt := &secret.Secret{
Name: faker.UUIDHyphenated(),
Data: faker.Word(),
Data: faker.UUIDHyphenated(),
}

data, err := json.Marshal(scrt)
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestApplyCommand_Execute(t *testing.T) {

scrt := &secret.Secret{
Name: faker.UUIDHyphenated(),
Data: faker.Word(),
Data: faker.UUIDHyphenated(),
}

_, err := secretStore.Store(ctx, scrt)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestApplyCommand_Execute(t *testing.T) {

chrt := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Name: faker.Word(),
Name: faker.UUIDHyphenated(),
}

data, err := json.Marshal(chrt)
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestApplyCommand_Execute(t *testing.T) {

chrt := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Name: faker.Word(),
Name: faker.UUIDHyphenated(),
}

_, err := chartStore.Store(ctx, chrt)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pkg/cli/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestDeleteCommand_Execute(t *testing.T) {

scrt := &secret.Secret{
Name: faker.UUIDHyphenated(),
Data: faker.Word(),
Data: faker.UUIDHyphenated(),
}

data, err := json.Marshal(scrt)
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestDeleteCommand_Execute(t *testing.T) {

chrt := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Name: faker.Word(),
Name: faker.UUIDHyphenated(),
}

data, err := json.Marshal(chrt)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pkg/cli/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestGetCommand_Execute(t *testing.T) {

scrt := &secret.Secret{
Name: faker.UUIDHyphenated(),
Data: faker.Word(),
Data: faker.UUIDHyphenated(),
}

_, err := secretStore.Store(ctx, scrt)
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestGetCommand_Execute(t *testing.T) {

chrt := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Name: faker.Word(),
Name: faker.UUIDHyphenated(),
}

_, err := chartStore.Store(ctx, chrt)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pkg/cli/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestStartCommand_Execute(t *testing.T) {
scrt := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
Namespace: resource.DefaultNamespace,
Data: faker.Word(),
Data: faker.UUIDHyphenated(),
}

data, _ := json.Marshal(scrt)
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestStartCommand_Execute(t *testing.T) {
chrt := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Namespace: resource.DefaultNamespace,
Name: faker.Word(),
Name: faker.UUIDHyphenated(),
}

data, _ := json.Marshal(chrt)
Expand Down
17 changes: 16 additions & 1 deletion driver/mongo/pkg/chart/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package chart

import (
"context"
"github.com/go-playground/validator/v10"
"github.com/siyul-park/uniflow/pkg/encoding"

"github.com/gofrs/uuid"
"github.com/pkg/errors"
Expand All @@ -17,6 +19,7 @@ import (
// Store manages storage and retrieval of Spec objects in a MongoDB collection.
type Store struct {
collection *mongo.Collection
validate *validator.Validate
}

// Stream represents a MongoDB change stream for tracking Spec changes.
Expand All @@ -39,7 +42,10 @@ var _ chart.Stream = (*Stream)(nil)

// NewStore creates a new Store with the specified MongoDB collection.
func NewStore(collection *mongo.Collection) *Store {
return &Store{collection: collection}
return &Store{
collection: collection,
validate: validator.New(validator.WithRequiredStructEnabled()),
}
}

// Index ensures the collection has the required indexes and updates them if necessary.
Expand Down Expand Up @@ -120,6 +126,10 @@ func (s *Store) Store(ctx context.Context, charts ...*chart.Chart) (int, error)
chrt.SetNamespace(resource.DefaultNamespace)
}

if err := s.validate.Struct(chrt); err != nil {
return 0, errors.WithMessage(encoding.ErrUnsupportedValue, err.Error())
}

docs = append(docs, chrt)
}

Expand All @@ -140,6 +150,11 @@ func (s *Store) Swap(ctx context.Context, charts ...*chart.Chart) (int, error) {
if chrt.GetID() == uuid.Nil {
chrt.SetID(uuid.Must(uuid.NewV7()))
}

if err := s.validate.Struct(chrt); err != nil {
return 0, errors.WithMessage(encoding.ErrUnsupportedValue, err.Error())
}

ids[i] = chrt.GetID()
}

Expand Down
13 changes: 10 additions & 3 deletions driver/mongo/pkg/chart/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package chart

import (
"context"
"github.com/siyul-park/uniflow/pkg/spec"
"testing"

"github.com/go-faker/faker/v4"
"github.com/gofrs/uuid"
"github.com/siyul-park/uniflow/driver/mongo/pkg/server"
"github.com/siyul-park/uniflow/pkg/chart"
"github.com/siyul-park/uniflow/pkg/resource"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
Expand Down Expand Up @@ -59,8 +59,7 @@ func TestStore_Watch(t *testing.T) {
}()

chrt := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Namespace: resource.DefaultNamespace,
ID: uuid.Must(uuid.NewV7()),
}

_, _ = st.Store(ctx, chrt)
Expand Down Expand Up @@ -110,6 +109,14 @@ func TestStore_Store(t *testing.T) {

chrt1 := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Specs: []*spec.Unstructured{
{
Meta: spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.UUIDHyphenated(),
},
},
},
}
chrt2 := &chart.Chart{
ID: uuid.Must(uuid.NewV7()),
Expand Down
17 changes: 16 additions & 1 deletion driver/mongo/pkg/secret/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package secret

import (
"context"
"github.com/go-playground/validator/v10"
"github.com/siyul-park/uniflow/pkg/encoding"

"github.com/gofrs/uuid"
"github.com/pkg/errors"
Expand All @@ -16,6 +18,7 @@ import (
// Store manages storage and retrieval of Spec objects in a MongoDB collection.
type Store struct {
collection *mongo.Collection
validate *validator.Validate
}

// Stream represents a MongoDB change stream for tracking Spec changes.
Expand All @@ -38,7 +41,10 @@ var _ secret.Stream = (*Stream)(nil)

// NewStore creates a new Store with the specified MongoDB collection.
func NewStore(collection *mongo.Collection) *Store {
return &Store{collection: collection}
return &Store{
collection: collection,
validate: validator.New(validator.WithRequiredStructEnabled()),
}
}

// Index ensures the collection has the required indexes and updates them if necessary.
Expand Down Expand Up @@ -119,6 +125,10 @@ func (s *Store) Store(ctx context.Context, secrets ...*secret.Secret) (int, erro
scrt.SetNamespace(resource.DefaultNamespace)
}

if err := s.validate.Struct(scrt); err != nil {
return 0, errors.WithMessage(encoding.ErrUnsupportedValue, err.Error())
}

docs = append(docs, scrt)
}

Expand All @@ -139,6 +149,11 @@ func (s *Store) Swap(ctx context.Context, secrets ...*secret.Secret) (int, error
if scrt.GetID() == uuid.Nil {
scrt.SetID(uuid.Must(uuid.NewV7()))
}

if err := s.validate.Struct(scrt); err != nil {
return 0, errors.WithMessage(encoding.ErrUnsupportedValue, err.Error())
}

ids[i] = scrt.GetID()
}

Expand Down
29 changes: 18 additions & 11 deletions driver/mongo/pkg/secret/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/go-faker/faker/v4"
"github.com/gofrs/uuid"
"github.com/siyul-park/uniflow/driver/mongo/pkg/server"
"github.com/siyul-park/uniflow/pkg/resource"
"github.com/siyul-park/uniflow/pkg/secret"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/v2/mongo"
Expand Down Expand Up @@ -59,8 +58,8 @@ func TestStore_Watch(t *testing.T) {
}()

scrt := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
Namespace: resource.DefaultNamespace,
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}

_, _ = st.Store(ctx, scrt)
Expand All @@ -81,10 +80,12 @@ func TestStore_Load(t *testing.T) {
st := NewStore(c.Database(faker.UUIDHyphenated()).Collection(faker.UUIDHyphenated()))

scrt1 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}
scrt2 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}

count, err := st.Store(ctx, scrt1, scrt2)
Expand All @@ -109,10 +110,12 @@ func TestStore_Store(t *testing.T) {
st := NewStore(c.Database(faker.UUIDHyphenated()).Collection(faker.UUIDHyphenated()))

scrt1 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}
scrt2 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}

count, err := st.Store(ctx, scrt1, scrt2)
Expand All @@ -137,10 +140,12 @@ func TestStore_Swap(t *testing.T) {
st := NewStore(c.Database(faker.UUIDHyphenated()).Collection(faker.UUIDHyphenated()))

scrt1 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}
scrt2 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}

count, err := st.Store(ctx, scrt1, scrt2)
Expand Down Expand Up @@ -169,10 +174,12 @@ func TestMemStore_Delete(t *testing.T) {
st := NewStore(c.Database(faker.UUIDHyphenated()).Collection(faker.UUIDHyphenated()))

scrt1 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}
scrt2 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
ID: uuid.Must(uuid.NewV7()),
Data: faker.UUIDHyphenated(),
}

count, err := st.Store(ctx, scrt1, scrt2)
Expand Down
Loading

0 comments on commit ee29973

Please sign in to comment.