-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37438f7
commit eca7757
Showing
3 changed files
with
59 additions
and
2 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
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,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() | ||
} |