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

[v11] Disable application launch in minimal handler (#22816) #23333

Merged
merged 1 commit into from
Mar 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,12 @@ func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if redir, ok := app.HasName(r, h.handler.cfg.ProxyPublicAddrs); ok {
http.Redirect(w, r, redir, http.StatusFound)
return
// Only try to redirect if the handler is serving the full Web API.
if !h.handler.cfg.MinimalReverseTunnelRoutesOnly {
if redir, ok := app.HasName(r, h.handler.cfg.ProxyPublicAddrs); ok {
http.Redirect(w, r, redir, http.StatusFound)
return
}
}

// Serve the Web UI.
Expand Down
33 changes: 33 additions & 0 deletions lib/web/apiserver_ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,36 @@ func TestPing_multiProxyAddr(t *testing.T) {
require.NoError(t, resp.Body.Close())
}
}

// TestPing_minimalAPI tests that pinging the minimal web API works correctly.
func TestPing_minimalAPI(t *testing.T) {
env := newWebPack(t, 1, func(cfg *proxyConfig) {
cfg.minimalHandler = true
})
proxy := env.proxies[0]
tests := []struct {
name string
host string
}{
{
name: "Default ping",
host: proxy.handler.handler.cfg.ProxyPublicAddrs[0].Host(),
},
{
// This test ensures that the API doesn't try to launch an application.
name: "Ping with alternate host",
host: "example.com",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, proxy.newClient(t).Endpoint("webapi", "ping"), nil)
require.NoError(t, err)
req.Host = tc.host
resp, err := client.NewInsecureWebClient().Do(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
})
}

}
47 changes: 29 additions & 18 deletions lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6471,7 +6471,7 @@ func (r CreateSessionResponse) response() (*CreateSessionResponse, error) {
return &CreateSessionResponse{TokenType: r.TokenType, Token: r.Token, TokenExpiresIn: r.TokenExpiresIn, SessionInactiveTimeoutMS: r.SessionInactiveTimeoutMS}, nil
}

func newWebPack(t *testing.T, numProxies int) *webPack {
func newWebPack(t *testing.T, numProxies int, opts ...proxyOption) *webPack {
ctx := context.Background()
clock := clockwork.NewFakeClockAt(time.Now())

Expand Down Expand Up @@ -6583,7 +6583,7 @@ func newWebPack(t *testing.T, numProxies int) *webPack {
var proxies []*testProxy
for p := 0; p < numProxies; p++ {
proxyID := fmt.Sprintf("proxy%v", p)
proxies = append(proxies, createProxy(ctx, t, proxyID, node, server.TLS, hostSigners, clock))
proxies = append(proxies, createProxy(ctx, t, proxyID, node, server.TLS, hostSigners, clock, opts...))
}

// Wait for proxies to fully register before starting the test.
Expand All @@ -6606,9 +6606,19 @@ func newWebPack(t *testing.T, numProxies int) *webPack {
}
}

type proxyConfig struct {
minimalHandler bool
}

type proxyOption func(cfg *proxyConfig)

func createProxy(ctx context.Context, t *testing.T, proxyID string, node *regular.Server, authServer *auth.TestTLSServer,
hostSigners []ssh.Signer, clock clockwork.FakeClock,
hostSigners []ssh.Signer, clock clockwork.FakeClock, opts ...proxyOption,
) *testProxy {
cfg := proxyConfig{}
for _, opt := range opts {
opt(&cfg)
}
// create reverse tunnel service:
client, err := authServer.NewClient(auth.TestIdentity{
I: auth.BuiltinRole{
Expand Down Expand Up @@ -6717,21 +6727,22 @@ func createProxy(ctx context.Context, t *testing.T, proxyID string, node *regula
fs, err := newDebugFileSystem()
require.NoError(t, err)
handler, err := NewHandler(Config{
Proxy: revTunServer,
AuthServers: utils.FromAddr(authServer.Addr()),
DomainName: authServer.ClusterName(),
ProxyClient: client,
ProxyPublicAddrs: utils.MustParseAddrList("proxy-1.example.com", "proxy-2.example.com"),
CipherSuites: utils.DefaultCipherSuites(),
AccessPoint: client,
Context: ctx,
HostUUID: proxyID,
Emitter: client,
StaticFS: fs,
ProxySettings: &mockProxySettings{},
SessionControl: sessionController,
Router: router,
HealthCheckAppServer: func(context.Context, string, string) error { return nil },
Proxy: revTunServer,
AuthServers: utils.FromAddr(authServer.Addr()),
DomainName: authServer.ClusterName(),
ProxyClient: client,
ProxyPublicAddrs: utils.MustParseAddrList("proxy-1.example.com", "proxy-2.example.com"),
CipherSuites: utils.DefaultCipherSuites(),
AccessPoint: client,
Context: ctx,
HostUUID: proxyID,
Emitter: client,
StaticFS: fs,
ProxySettings: &mockProxySettings{},
SessionControl: sessionController,
Router: router,
HealthCheckAppServer: func(context.Context, string, string) error { return nil },
MinimalReverseTunnelRoutesOnly: cfg.minimalHandler,
}, SetSessionStreamPollPeriod(200*time.Millisecond), SetClock(clock))
require.NoError(t, err)

Expand Down