Skip to content

[sql-47] Actions migration prep #1126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions accounts/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts
import (
"context"
"encoding/hex"
"errors"
"fmt"
"time"

Expand All @@ -16,6 +17,13 @@ import (
"gopkg.in/macaroon.v2"
)

var (
// ErrServerNotActive indicates that the server has started but hasn't
// fully finished the startup process.
ErrServerNotActive = errors.New("accounts server is still in the " +
"process of starting")
)

// RPCServer is the main server that implements the Accounts gRPC service.
type RPCServer struct {
litrpc.UnimplementedAccountsServer
Expand All @@ -26,13 +34,17 @@ type RPCServer struct {
}

// NewRPCServer returns a new RPC server for the given service.
func NewRPCServer(service *InterceptorService,
superMacBaker litmac.Baker) *RPCServer {
func NewRPCServer() *RPCServer {
return &RPCServer{}
}

return &RPCServer{
service: service,
superMacBaker: superMacBaker,
}
// Start adds the necessary dependencies for the RPCServer to be able to process
// requests, and starts the RPCServer.
func (s *RPCServer) Start(service *InterceptorService,
superMacBaker litmac.Baker) {

s.service = service
s.superMacBaker = superMacBaker
}

// CreateAccount adds an entry to the account database. This entry represents
Expand Down
89 changes: 50 additions & 39 deletions session_rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ import (
// other special cases.
const readOnlyAction = "***readonly***"

var (
// ErrServerNotActive indicates that the server has started but hasn't
// fully finished the startup process.
ErrServerNotActive = errors.New("session server is still in the " +
"process of starting")
)

// sessionRpcServer is the gRPC server for the Session RPC interface.
type sessionRpcServer struct {
litrpc.UnimplementedSessionsServer
Expand Down Expand Up @@ -70,42 +77,11 @@ type sessionRpcServerConfig struct {
privMap firewalldb.PrivacyMapper
}

// newSessionRPCServer creates a new sessionRpcServer using the passed config.
func newSessionRPCServer(cfg *sessionRpcServerConfig) (*sessionRpcServer,
error) {

// Create the gRPC server that handles adding/removing sessions and the
// actual mailbox server that spins up the Terminal Connect server
// interface.
server := session.NewServer(
func(id session.ID, opts ...grpc.ServerOption) *grpc.Server {
// Add the session ID injector interceptors first so
// that the session ID is available in the context of
// all interceptors that come after.
allOpts := []grpc.ServerOption{
addSessionIDToStreamCtx(id),
addSessionIDToUnaryCtx(id),
}

allOpts = append(allOpts, cfg.grpcOptions...)
allOpts = append(allOpts, opts...)

// Construct the gRPC server with the options.
grpcServer := grpc.NewServer(allOpts...)

// Register various grpc servers with the LNC session
// server.
cfg.registerGrpcServers(grpcServer)

return grpcServer
},
)

// newSessionRPCServer creates a new sessionRpcServer.
func newSessionRPCServer() *sessionRpcServer {
return &sessionRpcServer{
cfg: cfg,
sessionServer: server,
quit: make(chan struct{}),
}, nil
quit: make(chan struct{}),
}
}

// wrappedServerStream is a wrapper around the grpc.ServerStream that allows us
Expand Down Expand Up @@ -164,9 +140,42 @@ func addSessionIDToUnaryCtx(id session.ID) grpc.ServerOption {
})
}

// start all the components necessary for the sessionRpcServer to start serving
// requests. This includes resuming all non-revoked sessions.
func (s *sessionRpcServer) start(ctx context.Context) error {
// start starts a new sessionRpcServer using the passed config, and adds all
// components necessary for the sessionRpcServer to start serving requests. This
// includes resuming all non-revoked sessions.
func (s *sessionRpcServer) start(ctx context.Context,
cfg *sessionRpcServerConfig) error {

// Create the gRPC server that handles adding/removing sessions and the
// actual mailbox server that spins up the Terminal Connect server
// interface.
server := session.NewServer(
func(id session.ID, opts ...grpc.ServerOption) *grpc.Server {
// Add the session ID injector interceptors first so
// that the session ID is available in the context of
// all interceptors that come after.
allOpts := []grpc.ServerOption{
addSessionIDToStreamCtx(id),
addSessionIDToUnaryCtx(id),
}

allOpts = append(allOpts, cfg.grpcOptions...)
allOpts = append(allOpts, opts...)

// Construct the gRPC server with the options.
grpcServer := grpc.NewServer(allOpts...)

// Register various grpc servers with the LNC session
// server.
cfg.registerGrpcServers(grpcServer)

return grpcServer
},
)

s.cfg = cfg
s.sessionServer = server

// Delete all sessions in the Reserved state.
err := s.cfg.db.DeleteReservedSessions(ctx)
if err != nil {
Expand Down Expand Up @@ -255,7 +264,9 @@ func (s *sessionRpcServer) start(ctx context.Context) error {
func (s *sessionRpcServer) stop() error {
var returnErr error
s.stopOnce.Do(func() {
s.sessionServer.Stop()
if s.sessionServer != nil {
s.sessionServer.Stop()
}

close(s.quit)
s.wg.Wait()
Expand Down
Loading
Loading