Plumb connection limit to proxy and output it (#384)
This commit is contained in:
1 parent
7db0e2a5ed
commit
f7e48ec836
9 files changed
+149
-59
No files matched your search
@@ -206,7 +206,7 @@ func (ws *workerServiceServer) AuthorizeConnection(ctx context.Context, req *pbs
|
||||
return nil, status.Errorf(codes.Internal, "error getting session repo: %v", err)
|
||||
}
|
||||
|
||||
connectionInfo, connStates, _, err := sessRepo.AuthorizeConnection(ctx, req.GetSessionId())
|
||||
connectionInfo, connStates, authzSummary, err := sessRepo.AuthorizeConnection(ctx, req.GetSessionId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -217,8 +217,14 @@ func (ws *workerServiceServer) AuthorizeConnection(ctx context.Context, req *pbs
|
||||
return nil, status.Error(codes.Internal, "Invalid connection state in authorize response.")
|
||||
}
|
||||
|
||||
return &pbs.AuthorizeConnectionResponse{
|
||||
ConnectionId: connectionInfo.GetPublicId(),
|
||||
Status: connStates[0].Status.ProtoVal(),
|
||||
}, nil
|
||||
ret := &pbs.AuthorizeConnectionResponse{
|
||||
ConnectionId: connectionInfo.GetPublicId(),
|
||||
Status: connStates[0].Status.ProtoVal(),
|
||||
ConnectionsLeft: authzSummary.ConnectionLimit,
|
||||
}
|
||||
if ret.ConnectionsLeft != -1 {
|
||||
ret.ConnectionsLeft -= int32(authzSummary.CurrentConnectionCount)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
@@ -110,7 +110,8 @@ func (w *Worker) handleProxy() http.HandlerFunc {
|
||||
}
|
||||
|
||||
var ci *connInfo
|
||||
ci, err = w.authorizeConnection(r.Context(), sessionId)
|
||||
var connsLeft int32
|
||||
ci, connsLeft, err = w.authorizeConnection(r.Context(), sessionId)
|
||||
if err != nil {
|
||||
w.logger.Error("unable to authorize conneciton", "error", err)
|
||||
conn.Close(websocket.StatusInternalError, "unable to authorize connection")
|
||||
@@ -127,7 +128,8 @@ func (w *Worker) handleProxy() http.HandlerFunc {
|
||||
w.logger.Trace("authorized connection", "connection_id", ci.id)
|
||||
|
||||
handshakeResult := &proxy.HandshakeResult{
|
||||
Expiration: expiration,
|
||||
Expiration: expiration,
|
||||
ConnectionsLeft: connsLeft,
|
||||
}
|
||||
if err := wspb.Write(connCtx, conn, handshakeResult); err != nil {
|
||||
w.logger.Error("error sending handshake result to client", "error", err)
|
||||
|
||||
@@ -148,28 +148,28 @@ func (w *Worker) activateSession(ctx context.Context, sessionId, tofuToken strin
|
||||
return resp.GetStatus(), nil
|
||||
}
|
||||
|
||||
func (w *Worker) authorizeConnection(ctx context.Context, sessionId string) (*connInfo, error) {
|
||||
func (w *Worker) authorizeConnection(ctx context.Context, sessionId string) (*connInfo, int32, error) {
|
||||
rawConn := w.controllerSessionConn.Load()
|
||||
if rawConn == nil {
|
||||
return nil, errors.New("could not get a controller client")
|
||||
return nil, 0, errors.New("could not get a controller client")
|
||||
}
|
||||
conn, ok := rawConn.(pbs.SessionServiceClient)
|
||||
if !ok {
|
||||
return nil, errors.New("could not cast atomic controller client to the real thing")
|
||||
return nil, 0, errors.New("could not cast atomic controller client to the real thing")
|
||||
}
|
||||
if conn == nil {
|
||||
return nil, errors.New("controller client is nil")
|
||||
return nil, 0, errors.New("controller client is nil")
|
||||
}
|
||||
|
||||
resp, err := conn.AuthorizeConnection(ctx, &pbs.AuthorizeConnectionRequest{
|
||||
SessionId: sessionId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error authorizing connection: %w", err)
|
||||
return nil, 0, fmt.Errorf("error authorizing connection: %w", err)
|
||||
}
|
||||
|
||||
return &connInfo{
|
||||
id: resp.ConnectionId,
|
||||
status: resp.GetStatus(),
|
||||
}, nil
|
||||
}, resp.GetConnectionsLeft(), nil
|
||||
}
|
||||
Reference in new issue
Block a user