WorkerCoordination and GetSession API refactoring (#354)

* Possible api changes.

* Worker API update.

* Updating import alias name.

* Moving session down a level as a member of the GetSessionResponse.

* Marshal the auth flag into the GetSessionResponse proto instead of the session.
This commit is contained in:
Todd Knight authored and GitHub committed 2020-09-12 21:54:18 -07:00
1 parent 2914b4c14c
commit 33e7b4538e
18 files changed
+1923 -936

No files matched your search

+18 -14
View File
@@ -19,7 +19,9 @@ import (
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/auth"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
wpbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/servers"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/accounts"
@@ -342,7 +344,7 @@ func jobTestingHandler(c *Controller) http.Handler {
}
}
var workers []*services.WorkerInfo
var workers []*pb.WorkerInfo
repo, err := c.ServersRepoFn()
if err != nil {
errorResp(err)
@@ -354,7 +356,7 @@ func jobTestingHandler(c *Controller) http.Handler {
return
}
for _, v := range servers {
workers = append(workers, &services.WorkerInfo{Address: v.Address})
workers = append(workers, &pb.WorkerInfo{Address: v.Address})
}
wrapper, err := c.kms.GetWrapper(r.Context(), scope.Global.String(), kms.KeyPurposeSessions)
@@ -390,16 +392,18 @@ func jobTestingHandler(c *Controller) http.Handler {
return
}
ret := &services.ValidateSessionResponse{
Id: jobId,
ScopeId: scope.Global.String(),
UserId: "u_1234567890",
Type: "tcp",
Endpoint: endpoint,
Certificate: certBytes,
PrivateKey: privKey,
WorkerInfo: workers,
ExpirationTime: &timestamppb.Timestamp{Seconds: time.Now().Add(timeout).Unix()},
ret := &wpbs.GetSessionResponse{
Session: &pb.Session{
Id: jobId,
ScopeId: scope.Global.String(),
UserId: "u_1234567890",
Type: "tcp",
Endpoint: endpoint,
Certificate: certBytes,
PrivateKey: privKey,
WorkerInfo: workers,
ExpirationTime: &timestamppb.Timestamp{Seconds: time.Now().Add(timeout).Unix()},
},
}
marshaled, err := proto.Marshal(ret)
@@ -413,8 +417,8 @@ func jobTestingHandler(c *Controller) http.Handler {
return
}
ret.PrivateKey = nil
c.jobMap.Store(jobId, ret)
ret.Session.PrivateKey = nil
c.jobMap.Store(jobId, ret.GetSession())
})
}
@@ -5,7 +5,8 @@ import (
"sync"
"time"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/servers/controller/common"
"github.com/hashicorp/boundary/internal/sessions"
@@ -35,8 +36,11 @@ func NewWorkerServiceServer(logger hclog.Logger, repoFn common.ServersRepoFactor
}
}
var _ pbs.SessionServiceServer = &workerServiceServer{}
var _ pbs.ServerCoordinationServiceServer = &workerServiceServer{}
func (ws *workerServiceServer) Status(ctx context.Context, req *pbs.StatusRequest) (*pbs.StatusResponse, error) {
ws.logger.Trace("got status request from worker", "name", req.Worker.Name, "address", req.Worker.Address, "active_jobs", req.ActiveJobIds)
ws.logger.Trace("got status request from worker", "name", req.Worker.Name, "address", req.Worker.Address, "jobs", req.GetJobs())
ws.updateTimes.Store(req.Worker.Name, time.Now())
repo, err := ws.repoFn()
if err != nil {
@@ -53,41 +57,47 @@ func (ws *workerServiceServer) Status(ctx context.Context, req *pbs.StatusReques
Controllers: controllers,
}
ws.jobCancelMap.Range(func(key, value interface{}) bool {
ret.CancelJobIds = append(ret.CancelJobIds, key.(string))
ret.JobsRequests = append(ret.JobsRequests, &pbs.JobChangeRequest{
Job: &pbs.Job{
JobId: key.(string),
Type: pbs.Job_JOBTYPE_SESSION,
},
RequestType: 0,
})
return true
})
for _, id := range ret.CancelJobIds {
ws.jobCancelMap.Delete(id)
for _, j := range ret.JobsRequests {
ws.jobCancelMap.Delete(j.GetJob().GetJobId())
}
return ret, nil
}
func (ws *workerServiceServer) ValidateSession(ctx context.Context, req *pbs.ValidateSessionRequest) (*pbs.ValidateSessionResponse, error) {
func (ws *workerServiceServer) GetSession(ctx context.Context, req *pbs.GetSessionRequest) (*pbs.GetSessionResponse, error) {
ws.logger.Trace("got validate session request from worker", "job_id", req.GetId())
// Look up the job info
storedSessionInfo, loaded := ws.jobMap.LoadAndDelete(req.GetId())
if !loaded {
return &pbs.ValidateSessionResponse{}, status.Errorf(codes.PermissionDenied, "Unknown job ID: %v", req.GetId())
return nil, status.Errorf(codes.PermissionDenied, "Unknown job ID: %v", req.GetId())
}
sessionInfo := storedSessionInfo.(*pbs.ValidateSessionResponse)
sessionInfo := storedSessionInfo.(*pb.Session)
wrapper, err := ws.kms.GetWrapper(ctx, sessionInfo.ScopeId, kms.KeyPurposeSessions)
if err != nil {
return &pbs.ValidateSessionResponse{}, status.Errorf(codes.Internal, "Error getting sessions wrapper: %v", err)
return nil, status.Errorf(codes.Internal, "Error getting sessions wrapper: %v", err)
}
// Derive the private key, which should match. Deriving on both ends allows
// us to not store it in the DB.
_, privKey, err := sessions.DeriveED25519Key(wrapper, sessionInfo.GetUserId(), req.GetId())
if err != nil {
return &pbs.ValidateSessionResponse{}, status.Errorf(codes.Internal, "Error deriving session key: %v", err)
return nil, status.Errorf(codes.Internal, "Error deriving session key: %v", err)
}
if sessionInfo.ExpirationTime.GetSeconds() > 0 {
timeDiff := time.Until(sessionInfo.GetExpirationTime().AsTime())
if timeDiff < 0 {
return &pbs.ValidateSessionResponse{}, status.Errorf(codes.OutOfRange, "Session has already expired")
return nil, status.Errorf(codes.OutOfRange, "Session has already expired")
}
defer func() {
time.AfterFunc(timeDiff, func() {
@@ -97,5 +107,5 @@ func (ws *workerServiceServer) ValidateSession(ctx context.Context, req *pbs.Val
}
sessionInfo.PrivateKey = privKey
return sessionInfo, nil
return &pbs.GetSessionResponse{Session: sessionInfo}, nil
}
+4 -2
View File
@@ -12,7 +12,7 @@ import (
"time"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/workers"
"github.com/hashicorp/go-alpnmux"
"github.com/hashicorp/go-multierror"
@@ -120,7 +120,9 @@ func (c *Controller) startListeners() error {
grpc.MaxRecvMsgSize(math.MaxInt32),
grpc.MaxSendMsgSize(math.MaxInt32),
)
services.RegisterWorkerServiceServer(workerServer, workers.NewWorkerServiceServer(c.logger.Named("worker-handler"), c.ServersRepoFn, c.workerStatusUpdateTimes, c.kms, c.jobMap))
workerService := workers.NewWorkerServiceServer(c.logger.Named("worker-handler"), c.ServersRepoFn, c.workerStatusUpdateTimes, c.kms, c.jobMap)
pbs.RegisterServerCoordinationServiceServer(workerServer, workerService)
pbs.RegisterSessionServiceServer(workerServer, workerService)
interceptor := newInterceptingListener(c, l)
ln.ALPNListener = interceptor
@@ -19,7 +19,7 @@ import (
"time"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/vault/sdk/helper/base62"
"google.golang.org/grpc"
"google.golang.org/grpc/resolver"
@@ -111,7 +111,7 @@ func (w *Worker) createClientConn(addr string) error {
return fmt.Errorf("error dialing controller for worker auth: %w", err)
}
client := services.NewWorkerServiceClient(cc)
client := pbs.NewServerCoordinationServiceClient(cc)
w.controllerConn.Store(client)
w.logger.Info("connected to controller", "address", addr)
+2 -2
View File
@@ -6,7 +6,7 @@ import (
"time"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
"github.com/hashicorp/boundary/internal/proxy"
"github.com/hashicorp/shared-secure-libs/configutil"
"nhooyr.io/websocket"
@@ -45,7 +45,7 @@ func (w *Worker) handleProxy() http.HandlerFunc {
wr.WriteHeader(http.StatusInternalServerError)
return
}
jobInfo := jobInfoRaw.(*services.ValidateSessionResponse)
jobInfo := jobInfoRaw.(*pb.Session)
opts := &websocket.AcceptOptions{
Subprotocols: []string{globals.TcpProxyV1},
+6 -6
View File
@@ -10,7 +10,7 @@ import (
"strings"
"time"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
)
const (
@@ -30,7 +30,7 @@ func (w *Worker) getJobTls(hello *tls.ClientHelloInfo) (*tls.Config, error) {
if rawConn == nil {
return nil, errors.New("could not get a controller client")
}
conn, ok := rawConn.(services.WorkerServiceClient)
conn, ok := rawConn.(pbs.SessionServiceClient)
if !ok {
return nil, errors.New("could not cast atomic controller client to the real thing")
}
@@ -41,14 +41,14 @@ func (w *Worker) getJobTls(hello *tls.ClientHelloInfo) (*tls.Config, error) {
timeoutContext, cancel := context.WithTimeout(w.baseContext, validateSessionTimeout)
defer cancel()
resp, err := conn.ValidateSession(timeoutContext, &services.ValidateSessionRequest{
resp, err := conn.GetSession(timeoutContext, &pbs.GetSessionRequest{
Id: jobId,
})
if err != nil {
return nil, fmt.Errorf("error validating session: %w", err)
}
parsedCert, err := x509.ParseCertificate(resp.GetCertificate())
parsedCert, err := x509.ParseCertificate(resp.GetSession().GetCertificate())
if err != nil {
return nil, fmt.Errorf("error parsing session certificate: %w", err)
}
@@ -63,8 +63,8 @@ func (w *Worker) getJobTls(hello *tls.ClientHelloInfo) (*tls.Config, error) {
tlsConf := &tls.Config{
Certificates: []tls.Certificate{
{
Certificate: [][]byte{resp.GetCertificate()},
PrivateKey: ed25519.PrivateKey(resp.GetPrivateKey()),
Certificate: [][]byte{resp.GetSession().GetCertificate()},
PrivateKey: ed25519.PrivateKey(resp.GetSession().GetPrivateKey()),
Leaf: parsedCert,
},
},
+9 -7
View File
@@ -5,8 +5,7 @@ import (
"math/rand"
"time"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/boundary/internal/servers"
"github.com/hashicorp/boundary/internal/types/resource"
"google.golang.org/grpc/resolver"
@@ -46,14 +45,17 @@ func (w *Worker) startStatusTicking(cancelCtx context.Context) {
return
case <-timer.C:
var activeJobs []string
var activeJobs []*pbs.JobStatus
w.cancellationMap.Range(func(key, value interface{}) bool {
activeJobs = append(activeJobs, key.(string))
activeJobs = append(activeJobs, &pbs.JobStatus{
Job: &pbs.Job{JobId: key.(string)},
Status: pbs.JobStatus_STATUS_ACTIVE,
})
return true
})
client := w.controllerConn.Load().(services.WorkerServiceClient)
client := w.controllerConn.Load().(pbs.ServerCoordinationServiceClient)
result, err := client.Status(cancelCtx, &pbs.StatusRequest{
ActiveJobIds: activeJobs,
Jobs: activeJobs,
Worker: &servers.Server{
PrivateId: w.conf.RawConfig.Worker.Name,
Name: w.conf.RawConfig.Worker.Name,
@@ -76,7 +78,7 @@ func (w *Worker) startStatusTicking(cancelCtx context.Context) {
w.logger.Trace("found controllers", "addresses", strAddrs)
w.lastStatusSuccess.Store(&LastStatusInformation{StatusResponse: result, StatusTime: time.Now()})
for _, id := range result.GetCancelJobIds() {
for _, id := range result.GetJobsRequests() {
if cancel, ok := w.cancellationMap.LoadAndDelete(id); ok {
cancel.(context.CancelFunc)()
w.logger.Info("canceled job", "job_id", id)
+2 -2
View File
@@ -6,11 +6,11 @@ import (
"net"
"sync"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
"nhooyr.io/websocket"
)
func (w *Worker) handleTcpProxyV1(jobCtx context.Context, conn *websocket.Conn, jobInfo *services.ValidateSessionResponse) {
func (w *Worker) handleTcpProxyV1(jobCtx context.Context, conn *websocket.Conn, jobInfo *pb.Session) {
remoteConn, err := net.Dial("tcp", jobInfo.Endpoint)
if err != nil {
w.logger.Error("error dialing endpoint", "error", err, "endpoint", jobInfo.Endpoint)