-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encrypt each object with a random DEK and attach the encrypted DEK as object metadata. Encrpt the DEK with a key from the keyservice. All objects use the same KEK until a keyrotation takes place.
- Loading branch information
Showing
15 changed files
with
406 additions
and
63 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
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 |
---|---|---|
@@ -1,8 +1,24 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("//bazel/go:go_test.bzl", "go_test") | ||
|
||
go_library( | ||
name = "crypto", | ||
srcs = ["crypto.go"], | ||
importpath = "github.com/edgelesssys/constellation/v2/s3proxy/internal/crypto", | ||
visibility = ["//s3proxy:__subpackages__"], | ||
deps = [ | ||
"@com_github_tink_crypto_tink_go_v2//aead/subtle", | ||
"@com_github_tink_crypto_tink_go_v2//kwp/subtle", | ||
"@com_github_tink_crypto_tink_go_v2//subtle/random", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "crypto_test", | ||
srcs = ["crypto_test.go"], | ||
embed = [":crypto"], | ||
deps = [ | ||
"@com_github_stretchr_testify//assert", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
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,48 @@ | ||
/* | ||
Copyright (c) Edgeless Systems GmbH | ||
SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
package crypto | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEncryptDecrypt(t *testing.T) { | ||
tests := map[string]struct { | ||
plaintext []byte | ||
}{ | ||
"simple": { | ||
plaintext: []byte("hello, world"), | ||
}, | ||
"long": { | ||
plaintext: []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor."), | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
kek := [32]byte{} | ||
_, err := rand.Read(kek[:]) | ||
require.NoError(t, err) | ||
|
||
ciphertext, encryptedDEK, err := Encrypt(tt.plaintext, kek) | ||
require.NoError(t, err) | ||
|
||
assert.NotContains(t, ciphertext, tt.plaintext) | ||
|
||
// Decrypt the ciphertext using the KEK and encrypted DEK | ||
decrypted, err := Decrypt(ciphertext, encryptedDEK, kek) | ||
require.NoError(t, err) | ||
|
||
// Verify that the decrypted plaintext matches the original plaintext | ||
assert.Equal(t, tt.plaintext, decrypted, fmt.Sprintf("expected plaintext %s, got %s", tt.plaintext, decrypted)) | ||
}) | ||
} | ||
} |
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,29 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("//bazel/go:go_test.bzl", "go_test") | ||
|
||
go_library( | ||
name = "kms", | ||
srcs = ["kms.go"], | ||
importpath = "github.com/edgelesssys/constellation/v2/s3proxy/internal/kms", | ||
visibility = ["//s3proxy:__subpackages__"], | ||
deps = [ | ||
"//internal/logger", | ||
"//keyservice/keyserviceproto", | ||
"@org_golang_google_grpc//:go_default_library", | ||
"@org_golang_google_grpc//credentials/insecure", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "kms_test", | ||
srcs = ["kms_test.go"], | ||
embed = [":kms"], | ||
deps = [ | ||
"//internal/logger", | ||
"//keyservice/keyserviceproto", | ||
"@com_github_stretchr_testify//assert", | ||
"@org_golang_google_grpc//:go_default_library", | ||
"@org_golang_google_grpc//test/bufconn", | ||
"@org_uber_go_goleak//:goleak", | ||
], | ||
) |
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,76 @@ | ||
/* | ||
Copyright (c) Edgeless Systems GmbH | ||
SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
/* | ||
Package kms is used to interact with the Constellation keyservice. | ||
So far it is a copy of the joinservice's kms package. | ||
*/ | ||
package kms | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/edgelesssys/constellation/v2/internal/logger" | ||
"github.com/edgelesssys/constellation/v2/keyservice/keyserviceproto" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
// Client interacts with Constellation's keyservice. | ||
type Client struct { | ||
log *logger.Logger | ||
endpoint string | ||
grpc grpcClient | ||
} | ||
|
||
// New creates a new KMS. | ||
func New(log *logger.Logger, endpoint string) Client { | ||
return Client{ | ||
log: log, | ||
endpoint: endpoint, | ||
grpc: client{}, | ||
} | ||
} | ||
|
||
// GetDataKey returns a data encryption key for the given UUID. | ||
func (c Client) GetDataKey(ctx context.Context, keyID string, length int) ([]byte, error) { | ||
log := c.log.With("keyID", keyID, "endpoint", c.endpoint) | ||
// the KMS does not use aTLS since traffic is only routed through the Constellation cluster | ||
// cluster internal connections are considered trustworthy | ||
log.Infof("Connecting to KMS") | ||
conn, err := grpc.DialContext(ctx, c.endpoint, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
|
||
log.Infof("Requesting data key") | ||
res, err := c.grpc.GetDataKey( | ||
ctx, | ||
&keyserviceproto.GetDataKeyRequest{ | ||
DataKeyId: keyID, | ||
Length: uint32(length), | ||
}, | ||
conn, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("fetching data encryption key from Constellation KMS: %w", err) | ||
} | ||
|
||
log.Infof("Data key request successful") | ||
return res.DataKey, nil | ||
} | ||
|
||
type grpcClient interface { | ||
GetDataKey(context.Context, *keyserviceproto.GetDataKeyRequest, *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error) | ||
} | ||
|
||
type client struct{} | ||
|
||
func (c client) GetDataKey(ctx context.Context, req *keyserviceproto.GetDataKeyRequest, conn *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error) { | ||
return keyserviceproto.NewAPIClient(conn).GetDataKey(ctx, req) | ||
} |
Oops, something went wrong.