@@ -19,6 +19,7 @@ import (
19
19
20
20
"github.com/hashicorp/boundary/api"
21
21
apiproxy "github.com/hashicorp/boundary/api/proxy"
22
+ "github.com/hashicorp/boundary/api/sessions"
22
23
"github.com/hashicorp/boundary/api/targets"
23
24
"github.com/hashicorp/boundary/internal/cmd/base"
24
25
"github.com/hashicorp/boundary/internal/util"
@@ -336,6 +337,19 @@ func (c *Command) Run(args []string) (retCode int) {
336
337
}
337
338
}
338
339
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
+
339
353
authzString := c .flagAuthzToken
340
354
switch {
341
355
case authzString != "" :
@@ -431,28 +445,35 @@ func (c *Command) Run(args []string) (retCode int) {
431
445
HostId : sa .HostId ,
432
446
Credentials : sa .Credentials ,
433
447
}
434
- authzString = sa .AuthorizationToken
435
- }
436
448
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
+ }()
447
467
448
- listenAddr = netip .AddrPortFrom (addr , uint16 (c .flagListenPort ))
468
+ authzString = sa .AuthorizationToken
469
+ }
449
470
450
471
connsLeftCh := make (chan int32 )
451
472
apiProxyOpts := []apiproxy.Option {apiproxy .WithConnectionsLeftCh (connsLeftCh )}
452
473
if listenAddr .IsValid () {
453
474
apiProxyOpts = append (apiProxyOpts , apiproxy .WithListenAddrPort (listenAddr ))
454
475
}
455
- clientProxy , err : = apiproxy .New (
476
+ clientProxy , err = apiproxy .New (
456
477
c .proxyCtx ,
457
478
authzString ,
458
479
apiProxyOpts ... ,
0 commit comments