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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConstellationKMS is a key service using the Constellation Coordinator to fetch volume keys.
|
|
|
|
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.
|
|
|
|
func NewConstellationKMS(coordinatorEndpoint string) *ConstellationKMS {
|
|
|
|
return &ConstellationKMS{
|
2022-07-05 03:48:47 -04:00
|
|
|
endpoint: coordinatorEndpoint, // default: "kms.kube-system:9000"
|
|
|
|
kms: &constellationKMSClient{},
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDEK connects to the Constellation Coordinators VPN API to 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
|
|
|
}
|