Skip to content

Commit

Permalink
feat: Log cloud encryption attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
jachym-tousek-keboola committed Dec 18, 2024
1 parent 37438f7 commit eca7757
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/service/common/dependencies/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newEncryptionScope(ctx context.Context, cfg encryption.Config, d encryption
ctx, span := d.Telemetry().Tracer().Start(ctx, "keboola.go.common.dependencies.NewEncryptionScope")
defer span.End(&err)

encryptor, err := encryption.NewEncryptor(ctx, cfg)
encryptor, err := encryption.NewEncryptor(ctx, cfg, d.Logger())
if err != nil {
return nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/pkg/service/stream/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt"

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

const (
Expand All @@ -16,7 +18,7 @@ const (

type Provider string

func NewEncryptor(ctx context.Context, config Config) (cloudencrypt.Encryptor, error) {
func NewEncryptor(ctx context.Context, config Config, logger log.Logger) (cloudencrypt.Encryptor, error) {
var encryptor cloudencrypt.Encryptor
var err error

Expand Down Expand Up @@ -47,6 +49,11 @@ func NewEncryptor(ctx context.Context, config Config) (cloudencrypt.Encryptor, e
}
}

encryptor, err = NewLoggedEncryptor(ctx, encryptor, logger)
if err != nil {
return nil, err
}

encryptor, err = cloudencrypt.NewDualEncryptor(ctx, encryptor)
if err != nil {
return nil, err
Expand Down
50 changes: 50 additions & 0 deletions internal/pkg/service/stream/encryption/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package encryption

import (
"context"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt"

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

// LoggedEncryptor wraps another Encryptor and adds logging.
type LoggedEncryptor struct {
encryptor cloudencrypt.Encryptor
logger log.Logger
}

func NewLoggedEncryptor(ctx context.Context, encryptor cloudencrypt.Encryptor, logger log.Logger) (*LoggedEncryptor, error) {
return &LoggedEncryptor{
encryptor: encryptor,
logger: logger,
}, nil
}

func (encryptor *LoggedEncryptor) Encrypt(ctx context.Context, plaintext []byte, metadata cloudencrypt.Metadata) ([]byte, error) {
encryptedValue, err := encryptor.encryptor.Encrypt(ctx, plaintext, metadata)
if err != nil {
encryptor.logger.Infof(ctx, "encryption error: %s", err.Error())
return nil, err
}

encryptor.logger.Info(ctx, "encryption success")

return encryptedValue, nil
}

func (encryptor *LoggedEncryptor) Decrypt(ctx context.Context, ciphertext []byte, metadata cloudencrypt.Metadata) ([]byte, error) {
plaintext, err := encryptor.encryptor.Decrypt(ctx, ciphertext, metadata)
if err != nil {
encryptor.logger.Infof(ctx, "decryption error: %s", err.Error())
return nil, err
}

encryptor.logger.Info(ctx, "decryption success")

return plaintext, nil
}

func (encryptor *LoggedEncryptor) Close() error {
return encryptor.encryptor.Close()
}

0 comments on commit eca7757

Please sign in to comment.