mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-09-19 20:44:52 -04:00
s3proxy: add keyservice integration
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.
This commit is contained in:
parent
a7ceda37ea
commit
887dcda78b
15 changed files with 414 additions and 71 deletions
29
s3proxy/internal/kms/BUILD.bazel
Normal file
29
s3proxy/internal/kms/BUILD.bazel
Normal file
|
@ -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",
|
||||
],
|
||||
)
|
76
s3proxy/internal/kms/kms.go
Normal file
76
s3proxy/internal/kms/kms.go
Normal file
|
@ -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)
|
||||
}
|
72
s3proxy/internal/kms/kms_test.go
Normal file
72
s3proxy/internal/kms/kms_test.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package kms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/logger"
|
||||
"github.com/edgelesssys/constellation/v2/keyservice/keyserviceproto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/goleak"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/test/bufconn"
|
||||
)
|
||||
|
||||
type stubClient struct {
|
||||
getDataKeyErr error
|
||||
dataKey []byte
|
||||
}
|
||||
|
||||
func (c *stubClient) GetDataKey(context.Context, *keyserviceproto.GetDataKeyRequest, *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error) {
|
||||
return &keyserviceproto.GetDataKeyResponse{DataKey: c.dataKey}, c.getDataKeyErr
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
goleak.VerifyTestMain(m)
|
||||
}
|
||||
|
||||
func TestGetDataKey(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
client *stubClient
|
||||
wantErr bool
|
||||
}{
|
||||
"GetDataKey success": {
|
||||
client: &stubClient{dataKey: []byte{0x1, 0x2, 0x3}},
|
||||
},
|
||||
"GetDataKey error": {
|
||||
client: &stubClient{getDataKeyErr: errors.New("error")},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
listener := bufconn.Listen(1)
|
||||
defer listener.Close()
|
||||
|
||||
client := New(
|
||||
logger.NewTest(t),
|
||||
listener.Addr().String(),
|
||||
)
|
||||
|
||||
client.grpc = tc.client
|
||||
|
||||
res, err := client.GetDataKey(context.Background(), "disk-uuid", 32)
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.client.dataKey, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue