diff --git a/api.go b/api.go index 344878ce..845a3201 100644 --- a/api.go +++ b/api.go @@ -3,6 +3,7 @@ package headscale import ( "encoding/binary" "encoding/json" + "errors" "fmt" "io" "log" @@ -10,9 +11,9 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/jinzhu/gorm" "github.com/klauspost/compress/zstd" "gorm.io/datatypes" + "gorm.io/gorm" "inet.af/netaddr" "tailscale.com/tailcfg" "tailscale.com/wgengine/wgcfg" @@ -80,10 +81,9 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { c.String(http.StatusInternalServerError, ":(") return } - defer db.Close() var m Machine - if db.First(&m, "machine_key = ?", mKey.HexString()).RecordNotFound() { + if result := db.First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) { log.Println("New Machine!") m = Machine{ Expiry: &req.Expiry, @@ -209,9 +209,8 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { log.Printf("Cannot open DB: %s", err) return } - defer db.Close() var m Machine - if db.First(&m, "machine_key = ?", mKey.HexString()).RecordNotFound() { + if result := db.First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) { log.Printf("Ignoring request, cannot find machine with key %s", mKey.HexString()) return } diff --git a/app.go b/app.go index 947980d3..4a846e23 100644 --- a/app.go +++ b/app.go @@ -112,7 +112,6 @@ func (h *Headscale) expireEphemeralNodesWorker() { log.Printf("Cannot open DB: %s", err) return } - defer db.Close() namespaces, err := h.ListNamespaces() if err != nil { diff --git a/app_test.go b/app_test.go index b2515b47..6323655e 100644 --- a/app_test.go +++ b/app_test.go @@ -5,8 +5,6 @@ import ( "os" "testing" - _ "github.com/jinzhu/gorm/dialects/sqlite" // sql driver - "gopkg.in/check.v1" ) diff --git a/cli.go b/cli.go index 9829ac36..31419f3e 100644 --- a/cli.go +++ b/cli.go @@ -4,6 +4,7 @@ import ( "errors" "log" + "gorm.io/gorm" "tailscale.com/wgengine/wgcfg" ) @@ -22,9 +23,8 @@ func (h *Headscale) RegisterMachine(key string, namespace string) (*Machine, err log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() m := Machine{} - if db.First(&m, "machine_key = ?", mKey.HexString()).RecordNotFound() { + if result := db.First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, errors.New("Machine not found") } diff --git a/cli_test.go b/cli_test.go index d15e60fa..3268e1af 100644 --- a/cli_test.go +++ b/cli_test.go @@ -12,7 +12,6 @@ func (s *Suite) TestRegisterMachine(c *check.C) { if err != nil { c.Fatal(err) } - defer db.Close() m := Machine{ ID: 0, diff --git a/db.go b/db.go index 5e08d9bd..d7ce66e1 100644 --- a/db.go +++ b/db.go @@ -3,9 +3,9 @@ package headscale import ( "errors" - "github.com/jinzhu/gorm" - _ "github.com/jinzhu/gorm/dialects/postgres" // sql driver - _ "github.com/jinzhu/gorm/dialects/sqlite" // sql driver + "gorm.io/driver/postgres" + "gorm.io/driver/sqlite" + "gorm.io/gorm" ) const dbVersion = "1" @@ -17,30 +17,53 @@ type KV struct { } func (h *Headscale) initDB() error { - db, err := gorm.Open(h.dbType, h.dbString) + db, err := h.db() if err != nil { return err } if h.dbType == "postgres" { db.Exec("create extension if not exists \"uuid-ossp\";") } - db.AutoMigrate(&Machine{}) - db.AutoMigrate(&KV{}) - db.AutoMigrate(&Namespace{}) - db.AutoMigrate(&PreAuthKey{}) - db.Close() + err = db.AutoMigrate(&Machine{}) + if err != nil { + return err + } + err = db.AutoMigrate(&KV{}) + if err != nil { + return err + } + err = db.AutoMigrate(&Namespace{}) + if err != nil { + return err + } + err = db.AutoMigrate(&PreAuthKey{}) + if err != nil { + return err + } err = h.setValue("db_version", dbVersion) return err } func (h *Headscale) db() (*gorm.DB, error) { - db, err := gorm.Open(h.dbType, h.dbString) + var db *gorm.DB + var err error + switch h.dbType { + case "sqlite3": + db, err = gorm.Open(sqlite.Open(h.dbString), &gorm.Config{ + DisableForeignKeyConstraintWhenMigrating: true, + }) + case "postgres": + db, err = gorm.Open(postgres.Open(h.dbString), &gorm.Config{ + DisableForeignKeyConstraintWhenMigrating: true, + }) + } + if err != nil { return nil, err } if h.dbDebug { - db.LogMode(true) + db.Debug() } return db, nil } @@ -50,9 +73,8 @@ func (h *Headscale) getValue(key string) (string, error) { if err != nil { return "", err } - defer db.Close() var row KV - if db.First(&row, "key = ?", key).RecordNotFound() { + if result := db.First(&row, "key = ?", key); errors.Is(result.Error, gorm.ErrRecordNotFound) { return "", errors.New("not found") } return row.Value, nil @@ -67,7 +89,6 @@ func (h *Headscale) setValue(key string, value string) error { if err != nil { return err } - defer db.Close() _, err = h.getValue(key) if err == nil { db.Model(&kv).Where("key = ?", key).Update("value", value) diff --git a/go.mod b/go.mod index c01ab618..7b8976df 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/gin-gonic/gin v1.7.1 github.com/hako/durafmt v0.0.0-20210316092057-3a2c319c1acd - github.com/jinzhu/gorm v1.9.16 github.com/json-iterator/go v1.1.11 // indirect github.com/klauspost/compress v1.12.2 github.com/kr/text v0.2.0 // indirect @@ -22,6 +21,9 @@ require ( gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c gopkg.in/yaml.v2 v2.4.0 gorm.io/datatypes v1.0.1 + gorm.io/driver/postgres v1.0.8 + gorm.io/driver/sqlite v1.1.4 + gorm.io/gorm v1.21.6 inet.af/netaddr v0.0.0-20210511181906-37180328850c tailscale.com v1.6.0 diff --git a/go.sum b/go.sum index 67566ad9..d342199d 100644 --- a/go.sum +++ b/go.sum @@ -28,7 +28,6 @@ github.com/Microsoft/go-winio v0.4.15-0.20200908182639-5b44b70ab3ab/go.mod h1:tT github.com/Microsoft/hcsshim v0.8.6/go.mod h1:Op3hHsoHPAvb6lceZHDtd9OkTew38wNoXnJs8iY7rUg= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE= @@ -37,7 +36,6 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alexbrainman/sspi v0.0.0-20180613141037-e580b900e9f5/go.mod h1:976q2ETgjT2snVCf2ZaBnyBbVoPERGjUz+0sofzEfro= -github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/apenwarr/fixconsole v0.0.0-20191012055117-5a9f6489cc29/go.mod h1:JYWahgHer+Z2xbsgHPtaDYVWzeHDminu+YIBWkxpCAY= github.com/apenwarr/w32 v0.0.0-20190407065021-aa00fece76ab/go.mod h1:nfFtvHn2Hgs9G1u0/J6LHQv//EksNC+7G8vXmd1VTJ8= @@ -60,6 +58,7 @@ github.com/cilium/ebpf v0.0.0-20200110133405-4032b1d8aae3/go.mod h1:MA5e5Lr8slmE github.com/cilium/ebpf v0.2.0/go.mod h1:To2CFviqOWL/M0gIMsvSMlqe7em/l1ALkX1PyjrX2Qs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/containerd/cgroups v0.0.0-20201119153540-4cbc285b3327/go.mod h1:ZJeTFisyysqgcCdecO57Dj79RfL0LNeGiFUqLYQRYLE= github.com/containerd/console v0.0.0-20191206165004-02ecf6a7291e/go.mod h1:8Pf4gM6VEbTNRIT26AyyU7hxdQU3MvAvxVI0sc00XBE= @@ -87,7 +86,6 @@ github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/denisenkom/go-mssqldb v0.9.0 h1:RSohk2RsiZqLZ0zCjtfn3S4Gp4exhpBWHyQ7D0yGjAk= github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= @@ -108,8 +106,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= @@ -149,6 +145,7 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/flock v0.6.1-0.20180915234121-886344bea079/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= +github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/googleapis v1.4.0/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -260,6 +257,7 @@ github.com/jackc/pgconn v1.8.0 h1:FmjZ0rOyXTr1wfWs45i4a9vjnjWUAGpMuQLD9OSs+lw= github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= +github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2 h1:JVX6jT/XfzNqIjye4717ITLaNwV9mWbJx0dLCpcRzdA= github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= @@ -296,11 +294,8 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= -github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= @@ -347,7 +342,6 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.1 h1:6VXZrLU0jHBYyAqrSPa+MgPfnSvTPuMgK+k0o5kVFWo= @@ -369,7 +363,6 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA= github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= @@ -433,6 +426,7 @@ github.com/peterbourgon/ff/v2 v2.0.0/go.mod h1:xjwr+t+SjWm4L46fcj/D+Ap+6ME7+HqFz github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7/go.mod h1:zO8QMzTeZd5cpnIkz/Gn6iK0jDfGicM1nynOkkPIl28= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -461,6 +455,7 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= +github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc h1:jUIKcSPO9MoMJBbEoyE/RJoE8vz7Mb8AjvifMMwSyvY= github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -557,7 +552,6 @@ golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -592,7 +586,6 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -616,7 +609,6 @@ golang.org/x/net v0.0.0-20191007182048-72f939374954/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= diff --git a/machine.go b/machine.go index 548ce13b..9b15b1a1 100644 --- a/machine.go +++ b/machine.go @@ -159,7 +159,6 @@ func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) { log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() machines := []Machine{} if err = db.Where("namespace_id = ? AND machine_key <> ? AND registered", diff --git a/machine_test.go b/machine_test.go index ef16778d..25c9e951 100644 --- a/machine_test.go +++ b/machine_test.go @@ -15,7 +15,6 @@ func (s *Suite) TestGetMachine(c *check.C) { if err != nil { c.Fatal(err) } - defer db.Close() _, err = h.GetMachine("test", "testmachine") c.Assert(err, check.NotNil) diff --git a/namespaces.go b/namespaces.go index 723fd6b7..0eedad76 100644 --- a/namespaces.go +++ b/namespaces.go @@ -1,10 +1,11 @@ package headscale import ( + "errors" "log" "time" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "tailscale.com/tailcfg" ) @@ -29,7 +30,6 @@ func (h *Headscale) CreateNamespace(name string) (*Namespace, error) { log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() n := Namespace{} if err := db.Where("name = ?", name).First(&n).Error; err == nil { @@ -51,7 +51,6 @@ func (h *Headscale) DestroyNamespace(name string) error { log.Printf("Cannot open DB: %s", err) return err } - defer db.Close() n, err := h.GetNamespace(name) if err != nil { @@ -66,8 +65,7 @@ func (h *Headscale) DestroyNamespace(name string) error { return errorNamespaceNotEmpty } - err = db.Unscoped().Delete(&n).Error - if err != nil { + if result := db.Unscoped().Delete(&n); result.Error != nil { return err } @@ -81,10 +79,9 @@ func (h *Headscale) GetNamespace(name string) (*Namespace, error) { log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() n := Namespace{} - if db.First(&n, "name = ?", name).RecordNotFound() { + if result := db.First(&n, "name = ?", name); errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, errorNamespaceNotFound } return &n, nil @@ -97,7 +94,6 @@ func (h *Headscale) ListNamespaces() (*[]Namespace, error) { log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() namespaces := []Namespace{} if err := db.Find(&namespaces).Error; err != nil { return nil, err @@ -116,7 +112,6 @@ func (h *Headscale) ListMachinesInNamespace(name string) (*[]Machine, error) { log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() machines := []Machine{} if err := db.Preload("AuthKey").Where(&Machine{NamespaceID: n.ID}).Find(&machines).Error; err != nil { @@ -136,7 +131,6 @@ func (h *Headscale) SetMachineNamespace(m *Machine, namespaceName string) error log.Printf("Cannot open DB: %s", err) return err } - defer db.Close() m.NamespaceID = n.ID db.Save(&m) return nil diff --git a/namespaces_test.go b/namespaces_test.go index 64bd176b..fd6045e3 100644 --- a/namespaces_test.go +++ b/namespaces_test.go @@ -34,7 +34,6 @@ func (s *Suite) TestDestroyNamespaceErrors(c *check.C) { if err != nil { c.Fatal(err) } - defer db.Close() m := Machine{ ID: 0, MachineKey: "foo", diff --git a/preauth_keys.go b/preauth_keys.go index 2ec3df72..f0346e68 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -3,8 +3,11 @@ package headscale import ( "crypto/rand" "encoding/hex" + "errors" "log" "time" + + "gorm.io/gorm" ) const errorAuthKeyNotFound = Error("AuthKey not found") @@ -36,7 +39,6 @@ func (h *Headscale) CreatePreAuthKey(namespaceName string, reusable bool, epheme log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() now := time.Now().UTC() kstr, err := h.generateKey() @@ -69,7 +71,6 @@ func (h *Headscale) GetPreAuthKeys(namespaceName string) (*[]PreAuthKey, error) log.Printf("Cannot open DB: %s", err) return nil, err } - defer db.Close() keys := []PreAuthKey{} if err := db.Preload("Namespace").Where(&PreAuthKey{NamespaceID: n.ID}).Find(&keys).Error; err != nil { @@ -85,10 +86,9 @@ func (h *Headscale) checkKeyValidity(k string) (*PreAuthKey, error) { if err != nil { return nil, err } - defer db.Close() pak := PreAuthKey{} - if db.Preload("Namespace").First(&pak, "key = ?", k).RecordNotFound() { + if result := db.Preload("Namespace").First(&pak, "key = ?", k); errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, errorAuthKeyNotFound } diff --git a/preauth_keys_test.go b/preauth_keys_test.go index ebf7ea4e..471e4632 100644 --- a/preauth_keys_test.go +++ b/preauth_keys_test.go @@ -77,7 +77,6 @@ func (*Suite) TestAlreadyUsedKey(c *check.C) { if err != nil { c.Fatal(err) } - defer db.Close() m := Machine{ ID: 0, MachineKey: "foo", @@ -107,7 +106,6 @@ func (*Suite) TestReusableBeingUsedKey(c *check.C) { if err != nil { c.Fatal(err) } - defer db.Close() m := Machine{ ID: 1, MachineKey: "foo", @@ -149,7 +147,6 @@ func (*Suite) TestEphemeralKey(c *check.C) { if err != nil { c.Fatal(err) } - defer db.Close() now := time.Now() m := Machine{ ID: 0, diff --git a/routes.go b/routes.go index 59c32bb7..8b09e3f5 100644 --- a/routes.go +++ b/routes.go @@ -51,7 +51,6 @@ func (h *Headscale) EnableNodeRoute(namespace string, nodeName string, routeStr routes, _ := json.Marshal([]string{routeStr}) // TODO: only one for the time being, so overwriting the rest m.EnabledRoutes = datatypes.JSON(routes) db.Save(&m) - db.Close() // THIS IS COMPLETELY USELESS. // The peers map is stored in memory in the server process. diff --git a/routes_test.go b/routes_test.go index 429ed37e..19dab34e 100644 --- a/routes_test.go +++ b/routes_test.go @@ -20,7 +20,6 @@ func (s *Suite) TestGetRoutes(c *check.C) { if err != nil { c.Fatal(err) } - defer db.Close() _, err = h.GetMachine("test", "testmachine") c.Assert(err, check.NotNil) diff --git a/utils.go b/utils.go index 55310c8d..eff20e2d 100644 --- a/utils.go +++ b/utils.go @@ -18,6 +18,7 @@ import ( mathrand "math/rand" "golang.org/x/crypto/nacl/box" + "gorm.io/gorm" "tailscale.com/wgengine/wgcfg" ) @@ -81,7 +82,6 @@ func (h *Headscale) getAvailableIP() (*net.IP, error) { if err != nil { return nil, err } - defer db.Close() i := 0 for { ip, err := getRandomIP() @@ -89,7 +89,7 @@ func (h *Headscale) getAvailableIP() (*net.IP, error) { return nil, err } m := Machine{} - if db.First(&m, "ip_address = ?", ip.String()).RecordNotFound() { + if result := db.First(&m, "ip_address = ?", ip.String()); errors.Is(result.Error, gorm.ErrRecordNotFound) { return ip, nil } i++