Files
coder/tailnet/test/cases.go
T
Taruj Goyal 734a0cae07 feat: scope browser-only connections to a template (#30578)
Add per-template `browser_only` enforcement while preserving `CODER_BROWSER_ONLY` as a deployment-wide override.

Refs #29157. Supersedes #29204 (closed by stale bot). Rebased onto current `main`, with migration renumbered to `000643`.

### Changes

- Add `templates.browser_only` (default `false`) to the database, SDK, and template create/update APIs.
- Require the `browser_only` entitlement to enable the setting, but allow disabling and continue enforcement after license expiry.
- Reject agent coordinate connections with 409 and enforce per-template tunnel restrictions through `AuthorizeTunnel`.
- Return non-fatal tunnel policy errors in both AGPL coordinator and pgcoord, preserving other active tunnels on shared connections.
- Exclude browser-only workspaces from Desktop workspace updates and enforce the restriction in MCP agent connections.
- Fix `TestMigration000617ConnectionLogsMethod` to use raw SQL, avoiding schema compatibility issues.

### Open questions

- **Naming:** Keep `browser_only` or use `require_browser_connections`?
- **Performance:** `AuthorizeTunnel` adds a database query under the coordinator mutex. Consider a narrower query if needed.
- **Feature reporting:** Should the feature report as enabled when any template uses it?

### Out of scope

Dashboard UI, CLI flag, and Terraform provider support (requires a separate `coder/terraform-provider-coderd` change).

### Verification

- Relevant unit and integration tests passed, including coordinate rejection, tunnel isolation, workspace filtering, and non-fatal policy errors in both coordinators.
- `golangci-lint` passed with no issues.
- Regenerated database, SDK TypeScript, Swagger/API reference, and audit-log artifacts.
- Not manually verified with Coder Desktop, multi-node deployments, workspace proxies, or UI.

### AI disclosure

Most changes and the rebase were completed with Claude Code (Opus 5/5.5), following the [AI contribution guidelines](https://coder.com/docs/about/contributing/AI_CONTRIBUTING).
2026-11-18 22:07:12 +00:00

117 lines
3.2 KiB
Go

package test
import (
"context"
"fmt"
"testing"
"github.com/google/uuid"
"github.com/coder/coder/v2/tailnet"
)
func GracefulDisconnectTest(ctx context.Context, t *testing.T, coordinator tailnet.CoordinatorV2) {
p1 := NewPeer(ctx, t, coordinator, "p1")
defer p1.Close(ctx)
p2 := NewPeer(ctx, t, coordinator, "p2")
defer p2.Close(ctx)
p1.AddTunnel(p2.ID)
p1.UpdateDERP(1)
p2.UpdateDERP(2)
p1.AssertEventuallyHasDERP(p2.ID, 2)
p2.AssertEventuallyHasDERP(p1.ID, 1)
p2.Disconnect()
p1.AssertEventuallyDisconnected(p2.ID)
p2.AssertEventuallyResponsesClosed("")
}
func LostTest(ctx context.Context, t *testing.T, coordinator tailnet.CoordinatorV2) {
p1 := NewPeer(ctx, t, coordinator, "p1")
defer p1.Close(ctx)
p2 := NewPeer(ctx, t, coordinator, "p2")
defer p2.Close(ctx)
p1.AddTunnel(p2.ID)
p1.UpdateDERP(1)
p2.UpdateDERP(2)
p1.AssertEventuallyHasDERP(p2.ID, 2)
p2.AssertEventuallyHasDERP(p1.ID, 1)
p2.Close(ctx)
p1.AssertEventuallyLost(p2.ID)
}
func BidirectionalTunnels(ctx context.Context, t *testing.T, coordinator tailnet.CoordinatorV2) {
p1 := NewPeer(ctx, t, coordinator, "p1")
defer p1.Close(ctx)
p2 := NewPeer(ctx, t, coordinator, "p2")
defer p2.Close(ctx)
p1.AddTunnel(p2.ID)
p2.AddTunnel(p1.ID)
p1.UpdateDERP(1)
p2.UpdateDERP(2)
p1.AssertEventuallyHasDERP(p2.ID, 2)
p2.AssertEventuallyHasDERP(p1.ID, 1)
}
func ReadyForHandshakeTest(ctx context.Context, t *testing.T, coordinator tailnet.CoordinatorV2) {
p1 := NewPeer(ctx, t, coordinator, "p1")
defer p1.Close(ctx)
p2 := NewPeer(ctx, t, coordinator, "p2")
defer p2.Close(ctx)
p1.AddTunnel(p2.ID)
p2.AddTunnel(p1.ID)
p1.UpdateDERP(1)
p2.UpdateDERP(2)
p1.AssertEventuallyHasDERP(p2.ID, 2)
p2.AssertEventuallyHasDERP(p1.ID, 1)
p2.ReadyForHandshake(p1.ID)
p1.AssertEventuallyReadyForHandshake(p2.ID)
}
func ReadyForHandshakeNoPermissionTest(ctx context.Context, t *testing.T, coordinator tailnet.CoordinatorV2) {
p1 := NewPeer(ctx, t, coordinator, "p1")
defer p1.Close(ctx)
p2 := NewPeer(ctx, t, coordinator, "p2")
defer p2.Close(ctx)
p1.UpdateDERP(1)
p2.UpdateDERP(2)
p2.ReadyForHandshake(p1.ID)
p2.AssertEventuallyGetsError(fmt.Sprintf("you do not share a tunnel with %q", p1.ID.String()))
}
type policyRefusingAuthorizer struct {
refused uuid.UUID
}
func (a policyRefusingAuthorizer) AuthorizeTunnel(_ context.Context, agentID uuid.UUID) error {
if agentID == a.refused {
return tailnet.TunnelPolicyError{Message: "refused by policy"}
}
return nil
}
// TunnelPolicyRefusalTest checks that a policy refusal of one tunnel leaves
// the peer connected and able to reach other destinations.
func TunnelPolicyRefusalTest(ctx context.Context, t *testing.T, coordinator tailnet.CoordinatorV2) {
refused := NewPeer(ctx, t, coordinator, "refused")
defer refused.Close(ctx)
allowed := NewPeer(ctx, t, coordinator, "allowed")
defer allowed.Close(ctx)
allowed.UpdateDERP(2)
client := NewPeer(ctx, t, coordinator, "client", WithAuth(tailnet.ClientUserCoordinateeAuth{
Auth: policyRefusingAuthorizer{refused: refused.ID},
}))
defer client.Close(ctx)
client.AddTunnel(refused.ID)
client.AssertEventuallyGetsError(tailnet.TunnelRefusedError + ": refused by policy")
client.AddTunnel(allowed.ID)
client.AssertEventuallyHasDERP(allowed.ID, 2)
}