-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jiyong Huang <[email protected]>
- Loading branch information
Showing
6 changed files
with
205 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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 node | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lf-edge/ekuiper/contract/v2/api" | ||
"github.com/lf-edge/ekuiper/v2/internal/encryptor" | ||
"github.com/lf-edge/ekuiper/v2/internal/pkg/def" | ||
"github.com/lf-edge/ekuiper/v2/pkg/infra" | ||
"github.com/lf-edge/ekuiper/v2/pkg/message" | ||
) | ||
|
||
type EncryptNode struct { | ||
*defaultSinkNode | ||
tool message.Encryptor | ||
} | ||
|
||
func NewEncryptOp(name string, rOpt *def.RuleOption, encryptMethod string) (*EncryptNode, error) { | ||
dc, err := encryptor.GetEncryptor(encryptMethod) | ||
if err != nil { | ||
return nil, fmt.Errorf("get encryptor %s fail with error: %v", encryptMethod, err) | ||
} | ||
return &EncryptNode{ | ||
defaultSinkNode: newDefaultSinkNode(name, rOpt), | ||
tool: dc, | ||
}, nil | ||
} | ||
|
||
func (o *EncryptNode) Exec(ctx api.StreamContext, errCh chan<- error) { | ||
o.prepareExec(ctx, errCh, "op") | ||
go func() { | ||
err := infra.SafeRun(func() error { | ||
runWithOrder(ctx, o.defaultSinkNode, o.concurrency, o.Worker) | ||
return nil | ||
}) | ||
if err != nil { | ||
infra.DrainError(ctx, err, errCh) | ||
} | ||
}() | ||
} | ||
|
||
func (o *EncryptNode) Worker(_ api.Logger, item any) []any { | ||
o.statManager.ProcessTimeStart() | ||
defer o.statManager.ProcessTimeEnd() | ||
switch d := item.(type) { | ||
case []byte: | ||
r := o.tool.Encrypt(d) | ||
return []any{r} | ||
default: | ||
return []any{fmt.Errorf("unsupported data received: %v", d)} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 node | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/lf-edge/ekuiper/v2/internal/conf" | ||
"github.com/lf-edge/ekuiper/v2/internal/pkg/def" | ||
mockContext "github.com/lf-edge/ekuiper/v2/pkg/mock/context" | ||
) | ||
|
||
func TestNewEncryptOp(t *testing.T) { | ||
_, err := NewEncryptOp("test", &def.RuleOption{}, "non") | ||
assert.Error(t, err) | ||
assert.Equal(t, "get encryptor non fail with error: unsupported encryptor: non", err.Error()) | ||
_, err = NewEncryptOp("test", &def.RuleOption{}, "aes") | ||
assert.Error(t, err) | ||
assert.Equal(t, errors.New("get encryptor aes fail with error: AES key is not defined"), err) | ||
} | ||
|
||
func TestEncryptOp_Exec(t *testing.T) { | ||
conf.InitConf() | ||
op, err := NewEncryptOp("test", &def.RuleOption{BufferLength: 10, SendError: true}, "aes") | ||
assert.NoError(t, err) | ||
op.tool = &MockEncryptor{} | ||
out := make(chan any, 100) | ||
err = op.AddOutput(out, "test") | ||
assert.NoError(t, err) | ||
ctx := mockContext.NewMockContext("test1", "compress_test") | ||
errCh := make(chan error) | ||
op.Exec(ctx, errCh) | ||
|
||
cases := []any{ | ||
[]byte("{\"a\":1,\"b\":2}"), | ||
errors.New("go through error"), | ||
"invalid", | ||
} | ||
expects := [][]any{ | ||
{[]byte("mock encrypt")}, | ||
{errors.New("go through error")}, | ||
{errors.New("unsupported data received: invalid")}, | ||
} | ||
|
||
for i, c := range cases { | ||
op.input <- c | ||
for _, e := range expects[i] { | ||
r := <-out | ||
switch tr := r.(type) { | ||
case error: | ||
assert.EqualError(t, e.(error), tr.Error()) | ||
default: | ||
assert.Equal(t, e, r) | ||
} | ||
} | ||
} | ||
} | ||
|
||
type MockEncryptor struct{} | ||
|
||
func (m *MockEncryptor) Encrypt(_ []byte) []byte { | ||
return []byte("mock encrypt") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters