Close proxy listener if session is no longer active (#4850)

* Close proxy listener if session is no longer active
This commit is contained in:
Todd authored and GitHub committed 2024-05-30 16:12:17 -07:00
1 parent 4fd49f00f6
commit 98b5e3fa52
3 files changed
+50

No files matched your search

+25
View File
@@ -15,6 +15,8 @@ import (
"sync/atomic"
"time"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/api/targets"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-secure-stdlib/base62"
@@ -37,6 +39,7 @@ type ClientProxy struct {
connectionsLeft *atomic.Int32
connsLeftCh chan int32
callerConnectionsLeftCh chan int32
apiClient *api.Client
sessionAuthzData *targets.SessionAuthorizationData
createTime time.Time
expiration time.Time
@@ -97,6 +100,7 @@ func New(ctx context.Context, authzToken string, opt ...Option) (*ClientProxy, e
callerConnectionsLeftCh: opts.WithConnectionsLeftCh,
started: new(atomic.Bool),
skipSessionTeardown: opts.WithSkipSessionTeardown,
apiClient: opts.withApiClient,
}
if opts.WithListener != nil {
@@ -260,6 +264,27 @@ func (p *ClientProxy) Start(opt ...Option) (retErr error) {
p.cancel()
return
}
// TODO: Determine if this is useful or if there is a better approach
// that we may use in the long term.
if p.apiClient != nil {
// If we can tell that the session for the connection we just
// closed is terminated, we can close the listener, otherwise
// might as well leave it open so the next connection can be
// tried.
sess, err := sessions.NewClient(p.apiClient).Read(p.ctx, p.sessionAuthzData.SessionId)
if err != nil || sess == nil || sess.Item == nil || sess.Item.TerminationReason == "" {
return
}
// We got a valid session response for the session we just
// closed a connection for. Since there is a termination reason
// we can treat the session as being terminated so no more
// connections will be able to be established.
fin <- fmt.Errorf("session no longer active")
listenerCloseFunc()
p.cancel()
}
}()
}
}()