2023-05-21 16:37:59 +00:00
|
|
|
package db
|
2021-05-09 15:12:05 +00:00
|
|
|
|
|
|
|
import (
|
2024-11-23 10:19:52 +00:00
|
|
|
"context"
|
2023-11-19 21:37:04 +00:00
|
|
|
"log"
|
2024-11-23 10:19:52 +00:00
|
|
|
"net/url"
|
2021-05-09 15:12:05 +00:00
|
|
|
"os"
|
2024-11-23 10:19:52 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-05-09 15:12:05 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-02-09 06:27:00 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2021-05-09 15:12:05 +00:00
|
|
|
"gopkg.in/check.v1"
|
2024-11-23 10:19:52 +00:00
|
|
|
"zombiezen.com/go/postgrestest"
|
2021-05-09 15:12:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test(t *testing.T) {
|
|
|
|
check.TestingT(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = check.Suite(&Suite{})
|
|
|
|
|
|
|
|
type Suite struct{}
|
|
|
|
|
2021-11-13 08:36:45 +00:00
|
|
|
var (
|
|
|
|
tmpDir string
|
2023-05-21 16:37:59 +00:00
|
|
|
db *HSDatabase
|
2021-11-13 08:36:45 +00:00
|
|
|
)
|
2021-05-09 15:12:05 +00:00
|
|
|
|
|
|
|
func (s *Suite) SetUpTest(c *check.C) {
|
|
|
|
s.ResetDB(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TearDownTest(c *check.C) {
|
2023-11-19 21:37:04 +00:00
|
|
|
// os.RemoveAll(tmpDir)
|
2021-05-09 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) ResetDB(c *check.C) {
|
2023-11-19 21:37:04 +00:00
|
|
|
// if len(tmpDir) != 0 {
|
|
|
|
// os.RemoveAll(tmpDir)
|
|
|
|
// }
|
|
|
|
|
2021-05-09 15:12:05 +00:00
|
|
|
var err error
|
2024-11-23 10:19:52 +00:00
|
|
|
db, err = newSQLiteTestDB()
|
2021-05-09 15:12:05 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
2024-07-18 08:01:59 +00:00
|
|
|
}
|
|
|
|
|
2024-11-23 10:19:52 +00:00
|
|
|
// TODO(kradalby): make this a t.Helper when we dont depend
|
|
|
|
// on check test framework.
|
|
|
|
func newSQLiteTestDB() (*HSDatabase, error) {
|
2024-07-18 08:01:59 +00:00
|
|
|
var err error
|
|
|
|
tmpDir, err = os.MkdirTemp("", "headscale-db-test-*")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-11 07:09:18 +00:00
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
log.Printf("database path: %s", tmpDir+"/headscale_test.db")
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
db, err = NewHeadscaleDatabase(
|
2024-02-09 06:27:00 +00:00
|
|
|
types.DatabaseConfig{
|
2024-11-23 10:19:52 +00:00
|
|
|
Type: types.DatabaseSqlite,
|
2024-02-09 06:27:00 +00:00
|
|
|
Sqlite: types.SqliteConfig{
|
|
|
|
Path: tmpDir + "/headscale_test.db",
|
|
|
|
},
|
|
|
|
},
|
2023-05-11 07:09:18 +00:00
|
|
|
"",
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 12:50:17 +00:00
|
|
|
emptyCache(),
|
2023-05-11 07:09:18 +00:00
|
|
|
)
|
2021-07-04 19:40:46 +00:00
|
|
|
if err != nil {
|
2024-07-18 08:01:59 +00:00
|
|
|
return nil, err
|
2021-07-04 19:40:46 +00:00
|
|
|
}
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
return db, nil
|
2021-05-09 15:12:05 +00:00
|
|
|
}
|
2024-11-23 10:19:52 +00:00
|
|
|
|
|
|
|
func newPostgresTestDB(t *testing.T) *HSDatabase {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
tmpDir, err = os.MkdirTemp("", "headscale-db-test-*")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("database path: %s", tmpDir+"/headscale_test.db")
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
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)
|
|
|
|
|
|
|
|
pass, _ := pu.User.Password()
|
|
|
|
port, _ := strconv.Atoi(pu.Port())
|
|
|
|
|
|
|
|
db, err = NewHeadscaleDatabase(
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
emptyCache(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|