2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
// Package kms handles communication with Constellation's key service to request data encryption keys for new or rejoining nodes.
|
2022-05-23 05:36:54 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-01-11 04:08:57 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/keyservice/keyserviceproto"
|
2022-06-28 10:51:30 -04:00
|
|
|
"go.uber.org/zap"
|
2022-05-23 05:36:54 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
|
2023-01-11 04:08:57 -05:00
|
|
|
// Client interacts with Constellation's keyservice.
|
2022-05-23 05:36:54 -04:00
|
|
|
type Client struct {
|
2022-06-28 10:51:30 -04:00
|
|
|
log *logger.Logger
|
2022-05-23 05:36:54 -04:00
|
|
|
endpoint string
|
|
|
|
grpc grpcClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new KMS.
|
2022-06-28 10:51:30 -04:00
|
|
|
func New(log *logger.Logger, endpoint string) Client {
|
2022-05-23 05:36:54 -04:00
|
|
|
return Client{
|
2022-06-28 10:51:30 -04:00
|
|
|
log: log,
|
2022-05-23 05:36:54 -04:00
|
|
|
endpoint: endpoint,
|
|
|
|
grpc: client{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
// GetDataKey returns a data encryption key for the given UUID.
|
2022-07-26 04:58:39 -04:00
|
|
|
func (c Client) GetDataKey(ctx context.Context, keyID string, length int) ([]byte, error) {
|
|
|
|
log := c.log.With(zap.String("keyID", keyID), zap.String("endpoint", c.endpoint))
|
2022-07-29 03:52:47 -04:00
|
|
|
// the KMS does not use aTLS since traffic is only routed through the Constellation cluster
|
|
|
|
// cluster internal connections are considered trustworthy
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Infof("Connecting to KMS at %s", c.endpoint)
|
2022-05-23 05:36:54 -04:00
|
|
|
conn, err := grpc.DialContext(ctx, c.endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Infof("Requesting data key")
|
2022-05-23 05:36:54 -04:00
|
|
|
res, err := c.grpc.GetDataKey(
|
|
|
|
ctx,
|
2023-01-11 04:08:57 -05:00
|
|
|
&keyserviceproto.GetDataKeyRequest{
|
2022-07-26 04:58:39 -04:00
|
|
|
DataKeyId: keyID,
|
2022-05-23 05:36:54 -04:00
|
|
|
Length: uint32(length),
|
|
|
|
},
|
|
|
|
conn,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("fetching data encryption key from Constellation KMS: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Infof("Data key request successful")
|
2022-05-23 05:36:54 -04:00
|
|
|
return res.DataKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type grpcClient interface {
|
2023-01-11 04:08:57 -05:00
|
|
|
GetDataKey(context.Context, *keyserviceproto.GetDataKeyRequest, *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error)
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type client struct{}
|
|
|
|
|
2023-01-11 04:08:57 -05:00
|
|
|
func (c client) GetDataKey(ctx context.Context, req *keyserviceproto.GetDataKeyRequest, conn *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error) {
|
|
|
|
return keyserviceproto.NewAPIClient(conn).GetDataKey(ctx, req)
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|