2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-01-11 04:08:57 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/keyservice/keyserviceproto"
|
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,
|
2023-01-11 04:08:57 -05:00
|
|
|
&keyserviceproto.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 {
|
2023-01-11 04:08:57 -05:00
|
|
|
GetDataKey(context.Context, *keyserviceproto.GetDataKeyRequest, *grpc.ClientConn) (*keyserviceproto.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
|
|
|
|
2023-01-11 04:08:57 -05:00
|
|
|
func (c *constellationKMSClient) GetDataKey(ctx context.Context, req *keyserviceproto.GetDataKeyRequest, conn *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error) {
|
|
|
|
return keyserviceproto.NewAPIClient(conn).GetDataKey(ctx, req)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|