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).
157 lines
6.5 KiB
Go
157 lines
6.5 KiB
Go
package migrations_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
const (
|
|
insertConnectionLog000616 = `INSERT INTO connection_logs (id, connect_time, organization_id,
|
|
workspace_owner_id, workspace_id, workspace_name, agent_name, type, slug_or_port)
|
|
VALUES ($1, now(), $2, $3, $4, $5, 'main', $6, $7)`
|
|
insertConnectionLog000617 = `INSERT INTO connection_logs (id, connect_time, organization_id,
|
|
workspace_owner_id, workspace_id, workspace_name, agent_name, connection_method, app_name_or_port)
|
|
VALUES ($1, now(), $2, $3, $4, $5, 'main', $6, $7)`
|
|
selectConnectionLog000616 = `SELECT type::text, slug_or_port FROM connection_logs WHERE id = $1`
|
|
selectConnectionLog000617 = `SELECT connection_method::text, app_name_or_port FROM connection_logs WHERE id = $1`
|
|
)
|
|
|
|
// A pending migration batch can add tunnel to the old enum and then replace
|
|
// that enum. Comparisons must not materialize its uncommitted new value.
|
|
func TestMigration000617ConnectionLogsMethodInSingleTxn(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
|
|
sqlDB := testSQLDB(t)
|
|
ctx := testutil.Context(t, testutil.WaitSuperLong)
|
|
applyMigrationsInTxn(ctx, t, sqlDB, 1, 556)
|
|
orgID, ownerID, templateID, workspaceID := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
|
_, err := sqlDB.ExecContext(ctx, `
|
|
INSERT INTO organizations (id, name, description, display_name, created_at, updated_at, default_org_member_roles)
|
|
VALUES ($1, 'migration-org', '', '', now(), now(), '{}');
|
|
`, orgID)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, `
|
|
INSERT INTO users (id, email, username, hashed_password, created_at, updated_at) VALUES ($1, 'migration@example.com', 'migration-user', '', now(), now());
|
|
`, ownerID)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, `
|
|
INSERT INTO templates (id, created_at, updated_at, organization_id, name, provisioner, active_version_id, created_by)
|
|
VALUES ($1, now(), now(), $2, 'migration-template', 'echo', $3, $4);
|
|
`, templateID, orgID, uuid.New(), ownerID)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, `
|
|
INSERT INTO workspaces (id, created_at, updated_at, owner_id, organization_id, template_id, name)
|
|
VALUES ($1, now(), now(), $2, $3, $4, 'migration-workspace');
|
|
`, workspaceID, ownerID, orgID, templateID)
|
|
require.NoError(t, err)
|
|
id := uuid.New()
|
|
_, err = sqlDB.ExecContext(ctx, insertConnectionLog000616, id, orgID, ownerID, workspaceID, "migration-workspace", "vscode", nil)
|
|
require.NoError(t, err)
|
|
|
|
applyMigrationsInTxn(ctx, t, sqlDB, 557, 617)
|
|
var method string
|
|
var appName sql.NullString
|
|
require.NoError(t, sqlDB.QueryRowContext(ctx, selectConnectionLog000617, id).Scan(&method, &appName))
|
|
require.Equal(t, "ssh", method)
|
|
require.Equal(t, sql.NullString{String: "vscode", Valid: true}, appName)
|
|
}
|
|
|
|
// The up migration splits each connection type into a method and an app name,
|
|
// and the down migration folds app names back into types.
|
|
func TestMigration000617ConnectionLogsMethod(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
|
|
sqlDB := testSQLDB(t)
|
|
stepTo(t, sqlDB, 616)
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
db := database.New(sqlDB)
|
|
orgID := insertMigrationTestOrganization(ctx, t, sqlDB, []string{})
|
|
owner := dbgen.User(t, db, database.User{})
|
|
// dbgen.Template inserts columns that later migrations add to templates.
|
|
templateID := uuid.New()
|
|
_, err := sqlDB.ExecContext(ctx, `
|
|
INSERT INTO templates (id, created_at, updated_at, organization_id, name, provisioner, active_version_id, created_by)
|
|
VALUES ($1, now(), now(), $2, 'migration-template', 'echo', $3, $4);
|
|
`, templateID, orgID, uuid.New(), owner.ID)
|
|
require.NoError(t, err)
|
|
ws := dbgen.Workspace(t, db, database.WorkspaceTable{OrganizationID: orgID, OwnerID: owner.ID, TemplateID: templateID})
|
|
|
|
// row is (type, slug_or_port) before 000617 and
|
|
// (connection_method, app_name_or_port) after it.
|
|
type row struct {
|
|
kind string
|
|
appNameOrPort sql.NullString
|
|
}
|
|
app := func(name string) sql.NullString { return sql.NullString{String: name, Valid: true} }
|
|
ids := map[row]uuid.UUID{}
|
|
insert := func(query string, r row) {
|
|
ids[r] = uuid.New()
|
|
_, err := sqlDB.ExecContext(ctx, query, ids[r], orgID, owner.ID, ws.ID, ws.Name, r.kind, r.appNameOrPort)
|
|
require.NoError(t, err)
|
|
}
|
|
read := func(query string, r row) (got row) {
|
|
require.NoError(t, sqlDB.QueryRowContext(ctx, query, ids[r]).Scan(&got.kind, &got.appNameOrPort))
|
|
return got
|
|
}
|
|
migrate := func(file string) {
|
|
migration, err := os.ReadFile(file)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, string(migration))
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// Each released type and what the up migration turns it into.
|
|
converted := map[row]row{
|
|
{"ssh", sql.NullString{}}: {"ssh", sql.NullString{}},
|
|
{"vscode", sql.NullString{}}: {"ssh", app("vscode")},
|
|
{"jetbrains", sql.NullString{}}: {"ssh", app("jetbrains")},
|
|
{"reconnecting_pty", sql.NullString{}}: {"reconnecting_pty", sql.NullString{}},
|
|
{"workspace_app", app("code-server")}: {"workspace_app", app("code-server")},
|
|
{"port_forwarding", app("8080")}: {"port_forwarding", app("8080")},
|
|
{"tunnel", sql.NullString{}}: {"tunnel", sql.NullString{}},
|
|
}
|
|
for old := range converted {
|
|
insert(insertConnectionLog000616, old)
|
|
}
|
|
migrate("000617_connection_logs_method_app_name.up.sql")
|
|
for old, want := range converted {
|
|
require.Equal(t, want, read(selectConnectionLog000617, old), old.kind)
|
|
}
|
|
|
|
// App names only newer servers write. The down migration folds them into
|
|
// types with its snapshot of the app registry.
|
|
folded := map[row]row{
|
|
{"ssh", app("cursor")}: {"vscode", sql.NullString{}},
|
|
{"ssh", app("trae_cn")}: {"vscode", sql.NullString{}},
|
|
{"ssh", app("goland")}: {"jetbrains", sql.NullString{}},
|
|
{"ssh", app("zed")}: {"ssh", sql.NullString{}},
|
|
{"ssh", app("a_new_ide")}: {"ssh", sql.NullString{}},
|
|
{"reconnecting_pty", app("cursor")}: {"reconnecting_pty", sql.NullString{}},
|
|
}
|
|
for current := range folded {
|
|
insert(insertConnectionLog000617, current)
|
|
}
|
|
migrate("000617_connection_logs_method_app_name.down.sql")
|
|
for old := range converted {
|
|
require.Equal(t, old, read(selectConnectionLog000616, old), old.kind)
|
|
}
|
|
for current, want := range folded {
|
|
require.Equal(t, want, read(selectConnectionLog000616, current), current.kind+" "+current.appNameOrPort.String)
|
|
}
|
|
}
|