Skip to content

Commit

Permalink
Merge pull request #11 from poligonoio/feature/request-body-validator
Browse files Browse the repository at this point in the history
fix: Add validation for data source secrets
  • Loading branch information
eddydecena authored Oct 25, 2024
2 parents 1dcb8ae + 157fd8b commit cc793d7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 24 deletions.
24 changes: 12 additions & 12 deletions internal/models/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ type UpdateRequestDataSourceBody struct {
}

type PostgreSQLSecret struct {
Host string `json:"hostname"`
Port int `json:"port"`
User string `json:"username"`
Database string `json:"database"`
Password string `json:"password"`
SSL bool `json:"ssl"`
Host string `json:"hostname" validate:"required,hostname"`
Port int `json:"port" validate:"required,number"`
User string `json:"username" validate:"required"`
Database string `json:"database" validate:"required"`
Password string `json:"password" validate:"required"`
SSL bool `json:"ssl" validate:"required,boolean"`
}

type MySQLSecret struct {
Host string `json:"hostname"`
Port int `json:"port"`
User string `json:"username"`
Database string `json:"database"`
Password string `json:"password"`
SSL bool `json:"ssl"`
Host string `json:"hostname" validate:"required,hostname"`
Port int `json:"port" validate:"required,number"`
User string `json:"username" validate:"required"`
Database string `json:"database" validate:"required"`
Password string `json:"password" validate:"required"`
SSL bool `json:"ssl" validate:"required,boolean"`
}
17 changes: 10 additions & 7 deletions internal/services/ds.service.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"

validatorv10 "github.com/go-playground/validator/v10"
"github.com/poligonoio/vega-core/internal/models"
"github.com/poligonoio/vega-core/pkg/logger"
"github.com/poligonoio/vega-core/pkg/utils"
Expand All @@ -20,15 +21,17 @@ type DataSourceServiceImpl struct {
infisicalService InfisicalService
engineService EngineService
schemaService SchemaService
validate *validatorv10.Validate
}

