-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
coordinator: move userapi into authority (2)
Makes the necessary adjustments to userapi and recoveryapi after moving them from main to the authority package.
- Loading branch information
Showing
8 changed files
with
268 additions
and
488 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,33 @@ | ||
// Copyright 2024 Edgeless Systems GmbH | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package main | ||
package authority | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net" | ||
"time" | ||
"errors" | ||
|
||
"github.com/edgelesssys/contrast/internal/attestation/snp" | ||
"github.com/edgelesssys/contrast/internal/grpc/atlscredentials" | ||
"github.com/edgelesssys/contrast/internal/logger" | ||
"github.com/edgelesssys/contrast/internal/recoveryapi" | ||
grpcprometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/keepalive" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type recoveryAPIServer struct { | ||
grpc *grpc.Server | ||
logger *slog.Logger | ||
recoverable recoverable | ||
recoveryDoneC chan struct{} | ||
// ErrAlreadyRecovered is returned if recovery was requested but a seed is already set. | ||
var ErrAlreadyRecovered = errors.New("coordinator is already recovered") | ||
|
||
recoveryapi.UnimplementedRecoveryAPIServer | ||
} | ||
|
||
func newRecoveryAPIServer(recoveryTarget recoverable, reg *prometheus.Registry, log *slog.Logger) *recoveryAPIServer { | ||
issuer := snp.NewIssuer(logger.NewNamed(log, "snp-issuer")) | ||
credentials := atlscredentials.New(issuer, nil) | ||
// Recover recovers the Coordinator from a seed and salt. | ||
func (a *Authority) Recover(_ context.Context, req *recoveryapi.RecoverRequest) (*recoveryapi.RecoverResponse, error) { | ||
a.logger.Info("Recover called") | ||
|
||
grpcUserAPIMetrics := grpcprometheus.NewServerMetrics( | ||
grpcprometheus.WithServerCounterOptions( | ||
grpcprometheus.WithSubsystem("contrast_recoveryapi"), | ||
), | ||
grpcprometheus.WithServerHandlingTimeHistogram( | ||
grpcprometheus.WithHistogramSubsystem("contrast_recoveryapi"), | ||
grpcprometheus.WithHistogramBuckets([]float64{0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 2.5, 5}), | ||
), | ||
) | ||
|
||
grpcServer := grpc.NewServer( | ||
grpc.Creds(credentials), | ||
grpc.KeepaliveParams(keepalive.ServerParameters{Time: 15 * time.Second}), | ||
grpc.ChainStreamInterceptor( | ||
grpcUserAPIMetrics.StreamServerInterceptor(), | ||
), | ||
grpc.ChainUnaryInterceptor( | ||
grpcUserAPIMetrics.UnaryServerInterceptor(), | ||
), | ||
) | ||
s := &recoveryAPIServer{ | ||
grpc: grpcServer, | ||
logger: log.WithGroup("recoveryapi"), | ||
recoverable: recoveryTarget, | ||
recoveryDoneC: make(chan struct{}), | ||
} | ||
recoveryapi.RegisterRecoveryAPIServer(s.grpc, s) | ||
|
||
grpcUserAPIMetrics.InitializeMetrics(grpcServer) | ||
reg.MustRegister(grpcUserAPIMetrics) | ||
|
||
return s | ||
} | ||
err := a.recover(req.Seed, req.Salt) | ||
switch { | ||
case errors.Is(err, ErrAlreadyRecovered): | ||
return nil, status.Error(codes.FailedPrecondition, err.Error()) | ||
case err == nil: | ||
return &recoveryapi.RecoverResponse{}, nil | ||
default: | ||
// Pretty sure this failed because the seed was bad. | ||
return nil, status.Errorf(codes.InvalidArgument, err.Error()) | ||
|
||
func (s *recoveryAPIServer) Serve(endpoint string) error { | ||
lis, err := net.Listen("tcp", endpoint) | ||
if err != nil { | ||
return fmt.Errorf("listening on %s: %w", endpoint, err) | ||
} | ||
return s.grpc.Serve(lis) | ||
} | ||
|
||
func (s *recoveryAPIServer) WaitRecoveryDone() { | ||
<-s.recoveryDoneC | ||
} | ||
|
||
func (s *recoveryAPIServer) Recover(_ context.Context, req *recoveryapi.RecoverRequest) (*recoveryapi.RecoverResponse, error) { | ||
return &recoveryapi.RecoverResponse{}, s.recoverable.Recover(req.Seed, req.Salt) | ||
} | ||
|
||
type recoverable interface { | ||
Recover(seed, salt []byte) error | ||
} |
Oops, something went wrong.