Skip to content

Commit

Permalink
fix: Prevent empty byte slices
Browse files Browse the repository at this point in the history
  • Loading branch information
jachym-tousek-keboola committed Dec 18, 2024
1 parent eca7757 commit 194e200
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/pkg/service/stream/encryption/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt"

"github.com/keboola/keboola-as-code/internal/pkg/log"
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
)

// LoggedEncryptor wraps another Encryptor and adds logging.
Expand All @@ -22,6 +23,12 @@ func NewLoggedEncryptor(ctx context.Context, encryptor cloudencrypt.Encryptor, l
}

func (encryptor *LoggedEncryptor) Encrypt(ctx context.Context, plaintext []byte, metadata cloudencrypt.Metadata) ([]byte, error) {
if len(plaintext) == 0 {
err := errors.New("text should not be empty")
encryptor.logger.Infof(ctx, "encryption error: %s", err.Error())
return nil, err
}

encryptedValue, err := encryptor.encryptor.Encrypt(ctx, plaintext, metadata)
if err != nil {
encryptor.logger.Infof(ctx, "encryption error: %s", err.Error())
Expand All @@ -34,6 +41,12 @@ func (encryptor *LoggedEncryptor) Encrypt(ctx context.Context, plaintext []byte,
}

func (encryptor *LoggedEncryptor) Decrypt(ctx context.Context, ciphertext []byte, metadata cloudencrypt.Metadata) ([]byte, error) {
if len(ciphertext) == 0 {
err := errors.New("text should not be empty")
encryptor.logger.Infof(ctx, "decryption error: %s", err.Error())
return nil, err
}

plaintext, err := encryptor.encryptor.Decrypt(ctx, ciphertext, metadata)
if err != nil {
encryptor.logger.Infof(ctx, "decryption error: %s", err.Error())
Expand Down

0 comments on commit 194e200

Please sign in to comment.