mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-09-27 12:01:04 -04:00
Replace logging with default logging interface (#233)
* Add test logger * Refactor access manager logging * Refactor activation service logging * Refactor debugd logging * Refactor kms server logging * Refactor disk-mapper logging Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
parent
e3f78a5bff
commit
b10b13b173
42 changed files with 513 additions and 328 deletions
|
@ -9,13 +9,13 @@ import (
|
|||
|
||||
"github.com/edgelesssys/constellation/internal/constants"
|
||||
"github.com/edgelesssys/constellation/internal/file"
|
||||
"github.com/edgelesssys/constellation/internal/grpc/grpc_klog"
|
||||
"github.com/edgelesssys/constellation/internal/logger"
|
||||
"github.com/edgelesssys/constellation/kms/server/kmsapi"
|
||||
"github.com/edgelesssys/constellation/kms/server/kmsapi/kmsproto"
|
||||
"github.com/edgelesssys/constellation/kms/server/setup"
|
||||
"github.com/spf13/afero"
|
||||
"go.uber.org/zap"
|
||||
"k8s.io/klog/v2"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
@ -24,43 +24,42 @@ func main() {
|
|||
port := flag.String("port", "9000", "Port gRPC server listens on")
|
||||
masterSecretPath := flag.String("master-secret", "/constellation/constellation-mastersecret.base64", "Path to the Constellation master secret")
|
||||
|
||||
klog.InitFlags(nil)
|
||||
flag.Parse()
|
||||
defer klog.Flush()
|
||||
|
||||
klog.V(2).Infof("\nConstellation Key Management Service\nVersion: %s", constants.VersionInfo)
|
||||
log := logger.New(logger.JSONLog, zapcore.InfoLevel)
|
||||
|
||||
log.With(zap.String("version", constants.VersionInfo)).Infof("Constellation Key Management Service")
|
||||
|
||||
masterKey, err := readMainSecret(*masterSecretPath)
|
||||
if err != nil {
|
||||
klog.Exitf("Failed to read master secret: %v", err)
|
||||
log.With(zap.Error(err)).Fatalf("Failed to read master secret")
|
||||
}
|
||||
|
||||
conKMS, err := setup.SetUpKMS(context.Background(), setup.NoStoreURI, setup.ClusterKMSURI)
|
||||
if err != nil {
|
||||
klog.Exitf("Failed to setup KMS: %v", err)
|
||||
log.With(zap.Error(err)).Fatalf("Failed to setup KMS")
|
||||
}
|
||||
|
||||
if err := conKMS.CreateKEK(context.Background(), "Constellation", masterKey); err != nil {
|
||||
klog.Exitf("Failed to create KMS KEK from MasterKey: %v", err)
|
||||
log.With(zap.Error(err)).Fatalf("Failed to create KMS KEK from MasterKey")
|
||||
}
|
||||
|
||||
lis, err := net.Listen("tcp", net.JoinHostPort("", *port))
|
||||
if err != nil {
|
||||
klog.Exitf("Failed to listen: %v", err)
|
||||
log.With(zap.Error(err)).Fatalf("Failed to listen")
|
||||
}
|
||||
|
||||
srv := kmsapi.New(&zap.Logger{}, conKMS)
|
||||
srv := kmsapi.New(log.Named("server"), conKMS)
|
||||
|
||||
log.Named("gRPC").WithIncreasedLevel(zapcore.WarnLevel).ReplaceGRPCLogger()
|
||||
// TODO: Launch server with aTLS to allow attestation for clients.
|
||||
grpcServer := grpc.NewServer(
|
||||
grpc.UnaryInterceptor(grpc_klog.LogGRPC(2)),
|
||||
)
|
||||
grpcServer := grpc.NewServer(log.Named("gRPC").GetServerUnaryInterceptor())
|
||||
|
||||
kmsproto.RegisterAPIServer(grpcServer, srv)
|
||||
|
||||
klog.V(2).Infof("Starting key management service on %s", lis.Addr().String())
|
||||
log.Infof("Starting key management service on %s", lis.Addr().String())
|
||||
if err := grpcServer.Serve(lis); err != nil {
|
||||
klog.Exitf("Failed to serve: %s", err)
|
||||
log.With(zap.Error(err)).Fatalf("Failed to serve")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,46 +4,49 @@ package kmsapi
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/edgelesssys/constellation/internal/grpc/grpclog"
|
||||
"github.com/edgelesssys/constellation/internal/logger"
|
||||
"github.com/edgelesssys/constellation/kms/kms"
|
||||
"github.com/edgelesssys/constellation/kms/server/kmsapi/kmsproto"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// API resembles an encryption key management api server through logger, CloudKMS and proto-unimplemented server.
|
||||
type API struct {
|
||||
logger *zap.Logger
|
||||
log *logger.Logger
|
||||
conKMS kms.CloudKMS
|
||||
kmsproto.UnimplementedAPIServer
|
||||
}
|
||||
|
||||
// New creates a new API.
|
||||
func New(logger *zap.Logger, conKMS kms.CloudKMS) *API {
|
||||
func New(log *logger.Logger, conKMS kms.CloudKMS) *API {
|
||||
return &API{
|
||||
logger: logger,
|
||||
log: log,
|
||||
conKMS: conKMS,
|
||||
}
|
||||
}
|
||||
|
||||
// GetDataKey returns a data key.
|
||||
func (a *API) GetDataKey(ctx context.Context, in *kmsproto.GetDataKeyRequest) (*kmsproto.GetDataKeyResponse, error) {
|
||||
log := a.log.With("peerAddress", grpclog.PeerAddrFromContext(ctx))
|
||||
|
||||
// Error on 0 key length
|
||||
if in.Length == 0 {
|
||||
klog.Error("GetDataKey: requested key length is zero")
|
||||
log.Errorf("Requested key length is zero")
|
||||
return nil, status.Error(codes.InvalidArgument, "can't derive key with length zero")
|
||||
}
|
||||
|
||||
// Error on empty DataKeyId
|
||||
if in.DataKeyId == "" {
|
||||
klog.Error("GetDataKey: no data key ID specified")
|
||||
log.Errorf("No data key ID specified")
|
||||
return nil, status.Error(codes.InvalidArgument, "no data key ID specified")
|
||||
}
|
||||
|
||||
key, err := a.conKMS.GetDEK(ctx, "Constellation", "key-"+in.DataKeyId, int(in.Length))
|
||||
if err != nil {
|
||||
klog.Errorf("GetDataKey: failed to get data key: %v", err)
|
||||
log.With(zap.Error(err)).Errorf("Failed to get data key")
|
||||
return nil, status.Errorf(codes.Internal, "%v", err)
|
||||
}
|
||||
return &kmsproto.GetDataKeyResponse{DataKey: key}, nil
|
||||
|
|
|
@ -5,18 +5,20 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/internal/logger"
|
||||
"github.com/edgelesssys/constellation/kms/server/kmsapi/kmsproto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
func TestGetDataKey(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
log := logger.NewTest(t)
|
||||
|
||||
kms := &stubKMS{derivedKey: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5}}
|
||||
api := New(zaptest.NewLogger(t), kms)
|
||||
api := New(log, kms)
|
||||
|
||||
res, err := api.GetDataKey(context.Background(), &kmsproto.GetDataKeyRequest{DataKeyId: "1", Length: 32})
|
||||
require.NoError(err)
|
||||
|
@ -33,7 +35,7 @@ func TestGetDataKey(t *testing.T) {
|
|||
assert.Nil(res)
|
||||
|
||||
// Test derive key error
|
||||
api = New(zaptest.NewLogger(t), &stubKMS{deriveKeyErr: errors.New("error")})
|
||||
api = New(log, &stubKMS{deriveKeyErr: errors.New("error")})
|
||||
res, err = api.GetDataKey(context.Background(), &kmsproto.GetDataKeyRequest{DataKeyId: "1", Length: 32})
|
||||
assert.Error(err)
|
||||
assert.Nil(res)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue