Skip to content

Commit

Permalink
refactor: Move code to internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
jachym-tousek-keboola committed Nov 19, 2024
1 parent 407f88f commit 5ee40ab
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 38 deletions.
5 changes: 3 additions & 2 deletions pkg/cloudencrypt/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/dgraph-io/ristretto/v2"
"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/encode"
)

// CachedEncryptor wraps another Encryptor and adds a caching mechanism.
Expand All @@ -23,7 +24,7 @@ func NewCachedEncryptor(ctx context.Context, encryptor Encryptor, ttl time.Durat
}

func (encryptor *CachedEncryptor) Encrypt(ctx context.Context, plaintext []byte, metadata Metadata) ([]byte, error) {
key, err := encode(metadata)
key, err := encode.Encode(metadata)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +42,7 @@ func (encryptor *CachedEncryptor) Encrypt(ctx context.Context, plaintext []byte,
}

func (encryptor *CachedEncryptor) Decrypt(ctx context.Context, ciphertext []byte, metadata Metadata) ([]byte, error) {
key, err := encode(metadata)
key, err := encode.Encode(metadata)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloudencrypt/cached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/dgraph-io/ristretto/v2"
"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/random"
"github.com/keboola/go-utils/pkg/wildcards"
"github.com/stretchr/testify/assert"
)
Expand All @@ -17,7 +18,7 @@ func TestCachedEncryptor(t *testing.T) {

ctx := context.Background()

secretKey, err := generateSecretKey()
secretKey, err := random.SecretKey()
assert.NoError(t, err)

nativeEncryptor, err := NewNativeEncryptor(secretKey)
Expand Down
13 changes: 6 additions & 7 deletions pkg/cloudencrypt/dual.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cloudencrypt

import (
"context"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/encode"
"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/random"
)

const (
Expand All @@ -23,7 +26,7 @@ func NewDualEncryptor(ctx context.Context, encryptor Encryptor) (*DualEncryptor,

func (encryptor *DualEncryptor) Encrypt(ctx context.Context, plaintext []byte, metadata Metadata) ([]byte, error) {
// Generate a random secret key
secretKey, err := generateSecretKey()
secretKey, err := random.SecretKey()
if err != nil {
return nil, err
}
Expand All @@ -43,7 +46,7 @@ func (encryptor *DualEncryptor) Encrypt(ctx context.Context, plaintext []byte, m
output[mapKeySecretKey] = encryptedSecretKey
output[mapKeyCipherText] = ciphertext

encoded, err := encode(output)
encoded, err := encode.Encode(output)
if err != nil {
return nil, err
}
Expand All @@ -52,7 +55,7 @@ func (encryptor *DualEncryptor) Encrypt(ctx context.Context, plaintext []byte, m
}

func (encryptor *DualEncryptor) Decrypt(ctx context.Context, ciphertext []byte, metadata Metadata) ([]byte, error) {
decoded, err := decode[map[string][]byte](ciphertext)
decoded, err := encode.Decode[map[string][]byte](ciphertext)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,7 +111,3 @@ func nativeDecrypt(ctx context.Context, secretKey []byte, ciphertext []byte, met

return plaintext, nil
}

func generateSecretKey() ([]byte, error) {
return randomBytes(32)
}
3 changes: 2 additions & 1 deletion pkg/cloudencrypt/dual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/random"
"github.com/stretchr/testify/assert"
)

Expand All @@ -12,7 +13,7 @@ func TestDualEncryptor(t *testing.T) {

ctx := context.Background()

secretKey, err := generateSecretKey()
secretKey, err := random.SecretKey()
assert.NoError(t, err)

nativeEncryptor, err := NewNativeEncryptor(secretKey)
Expand Down
13 changes: 0 additions & 13 deletions pkg/cloudencrypt/encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package cloudencrypt

import (
"context"
"crypto/rand"

"github.com/pkg/errors"
)

type Metadata map[string]string
Expand All @@ -14,13 +11,3 @@ type Encryptor interface {
Decrypt(ctx context.Context, ciphertext []byte, metadata Metadata) ([]byte, error)
Close() error
}

func randomBytes(size int) ([]byte, error) {
bytes := make([]byte, size)
_, err := rand.Read(bytes)
if err != nil {
return nil, errors.Wrapf(err, "can't generate random bytes: %s", err.Error())
}

return bytes, err
}
5 changes: 3 additions & 2 deletions pkg/cloudencrypt/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

kms "cloud.google.com/go/kms/apiv1"
"cloud.google.com/go/kms/apiv1/kmspb"
"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/encode"
"github.com/pkg/errors"
)

Expand All @@ -27,7 +28,7 @@ func NewGCPEncryptor(ctx context.Context, keyID string) (*GCPEncryptor, error) {
}

func (encryptor *GCPEncryptor) Encrypt(ctx context.Context, plaintext []byte, metadata Metadata) ([]byte, error) {
additionalData, err := encode(metadata)
additionalData, err := encode.Encode(metadata)
if err != nil {
return nil, err
}
Expand All @@ -47,7 +48,7 @@ func (encryptor *GCPEncryptor) Encrypt(ctx context.Context, plaintext []byte, me
}

func (encryptor *GCPEncryptor) Decrypt(ctx context.Context, ciphertext []byte, metadata Metadata) ([]byte, error) {
additionalData, err := encode(metadata)
additionalData, err := encode.Encode(metadata)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cloudencrypt
package encode

import (
"bytes"
Expand All @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
)

func encode(data any) ([]byte, error) {
func Encode(data any) ([]byte, error) {
var buffer bytes.Buffer

// Base64 encode
Expand Down Expand Up @@ -37,7 +37,7 @@ func encode(data any) ([]byte, error) {
return buffer.Bytes(), nil
}

func decode[T any](data []byte) (decoded T, err error) {
func Decode[T any](data []byte) (decoded T, err error) {
// Base64 decode
decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(data))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package cloudencrypt
package encode

import (
"testing"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/random"
"github.com/stretchr/testify/assert"
)

func TestEncodeDecode(t *testing.T) {
t.Parallel()

secretKey, err := generateSecretKey()
secretKey, err := random.SecretKey()
assert.NoError(t, err)

data := make(map[string][]byte)
data["test"] = secretKey

encoded, err := encode(data)
encoded, err := Encode(data)
assert.NoError(t, err)
assert.NotNil(t, encoded)

decoded, err := decode[map[string][]byte](encoded)
decoded, err := Decode[map[string][]byte](encoded)
assert.NoError(t, err)
assert.NotNil(t, decoded)

Expand Down
21 changes: 21 additions & 0 deletions pkg/cloudencrypt/internal/random/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package random

import (
"crypto/rand"

"github.com/pkg/errors"
)

func Bytes(size int) ([]byte, error) {
bytes := make([]byte, size)
_, err := rand.Read(bytes)
if err != nil {
return nil, errors.Wrapf(err, "can't generate random bytes: %s", err.Error())
}

return bytes, err
}

func SecretKey() ([]byte, error) {
return Bytes(32)
}
3 changes: 2 additions & 1 deletion pkg/cloudencrypt/logged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"testing"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/random"
"github.com/keboola/go-utils/pkg/wildcards"
"github.com/stretchr/testify/assert"
)
Expand All @@ -15,7 +16,7 @@ func TestLoggedEncryptor(t *testing.T) {

ctx := context.Background()

secretKey, err := generateSecretKey()
secretKey, err := random.SecretKey()
assert.NoError(t, err)

nativeEncryptor, err := NewNativeEncryptor(secretKey)
Expand Down
8 changes: 5 additions & 3 deletions pkg/cloudencrypt/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/aes"
"crypto/cipher"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/encode"
"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/random"
"github.com/pkg/errors"
)

Expand All @@ -30,12 +32,12 @@ func NewNativeEncryptor(secretKey []byte) (*NativeEncryptor, error) {
}

func (encryptor *NativeEncryptor) Encrypt(ctx context.Context, plaintext []byte, metadata Metadata) ([]byte, error) {
additionalData, err := encode(metadata)
additionalData, err := encode.Encode(metadata)
if err != nil {
return nil, err
}

nonce, err := randomBytes(encryptor.gcm.NonceSize())
nonce, err := random.Bytes(encryptor.gcm.NonceSize())
if err != nil {
return nil, err
}
Expand All @@ -45,7 +47,7 @@ func (encryptor *NativeEncryptor) Encrypt(ctx context.Context, plaintext []byte,
}

func (encryptor *NativeEncryptor) Decrypt(ctx context.Context, ciphertext []byte, metadata Metadata) ([]byte, error) {
additionalData, err := encode(metadata)
additionalData, err := encode.Encode(metadata)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloudencrypt/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt/internal/random"
"github.com/stretchr/testify/assert"
)

Expand All @@ -12,7 +13,7 @@ func TestNativeEncryptor(t *testing.T) {

ctx := context.Background()

secretKey, err := generateSecretKey()
secretKey, err := random.SecretKey()
assert.NoError(t, err)

encryptor, err := NewNativeEncryptor(secretKey)
Expand Down

0 comments on commit 5ee40ab

Please sign in to comment.