create a deferral to automatically close sessions after creating them during boundary connect (#6054)

pull/6060/head
dani 8 months ago committed by GitHub
parent 5f6cb00108
commit 5ebf11caeb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -350,12 +350,20 @@ func (p *ClientProxy) Start(opt ...Option) (retErr error) {
return nil return nil
} }
ctx, cancel := context.WithTimeout(context.Background(), opts.withSessionTeardownTimeout) return p.CloseSession(opts.withSessionTeardownTimeout)
}
// CloseSession attempts to close the currently proxied session by sending a
// request to do so to the worker proxying the connection
func (p *ClientProxy) CloseSession(sessionTeardownTimeout time.Duration) error {
if sessionTeardownTimeout == 0 {
sessionTeardownTimeout = sessionCancelTimeout
}
ctx, cancel := context.WithTimeout(context.Background(), sessionTeardownTimeout)
defer cancel() defer cancel()
if err := p.sendSessionTeardown(ctx); err != nil { if err := p.sendSessionTeardown(ctx); err != nil {
return fmt.Errorf("error sending session teardown request to worker: %w", err) return fmt.Errorf("error sending session teardown request to worker: %w", err)
} }
return nil return nil
} }

@ -19,6 +19,7 @@ import (
"github.com/hashicorp/boundary/api" "github.com/hashicorp/boundary/api"
apiproxy "github.com/hashicorp/boundary/api/proxy" apiproxy "github.com/hashicorp/boundary/api/proxy"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/api/targets"
"github.com/hashicorp/boundary/internal/cmd/base" "github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/util" "github.com/hashicorp/boundary/internal/util"
@ -336,6 +337,19 @@ func (c *Command) Run(args []string) (retCode int) {
} }
} }
var addr netip.Addr
if c.flagListenAddr == "" {
c.flagListenAddr = "127.0.0.1"
}
addr, err := netip.ParseAddr(c.flagListenAddr)
if err != nil {
c.PrintCliError(fmt.Errorf("Error parsing listen address: %w", err))
return base.CommandCliError
}
listenAddr := netip.AddrPortFrom(addr, uint16(c.flagListenPort))
var clientProxy *apiproxy.ClientProxy
authzString := c.flagAuthzToken authzString := c.flagAuthzToken
switch { switch {
case authzString != "": case authzString != "":
@ -431,28 +445,35 @@ func (c *Command) Run(args []string) (retCode int) {
HostId: sa.HostId, HostId: sa.HostId,
Credentials: sa.Credentials, Credentials: sa.Credentials,
} }
authzString = sa.AuthorizationToken
}
var listenAddr netip.AddrPort // the session was created specifically for this `boundary connect`
var addr netip.Addr // command, and should be closed as soon as the command has exited
if c.flagListenAddr == "" { defer func() {
c.flagListenAddr = "127.0.0.1" var err error
} switch {
addr, err := netip.ParseAddr(c.flagListenAddr) case clientProxy != nil:
if err != nil { err = clientProxy.CloseSession(0)
c.PrintCliError(fmt.Errorf("Error parsing listen address: %w", err)) default:
return base.CommandCliError // this is a weird special case. normally we let the client proxy end
} // the session, but it failed to be inited, so we need to create the
// session client to ensure we don't leave hanging sessions
sClient := sessions.NewClient(client)
_, err = sClient.Cancel(c.Context, sa.SessionId, 0, sessions.WithAutomaticVersioning(true))
}
if err != nil {
c.PrintCliError(fmt.Errorf("Error closing session after command end: %w", err))
}
}()
listenAddr = netip.AddrPortFrom(addr, uint16(c.flagListenPort)) authzString = sa.AuthorizationToken
}
connsLeftCh := make(chan int32) connsLeftCh := make(chan int32)
apiProxyOpts := []apiproxy.Option{apiproxy.WithConnectionsLeftCh(connsLeftCh)} apiProxyOpts := []apiproxy.Option{apiproxy.WithConnectionsLeftCh(connsLeftCh)}
if listenAddr.IsValid() { if listenAddr.IsValid() {
apiProxyOpts = append(apiProxyOpts, apiproxy.WithListenAddrPort(listenAddr)) apiProxyOpts = append(apiProxyOpts, apiproxy.WithListenAddrPort(listenAddr))
} }
clientProxy, err := apiproxy.New( clientProxy, err = apiproxy.New(
c.proxyCtx, c.proxyCtx,
authzString, authzString,
apiProxyOpts..., apiProxyOpts...,

Loading…
Cancel
Save