Skip to content

Commit 6eada99

Browse files
Don't hardcode local credentials server prot
1 parent c55f66a commit 6eada99

File tree

6 files changed

+51
-22
lines changed

6 files changed

+51
-22
lines changed

cmd/agent/container/credentials_server.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/loft-sh/devpod/pkg/agent/tunnel"
1414
"github.com/loft-sh/devpod/pkg/agent/tunnelserver"
1515
"github.com/loft-sh/devpod/pkg/credentials"
16-
locald "github.com/loft-sh/devpod/pkg/daemon/local"
1716
"github.com/loft-sh/devpod/pkg/dockercredentials"
1817
"github.com/loft-sh/devpod/pkg/gitcredentials"
1918
"github.com/loft-sh/devpod/pkg/gitsshsigning"
@@ -24,14 +23,18 @@ import (
2423
"github.com/spf13/cobra"
2524
)
2625

27-
const ExitCodeIO int = 64
26+
const (
27+
ExitCodeIO int = 64
28+
DefaultLogFile string = "/tmp/devpod-credentials-server.log"
29+
)
2830

2931
// CredentialsServerCmd holds the cmd flags
3032
type CredentialsServerCmd struct {
3133
*flags.GlobalFlags
3234

3335
User string
3436
Client string
37+
Port int
3538

3639
ConfigureGitHelper bool
3740
ConfigureDockerHelper bool
@@ -65,6 +68,7 @@ func NewCredentialsServerCmd(flags *flags.GlobalFlags) *cobra.Command {
6568
credentialsServerCmd.Flags().StringVar(&cmd.User, "user", "", "The user to use")
6669
_ = credentialsServerCmd.MarkFlagRequired("user")
6770
credentialsServerCmd.Flags().StringVar(&cmd.Client, "client", "", "client host")
71+
credentialsServerCmd.Flags().IntVar(&cmd.Port, "port", 0, "port of credentials server running locally on client machine to connect to")
6872

6973
return credentialsServerCmd
7074
}
@@ -73,12 +77,12 @@ func NewCredentialsServerCmd(flags *flags.GlobalFlags) *cobra.Command {
7377
func (cmd *CredentialsServerCmd) Run(ctx context.Context, port int) error {
7478
var tunnelClient tunnel.TunnelClient
7579
var err error
76-
fileLogger := log.NewFileLogger("/tmp/credentials_server_cmd.log", logrus.DebugLevel)
80+
fileLogger := log.NewFileLogger(DefaultLogFile, logrus.DebugLevel)
7781
// create a grpc client
7882
// if we have client address, lets use the http client
7983
if cmd.Client != "" {
8084
// address := ts.EnsureURL(cmd.Client, locald.LocalCredentialsServerPort)
81-
tunnelClient, err = tunnelserver.NewHTTPTunnelClient(cmd.Client, fmt.Sprintf("%d", locald.LocalCredentialsServerPort), fileLogger)
85+
tunnelClient, err = tunnelserver.NewHTTPTunnelClient(cmd.Client, fmt.Sprintf("%d", cmd.Port), fileLogger)
8286
if err != nil {
8387
return fmt.Errorf("error creating tunnel client: %w", err)
8488
}

pkg/agent/tunnelserver/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88

99
"github.com/loft-sh/devpod/pkg/agent/tunnel"
10+
locald "github.com/loft-sh/devpod/pkg/daemon/local"
1011
"github.com/loft-sh/devpod/pkg/daemon/workspace/network"
1112
"github.com/loft-sh/devpod/pkg/stdio"
1213
"github.com/loft-sh/log"
@@ -55,6 +56,7 @@ func NewHTTPTunnelClient(targetHost string, targetPort string, log log.Logger) (
5556
) error {
5657
md := metadata.New(map[string]string{
5758
"x-target-host": targetHost,
59+
"x-proxy-port": fmt.Sprintf("%d", locald.LocalCredentialsServerPort),
5860
"x-target-port": targetPort,
5961
})
6062
// Create a new outgoing context with the metadata attached.

pkg/agent/tunnelserver/tunnelserver.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,23 @@ import (
3333
)
3434

3535
// GetListener returns correct listener for services server - either stdio or tcp
36-
func GetListener(client string, reader io.Reader, writer io.WriteCloser, exitOnClose bool, log log.Logger) (net.Listener, error) {
36+
func GetListener(client string, reader io.Reader, writer io.WriteCloser, exitOnClose bool, log log.Logger) (net.Listener, int, error) {
3737
if client == "" {
38-
log.Info("GetListener - returning stdio listener")
39-
return stdio.NewStdioListener(reader, writer, exitOnClose), nil
38+
log.Debug("GetListener - returning stdio listener")
39+
return stdio.NewStdioListener(reader, writer, exitOnClose), 0, nil
4040
}
41-
log.Info("GetListener - returning tcp listener")
42-
listener, err := net.Listen("tcp", ":4795") // FIXME
41+
log.Debug("GetListener - returning tcp listener")
42+
listener, err := net.Listen("tcp", ":0")
4343
if err != nil {
44-
return nil, err
44+
return nil, 0, err
4545
}
4646

47-
return listener, nil
47+
// Extract the actual TCP port the OS has bound to.
48+
tcpAddr, ok := listener.Addr().(*net.TCPAddr)
49+
if !ok {
50+
return nil, 0, fmt.Errorf("listener.Addr() is not a *net.TCPAddr")
51+
}
52+
return listener, tcpAddr.Port, nil
4853
}
4954

5055
func RunServicesServer(ctx context.Context, lis net.Listener, allowGitCredentials, allowDockerCredentials bool, forwarder netstat.Forwarder, workspace *provider2.Workspace, log log.Logger, options ...Option) error {

pkg/daemon/local/credentials_proxy.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ func (s *LocalCredentialsServerProxy) Listen(ctx context.Context) error {
6464
s.log.Error("LocalCredentialsServerProxy: Director missing x-target-port metadata")
6565
return nil, nil, status.Errorf(codes.InvalidArgument, "missing x-target-port metadata")
6666
}
67-
// targetPort := targetPorts[0]
68-
targetPort := "4795" // FIXME
69-
67+
targetPort := targetPorts[0]
7068
targetAddr := net.JoinHostPort(DefaultTargetHost, targetPort)
7169

7270
s.log.Infof("[LocalCredentialsServerProxy] [gRPC] Proxying call %q to target %s", fullMethodName, targetAddr)

pkg/daemon/workspace/network/network_proxy.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,17 @@ func (s *NetworkProxyService) Start(ctx context.Context) error {
6363

6464
targetHosts := mdCopy.Get("x-target-host")
6565
targetPorts := mdCopy.Get("x-target-port")
66-
if len(targetHosts) == 0 || len(targetPorts) == 0 {
67-
s.log.Errorf("[NetworkProxyService] [gRPC] Director missing x-target-host or x-target-port metadata for call %q", fullMethodName)
68-
return nil, nil, status.Errorf(codes.InvalidArgument, "missing x-target-host or x-target-port metadata")
66+
proxyPorts := mdCopy.Get("x-proxy-port")
67+
if len(targetHosts) == 0 || len(targetPorts) == 0 || len(proxyPorts) == 0 {
68+
s.log.Errorf("[NetworkProxyService] [gRPC] Director missing x-target-host, x-proxy-port or x-target-port metadata for call %q", fullMethodName)
69+
return nil, nil, status.Errorf(codes.InvalidArgument, "missing x-target-host, x-proxy-port or x-target-port metadata")
6970
}
7071

71-
port, err := strconv.Atoi(targetPorts[0])
72+
proxyPort, err := strconv.Atoi(proxyPorts[0])
7273
if err != nil {
7374
return nil, nil, err
7475
}
75-
targetAddr := ts.EnsureURL(targetHosts[0], port)
76+
targetAddr := ts.EnsureURL(targetHosts[0], proxyPort)
7677

7778
s.log.Infof("[NetworkProxyService] [gRPC] Proxying call %q to target %s", fullMethodName, targetAddr)
7879

pkg/tunnel/services.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,20 @@ func RunServices(
8888
forwarder = newForwarder(containerClient, append(forwardedPorts, fmt.Sprintf("%d", openvscode.DefaultVSCodePort)), log)
8989
}
9090

91+
// Create channels for the port and errors.
92+
portChan := make(chan int, 1)
9193
errChan := make(chan error, 1)
94+
9295
go func() {
9396
defer cancel()
9497
defer stdinWriter.Close()
95-
listener, err := tunnelserver.GetListener(client, stdoutReader, stdinWriter, false, log)
98+
listener, port, err := tunnelserver.GetListener(client, stdoutReader, stdinWriter, false, log)
9699
if err != nil {
97100
errChan <- errors.Wrap(err, "create tunnel server listener")
101+
return
98102
}
99-
log.Infof("DEBUG GRPC - GOT LISTENER FOR LOCAL SERVER - %v\n", listener)
103+
// Send the generated port back.
104+
portChan <- port
100105
defer listener.Close()
101106

102107
// Start local credentials server on clients machine and forward credentials to container
@@ -116,7 +121,16 @@ func RunServices(
116121
close(errChan)
117122
}()
118123

119-
// run credentials server
124+
log.Infof("Waiting for credentials server port to be assigned...")
125+
var port int
126+
select {
127+
case port = <-portChan:
128+
log.Infof("Credentials server running on port %d\n", port)
129+
case err = <-errChan:
130+
return err
131+
}
132+
133+
// Run credentials server process.
120134
writer := log.ErrorStreamOnly().Writer(logrus.DebugLevel, false)
121135
defer writer.Close()
122136

@@ -129,6 +143,11 @@ func RunServices(
129143
if configureGitCredentials {
130144
command += " --configure-git-helper"
131145
}
146+
147+
if port != 0 {
148+
command += fmt.Sprintf(" --port %d", port)
149+
}
150+
132151
if configureGitSSHSignatureHelper {
133152
format, userSigningKey, err := gitsshsigning.ExtractGitConfiguration()
134153
if err == nil && format == gitsshsigning.GPGFormatSSH && userSigningKey != "" {

0 commit comments

Comments
 (0)