mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-08 01:05:16 -04:00
deps: convert zap to slog (#2825)
This commit is contained in:
parent
3765cb0762
commit
54cce77bab
182 changed files with 1474 additions and 1509 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
@ -20,8 +21,6 @@ import (
|
|||
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||
"github.com/edgelesssys/constellation/v2/internal/logger"
|
||||
"github.com/edgelesssys/constellation/v2/verify/verifyproto"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
|
@ -37,13 +36,13 @@ type attestation struct {
|
|||
// The server exposes both HTTP and gRPC endpoints
|
||||
// to retrieve attestation statements.
|
||||
type Server struct {
|
||||
log *logger.Logger
|
||||
log *slog.Logger
|
||||
issuer AttestationIssuer
|
||||
verifyproto.UnimplementedAPIServer
|
||||
}
|
||||
|
||||
// New initializes a new verification server.
|
||||
func New(log *logger.Logger, issuer AttestationIssuer) *Server {
|
||||
func New(log *slog.Logger, issuer AttestationIssuer) *Server {
|
||||
return &Server{
|
||||
log: log,
|
||||
issuer: issuer,
|
||||
|
@ -57,9 +56,9 @@ func (s *Server) Run(httpListener, grpcListener net.Listener) error {
|
|||
var wg sync.WaitGroup
|
||||
var once sync.Once
|
||||
|
||||
s.log.WithIncreasedLevel(zapcore.WarnLevel).Named("grpc").ReplaceGRPCLogger()
|
||||
logger.ReplaceGRPCLogger(slog.New(logger.NewLevelHandler(slog.LevelWarn, s.log.Handler()).WithGroup("grpc")))
|
||||
grpcServer := grpc.NewServer(
|
||||
s.log.Named("gRPC").GetServerUnaryInterceptor(),
|
||||
logger.GetServerUnaryInterceptor(s.log.WithGroup("gRPC")),
|
||||
grpc.KeepaliveParams(keepalive.ServerParameters{Time: 15 * time.Second}),
|
||||
)
|
||||
verifyproto.RegisterAPIServer(grpcServer, s)
|
||||
|
@ -73,7 +72,7 @@ func (s *Server) Run(httpListener, grpcListener net.Listener) error {
|
|||
defer wg.Done()
|
||||
defer grpcServer.GracefulStop()
|
||||
|
||||
s.log.Infof("Starting HTTP server on %s", httpListener.Addr().String())
|
||||
s.log.Info(fmt.Sprintf("Starting HTTP server on %s", httpListener.Addr().String()))
|
||||
httpErr := httpServer.Serve(httpListener)
|
||||
if httpErr != nil && httpErr != http.ErrServerClosed {
|
||||
once.Do(func() { err = httpErr })
|
||||
|
@ -85,7 +84,7 @@ func (s *Server) Run(httpListener, grpcListener net.Listener) error {
|
|||
defer wg.Done()
|
||||
defer func() { _ = httpServer.Shutdown(context.Background()) }()
|
||||
|
||||
s.log.Infof("Starting gRPC server on %s", grpcListener.Addr().String())
|
||||
s.log.Info(fmt.Sprintf("Starting gRPC server on %s", grpcListener.Addr().String()))
|
||||
grpcErr := grpcServer.Serve(grpcListener)
|
||||
if grpcErr != nil {
|
||||
once.Do(func() { err = grpcErr })
|
||||
|
@ -103,49 +102,49 @@ func (s *Server) GetAttestation(ctx context.Context, req *verifyproto.GetAttesta
|
|||
peerAddr = peer.Addr.String()
|
||||
}
|
||||
|
||||
log := s.log.With(zap.String("peerAddress", peerAddr)).Named("gRPC")
|
||||
s.log.Infof("Received attestation request")
|
||||
log := s.log.With(slog.String("peerAddress", peerAddr)).WithGroup("gRPC")
|
||||
s.log.Info("Received attestation request")
|
||||
if len(req.Nonce) == 0 {
|
||||
log.Errorf("Received attestation request with empty nonce")
|
||||
log.Error("Received attestation request with empty nonce")
|
||||
return nil, status.Error(codes.InvalidArgument, "nonce is required to issue attestation")
|
||||
}
|
||||
|
||||
log.Infof("Creating attestation")
|
||||
log.Info("Creating attestation")
|
||||
statement, err := s.issuer.Issue(ctx, []byte(constants.ConstellationVerifyServiceUserData), req.Nonce)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "issuing attestation statement: %v", err)
|
||||
}
|
||||
|
||||
log.Infof("Attestation request successful")
|
||||
log.Info("Attestation request successful")
|
||||
return &verifyproto.GetAttestationResponse{Attestation: statement}, nil
|
||||
}
|
||||
|
||||
// getAttestationHTTP implements the HTTP endpoint for retrieving attestation statements.
|
||||
func (s *Server) getAttestationHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log := s.log.With(zap.String("peerAddress", r.RemoteAddr)).Named("http")
|
||||
log := s.log.With(slog.String("peerAddress", r.RemoteAddr)).WithGroup("http")
|
||||
|
||||
nonceB64 := r.URL.Query()["nonce"]
|
||||
if len(nonceB64) != 1 || nonceB64[0] == "" {
|
||||
log.Errorf("Received attestation request with empty or multiple nonce parameter")
|
||||
log.Error("Received attestation request with empty or multiple nonce parameter")
|
||||
http.Error(w, "nonce parameter is required exactly once", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
nonce, err := base64.URLEncoding.DecodeString(nonceB64[0])
|
||||
if err != nil {
|
||||
log.With(zap.Error(err)).Errorf("Received attestation request with invalid nonce")
|
||||
log.With(slog.Any("error", err)).Error("Received attestation request with invalid nonce")
|
||||
http.Error(w, fmt.Sprintf("invalid base64 encoding for nonce: %v", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Infof("Creating attestation")
|
||||
log.Info("Creating attestation")
|
||||
quote, err := s.issuer.Issue(r.Context(), []byte(constants.ConstellationVerifyServiceUserData), nonce)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("issuing attestation statement: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Infof("Attestation request successful")
|
||||
log.Info("Attestation request successful")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(attestation{quote}); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue