hscontrol: build tailsql DSN with sqliteconfig

Its hand-built file: URI did not escape the path, so tailsql
read another file than headscale. Add Config.ReadOnly for mode=ro.
This commit is contained in:
Kristoffer Dalby committed 2026-11-11 16:58:07 +01:00
1 parent 1a44a0ac54
commit 687bb762e0
3 files changed
+31 -1

No files matched your search

+5
View File
@@ -277,6 +277,7 @@ type Config struct {
Synchronous Synchronous // synchronous mode (affects durability vs performance)
ForeignKeys bool // enable foreign key constraints (data integrity)
TxLock TxLock // transaction lock mode (affects write concurrency)
ReadOnly bool // open with SQLite's mode=ro
}
// Default returns the production configuration optimized for Headscale's usage patterns.
@@ -393,6 +394,10 @@ func (c *Config) ToURL() (string, error) {
queryParts = append(queryParts, "_pragma=foreign_keys=ON")
}
if c.ReadOnly {
queryParts = append(queryParts, "mode=ro")
}
if len(queryParts) > 0 {
baseURL += "?" + strings.Join(queryParts, "&")
}
@@ -309,6 +309,21 @@ func TestToURLOpensExactPath(t *testing.T) {
var mode string
require.NoError(t, db.QueryRowContext(t.Context(), "PRAGMA journal_mode").Scan(&mode))
require.Equal(t, "wal", mode)
// The read-only form tailsql uses must open the same file.
roURL, err := (&Config{Path: dbPath, WALAutocheckpoint: -1, ReadOnly: true}).ToURL()
require.NoError(t, err)
ro, err := sql.Open("sqlite", roURL)
require.NoError(t, err)
defer ro.Close()
var n int
require.NoError(t, ro.QueryRowContext(t.Context(), "SELECT count(*) FROM t").Scan(&n))
_, err = ro.ExecContext(t.Context(), "INSERT INTO t VALUES (1)")
require.ErrorContains(t, err, "readonly")
})
}
}
+11 -1
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"github.com/juanfont/headscale/hscontrol/db/sqliteconfig"
"github.com/tailscale/tailsql/server/tailsql"
"tailscale.com/tsnet"
"tailscale.com/tsweb"
@@ -17,6 +18,15 @@ import (
var ErrNoCertDomains = errors.New("no cert domains available for HTTPS")
func runTailSQLService(ctx context.Context, logf logger.Logf, stateDir, dbPath string) error {
dsn, err := (&sqliteconfig.Config{
Path: dbPath,
WALAutocheckpoint: -1, // not set
ReadOnly: true,
}).ToURL()
if err != nil {
return fmt.Errorf("building sqlite connection URL: %w", err)
}
opts := tailsql.Options{
Hostname: "tailsql-headscale",
StateDir: stateDir,
@@ -25,7 +35,7 @@ func runTailSQLService(ctx context.Context, logf logger.Logf, stateDir, dbPath s
Source: "headscale",
Label: "headscale - sqlite",
Driver: "sqlite",
URL: fmt.Sprintf("file:%s?mode=ro", dbPath),
URL: dsn,
Named: map[string]string{
"schema": `select * from sqlite_schema`,
},