diff --git a/etc/kuiper.yaml b/etc/kuiper.yaml index 140d208b7e..a0a057b32c 100644 --- a/etc/kuiper.yaml +++ b/etc/kuiper.yaml @@ -61,6 +61,8 @@ basic: cfgStorageType: file # enableOpenZiti indicates whether to enable OpenZiti for eKuiper REST service. Currently, it is only supported to work with EdgeX secure mode. enableOpenZiti: false + # AES Key, base64 encoded + aesKey: MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3 # The default options for all rules. Each rule can override this setting by defining its own option rule: diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 9bbdc6795d..bfc071b650 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -15,6 +15,7 @@ package conf import ( + "encoding/base64" "errors" "fmt" "io" @@ -193,6 +194,7 @@ type KuiperConf struct { RulePatrolInterval string `yaml:"rulePatrolInterval"` CfgStorageType string `yaml:"cfgStorageType"` EnableOpenZiti bool `yaml:"enableOpenZiti"` + AesKey string `yaml:"aesKey"` } Rule def.RuleOption Sink *SinkConf @@ -218,6 +220,7 @@ type KuiperConf struct { PythonBin string `yaml:"pythonBin"` InitTimeout int `yaml:"initTimeout"` } + AesKey []byte } func SetLogLevel(level string, debug bool) { @@ -362,6 +365,14 @@ func InitConf() { } } + if Config.Basic.AesKey != "" { + key, err := base64.StdEncoding.DecodeString(Config.Basic.AesKey) + if err != nil { + Log.Fatal(err) + } + Config.AesKey = key + } + if Config.Store.Type == "redis" && Config.Store.Redis.ConnectionSelector != "" { if err := RedisStorageConSelectorApply(Config.Store.Redis.ConnectionSelector, Config); err != nil { Log.Fatal(err) diff --git a/internal/encryptor/aes/aes.go b/internal/encryptor/aes/aes.go new file mode 100644 index 0000000000..4ad19e5d41 --- /dev/null +++ b/internal/encryptor/aes/aes.go @@ -0,0 +1,57 @@ +// Copyright 2023-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 aes + +import ( + "crypto/cipher" + "fmt" + "io" +) + +type StreamEncrypter struct { + stream cipher.Stream + iv []byte +} + +func (a *StreamEncrypter) Encrypt(data []byte) []byte { + ciphertext := make([]byte, len(data)) + a.stream.XORKeyStream(ciphertext, data) + result := append(a.iv, ciphertext...) + return result +} + +func NewStreamEncrypter(key, iv []byte) (*StreamEncrypter, error) { + s, err := newAesStream(key, iv) + if err != nil { + return nil, err + } + return &StreamEncrypter{ + stream: s, + iv: iv, + }, nil +} + +func NewStreamWriter(key, iv []byte, output io.Writer) (*cipher.StreamWriter, error) { + blockMode, err := newAesStream(key, iv) + if err != nil { + return nil, err + } + writer := &cipher.StreamWriter{S: blockMode, W: output} + _, err = writer.W.Write(iv) + if err != nil { + return nil, fmt.Errorf("failed to write iv: %v", err) + } + return writer, nil +} diff --git a/internal/encryptor/aes/aes_test.go b/internal/encryptor/aes/aes_test.go new file mode 100644 index 0000000000..3756efa289 --- /dev/null +++ b/internal/encryptor/aes/aes_test.go @@ -0,0 +1,81 @@ +// Copyright 2023-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 aes + +import ( + "bytes" + "crypto/aes" + "crypto/cipher" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMessage(t *testing.T) { + key := []byte("0123456789abcdef01234567") + iv := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} + // plaintext + pt := "Using the Input type selection, choose the type of input – a text string or a file. In case of the text string input, enter your input into the Input text textarea1,2. Otherwise, use the \"Browse\" button to select the input file to upload. Then select the cryptographic function you want to use in the Function field. Depending on the selected function the Initialization vector (IV) field is shown or hidden. Initialization vector is always a sequence of bytes, each byte has to be represented in hexadecimal form." + + stream, err := NewStreamEncrypter(key, iv) + assert.NoError(t, err) + secret := stream.Encrypt([]byte(pt)) + + niv := secret[:aes.BlockSize] + assert.Equal(t, iv, niv) + secret = secret[aes.BlockSize:] + dstream, err := NewAESStreamDecrypter(key, iv) + assert.NoError(t, err) + revert := make([]byte, len(secret)) + dstream.XORKeyStream(revert, secret) + assert.Equal(t, pt, string(revert)) +} + +func TestStreamWriter(t *testing.T) { + key := []byte("0123456789abcdef01234567") + iv := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} + // plaintext + pt := "Using the Input type selection, choose the type of input – a text string or a file. In case of the text string input, enter your input into the Input text textarea1,2. Otherwise, use the \"Browse\" button to select the input file to upload. Then select the cryptographic function you want to use in the Function field. Depending on the selected function the Initialization vector (IV) field is shown or hidden. Initialization vector is always a sequence of bytes, each byte has to be represented in hexadecimal form." + + output := new(bytes.Buffer) + writer, err := NewStreamWriter(key, iv, output) + assert.NoError(t, err) + _, err = writer.Write([]byte(pt)) + assert.NoError(t, err) + err = writer.Close() + assert.NoError(t, err) + secret := output.Bytes() + + // Read the appended iv + niv := secret[:aes.BlockSize] + assert.Equal(t, iv, niv) + secret = secret[aes.BlockSize:] + // Decrypt + dstream, err := NewAESStreamDecrypter(key, iv) + assert.NoError(t, err) + revert := make([]byte, len(secret)) + dstream.XORKeyStream(revert, secret) + assert.Equal(t, pt, string(revert)) +} + +func NewAESStreamDecrypter(key, iv []byte) (cipher.Stream, error) { + // Create a new AES cipher block using the key + block, err := aes.NewCipher(key) + if err != nil { + return nil, fmt.Errorf("Error creating AES cipher block: %v", err) + } + return cipher.NewCFBDecrypter(block, iv), nil +} diff --git a/internal/encryptor/aes/cipher.go b/internal/encryptor/aes/cipher.go new file mode 100644 index 0000000000..9bc8fa8ec5 --- /dev/null +++ b/internal/encryptor/aes/cipher.go @@ -0,0 +1,30 @@ +// Copyright 2023 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 aes + +import ( + "crypto/aes" + "crypto/cipher" + "fmt" +) + +func newAesStream(key, iv []byte) (cipher.Stream, error) { + // Create a new AES cipher block using the key + block, err := aes.NewCipher(key) + if err != nil { + return nil, fmt.Errorf("Error creating AES cipher block: %v", err) + } + return cipher.NewCFBEncrypter(block, iv), nil +} diff --git a/internal/encryptor/encryptor.go b/internal/encryptor/encryptor.go new file mode 100644 index 0000000000..74a7ee70b9 --- /dev/null +++ b/internal/encryptor/encryptor.go @@ -0,0 +1,49 @@ +// Copyright 2023-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 encryptor + +import ( + "crypto/rand" + "fmt" + "io" + + "github.com/lf-edge/ekuiper/v2/internal/conf" + "github.com/lf-edge/ekuiper/v2/internal/encryptor/aes" + "github.com/lf-edge/ekuiper/v2/pkg/message" +) + +func GetEncryptor(name string) (message.Encryptor, error) { + if name == "aes" { + key, iv := getKeyIv() + return aes.NewStreamEncrypter(key, iv) + } + return nil, fmt.Errorf("unsupported encryptor: %s", name) +} + +func GetEncryptWriter(name string, output io.Writer) (io.Writer, error) { + if name == "aes" { + key, iv := getKeyIv() + return aes.NewStreamWriter(key, iv, output) + } + return nil, fmt.Errorf("unsupported encryptor: %s", name) +} + +func getKeyIv() ([]byte, []byte) { + key := conf.Config.AesKey + iv := make([]byte, 16) + // Use the crypto/rand package to generate random bytes + _, _ = rand.Read(iv) + return key, iv +} diff --git a/internal/encryptor/encryptor_test.go b/internal/encryptor/encryptor_test.go new file mode 100644 index 0000000000..07b28d3586 --- /dev/null +++ b/internal/encryptor/encryptor_test.go @@ -0,0 +1,31 @@ +// Copyright 2023-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 encryptor + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/lf-edge/ekuiper/v2/internal/conf" +) + +func TestGetEncryptor(t *testing.T) { + conf.InitConf() + _, err := GetEncryptor("aes") + assert.NoError(t, err) + _, err = GetEncryptor("unknown") + assert.Error(t, err) +} diff --git a/pkg/message/artifacts.go b/pkg/message/artifacts.go index 037417c26f..3708a42cd5 100644 --- a/pkg/message/artifacts.go +++ b/pkg/message/artifacts.go @@ -60,3 +60,8 @@ type Compressor interface { type Decompressor interface { Decompress([]byte) ([]byte, error) } + +// Encryptor encrypts bytes +type Encryptor interface { + Encrypt([]byte) []byte +}