Skip to content

Commit 5ebf11c

Browse files
authored
create a deferral to automatically close sessions after creating them during boundary connect (#6054)
1 parent 5f6cb00 commit 5ebf11c

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

api/proxy/proxy.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,20 @@ func (p *ClientProxy) Start(opt ...Option) (retErr error) {
350350
return nil
351351
}
352352

353-
ctx, cancel := context.WithTimeout(context.Background(), opts.withSessionTeardownTimeout)
353+
return p.CloseSession(opts.withSessionTeardownTimeout)
354+
}
355+
356+
// CloseSession attempts to close the currently proxied session by sending a
357+
// request to do so to the worker proxying the connection
358+
func (p *ClientProxy) CloseSession(sessionTeardownTimeout time.Duration) error {
359+
if sessionTeardownTimeout == 0 {
360+
sessionTeardownTimeout = sessionCancelTimeout
361+
}
362+
ctx, cancel := context.WithTimeout(context.Background(), sessionTeardownTimeout)
354363
defer cancel()
355364
if err := p.sendSessionTeardown(ctx); err != nil {
356365
return fmt.Errorf("error sending session teardown request to worker: %w", err)
357366
}
358-
359367
return nil
360368
}
361369

internal/cmd/commands/connect/connect.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/hashicorp/boundary/api"
2121
apiproxy "github.com/hashicorp/boundary/api/proxy"
22+
"github.com/hashicorp/boundary/api/sessions"
2223
"github.com/hashicorp/boundary/api/targets"
2324
"github.com/hashicorp/boundary/internal/cmd/base"
2425
"github.com/hashicorp/boundary/internal/util"
@@ -336,6 +337,19 @@ func (c *Command) Run(args []string) (retCode int) {
336337
}
337338
}
338339

340+
var addr netip.Addr
341+
if c.flagListenAddr == "" {
342+
c.flagListenAddr = "127.0.0.1"
343+
}
344+
addr, err := netip.ParseAddr(c.flagListenAddr)
345+
if err != nil {
346+
c.PrintCliError(fmt.Errorf("Error parsing listen address: %w", err))
347+
return base.CommandCliError
348+
}
349+
listenAddr := netip.AddrPortFrom(addr, uint16(c.flagListenPort))
350+
351+
var clientProxy *apiproxy.ClientProxy
352+
339353
authzString := c.flagAuthzToken
340354
switch {
341355
case authzString != "":
@@ -431,28 +445,35 @@ func (c *Command) Run(args []string) (retCode int) {
431445
HostId: sa.HostId,
432446
Credentials: sa.Credentials,
433447
}
434-
authzString = sa.AuthorizationToken
435-
}
436448

437-
var listenAddr netip.AddrPort
438-
var addr netip.Addr
439-
if c.flagListenAddr == "" {
440-
c.flagListenAddr = "127.0.0.1"
441-
}
442-
addr, err := netip.ParseAddr(c.flagListenAddr)
443-
if err != nil {
444-
c.PrintCliError(fmt.Errorf("Error parsing listen address: %w", err))
445-
return base.CommandCliError
446-
}
449+
// the session was created specifically for this `boundary connect`
450+
// command, and should be closed as soon as the command has exited
451+
defer func() {
452+
var err error
453+
switch {
454+
case clientProxy != nil:
455+
err = clientProxy.CloseSession(0)
456+
default:
457+
// this is a weird special case. normally we let the client proxy end
458+
// the session, but it failed to be inited, so we need to create the
459+
// session client to ensure we don't leave hanging sessions
460+
sClient := sessions.NewClient(client)
461+
_, err = sClient.Cancel(c.Context, sa.SessionId, 0, sessions.WithAutomaticVersioning(true))
462+
}
463+
if err != nil {
464+
c.PrintCliError(fmt.Errorf("Error closing session after command end: %w", err))
465+
}
466+
}()
447467

448-
listenAddr = netip.AddrPortFrom(addr, uint16(c.flagListenPort))
468+
authzString = sa.AuthorizationToken
469+
}
449470

450471
connsLeftCh := make(chan int32)
451472
apiProxyOpts := []apiproxy.Option{apiproxy.WithConnectionsLeftCh(connsLeftCh)}
452473
if listenAddr.IsValid() {
453474
apiProxyOpts = append(apiProxyOpts, apiproxy.WithListenAddrPort(listenAddr))
454475
}
455-
clientProxy, err := apiproxy.New(
476+
clientProxy, err = apiproxy.New(
456477
c.proxyCtx,
457478
authzString,
458479
apiProxyOpts...,

0 commit comments

Comments
 (0)