Skip to content

Commit

Permalink
Merge 83d60e7 into a5b339d
Browse files Browse the repository at this point in the history
  • Loading branch information
humingcheng committed Apr 14, 2020
2 parents a5b339d + 83d60e7 commit f97a572
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 43 deletions.
7 changes: 7 additions & 0 deletions examples/dev/conf/chassis.yaml
Expand Up @@ -11,6 +11,13 @@ cse:
chain:
Provider:
default: ratelimiter-provider,monitoring,auth-handler,track-handler
transport:
maxBodyBytes:
rest: 2621440 #2.5M
maxHeaderBytes:
rest: 32768 #32K
timeout:
rest: 60s
servicecomb:
service:
quota:
Expand Down
20 changes: 14 additions & 6 deletions pkg/model/db_schema.go
Expand Up @@ -31,20 +31,19 @@ type LabelDoc struct {
type KVDoc struct {
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
LabelFormat string `json:"label_format,omitempty" bson:"label_format,omitempty" yaml:"label_format,omitempty"`
Key string `json:"key" yaml:"key" validate:"commonName"`
Value string `json:"value" yaml:"value" validate:"ascii,min=1,max=2097152"`
Key string `json:"key" yaml:"key" validate:"key"`
Value string `json:"value" yaml:"value" validate:"value"`
ValueType string `json:"value_type,omitempty" bson:"value_type,omitempty" yaml:"value_type,omitempty" validate:"valueType"` //ini,json,text,yaml,properties
Checker string `json:"check,omitempty" yaml:"check,omitempty"` //python script
Checker string `json:"check,omitempty" yaml:"check,omitempty" validate:"check"` //python script
CreateRevision int64 `json:"create_revision,omitempty" bson:"create_revision," yaml:"create_revision,omitempty"`
UpdateRevision int64 `json:"update_revision,omitempty" bson:"update_revision," yaml:"update_revision,omitempty"`
Project string `json:"project,omitempty" yaml:"project,omitempty" validate:"commonName"`
Status string `json:"status,omitempty" yaml:"status,omitempty" validate:"kvStatus"`
CreateTime int64 `json:"create_time,omitempty" bson:"create_time," yaml:"create_time,omitempty"`
UpdateTime int64 `json:"update_time,omitempty" bson:"update_time," yaml:"update_time,omitempty"`

Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" validate:"max=64,dive,keys,commonName,endkeys,commonName"` //redundant
Domain string `json:"domain,omitempty" yaml:"domain,omitempty" validate:"commonName"` //redundant

Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" validate:"max=8,dive,keys,lableKV,endkeys,lableKV"` //redundant
Domain string `json:"domain,omitempty" yaml:"domain,omitempty" validate:"commonName"` //redundant
}

//ViewDoc is db struct, it saves user's custom view name and criteria
Expand All @@ -69,3 +68,12 @@ type PollingDetail struct {
ResponseHeader map[string][]string `json:"response_header,omitempty" bson:"response_header," yaml:"response_header,omitempty"`
ResponseCode int `json:"response_code,omitempty" bson:"response_code," yaml:"response_code,omitempty"`
}

// UpdateKVRequest is db struct, it contains kv update request params
type UpdateKVRequest struct {
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string" validate:"uuid"`
Value string `json:"value,omitempty" yaml:"value,omitempty" validate:"value"`
Project string `json:"project,omitempty" yaml:"project,omitempty" validate:"commonName"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty" validate:"commonName"` //redundant
Status string `json:"status,omitempty" yaml:"status,omitempty" validate:"kvStatus"`
}
14 changes: 12 additions & 2 deletions pkg/validate/instance.go
Expand Up @@ -2,17 +2,27 @@ package validate

var defaultValidator = NewValidator()

const (
key = "key"
commonNameRegexString = `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$`
asciiRegexString = `^[\x00-\x7F]*$`
)

// custom validate rules
// please use different tag names from third party tags
var customRules = []*RegexValidateRule{
NewRule("commonName", `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$`, &Option{Min: 1, Max: 256}),
NewRule(key, commonNameRegexString, &Option{Min: 1, Max: 2048}), //2K
NewRule("commonName", commonNameRegexString, &Option{Min: 1, Max: 256}),
NewRule("valueType", `^(ini|json|text|yaml|properties){0,1}$`, nil),
NewRule("kvStatus", `^(enabled|disabled){0,1}$`, nil),
NewRule("value", asciiRegexString, &Option{Max: 2097152}), //ASCII, 2M
NewRule("lableKV", commonNameRegexString, &Option{Min: 1, Max: 32}),
NewRule("check", asciiRegexString, &Option{Max: 1048576}), //ASCII, 1M
}

// tags of third party validate rules we used, for error translation
var thirdPartyTags = []string{
"min", "max", "length",
"min", "max", "length", "uuid",
}

// Init initializes validate
Expand Down
13 changes: 8 additions & 5 deletions pkg/validate/instance_test.go
Expand Up @@ -10,8 +10,11 @@ import (
)

func TestValidate(t *testing.T) {
s := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" //64
testStr := s + s + s + s //256
string32 := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" //32
string2k := "a"
for i := 0; i < 11; i++ { //2k
string2k = string2k + string2k
}
err := validate.Init()
assert.NoError(t, err)

Expand All @@ -34,7 +37,7 @@ func TestValidate(t *testing.T) {
assert.Error(t, validate.Validate(kvDoc))

kvDoc = &model.KVDoc{Project: "a", Domain: "a",
Key: testStr + "a",
Key: string2k + "a",
Value: "a",
}
assert.Error(t, validate.Validate(kvDoc))
Expand All @@ -43,7 +46,7 @@ func TestValidate(t *testing.T) {
Key: "a",
Value: "",
}
assert.Error(t, validate.Validate(kvDoc))
assert.NoError(t, validate.Validate(kvDoc))

kvDoc = &model.KVDoc{Project: "a", Domain: "a",
Key: "a",
Expand Down Expand Up @@ -104,7 +107,7 @@ func TestValidate(t *testing.T) {
kvDoc = &model.KVDoc{Project: "a", Domain: "a",
Key: "a",
Value: "a",
Labels: map[string]string{testStr + "a": "a"},
Labels: map[string]string{string32 + "a": "a"},
}
assert.Error(t, validate.Validate(kvDoc))
}
9 changes: 0 additions & 9 deletions server/resource/v1/common.go
Expand Up @@ -211,15 +211,6 @@ func checkPagination(offsetStr, limitStr string) (int64, int64, error) {
return offset, limit, err
}

func validatePut(kv *model.KVDoc) error {
err := validateGet(kv.Domain, kv.Project, kv.ID)
if err != nil {
return err
}
_, err = checkStatus(kv.Status)
return err
}

func validateGet(domain, project, kvID string) error {
if kvID == "" {
return session.ErrIDIsNil
Expand Down
11 changes: 8 additions & 3 deletions server/resource/v1/history_resource_test.go
Expand Up @@ -65,9 +65,14 @@ func TestHistoryResource_GetRevisions(t *testing.T) {
assert.GreaterOrEqual(t, before, 1)

t.Run("put again, should has 2 revision", func(t *testing.T) {
kv.Domain = "default"
kv.Project = "history_test"
kv, err = service.KVService.Update(context.Background(), kv)
updateKv := &model.UpdateKVRequest{
ID: kv.ID,
Value: kv.Value,
Domain: "default",
Project: "history_test",
Status: kv.Status,
}
kv, err = service.KVService.Update(context.Background(), updateKv)
assert.NoError(t, err)
path := fmt.Sprintf("/v1/history_test/kie/revision/%s", kv.ID)
r, _ := http.NewRequest("GET", path, nil)
Expand Down
14 changes: 7 additions & 7 deletions server/resource/v1/kv_resource.go
Expand Up @@ -102,21 +102,21 @@ func (r *KVResource) Put(rctx *restful.Context) {
var err error
kvID := rctx.ReadPathParameter(common.PathParamKVID)
project := rctx.ReadPathParameter(common.PathParameterProject)
kv := new(model.KVDoc)
if err = readRequest(rctx, kv); err != nil {
kvReq := new(model.UpdateKVRequest)
if err = readRequest(rctx, kvReq); err != nil {
WriteErrResponse(rctx, http.StatusBadRequest, fmt.Sprintf(FmtReadRequestError, err))
return
}
domain := ReadDomain(rctx)
kv.ID = kvID
kv.Domain = domain.(string)
kv.Project = project
err = validatePut(kv)
kvReq.ID = kvID
kvReq.Domain = domain.(string)
kvReq.Project = project
err = validate.Validate(kvReq)
if err != nil {
WriteErrResponse(rctx, http.StatusBadRequest, err.Error())
return
}
kv, err = service.KVService.Update(rctx.Ctx, kv)
kv, err := service.KVService.Update(rctx.Ctx, kvReq)
if err != nil {
openlogging.Error(fmt.Sprintf("put [%s] err:%s", kvID, err.Error()))
WriteErrResponse(rctx, http.StatusInternalServerError, "update kv failed")
Expand Down
2 changes: 1 addition & 1 deletion server/resource/v1/kv_resource_test.go
Expand Up @@ -415,7 +415,7 @@ func TestKVResource_PutAndGet(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, kv.Value, result.Value)
})
kvUpdate := &model.KVDoc{
kvUpdate := &model.UpdateKVRequest{
Value: "admin",
}
t.Run("update the kv, set the value of user to admin", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion server/service/mongo/kv/kv_service.go
Expand Up @@ -71,7 +71,7 @@ func (s *Service) Create(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, er
}

//Update will update a key value record
func (s *Service) Update(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) {
func (s *Service) Update(ctx context.Context, kv *model.UpdateKVRequest) (*model.KVDoc, error) {
ctx, _ = context.WithTimeout(ctx, session.Timeout)
oldKV, err := s.Get(ctx, kv.Domain, kv.Project, kv.ID)
if err != nil {
Expand Down
12 changes: 4 additions & 8 deletions server/service/mongo/kv/kv_service_test.go
Expand Up @@ -95,13 +95,9 @@ func TestService_CreateOrUpdate(t *testing.T) {
Project: "kv-test",
})
assert.NoError(t, err)
afterKV, err := kvsvc.Update(context.Background(), &model.KVDoc{
ID: beforeKV.ID,
Key: "timeout",
Value: "3s",
Labels: map[string]string{
"app": "mall",
},
afterKV, err := kvsvc.Update(context.Background(), &model.UpdateKVRequest{
ID: beforeKV.ID,
Value: "3s",
Domain: "default",
Project: "kv-test",
})
Expand Down Expand Up @@ -151,7 +147,7 @@ func TestService_Create(t *testing.T) {
func TestService_Update(t *testing.T) {
kvsvc := &kv.Service{}
t.Run("update kv by kvID", func(t *testing.T) {
result, err := kvsvc.Update(context.TODO(), &model.KVDoc{
result, err := kvsvc.Update(context.TODO(), &model.UpdateKVRequest{
ID: id,
Value: "3s",
Domain: "default",
Expand Down
2 changes: 1 addition & 1 deletion server/service/service.go
Expand Up @@ -43,7 +43,7 @@ var (
type KV interface {
//below 3 methods is usually for admin console
Create(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error)
Update(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error)
Update(ctx context.Context, kv *model.UpdateKVRequest) (*model.KVDoc, error)
List(ctx context.Context, domain, project string, options ...FindOption) (*model.KVResponse, error)
//FindOneAndDelete deletes one kv by id and return the deleted kv as these appeared before deletion
FindOneAndDelete(ctx context.Context, kvID string, domain, project string) (*model.KVDoc, error)
Expand Down

0 comments on commit f97a572

Please sign in to comment.