Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions enterprise/aibridgeproxyd/aibridgeproxyd.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ type Server struct {
listener net.Listener
tlsEnabled bool
coderAccessURL *url.URL
// coderAccessPort is the resolved port for the Coder access URL.
coderAccessPort string
// refreshProviders fetches the live provider snapshot on Reload.
// Nil disables hot-reload.
refreshProviders RefreshProvidersFunc
Expand Down Expand Up @@ -265,7 +267,6 @@ func New(ctx context.Context, logger slog.Logger, opts Options) (*Server, error)
coderAccessPort = "80"
}
}
coderAccessURL.Host = net.JoinHostPort(coderAccessURL.Hostname(), coderAccessPort)

// MITM cert and key are required to intercept and decrypt HTTPS traffic.
if opts.MITMCertFile == "" || opts.MITMKeyFile == "" {
Expand Down Expand Up @@ -325,6 +326,7 @@ func New(ctx context.Context, logger slog.Logger, opts Options) (*Server, error)
proxy: proxy,
tlsEnabled: opts.TLSCertFile != "",
coderAccessURL: coderAccessURL,
coderAccessPort: coderAccessPort,
refreshProviders: opts.RefreshProviders,
allowedPorts: allowedPorts,
caCert: certPEM,
Expand Down Expand Up @@ -801,7 +803,7 @@ func (s *Server) isBlockedIP(ip net.IP, hostname string, port string) bool {
// block connections to its own deployment. Hostname-based (not IP-based)
// to handle dynamic IPs (DNS changes, load balancers, k8s rescheduling).
// The port is normalized at startup to handle URLs without explicit ports.
if strings.EqualFold(hostname, s.coderAccessURL.Hostname()) && port == s.coderAccessURL.Port() {
if strings.EqualFold(hostname, s.coderAccessURL.Hostname()) && port == s.coderAccessPort {
return false
}

Expand Down
45 changes: 41 additions & 4 deletions enterprise/aibridgeproxyd/aibridgeproxyd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,7 @@ func TestNew(t *testing.T) {
MITMKeyFile: mitmKeyFile,
})
require.NoError(t, err)
require.Equal(t, "localhost", srv.CoderAccessURL().Hostname())
require.Equal(t, "80", srv.CoderAccessURL().Port())
require.Equal(t, "localhost", srv.CoderAccessURL().Host)
})

t.Run("CoderAccessURLDefaultHTTPSPort", func(t *testing.T) {
Expand All @@ -639,8 +638,7 @@ func TestNew(t *testing.T) {
MITMKeyFile: mitmKeyFile,
})
require.NoError(t, err)
require.Equal(t, "localhost", srv.CoderAccessURL().Hostname())
require.Equal(t, "443", srv.CoderAccessURL().Port())
require.Equal(t, "localhost", srv.CoderAccessURL().Host)
})

t.Run("CoderAccessURLExplicitPort", func(t *testing.T) {
Expand Down Expand Up @@ -949,6 +947,45 @@ func TestNew(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, srv)
})

t.Run("CoderAccessURLHostPreserved", func(t *testing.T) {
t.Parallel()

mitmCertFile, mitmKeyFile := getSharedTestMITMCert(t)
logger := slogtest.Make(t, nil)

srv, err := aibridgeproxyd.New(t.Context(), logger, aibridgeproxyd.Options{
ListenAddr: "127.0.0.1:0",
CoderAccessURL: "https://coder.example.com",
MITMCertFile: mitmCertFile,
MITMKeyFile: mitmKeyFile,
AllowedPrivateCIDRs: []string{"127.0.0.1/32"},
})
require.NoError(t, err)
t.Cleanup(func() { _ = srv.Close() })

require.Equal(t, "coder.example.com", srv.CoderAccessURL().Host,
"Host must not have :443 appended")
})

t.Run("CoderAccessURLExplicitPortPreserved", func(t *testing.T) {
t.Parallel()

mitmCertFile, mitmKeyFile := getSharedTestMITMCert(t)
logger := slogtest.Make(t, nil)

srv, err := aibridgeproxyd.New(t.Context(), logger, aibridgeproxyd.Options{
ListenAddr: "127.0.0.1:0",
CoderAccessURL: "https://coder.example.com:8443",
MITMCertFile: mitmCertFile,
MITMKeyFile: mitmKeyFile,
AllowedPrivateCIDRs: []string{"127.0.0.1/32"},
})
require.NoError(t, err)
t.Cleanup(func() { _ = srv.Close() })

require.Equal(t, "coder.example.com:8443", srv.CoderAccessURL().Host)
})
}

func TestClose(t *testing.T) {
Expand Down
Loading