add(validation config)

This commit is contained in:
DeniskaRediska committed 2019-10-31 16:17:15 +03:00
1 parent db68505cca
commit 0d5094cfb5
10 files changed
+208 -26

No files matched your search

Generated
+63 -12
View File
@@ -41,6 +41,14 @@
revision = "4b4cebaf850ec58f1bb1fec5bdebdf8501c2bc3f"
version = "v3.0.0"
[[projects]]
digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec"
name = "github.com/davecgh/go-spew"
packages = ["spew"]
pruneopts = "UT"
revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73"
version = "v1.1.1"
[[projects]]
digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd"
name = "github.com/fsnotify/fsnotify"
@@ -278,8 +286,8 @@
"zlib",
]
pruneopts = "UT"
revision = "a41f1a10bd3b167958ff6df80b800fe2969dd3ca"
version = "v1.9.0"
revision = "16a4d3d7137cdefd94d420f22b5c20260674b95c"
version = "v1.9.1"
[[projects]]
digest = "1:31e761d97c76151dde79e9d28964a812c46efc5baee4085b86f68f0c654450de"
@@ -366,12 +374,12 @@
revision = "c48cc78d482608239f6c4c92a4abd87eb8761c90"
[[projects]]
digest = "1:bbd3997f0121200f72b64d7a3826eb8a0b910d6a4c19894c9fe2852b9e5eaf3b"
digest = "1:6eea828983c70075ca297bb915ffbcfd3e34c5a50affd94428a65df955c0ff9c"
name = "github.com/pelletier/go-toml"
packages = ["."]
pruneopts = "UT"
revision = "8fe62057ea2d46ce44254c98e84e810044dbe197"
version = "v1.5.0"
revision = "903d9455db9ff1d7ac1ab199062eca7266dd11a3"
version = "v1.6.0"
[[projects]]
digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b"
@@ -381,6 +389,14 @@
revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4"
version = "v0.8.1"
[[projects]]
digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]
pruneopts = "UT"
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]]
digest = "1:7d99649602f68b73ade640ca6975ccc38735f329e159c6a507b63f00213678c0"
name = "github.com/pressly/goose"
@@ -456,6 +472,14 @@
revision = "b5bf975e5823809fb22c7644d008757f78a4259e"
version = "v1.4.0"
[[projects]]
digest = "1:8548c309c65a85933a625be5e7d52b6ac927ca30c56869fae58123b8a77a75e1"
name = "github.com/stretchr/testify"
packages = ["assert"]
pruneopts = "UT"
revision = "221dbe5ed46703ee255b1da0dec05086f5035f62"
version = "v1.4.0"
[[projects]]
branch = "master"
digest = "1:d32b64a7a82dd36b674fb73ff767e949eb7155b6b574ff276f49d4edc8af105f"
@@ -474,7 +498,7 @@
[[projects]]
branch = "master"
digest = "1:77f7e2f75e4c44ede24f47aca6c514f24f06c6a0ee3c47c8d7ce2b77b49ac482"
digest = "1:35cbf67ac1c92eb7bd6fe70d17b75b60bd38fb2222fa71ac82d44d3813897d22"
name = "github.com/valyala/fasthttp"
packages = [
".",
@@ -482,7 +506,7 @@
"stackless",
]
pruneopts = "UT"
revision = "9bc6da12192c610a272a9289b9574eb5d8cfdfe1"
revision = "87c4546c7b1268a1c29c46e8f54def6f0e7596eb"
[[projects]]
digest = "1:39a425f98fb19427061a693fe6bf0683c9bddec4b25b17067e34fdb465e39cb9"
@@ -492,6 +516,30 @@
revision = "bb2702d423886830dee131692131d35648c382e2"
version = "v0.5.2"
[[projects]]
branch = "master"
digest = "1:87fe9bca786484cef53d52adeec7d1c52bc2bfbee75734eddeb75fc5c7023871"
name = "github.com/xeipuuv/gojsonpointer"
packages = ["."]
pruneopts = "UT"
revision = "02993c407bfbf5f6dae44c4f4b1cf6a39b5fc5bb"
[[projects]]
branch = "master"
digest = "1:dc6a6c28ca45d38cfce9f7cb61681ee38c5b99ec1425339bfc1e1a7ba769c807"
name = "github.com/xeipuuv/gojsonreference"
packages = ["."]
pruneopts = "UT"
revision = "bd5ef7bd5415a7ac448318e64f11a24cd21e594b"
[[projects]]
digest = "1:a8a0ed98532819a3b0dc5cf3264a14e30aba5284b793ba2850d6f381ada5f987"
name = "github.com/xeipuuv/gojsonschema"
packages = ["."]
pruneopts = "UT"
revision = "82fcdeb203eb6ab2a67d0a623d9c19e5e5a64927"
version = "v1.2.0"
[[projects]]
branch = "master"
digest = "1:35ce874cf16f78a2da908ab28b0277497e36603d289db00d39da5200bc8ace08"
@@ -501,7 +549,7 @@
"ssh/terminal",
]
pruneopts = "UT"
revision = "87dc89f01550277dc22b74ffcf4cd89fa2f40f4c"
revision = "8986dd9e96cf0a6f74da406c005ba3df38527c04"
[[projects]]
branch = "master"
@@ -517,18 +565,18 @@
"trace",
]
pruneopts = "UT"
revision = "ec77196f6094c3492a8b61f2c11cf937f78992ae"
revision = "fe3aa8a4527195a6057b3fad46619d7d090e99b5"
[[projects]]
branch = "master"
digest = "1:de0084adc1c0b8cf6fd192d7785b098b155ba355cbeec9ee361aea55b887ab9d"
digest = "1:2c482fc32bdcfc572ce8b9cc37a409ae8a24c604b0dde73301d8de05d2c1f3e7"
name = "golang.org/x/sys"
packages = [
"unix",
"windows",
]
pruneopts = "UT"
revision = "53bf42e6b339f7ed00535fc6915737b3959eac89"
revision = "f43be2a4598cf3a47be9f94f0c28197ed9eae611"
[[projects]]
digest = "1:8d8faad6b12a3a4c819a3f9618cb6ee1fa1cfc33253abeeea8b55336721e3405"
@@ -564,7 +612,7 @@
"googleapis/rpc/status",
]
pruneopts = "UT"
revision = "548a555dbc03994223efbaba0090152849259498"
revision = "919d9bdd9fe6f1a5dd95ce5d5e4cdb8fd3c516d0"
[[projects]]
digest = "1:6cd77d0b616d2dcebd363dfecba593f27b0151fc82cdb5fbfb96c5a7cfbc95b5"
@@ -648,13 +696,16 @@
"github.com/integration-system/isp-lib/utils",
"github.com/integration-system/isp-log",
"github.com/integration-system/isp-log/stdcodes",
"github.com/integration-system/jsonschema",
"github.com/json-iterator/go",
"github.com/mohae/deepcopy",
"github.com/pkg/errors",
"github.com/satori/go.uuid",
"github.com/sirupsen/logrus",
"github.com/spf13/cast",
"github.com/stretchr/testify/assert",
"github.com/thecodeteam/goodbye",
"github.com/xeipuuv/gojsonschema",
"google.golang.org/grpc/codes",
"google.golang.org/grpc/status",
]
+1
View File
@@ -29,6 +29,7 @@ type ActivateConfig struct {
type UpsertConfig struct {
Config entity.Config
Create bool
Unsafe bool
}
type DeleteConfigs struct {
+8 -4
View File
@@ -54,13 +54,14 @@ func (c *config) GetActiveConfigByModuleName(request domain.GetByModuleNameReque
// @Failure 404 {object} structure.GrpcError "если конфигурация не найдена"
// @Failure 500 {object} structure.GrpcError
// @Router /config/create_update_config [POST]
func (c *config) CreateUpdateConfig(config entity.Config) (*entity.Config, error) {
var response entity.Config
func (c *config) CreateUpdateConfig(config domain.CreateUpdateConfigRequest) (*entity.Config, error) {
var response domain.CreateUpdateConfigInvalidResponse
now := state.GenerateDate()
config.CreatedAt = now
config.UpdatedAt = now
upsertConfig := cluster.UpsertConfig{
Config: config,
Config: config.Config,
Unsafe: config.Unsafe,
}
if config.Id == "" {
upsertConfig.Config.Id = state.GenerateId()
@@ -70,8 +71,11 @@ func (c *config) CreateUpdateConfig(config entity.Config) (*entity.Config, error
err := PerformSyncApplyWithError(command, "UpsertConfigCommand", &response)
if err != nil {
return nil, err
} else if response.Valid {
return response.Config, nil
} else {
return nil, utils.CreateValidationErrorDetails(codes.InvalidArgument, utils.ValidationError, response.Details)
}
return &response, nil
}
// MarkConfigAsActive godoc
+8 -1
View File
@@ -1,12 +1,14 @@
package domain
import "isp-config-service/entity"
type ConfigIdRequest struct {
Id string `json:"id" valid:"required~Required"`
}
type CompileConfigsRequest struct {
Data map[string]interface{}
CommonConfigsIdList []string `valid:"required~Required"`
CommonConfigsIdList []string
}
type GetByModuleIdRequest struct {
@@ -16,3 +18,8 @@ type GetByModuleIdRequest struct {
type GetByModuleNameRequest struct {
ModuleName string `valid:"required~Required"`
}
type CreateUpdateConfigRequest struct {
entity.Config
Unsafe bool
}
+14 -3
View File
@@ -18,9 +18,9 @@ type ModuleInfo struct {
CreatedAt time.Time
LastConnectedAt time.Time
LastDisconnectedAt time.Time
Configs []entity.Config `json:",omitempty"`
ConfigSchema *schema.Schema `json:",omitempty"`
Status []Connection `json:",omitempty"`
Configs []ConfigModuleInfo `json:",omitempty"`
ConfigSchema *schema.Schema `json:",omitempty"`
Status []Connection `json:",omitempty"`
}
type Connection struct {
@@ -37,3 +37,14 @@ type DeleteCommonConfigResponse struct {
Deleted bool
Links CommonConfigLinks
}
type CreateUpdateConfigInvalidResponse struct {
Valid bool
Details map[string]string
Config *entity.Config
}
type ConfigModuleInfo struct {
entity.Config
Valid bool
}
+4 -4
View File
@@ -13,10 +13,10 @@ type Handlers struct {
GetModulesInfo func() ([]domain.ModuleInfo, error) `method:"get_modules_info" group:"module" inner:"true"`
// ===== CONFIG =====
GetActiveConfigByModuleName func(request domain.GetByModuleNameRequest) (*entity.Config, error) `method:"get_active_config_by_module_name" group:"config" inner:"true"`
CreateUpdateConfig func(config entity.Config) (*entity.Config, error) `method:"create_update_config" group:"config" inner:"true"`
MarkConfigAsActive func(identity domain.ConfigIdRequest) (*entity.Config, error) `method:"mark_config_as_active" group:"config" inner:"true"`
DeleteConfig func(identities []string) (*domain.DeleteResponse, error) `method:"delete_config" group:"config" inner:"true"`
GetActiveConfigByModuleName func(request domain.GetByModuleNameRequest) (*entity.Config, error) `method:"get_active_config_by_module_name" group:"config" inner:"true"`
CreateUpdateConfig func(config domain.CreateUpdateConfigRequest) (*entity.Config, error) `method:"create_update_config" group:"config" inner:"true"`
MarkConfigAsActive func(identity domain.ConfigIdRequest) (*entity.Config, error) `method:"mark_config_as_active" group:"config" inner:"true"`
DeleteConfig func(identities []string) (*domain.DeleteResponse, error) `method:"delete_config" group:"config" inner:"true"`
// ===== COMMON CONFIG =====
GetCommonConfigs func(identities []string) []entity.CommonConfig `method:"get_configs" group:"common_config" inner:"true"`
+61 -1
View File
@@ -2,13 +2,16 @@ package service
import (
"encoding/json"
"fmt"
"github.com/integration-system/bellows"
"github.com/integration-system/isp-lib/utils"
log "github.com/integration-system/isp-log"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
codes2 "google.golang.org/grpc/codes"
"isp-config-service/cluster"
"isp-config-service/codes"
"isp-config-service/domain"
"isp-config-service/entity"
"isp-config-service/holder"
"isp-config-service/model"
@@ -23,6 +26,18 @@ const ConfigWatchersRoomSuffix = "_config"
type configService struct{}
type validationSchemaError struct {
Description map[string]string
}
func (e validationSchemaError) Error() string {
resp := ""
for field, desc := range e.Description {
resp = fmt.Sprintf("%s- %s%s\n", resp, field, desc)
}
return resp
}
func (s configService) GetCompiledConfig(moduleName string, state state.ReadonlyState) (map[string]interface{}, error) {
module := state.Modules().GetByName(moduleName)
if module == nil {
@@ -89,6 +104,29 @@ func (cs configService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCon
if module == nil {
return cluster.NewResponseErrorf(codes2.InvalidArgument, "invalid moduleId %s", config.ModuleId)
}
if !upsertConfig.Unsafe {
schemaStorage := state.Schemas().GetByModuleIds([]string{config.ModuleId})
if len(schemaStorage) == 0 {
return cluster.NewResponseErrorf(codes2.NotFound, "schema for moduleId %s not found", config.Id)
}
dataForValidate := cs.CompileConfig(config.Data, state, config.CommonConfigs...)
if _, err := cs.validateSchema(schemaStorage[0], dataForValidate); err != nil {
switch err := err.(type) {
case validationSchemaError:
return cluster.NewResponse(
domain.CreateUpdateConfigInvalidResponse{
Config: nil,
Valid: false,
Details: err.Description,
})
default:
return cluster.NewResponseErrorf(codes2.Internal, "%v", err)
}
}
}
if upsertConfig.Create {
config = state.WritableConfigs().Create(config)
} else {
@@ -111,7 +149,11 @@ func (cs configService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCon
}
}
cs.BroadcastNewConfig(state, config)
return cluster.NewResponse(config)
return cluster.NewResponse(domain.CreateUpdateConfigInvalidResponse{
Config: &config,
Valid: true,
Details: nil,
})
}
func (cs configService) BroadcastNewConfig(state state.ReadonlyState, configs ...entity.Config) {
@@ -142,6 +184,24 @@ func (cs configService) broadcast(room, event, data string) {
}
}
func (configService) validateSchema(schema entity.ConfigSchema, data map[string]interface{}) (bool, error) {
schemaLoader := gojsonschema.NewGoLoader(schema.Schema)
documentLoader := gojsonschema.NewGoLoader(data)
if result, err := gojsonschema.Validate(schemaLoader, documentLoader); err != nil {
return false, err
} else if len(result.Errors()) > 0 {
desc := make(map[string]string)
for _, value := range result.Errors() {
desc[value.Field()] = value.Description()
}
return false, validationSchemaError{Description: desc}
} else if result.Valid() {
return true, nil
} else {
return false, nil
}
}
func mergeNestedMaps(maps ...map[string]interface{}) map[string]interface{} {
if len(maps) == 1 {
return maps[0]
+37
View File
@@ -0,0 +1,37 @@
package service
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"isp-config-service/entity"
"testing"
)
func TestConfigService_validateSchema(t *testing.T) {
var (
a = assert.New(t)
schemaDesc = []byte(`{"schema":{"properties":{"configuration":{"required":["address","token"],"properties":{"address":{"type":"string","title":"Адрес"},"token":{"type":"string","title":"Токен"}},"additionalProperties":false,"type":"object"}},"additionalProperties":false,"type":"object","title":"Remote config"},"title":"module_name"}`)
validData = map[string]interface{}{"configuration": map[string]interface{}{"address": "127.0.0.1", "token": "token"}}
invalidValue = map[string]interface{}{"configuration": map[string]interface{}{"address": 127001, "token": "token"}}
invalidName = map[string]interface{}{"config": map[string]interface{}{"test": "127.0.0.1", "token": "token"}}
schema entity.ConfigSchema
valid bool
err error
)
a.NoError(json.Unmarshal(schemaDesc, &schema))
valid, err = ConfigService.validateSchema(schema, validData)
a.NoError(err)
a.True(valid)
valid, err = ConfigService.validateSchema(schema, invalidValue)
a.Error(err)
a.False(valid)
valid, err = ConfigService.validateSchema(schema, invalidName)
a.Error(err)
a.False(valid)
}
+11 -1
View File
@@ -126,7 +126,11 @@ func (moduleRegistryService) GetAggregatedModuleInfo(state state.ReadonlyState)
configs := state.Configs().GetByModuleIds(idList)
for _, cfg := range configs {
info := resMap[cfg.ModuleId]
info.Configs = append(info.Configs, cfg)
info.Configs = append(info.Configs, domain.ConfigModuleInfo{
Config: cfg,
Valid: false,
})
resMap[cfg.ModuleId] = info
}
schemas := state.Schemas().GetByModuleIds(idList)
@@ -134,6 +138,12 @@ func (moduleRegistryService) GetAggregatedModuleInfo(state state.ReadonlyState)
info := resMap[s.ModuleId]
schema := s.Schema
info.ConfigSchema = &schema
for i, config := range info.Configs {
dataForValidate := ConfigService.CompileConfig(config.Data, state, config.CommonConfigs...)
info.Configs[i].Valid, _ = ConfigService.validateSchema(s, dataForValidate)
}
resMap[s.ModuleId] = info
}
+1
View File
@@ -126,6 +126,7 @@ func (h *socketEventHandler) handleConfigSchema(conn ws.Conn, data []byte) strin
upsertConfig := cluster.UpsertConfig{
Config: config,
Create: true,
Unsafe: true,
}
command := cluster.PrepareUpsertConfigCommand(upsertConfig)