Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v14] Allow a unix socket to be used as listener for Machine ID database-tunnel #41042

Merged
merged 1 commit into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions lib/tbot/service_database_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"fmt"
"net"
"net/url"
"os"
"path/filepath"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -172,19 +174,46 @@ func (s *DatabaseTunnelService) buildLocalProxyConfig(ctx context.Context) (lpCf
return lpConfig, nil
}

func createListener(log logrus.FieldLogger, addr string) (net.Listener, error) {
parsed, err := url.Parse(addr)
if err != nil {
return nil, trace.Wrap(err, "parsing %q", addr)
}

switch parsed.Scheme {
// If no scheme is provided, default to TCP.
case "tcp", "":
return net.Listen("tcp", parsed.Host)
case "unix":
absPath, err := filepath.Abs(parsed.Path)
if err != nil {
return nil, trace.Wrap(err, "resolving absolute path for %q", parsed.Path)
}

// Remove the file if it already exists. This is necessary to handle
// unclean exits.
if err := os.Remove(absPath); err != nil && !os.IsNotExist(err) {
log.WithError(err).Warn("Failed to remove existing socket file")
}

return net.ListenUnix("unix", &net.UnixAddr{
Net: "unix",
Name: absPath,
})
default:
return nil, trace.BadParameter("unsupported scheme %q", parsed.Scheme)
}
}

func (s *DatabaseTunnelService) Run(ctx context.Context) error {
ctx, span := tracer.Start(ctx, "DatabaseTunnelService/Run")
defer span.End()

l := s.cfg.Listener
if l == nil {
listenUrl, err := url.Parse(s.cfg.Listen)
if err != nil {
return trace.Wrap(err, "parsing listen url")
}

s.log.WithField("address", listenUrl.String()).Debug("Opening listener for database tunnel.")
l, err = net.Listen("tcp", listenUrl.Host)
s.log.WithField("address", s.cfg.Listen).Debug("Opening listener for database tunnel.")
var err error
l, err = createListener(s.log, s.cfg.Listen)
if err != nil {
return trace.Wrap(err, "opening listener")
}
Expand Down
Loading