2022-03-22 11:03:15 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
"github.com/edgelesssys/constellation/kms/kmsproto"
|
2022-03-22 11:03:15 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
// ConstellationKMS is a key service to fetch volume keys.
|
2022-03-22 11:03:15 -04:00
|
|
|
type ConstellationKMS struct {
|
|
|
|
endpoint string
|
2022-07-05 03:48:47 -04:00
|
|
|
kms kmsClient
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConstellationKMS initializes a ConstellationKMS.
|
2022-07-14 09:45:04 -04:00
|
|
|
func NewConstellationKMS(endpoint string) *ConstellationKMS {
|
2022-03-22 11:03:15 -04:00
|
|
|
return &ConstellationKMS{
|
2022-08-01 10:51:34 -04:00
|
|
|
endpoint: endpoint, // default: "kms.kube-system:port"
|
2022-07-05 03:48:47 -04:00
|
|
|
kms: &constellationKMSClient{},
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
// GetDEK request a data encryption key derived from the Constellation's master secret.
|
2022-03-24 10:21:19 -04:00
|
|
|
func (k *ConstellationKMS) GetDEK(ctx context.Context, dekID string, dekSize int) ([]byte, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
conn, err := grpc.DialContext(ctx, k.endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
res, err := k.kms.GetDataKey(
|
2022-03-22 11:03:15 -04:00
|
|
|
ctx,
|
2022-07-05 03:48:47 -04:00
|
|
|
&kmsproto.GetDataKeyRequest{
|
2022-03-22 11:03:15 -04:00
|
|
|
DataKeyId: dekID,
|
|
|
|
Length: uint32(dekSize),
|
|
|
|
},
|
|
|
|
conn,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-07-05 03:48:47 -04:00
|
|
|
return nil, fmt.Errorf("fetching data encryption key from Constellation KMS: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.DataKey, nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
type kmsClient interface {
|
|
|
|
GetDataKey(context.Context, *kmsproto.GetDataKeyRequest, *grpc.ClientConn) (*kmsproto.GetDataKeyResponse, error)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
type constellationKMSClient struct{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
func (c *constellationKMSClient) GetDataKey(ctx context.Context, req *kmsproto.GetDataKeyRequest, conn *grpc.ClientConn) (*kmsproto.GetDataKeyResponse, error) {
|
|
|
|
return kmsproto.NewAPIClient(conn).GetDataKey(ctx, req)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|