mirror of
https://github.com/juanfont/headscale.git
synced 2026-01-23 02:24:10 +00:00
Refactor the RequestTags migration (202601121700-migrate-hostinfo-request-tags) to use PolicyManager.NodeCanHaveTag() instead of reimplementing tag validation. Changes: - NewHeadscaleDatabase now accepts *types.Config to allow migrations access to policy configuration - Add loadPolicyBytes helper to load policy from file or DB based on config - Add standalone GetPolicy(tx *gorm.DB) for use during migrations - Replace custom tag validation logic with PolicyManager Benefits: - Full HuJSON parsing support (not just JSON) - Proper group expansion via PolicyManager - Support for nested tags and autogroups - Works with both file and database policy modes - Single source of truth for tag validation Co-Authored-By: Shourya Gautam <shouryamgautam@gmail.com>
102 lines
1.8 KiB
Go
102 lines
1.8 KiB
Go
package db
|
|
|
|
import (
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
"github.com/rs/zerolog"
|
|
"zombiezen.com/go/postgrestest"
|
|
)
|
|
|
|
func newSQLiteTestDB() (*HSDatabase, error) {
|
|
tmpDir, err := os.MkdirTemp("", "headscale-db-test-*")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Printf("database path: %s", tmpDir+"/headscale_test.db")
|
|
zerolog.SetGlobalLevel(zerolog.Disabled)
|
|
|
|
db, err := NewHeadscaleDatabase(
|
|
&types.Config{
|
|
Database: types.DatabaseConfig{
|
|
Type: types.DatabaseSqlite,
|
|
Sqlite: types.SqliteConfig{
|
|
Path: tmpDir + "/headscale_test.db",
|
|
},
|
|
},
|
|
Policy: types.PolicyConfig{
|
|
Mode: types.PolicyModeDB,
|
|
},
|
|
},
|
|
emptyCache(),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func newPostgresTestDB(t *testing.T) *HSDatabase {
|
|
t.Helper()
|
|
|
|
return newHeadscaleDBFromPostgresURL(t, newPostgresDBForTest(t))
|
|
}
|
|
|
|
func newPostgresDBForTest(t *testing.T) *url.URL {
|
|
t.Helper()
|
|
|
|
ctx := t.Context()
|
|
srv, err := postgrestest.Start(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(srv.Cleanup)
|
|
|
|
u, err := srv.CreateDatabase(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("created local postgres: %s", u)
|
|
pu, _ := url.Parse(u)
|
|
|
|
return pu
|
|
}
|
|
|
|
func newHeadscaleDBFromPostgresURL(t *testing.T, pu *url.URL) *HSDatabase {
|
|
t.Helper()
|
|
|
|
pass, _ := pu.User.Password()
|
|
port, _ := strconv.Atoi(pu.Port())
|
|
|
|
db, err := NewHeadscaleDatabase(
|
|
&types.Config{
|
|
Database: types.DatabaseConfig{
|
|
Type: types.DatabasePostgres,
|
|
Postgres: types.PostgresConfig{
|
|
Host: pu.Hostname(),
|
|
User: pu.User.Username(),
|
|
Name: strings.TrimLeft(pu.Path, "/"),
|
|
Pass: pass,
|
|
Port: port,
|
|
Ssl: "disable",
|
|
},
|
|
},
|
|
Policy: types.PolicyConfig{
|
|
Mode: types.PolicyModeDB,
|
|
},
|
|
},
|
|
emptyCache(),
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return db
|
|
}
|