Skip to content

Commit ea822df

Browse files
authored
Merge pull request #293 from chainifynet/feature/restruct-pkg
refactor(serialization): move serialization under internal pkg
2 parents e93d538 + b98dabc commit ea822df

31 files changed

+31
-24
lines changed

pkg/client/client.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
"github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
1818
)
1919

20+
// ErrInvalidConfig is returned when client configuration is invalid.
21+
var ErrInvalidConfig = errors.New("client config invalid")
22+
2023
// NewClient returns a new client with default [clientconfig.ClientConfig] config
2124
func NewClient() *Client {
2225
cfg, _ := clientconfig.NewConfig()
@@ -82,7 +85,7 @@ func (c *Client) clientConfig() clientconfig.ClientConfig {
8285
// respectively. If these functions are not used, default values are applied.
8386
func (c *Client) Encrypt(ctx context.Context, source []byte, ec suite.EncryptionContext, materialsManager model.CryptoMaterialsManager, optFns ...EncryptOptionFunc) ([]byte, format.MessageHeader, error) {
8487
if err := validateParams(ctx, source, materialsManager); err != nil {
85-
return nil, nil, fmt.Errorf("validation error: %w", errors.Join(crypto.ErrEncryption, err))
88+
return nil, nil, fmt.Errorf("validation error: %w", errors.Join(ErrInvalidConfig, crypto.ErrEncryption, err))
8689
}
8790
opts := EncryptOptions{
8891
Algorithm: suite.AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
@@ -91,7 +94,7 @@ func (c *Client) Encrypt(ctx context.Context, source []byte, ec suite.Encryption
9194
}
9295
for _, optFn := range optFns {
9396
if err := optFn(&opts); err != nil {
94-
return nil, nil, fmt.Errorf("invalid encrypt option: %w", errors.Join(crypto.ErrEncryption, err))
97+
return nil, nil, fmt.Errorf("invalid encrypt option: %w", errors.Join(ErrInvalidConfig, crypto.ErrEncryption, err))
9598
}
9699
}
97100
conf := crypto.EncrypterConfig{
@@ -125,15 +128,15 @@ func (c *Client) Encrypt(ctx context.Context, source []byte, ec suite.Encryption
125128
// - error: An error if decryption fails.
126129
func (c *Client) Decrypt(ctx context.Context, ciphertext []byte, materialsManager model.CryptoMaterialsManager, optFns ...DecryptOptionFunc) ([]byte, format.MessageHeader, error) {
127130
if err := validateParams(ctx, ciphertext, materialsManager); err != nil {
128-
return nil, nil, fmt.Errorf("validation error: %w", errors.Join(crypto.ErrDecryption, err))
131+
return nil, nil, fmt.Errorf("validation error: %w", errors.Join(ErrInvalidConfig, crypto.ErrDecryption, err))
129132
}
130133

131134
opts := DecryptOptions{
132135
Handler: decrypter.New,
133136
}
134137
for _, optFn := range optFns {
135138
if err := optFn(&opts); err != nil {
136-
return nil, nil, fmt.Errorf("invalid decrypt option: %w", errors.Join(crypto.ErrDecryption, err))
139+
return nil, nil, fmt.Errorf("invalid decrypt option: %w", errors.Join(ErrInvalidConfig, crypto.ErrDecryption, err))
137140
}
138141
}
139142
handler := opts.Handler(crypto.DecrypterConfig{ClientCfg: c.clientConfig()}, materialsManager)

pkg/client/client_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestClient_Decrypt(t *testing.T) {
7777
want: nil,
7878
wantErr: true,
7979
wantErrStr: "validation error",
80-
wantErrType: crypto.ErrDecryption,
80+
wantErrType: client.ErrInvalidConfig,
8181
},
8282
{
8383
name: "Nil Ciphertext",
@@ -89,7 +89,7 @@ func TestClient_Decrypt(t *testing.T) {
8989
want: nil,
9090
wantErr: true,
9191
wantErrStr: "validation error",
92-
wantErrType: crypto.ErrDecryption,
92+
wantErrType: client.ErrInvalidConfig,
9393
},
9494
{
9595
name: "Empty Ciphertext",
@@ -101,7 +101,7 @@ func TestClient_Decrypt(t *testing.T) {
101101
want: nil,
102102
wantErr: true,
103103
wantErrStr: "validation error",
104-
wantErrType: crypto.ErrDecryption,
104+
wantErrType: client.ErrInvalidConfig,
105105
},
106106
{
107107
name: "Invalid Decrypt Handler",
@@ -115,7 +115,7 @@ func TestClient_Decrypt(t *testing.T) {
115115
want: nil,
116116
wantErr: true,
117117
wantErrStr: "invalid decrypt option",
118-
wantErrType: crypto.ErrDecryption,
118+
wantErrType: client.ErrInvalidConfig,
119119
},
120120
{
121121
name: "Decrypt Error",
@@ -237,7 +237,7 @@ func TestClient_Encrypt(t *testing.T) {
237237
want: nil,
238238
wantErr: true,
239239
wantErrStr: "validation error",
240-
wantErrType: crypto.ErrEncryption,
240+
wantErrType: client.ErrInvalidConfig,
241241
},
242242
{
243243
name: "Nil Source",
@@ -252,7 +252,7 @@ func TestClient_Encrypt(t *testing.T) {
252252
want: nil,
253253
wantErr: true,
254254
wantErrStr: "validation error",
255-
wantErrType: crypto.ErrEncryption,
255+
wantErrType: client.ErrInvalidConfig,
256256
},
257257
{
258258
name: "Empty Source",
@@ -267,7 +267,7 @@ func TestClient_Encrypt(t *testing.T) {
267267
want: nil,
268268
wantErr: true,
269269
wantErrStr: "validation error",
270-
wantErrType: crypto.ErrEncryption,
270+
wantErrType: client.ErrInvalidConfig,
271271
},
272272
{
273273
name: "Invalid Encrypt Handler",
@@ -284,7 +284,7 @@ func TestClient_Encrypt(t *testing.T) {
284284
want: nil,
285285
wantErr: true,
286286
wantErrStr: "invalid encrypt option",
287-
wantErrType: crypto.ErrEncryption,
287+
wantErrType: client.ErrInvalidConfig,
288288
},
289289
{
290290
name: "Invalid Frame Length",
@@ -301,7 +301,7 @@ func TestClient_Encrypt(t *testing.T) {
301301
want: nil,
302302
wantErr: true,
303303
wantErrStr: "invalid encrypt option",
304-
wantErrType: crypto.ErrEncryption,
304+
wantErrType: client.ErrInvalidConfig,
305305
},
306306
{
307307
name: "Unsupported Algorithm",
@@ -318,7 +318,7 @@ func TestClient_Encrypt(t *testing.T) {
318318
want: nil,
319319
wantErr: true,
320320
wantErrStr: "invalid encrypt option",
321-
wantErrType: crypto.ErrEncryption,
321+
wantErrType: client.ErrInvalidConfig,
322322
},
323323
{
324324
name: "Encrypt Error",

pkg/crypto/errors.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
)
99

1010
var (
11-
ErrInvalidMessage = errors.New("invalid message format")
12-
ErrDecryption = errors.New("decryption error")
13-
ErrEncryption = errors.New("encryption error")
11+
// ErrDecryption is returned when decryption fails.
12+
ErrDecryption = errors.New("decryption error")
13+
// ErrEncryption is returned when encryption fails.
14+
ErrEncryption = errors.New("encryption error")
1415
)

pkg/internal/crypto/decrypter/decrypt.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ import (
1313
"github.com/chainifynet/aws-encryption-sdk-go/pkg/crypto"
1414
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/crypto/policy"
1515
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/crypto/signature"
16+
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/serialization"
1617
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/utils/bodyaad"
1718
"github.com/chainifynet/aws-encryption-sdk-go/pkg/model"
1819
"github.com/chainifynet/aws-encryption-sdk-go/pkg/model/format"
19-
"github.com/chainifynet/aws-encryption-sdk-go/pkg/serialization"
2020
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/encryption"
2121
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/keyderivation"
2222
)
2323

24+
// ErrInvalidMessage is returned when the message format is invalid.
25+
var ErrInvalidMessage = errors.New("invalid message format")
26+
2427
type Decrypter struct {
2528
cmm model.CryptoMaterialsManager
2629
cfg crypto.DecrypterConfig
@@ -64,7 +67,7 @@ func (d *Decrypter) decryptData(ctx context.Context, ciphertext []byte) ([]byte,
6467
// early stage check if cipher text contains needed first byte of message version
6568
// by doing this we avoid mistakes with base64 byte sequence
6669
if ciphertext[0] != firstByteEncryptedMessageV1 && ciphertext[0] != firstByteEncryptedMessageV2 {
67-
return nil, nil, fmt.Errorf("first byte does not contain message version: %w", crypto.ErrInvalidMessage)
70+
return nil, nil, fmt.Errorf("first byte does not contain message version: %w", ErrInvalidMessage)
6871
}
6972
buf := bytes.NewBuffer(b)
7073

pkg/internal/crypto/decrypter/decrypt_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func TestDecrypter_decryptData(t *testing.T) {
353353
},
354354
want: nil,
355355
wantErr: true,
356-
wantErrType: crypto.ErrInvalidMessage,
356+
wantErrType: ErrInvalidMessage,
357357
wantErrStr: "first byte does not contain message version",
358358
},
359359
{

pkg/internal/crypto/encrypter/encrypt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"github.com/chainifynet/aws-encryption-sdk-go/pkg/crypto"
1414
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/crypto/policy"
1515
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/crypto/signature"
16+
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/serialization"
1617
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/utils/bodyaad"
1718
"github.com/chainifynet/aws-encryption-sdk-go/pkg/model"
1819
"github.com/chainifynet/aws-encryption-sdk-go/pkg/model/format"
19-
"github.com/chainifynet/aws-encryption-sdk-go/pkg/serialization"
2020
"github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
2121
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/encryption"
2222
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/keyderivation"
File renamed without changes.
File renamed without changes.
File renamed without changes.

pkg/serialization/wrappingkey/wrapping_test.go renamed to pkg/internal/serialization/wrappingkey/wrapping_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010

11-
"github.com/chainifynet/aws-encryption-sdk-go/pkg/serialization/wrappingkey"
11+
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/serialization/wrappingkey"
1212
)
1313

1414
func TestWrappingKey_SerializeEncryptedDataKey(t *testing.T) {

pkg/keys/raw/raw.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"errors"
1010
"fmt"
1111

12+
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/serialization/wrappingkey"
1213
"github.com/chainifynet/aws-encryption-sdk-go/pkg/keys"
1314
"github.com/chainifynet/aws-encryption-sdk-go/pkg/model"
14-
"github.com/chainifynet/aws-encryption-sdk-go/pkg/serialization/wrappingkey"
1515
"github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
1616
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/encryption"
1717
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/keyderivation"

pkg/keys/raw/raw_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414

1515
mocks "github.com/chainifynet/aws-encryption-sdk-go/mocks/github.com/chainifynet/aws-encryption-sdk-go/pkg/model"
1616
mocksrand "github.com/chainifynet/aws-encryption-sdk-go/mocks/github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/rand"
17+
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/serialization/wrappingkey"
1718
"github.com/chainifynet/aws-encryption-sdk-go/pkg/keys"
1819
"github.com/chainifynet/aws-encryption-sdk-go/pkg/model"
19-
"github.com/chainifynet/aws-encryption-sdk-go/pkg/serialization/wrappingkey"
2020
"github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
2121
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/encryption"
2222
"github.com/chainifynet/aws-encryption-sdk-go/pkg/utils/rand"

0 commit comments

Comments
 (0)