Skip to content

Commit

Permalink
feat: support validate service name (#2775)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <[email protected]>
  • Loading branch information
Yisaer committed Apr 18, 2024
1 parent 1fab120 commit 06caa8a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
16 changes: 2 additions & 14 deletions internal/processor/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/lf-edge/ekuiper/internal/conf"
"github.com/lf-edge/ekuiper/internal/meta"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/lf-edge/ekuiper/pkg/cast"
"github.com/lf-edge/ekuiper/pkg/errorx"
"github.com/lf-edge/ekuiper/pkg/kv"
"github.com/lf-edge/ekuiper/pkg/validate"
)

type RuleProcessor struct {
Expand Down Expand Up @@ -199,20 +199,8 @@ func (p *RuleProcessor) GetRuleByJson(id, ruleJson string) (*api.Rule, error) {
return rule, nil
}

var invalidRuleChars = []string{
"/", "#", "%",
}

func validateRuleID(id string) error {
if id != strings.TrimSpace(id) {
return fmt.Errorf("ruleID: %v should be trimed", id)
}
for _, invalidChar := range invalidRuleChars {
if strings.Contains(id, invalidChar) {
return fmt.Errorf("ruleID:%s contains invalidChar:%v", id, invalidChar)
}
}
return nil
return validate.ValidateID(id)
}

func clone(opt api.RuleOption) *api.RuleOption {
Expand Down
4 changes: 4 additions & 0 deletions internal/service/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/lf-edge/ekuiper/pkg/api"
"github.com/lf-edge/ekuiper/pkg/cast"
"github.com/lf-edge/ekuiper/pkg/kv"
"github.com/lf-edge/ekuiper/pkg/validate"
)

var (
Expand Down Expand Up @@ -359,6 +360,9 @@ func (m *Manager) List() ([]string, error) {

func (m *Manager) Create(r *ServiceCreationRequest) error {
name, uri := r.Name, r.File
if err := validate.ValidateID(r.Name); err != nil {
return err
}
if ok, _ := m.serviceKV.Get(name, &serviceInfo{}); ok {
return fmt.Errorf("service %s exist", name)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/service/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/internal/binder"
"github.com/lf-edge/ekuiper/internal/binder/function"
Expand Down Expand Up @@ -450,6 +451,12 @@ func TestManage(t *testing.T) {
t.Errorf("Create dynamic service failed: %v", err)
return
}
err = m.Create(&ServiceCreationRequest{
Name: "@#$2323",
File: url.String(),
})
require.Error(t, err)

dService, err := m.Get("dynamic")
if err != nil {
t.Errorf("Get dynamic service error: %v", err)
Expand Down
36 changes: 36 additions & 0 deletions pkg/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validate

import (
"fmt"
"strings"
)

var invalidRuleChars = []string{
"/", "#", "%",
}

func ValidateID(id string) error {
if id != strings.TrimSpace(id) {
return fmt.Errorf("ruleID: %v should be trimed", id)
}
for _, invalidChar := range invalidRuleChars {
if strings.Contains(id, invalidChar) {
return fmt.Errorf("ruleID:%s contains invalidChar:%v", id, invalidChar)
}
}
return nil
}
51 changes: 51 additions & 0 deletions pkg/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package validate

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestValidateRuleID(t *testing.T) {
testcases := []struct {
id string
err error
}{
{
"abc",
nil,
},
{
"ABC",
nil,
},
{
"123",
nil,
},
{
"1/2",
fmt.Errorf("ruleID:%s contains invalidChar:%v", "1/2", "/"),
},
{
"1#2",
fmt.Errorf("ruleID:%s contains invalidChar:%v", "1#2", "#"),
},
{
"1%2",
fmt.Errorf("ruleID:%s contains invalidChar:%v", "1%2", "%"),
},
{
id: "\t123",
err: fmt.Errorf("ruleID: %v should be trimed", "\t123"),
},
{
id: "123\t",
err: fmt.Errorf("ruleID: %v should be trimed", "123\t"),
},
}
for _, tc := range testcases {
require.Equal(t, tc.err, ValidateID(tc.id))
}
}

0 comments on commit 06caa8a

Please sign in to comment.