func NewDataSourceService(ctx context.Context, dataSourceCollection *mongo.Collection, infisicalService InfisicalService, engineService EngineService, schemaService SchemaService) DataSourceService {
func NewDataSourceService(ctx context.Context, dataSourceCollection *mongo.Collection, infisicalService InfisicalService, engineService EngineService, schemaService SchemaService, validate *validatorv10.Validate) DataSourceService {
return &DataSourceServiceImpl{
ctx: ctx,
dataSourceCollection: dataSourceCollection,
infisicalService: infisicalService,
engineService: engineService,
schemaService: schemaService,
validate: validate,
}
}

Expand Down Expand Up @@ -259,13 +262,13 @@ func (self *DataSourceServiceImpl) CreateCatalog(catalogName string, dataSourceT

switch dataSourceType {
case models.PostgreSQL:
psql := NewPostgreSQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
psql := NewPostgreSQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService, self.validate)
err = psql.CreateCatalog(catalogName, dataSourceType, secret)
case models.MySQL:
mysql := NewMySQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
mysql := NewMySQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService, self.validate)
err = mysql.CreateCatalog(catalogName, dataSourceType, secret)
case models.MariaDB:
mariadb := NewMariaDBDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
mariadb := NewMariaDBDataSourceDatabase(self.ctx, self.engineService, self.schemaService, self.validate)
err = mariadb.CreateCatalog(catalogName, dataSourceType, secret)
default:
return errors.New("Invalid Data Source Type")
Expand All @@ -283,13 +286,13 @@ func (self *DataSourceServiceImpl) Sync(id primitive.ObjectID, dataSourceType mo

switch dataSourceType {
case models.PostgreSQL:
psql := NewPostgreSQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
psql := NewPostgreSQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService, self.validate)
err = psql.Sync(id)
case models.MySQL:
mysql := NewMySQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
mysql := NewMySQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService, self.validate)
err = mysql.Sync(id)
case models.MariaDB:
mariadb := NewMariaDBDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
mariadb := NewMariaDBDataSourceDatabase(self.ctx, self.engineService, self.schemaService, self.validate)
err = mariadb.Sync(id)
default:
return errors.New("Invalid Data Source Type")
Expand Down
12 changes: 11 additions & 1 deletion internal/services/ds.type.mariadb.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import (
"fmt"
"strconv"

validatorv10 "github.com/go-playground/validator/v10"
"github.com/poligonoio/vega-core/internal/models"
"github.com/poligonoio/vega-core/pkg/logger"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type MariaDBDataSourceTypeImpl struct {
ctx context.Context
engineService EngineService
schemaService SchemaService
validate *validatorv10.Validate
}

func NewMariaDBDataSourceDatabase(ctx context.Context, engineService EngineService, schemaService SchemaService) DataSourceTypeInter {
func NewMariaDBDataSourceDatabase(ctx context.Context, engineService EngineService, schemaService SchemaService, validate *validatorv10.Validate) DataSourceTypeInter {
return &MariaDBDataSourceTypeImpl{
ctx: ctx,
engineService: engineService,
schemaService: schemaService,
validate: validate,
}
}

Expand Down Expand Up @@ -83,6 +87,12 @@ func (self *MariaDBDataSourceTypeImpl) CreateCatalog(catalogName string, dataSou
return err
}

if err := self.validate.Struct(mysql); err != nil {
validationErr := err.(validatorv10.ValidationErrors)
logger.Error.Println(fmt.Printf("One or more secret fields are invalid: %s\n", validationErr))
return validationErr
}

var mysqlString string

if mysql.SSL {
Expand Down
12 changes: 11 additions & 1 deletion internal/services/ds.type.mysql.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import (
"fmt"
"strconv"

validatorv10 "github.com/go-playground/validator/v10"
"github.com/poligonoio/vega-core/internal/models"
"github.com/poligonoio/vega-core/pkg/logger"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type MySQLDataSourceTypeImpl struct {
ctx context.Context
engineService EngineService
schemaService SchemaService
validate *validatorv10.Validate
}

func NewMySQLDataSourceDatabase(ctx context.Context, engineService EngineService, schemaService SchemaService) DataSourceTypeInter {
func NewMySQLDataSourceDatabase(ctx context.Context, engineService EngineService, schemaService SchemaService, validate *validatorv10.Validate) DataSourceTypeInter {
return &MySQLDataSourceTypeImpl{
ctx: ctx,
engineService: engineService,
schemaService: schemaService,
validate: validate,
}
}

Expand Down Expand Up @@ -83,6 +87,12 @@ func (self *MySQLDataSourceTypeImpl) CreateCatalog(catalogName string, dataSourc
return err
}

if err := self.validate.Struct(mysql); err != nil {
validationErr := err.(validatorv10.ValidationErrors)
logger.Error.Println(fmt.Printf("One or more secret fields are invalid: %s\n", validationErr))
return validationErr
}

var mysqlString string

if mysql.SSL {
Expand Down
12 changes: 11 additions & 1 deletion internal/services/ds.type.psql.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import (
"fmt"
"strconv"

validatorv10 "github.com/go-playground/validator/v10"
"github.com/poligonoio/vega-core/internal/models"
"github.com/poligonoio/vega-core/pkg/logger"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type PostgresSQLDataSourceTypeImpl struct {
ctx context.Context
engineService EngineService
schemaService SchemaService
validate *validatorv10.Validate
}

func NewPostgreSQLDataSourceDatabase(ctx context.Context, engineService EngineService, schemaService SchemaService) DataSourceTypeInter {
func NewPostgreSQLDataSourceDatabase(ctx context.Context, engineService EngineService, schemaService SchemaService, validate *validatorv10.Validate) DataSourceTypeInter {
return &PostgresSQLDataSourceTypeImpl{
ctx: ctx,
engineService: engineService,
schemaService: schemaService,
validate: validate,
}
}

Expand Down Expand Up @@ -84,6 +88,12 @@ func (self *PostgresSQLDataSourceTypeImpl) CreateCatalog(catalogName string, dat
return err
}

if err := self.validate.Struct(psql); err != nil {
validationErr := err.(validatorv10.ValidationErrors)
logger.Error.Println(fmt.Printf("One or more secret fields are invalid: %s\n", validationErr))
return validationErr
}

var psqlString string

if psql.SSL {
Expand Down
3 changes: 1 addition & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var server *http.Server
var version string

func init() {
// version := os.Getenv("POLIGONO_VERSION")
basePathStr := "/v1alpha1"

port := os.Getenv("PORT")
Expand Down Expand Up @@ -99,7 +98,7 @@ func init() {
// Initialize Data source service and controller
logger.Info.Println("Initializing Data source service and controller...")
dataSourceCollection := mongoClient.Database("poligono").Collection("datasources")
dataSourceService := services.NewDataSourceService(ctx, dataSourceCollection, infisicalService, trinoEngineService, schemaService)
dataSourceService := services.NewDataSourceService(ctx, dataSourceCollection, infisicalService, trinoEngineService, schemaService, validate)
dataSourceController := controllers.NewDataSourceController(dataSourceService, trinoEngineService, schemaService, validate)
logger.Info.Println("Data source service and controller Initialized successfully!")

Expand Down

0 comments on commit cc793d7

Please sign in to comment.