diff --git a/cmd/soft/hook.go b/cmd/soft/hook.go index 05a3e2786..9249c53fb 100644 --- a/cmd/soft/hook.go +++ b/cmd/soft/hook.go @@ -21,7 +21,7 @@ import ( var ( configPath string - logFileCtxKey = struct{}{} + dbCtxKey = struct{ string }{"db"} hookCmd = &cobra.Command{ Use: "hook", @@ -37,18 +37,24 @@ var ( ctx = config.WithContext(ctx, cfg) cmd.SetContext(ctx) - db, err := db.Open(ctx, cfg.DB.Driver, cfg.DB.DataSource) + dbx, err := db.Open(ctx, cfg.DB.Driver, cfg.DB.DataSource) if err != nil { return fmt.Errorf("open database: %w", err) } + ctx = db.WithContext(ctx, dbx) + // Set up the backend - sb := backend.New(ctx, cfg, db) + sb := backend.New(ctx, cfg, dbx) ctx = backend.WithContext(ctx, sb) cmd.SetContext(ctx) return nil }, + PersistentPostRunE: func(cmd *cobra.Command, _ []string) error { + db := db.FromContext(cmd.Context()) + return db.Close() + }, } hooksRunE = func(cmd *cobra.Command, args []string) error { diff --git a/cmd/soft/migrate_config.go b/cmd/soft/migrate_config.go index 225c328ad..551ec8420 100644 --- a/cmd/soft/migrate_config.go +++ b/cmd/soft/migrate_config.go @@ -46,6 +46,7 @@ var ( return fmt.Errorf("open database: %w", err) } + defer db.Close() // nolint: errcheck sb := backend.New(ctx, cfg, db) // FIXME: Admin user gets created when the database is created. diff --git a/cmd/soft/root.go b/cmd/soft/root.go index 391d09e61..fd7187b2a 100644 --- a/cmd/soft/root.go +++ b/cmd/soft/root.go @@ -13,6 +13,7 @@ import ( _ "github.com/lib/pq" // postgres driver "github.com/spf13/cobra" "go.uber.org/automaxprocs/maxprocs" + _ "modernc.org/sqlite" // sqlite driver ) diff --git a/cmd/soft/serve.go b/cmd/soft/serve.go index d592f92ac..332069fce 100644 --- a/cmd/soft/serve.go +++ b/cmd/soft/serve.go @@ -57,6 +57,8 @@ var ( return fmt.Errorf("open database: %w", err) } + defer db.Close() // nolint: errcheck + if rollback { if err := migrate.Rollback(ctx, db); err != nil { return fmt.Errorf("rollback error: %w", err) @@ -67,7 +69,7 @@ var ( } } - s, err := server.NewServer(ctx) + s, err := server.NewServer(ctx, db) if err != nil { return fmt.Errorf("start server: %w", err) } diff --git a/examples/setuid/main.go b/examples/setuid/main.go deleted file mode 100644 index 4b6c4770d..000000000 --- a/examples/setuid/main.go +++ /dev/null @@ -1,74 +0,0 @@ -//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris -// +build darwin dragonfly freebsd linux netbsd openbsd solaris - -// This is an example of binding soft-serve ssh port to a restricted port (<1024) and -// then droping root privileges to a different user to run the server. -// Make sure you run this as root. - -package main - -import ( - "context" - "flag" - "fmt" - "net" - "os" - "os/signal" - "syscall" - "time" - - "github.com/charmbracelet/log" - - "github.com/charmbracelet/soft-serve/server" - "github.com/charmbracelet/soft-serve/server/config" -) - -var ( - port = flag.Int("port", 22, "port to listen on") - gid = flag.Int("gid", 1000, "group id to run as") - uid = flag.Int("uid", 1000, "user id to run as") -) - -func main() { - flag.Parse() - addr := fmt.Sprintf(":%d", *port) - // To listen on port 22 we need root privileges - ls, err := net.Listen("tcp", addr) - if err != nil { - log.Fatal("Can't listen", "err", err) - } - // We don't need root privileges any more - if err := syscall.Setgid(*gid); err != nil { - log.Fatal("Setgid error", "err", err) - } - if err := syscall.Setuid(*uid); err != nil { - log.Fatal("Setuid error", "err", err) - } - ctx := context.Background() - cfg := config.DefaultConfig() - ctx = config.WithContext(ctx, cfg) - cfg.SSH.ListenAddr = fmt.Sprintf(":%d", *port) - s, err := server.NewServer(ctx) - if err != nil { - log.Fatal(err) - } - - done := make(chan os.Signal, 1) - signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - - log.Print("Starting SSH server", "addr", cfg.SSH.ListenAddr) - go func() { - if err := s.SSHServer.Serve(ls); err != nil { - log.Fatal(err) - } - }() - - <-done - - log.Print("Stopping SSH server", "addr", cfg.SSH.ListenAddr) - ctx, cancel := context.WithTimeout(ctx, 30*time.Second) - defer func() { cancel() }() - if err := s.Shutdown(ctx); err != nil { - log.Fatal(err) - } -} diff --git a/go.mod b/go.mod index 44852c4e8..31c732b51 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/caarlos0/env/v8 v8.0.0 github.com/charmbracelet/keygen v0.4.3 github.com/charmbracelet/log v0.2.2 - github.com/charmbracelet/ssh v0.0.0-20221117183211-483d43d97103 + github.com/charmbracelet/ssh v0.0.0-20230712221603-7e03c5063afc github.com/gobwas/glob v0.2.3 github.com/gogs/git-module v1.8.2 github.com/hashicorp/golang-lru/v2 v2.0.4 diff --git a/go.sum b/go.sum index 1744b5f06..ec80a32f5 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,8 @@ github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZ github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c= github.com/charmbracelet/log v0.2.2 h1:CaXgos+ikGn5tcws5Cw3paQuk9e/8bIwuYGhnkqQFjo= github.com/charmbracelet/log v0.2.2/go.mod h1:Zs11hKpb8l+UyX4y1srwZIGW+MPCXJHIty3MB9l/sno= -github.com/charmbracelet/ssh v0.0.0-20221117183211-483d43d97103 h1:wpHMERIN0pQZE635jWwT1dISgfjbpUcEma+fbPKSMCU= -github.com/charmbracelet/ssh v0.0.0-20221117183211-483d43d97103/go.mod h1:0Vm2/8yBljiLDnGJHU8ehswfawrEybGk33j5ssqKQVM= +github.com/charmbracelet/ssh v0.0.0-20230712221603-7e03c5063afc h1:JUm+5HigAM5utFiThwIDX9iU0BaheKpuNVr+umi3sFg= +github.com/charmbracelet/ssh v0.0.0-20230712221603-7e03c5063afc/go.mod h1:F1vgddWsb/Yr/OZilFeRZEh5sE/qU0Dt1mKkmke6Zvg= github.com/charmbracelet/wish v1.1.1 h1:KdICASKd2oh2JPvk1Z4CJtAi97cFErXF7NKienPICO4= github.com/charmbracelet/wish v1.1.1/go.mod h1:xh4KZpSULw+Xqb9bcbhw92QAinVB75CVLWrFuyY6IVs= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= @@ -189,14 +189,14 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/server/backend/repo.go b/server/backend/repo.go index 412a02119..bd6479c54 100644 --- a/server/backend/repo.go +++ b/server/backend/repo.go @@ -177,7 +177,6 @@ func (d *Backend) RenameRepository(ctx context.Context, oldName string, newName func (d *Backend) Repositories(ctx context.Context) ([]store.Repository, error) { repos := make([]store.Repository, 0) - d.logger.Debugf("get all repositories %v %v %v", ctx, d.db, d.store) if err := d.db.TransactionContext(ctx, func(tx *db.Tx) error { ms, err := d.store.GetAllRepos(ctx, tx) if err != nil { diff --git a/server/backend/user.go b/server/backend/user.go index df54640a8..438267f25 100644 --- a/server/backend/user.go +++ b/server/backend/user.go @@ -86,7 +86,7 @@ func (d *Backend) User(ctx context.Context, username string) (store.User, error) var m models.User var pks []ssh.PublicKey - if err := d.db.TransactionContext(context.Background(), func(tx *db.Tx) error { + if err := d.db.TransactionContext(ctx, func(tx *db.Tx) error { var err error m, err = d.store.FindUserByUsername(ctx, tx, username) if err != nil { @@ -111,7 +111,7 @@ func (d *Backend) User(ctx context.Context, username string) (store.User, error) func (d *Backend) UserByPublicKey(ctx context.Context, pk ssh.PublicKey) (store.User, error) { var m models.User var pks []ssh.PublicKey - if err := d.db.TransactionContext(context.Background(), func(tx *db.Tx) error { + if err := d.db.TransactionContext(ctx, func(tx *db.Tx) error { var err error m, err = d.store.FindUserByPublicKey(ctx, tx, pk) if err != nil { diff --git a/server/daemon/daemon.go b/server/daemon/daemon.go index 62e395739..001c9ad77 100644 --- a/server/daemon/daemon.go +++ b/server/daemon/daemon.go @@ -95,7 +95,7 @@ func (d *GitDaemon) Start() error { default: d.logger.Debugf("git: error accepting connection: %v", err) } - if ne, ok := err.(net.Error); ok && ne.Temporary() { + if ne, ok := err.(net.Error); ok && ne.Temporary() { // nolint: staticcheck if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { @@ -147,7 +147,7 @@ func (d *GitDaemon) handleClient(conn net.Conn) { } d.conns.Add(c) defer func() { - d.conns.Close(c) + d.conns.Close(c) // nolint: errcheck }() readc := make(chan struct{}, 1) @@ -303,7 +303,7 @@ func (d *GitDaemon) handleClient(conn net.Conn) { func (d *GitDaemon) Close() error { d.once.Do(func() { close(d.finished) }) err := d.listener.Close() - d.conns.CloseAll() + d.conns.CloseAll() // nolint: errcheck return err } diff --git a/server/daemon/daemon_test.go b/server/daemon/daemon_test.go index 3e4b33f1b..06987405d 100644 --- a/server/daemon/daemon_test.go +++ b/server/daemon/daemon_test.go @@ -42,6 +42,7 @@ func TestMain(m *testing.M) { if err != nil { log.Fatal(err) } + defer db.Close() // nolint: errcheck if err := migrate.Migrate(ctx, db); err != nil { log.Fatal(err) } diff --git a/server/db/context.go b/server/db/context.go new file mode 100644 index 000000000..17c70ee49 --- /dev/null +++ b/server/db/context.go @@ -0,0 +1,18 @@ +package db + +import "context" + +var contextKey = struct{ string }{"db"} + +// FromContext returns the database from the context. +func FromContext(ctx context.Context) *DB { + if db, ok := ctx.Value(contextKey).(*DB); ok { + return db + } + return nil +} + +// WithContext returns a new context with the database. +func WithContext(ctx context.Context, db *DB) context.Context { + return context.WithValue(ctx, contextKey, db) +} diff --git a/server/db/db.go b/server/db/db.go index 587880dc6..96f96e5da 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -48,6 +48,8 @@ type Tx struct { logger *log.Logger } +var txContextKey = struct{ string }{"tx"} + // Transaction implements db.DB. func (d *DB) Transaction(fn func(tx *Tx) error) error { return d.TransactionContext(context.Background(), fn) diff --git a/server/server.go b/server/server.go index 1ccc9b16a..9e7651c1b 100644 --- a/server/server.go +++ b/server/server.go @@ -40,13 +40,9 @@ type Server struct { // key can be provided with authKey. If authKey is provided, access will be // restricted to that key. If authKey is not provided, the server will be // publicly writable until configured otherwise by cloning the `config` repo. -func NewServer(ctx context.Context) (*Server, error) { +func NewServer(ctx context.Context, db *db.DB) (*Server, error) { + var err error cfg := config.FromContext(ctx) - db, err := db.Open(ctx, cfg.DB.Driver, cfg.DB.DataSource) - if err != nil { - return nil, fmt.Errorf("open database: %w", err) - } - be := backend.New(ctx, cfg, db) ctx = backend.WithContext(ctx, be) srv := &Server{ @@ -84,47 +80,33 @@ func NewServer(ctx context.Context) (*Server, error) { return srv, nil } -func start(ctx context.Context, fn func() error) error { - errc := make(chan error, 1) - go func() { - errc <- fn() - }() - - select { - case err := <-errc: - return err - case <-ctx.Done(): - return ctx.Err() - } -} - // Start starts the SSH server. func (s *Server) Start() error { - errg, ctx := errgroup.WithContext(s.ctx) + errg, _ := errgroup.WithContext(s.ctx) errg.Go(func() error { s.logger.Print("Starting Git daemon", "addr", s.Config.Git.ListenAddr) - if err := start(ctx, s.GitDaemon.Start); !errors.Is(err, daemon.ErrServerClosed) { + if err := s.GitDaemon.Start(); !errors.Is(err, daemon.ErrServerClosed) { return err } return nil }) errg.Go(func() error { s.logger.Print("Starting HTTP server", "addr", s.Config.HTTP.ListenAddr) - if err := start(ctx, s.HTTPServer.ListenAndServe); !errors.Is(err, http.ErrServerClosed) { + if err := s.HTTPServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { return err } return nil }) errg.Go(func() error { s.logger.Print("Starting SSH server", "addr", s.Config.SSH.ListenAddr) - if err := start(ctx, s.SSHServer.ListenAndServe); !errors.Is(err, ssh.ErrServerClosed) { + if err := s.SSHServer.ListenAndServe(); !errors.Is(err, ssh.ErrServerClosed) { return err } return nil }) errg.Go(func() error { s.logger.Print("Starting Stats server", "addr", s.Config.Stats.ListenAddr) - if err := start(ctx, s.StatsServer.ListenAndServe); !errors.Is(err, http.ErrServerClosed) { + if err := s.StatsServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { return err } return nil @@ -138,7 +120,7 @@ func (s *Server) Start() error { // Shutdown lets the server gracefully shutdown. func (s *Server) Shutdown(ctx context.Context) error { - var errg errgroup.Group + errg, ctx := errgroup.WithContext(ctx) errg.Go(func() error { return s.GitDaemon.Shutdown(ctx) }) @@ -155,7 +137,7 @@ func (s *Server) Shutdown(ctx context.Context) error { s.Cron.Stop() return nil }) - defer s.DB.Close() // nolint: errcheck + // defer s.DB.Close() // nolint: errcheck return errg.Wait() } @@ -170,6 +152,6 @@ func (s *Server) Close() error { s.Cron.Stop() return nil }) - defer s.DB.Close() // nolint: errcheck + // defer s.DB.Close() // nolint: errcheck return errg.Wait() } diff --git a/server/server_test.go b/server/server_test.go deleted file mode 100644 index a856f1f4c..000000000 --- a/server/server_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package server - -import ( - "context" - "fmt" - "path/filepath" - "strings" - "testing" - - "github.com/charmbracelet/keygen" - "github.com/charmbracelet/soft-serve/server/config" - "github.com/charmbracelet/soft-serve/server/test" - "github.com/charmbracelet/ssh" - "github.com/matryer/is" - gossh "golang.org/x/crypto/ssh" -) - -func setupServer(tb testing.TB) (*Server, *config.Config, string) { - tb.Helper() - tb.Log("creating keypair") - pub, pkPath := createKeyPair(tb) - dp := tb.TempDir() - sshPort := fmt.Sprintf(":%d", test.RandomPort()) - tb.Setenv("SOFT_SERVE_DATA_PATH", dp) - tb.Setenv("SOFT_SERVE_INITIAL_ADMIN_KEY", authorizedKey(pub)) - tb.Setenv("SOFT_SERVE_SSH_LISTEN_ADDR", sshPort) - tb.Setenv("SOFT_SERVE_GIT_LISTEN_ADDR", fmt.Sprintf(":%d", test.RandomPort())) - ctx := context.TODO() - cfg := config.DefaultConfig() - ctx = config.WithContext(ctx, cfg) - tb.Log("configuring server") - s, err := NewServer(ctx) - if err != nil { - tb.Fatal(err) - } - go func() { - tb.Log("starting server") - s.Start() - }() - tb.Cleanup(func() { - s.Close() - }) - return s, cfg, pkPath -} - -func createKeyPair(tb testing.TB) (ssh.PublicKey, string) { - tb.Helper() - is := is.New(tb) - keyDir := tb.TempDir() - fp := filepath.Join(keyDir, "id_ed25519") - kp, err := keygen.New(fp, keygen.WithKeyType(keygen.Ed25519), keygen.WithWrite()) - is.NoErr(err) - return kp.PublicKey(), fp -} - -func authorizedKey(pk ssh.PublicKey) string { - return strings.TrimSpace(string(gossh.MarshalAuthorizedKey(pk))) -} diff --git a/server/store/database/collab.go b/server/store/database/collab.go index 572d09ccd..50424445e 100644 --- a/server/store/database/collab.go +++ b/server/store/database/collab.go @@ -14,7 +14,7 @@ type collabStore struct{} var _ store.CollaboratorStore = (*collabStore)(nil) -// AddCollabByUsernameAndRepo implements store.CollaboratorStore +// AddCollabByUsernameAndRepo implements store.CollaboratorStore. func (*collabStore) AddCollabByUsernameAndRepo(ctx context.Context, tx *db.Tx, username string, repo string) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { @@ -37,7 +37,7 @@ func (*collabStore) AddCollabByUsernameAndRepo(ctx context.Context, tx *db.Tx, u return err } -// GetCollabByUsernameAndRepo implements store.CollaboratorStore +// GetCollabByUsernameAndRepo implements store.CollaboratorStore. func (*collabStore) GetCollabByUsernameAndRepo(ctx context.Context, tx *db.Tx, username string, repo string) (models.Collab, error) { var m models.Collab @@ -62,7 +62,7 @@ func (*collabStore) GetCollabByUsernameAndRepo(ctx context.Context, tx *db.Tx, u return m, err } -// ListCollabsByRepo implements store.CollaboratorStore +// ListCollabsByRepo implements store.CollaboratorStore. func (*collabStore) ListCollabsByRepo(ctx context.Context, tx *db.Tx, repo string) ([]models.Collab, error) { var m []models.Collab @@ -101,7 +101,7 @@ func (*collabStore) ListCollabsByRepoAsUsers(ctx context.Context, tx *db.Tx, rep return m, err } -// RemoveCollabByUsernameAndRepo implements store.CollaboratorStore +// RemoveCollabByUsernameAndRepo implements store.CollaboratorStore. func (*collabStore) RemoveCollabByUsernameAndRepo(ctx context.Context, tx *db.Tx, username string, repo string) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { diff --git a/server/store/database/repo.go b/server/store/database/repo.go index 4a3eb8fbd..76436b9bc 100644 --- a/server/store/database/repo.go +++ b/server/store/database/repo.go @@ -13,7 +13,7 @@ type repoStore struct{} var _ store.RepositoryStore = (*repoStore)(nil) -// CreateRepo implements store.RepositoryStore +// CreateRepo implements store.RepositoryStore. func (*repoStore) CreateRepo(ctx context.Context, tx *db.Tx, name string, projectName string, description string, isPrivate bool, isHidden bool, isMirror bool) error { name = utils.SanitizeRepo(name) query := tx.Rebind(`INSERT INTO repos (name, project_name, description, private, mirror, hidden, updated_at) @@ -23,7 +23,7 @@ func (*repoStore) CreateRepo(ctx context.Context, tx *db.Tx, name string, projec return db.WrapError(err) } -// DeleteRepoByName implements store.RepositoryStore +// DeleteRepoByName implements store.RepositoryStore. func (*repoStore) DeleteRepoByName(ctx context.Context, tx *db.Tx, name string) error { name = utils.SanitizeRepo(name) query := tx.Rebind("DELETE FROM repos WHERE name = ?;") @@ -31,7 +31,7 @@ func (*repoStore) DeleteRepoByName(ctx context.Context, tx *db.Tx, name string) return db.WrapError(err) } -// GetAllRepos implements store.RepositoryStore +// GetAllRepos implements store.RepositoryStore. func (*repoStore) GetAllRepos(ctx context.Context, tx *db.Tx) ([]models.Repo, error) { var repos []models.Repo query := tx.Rebind("SELECT * FROM repos;") @@ -39,7 +39,7 @@ func (*repoStore) GetAllRepos(ctx context.Context, tx *db.Tx) ([]models.Repo, er return repos, db.WrapError(err) } -// GetRepoByName implements store.RepositoryStore +// GetRepoByName implements store.RepositoryStore. func (*repoStore) GetRepoByName(ctx context.Context, tx *db.Tx, name string) (models.Repo, error) { var repo models.Repo name = utils.SanitizeRepo(name) @@ -48,7 +48,7 @@ func (*repoStore) GetRepoByName(ctx context.Context, tx *db.Tx, name string) (mo return repo, db.WrapError(err) } -// GetRepoDescriptionByName implements store.RepositoryStore +// GetRepoDescriptionByName implements store.RepositoryStore. func (*repoStore) GetRepoDescriptionByName(ctx context.Context, tx *db.Tx, name string) (string, error) { var description string name = utils.SanitizeRepo(name) @@ -57,7 +57,7 @@ func (*repoStore) GetRepoDescriptionByName(ctx context.Context, tx *db.Tx, name return description, db.WrapError(err) } -// GetRepoIsHiddenByName implements store.RepositoryStore +// GetRepoIsHiddenByName implements store.RepositoryStore. func (*repoStore) GetRepoIsHiddenByName(ctx context.Context, tx *db.Tx, name string) (bool, error) { var isHidden bool name = utils.SanitizeRepo(name) @@ -66,7 +66,7 @@ func (*repoStore) GetRepoIsHiddenByName(ctx context.Context, tx *db.Tx, name str return isHidden, db.WrapError(err) } -// GetRepoIsMirrorByName implements store.RepositoryStore +// GetRepoIsMirrorByName implements store.RepositoryStore. func (*repoStore) GetRepoIsMirrorByName(ctx context.Context, tx *db.Tx, name string) (bool, error) { var isMirror bool name = utils.SanitizeRepo(name) @@ -75,7 +75,7 @@ func (*repoStore) GetRepoIsMirrorByName(ctx context.Context, tx *db.Tx, name str return isMirror, db.WrapError(err) } -// GetRepoIsPrivateByName implements store.RepositoryStore +// GetRepoIsPrivateByName implements store.RepositoryStore. func (*repoStore) GetRepoIsPrivateByName(ctx context.Context, tx *db.Tx, name string) (bool, error) { var isPrivate bool name = utils.SanitizeRepo(name) @@ -84,7 +84,7 @@ func (*repoStore) GetRepoIsPrivateByName(ctx context.Context, tx *db.Tx, name st return isPrivate, db.WrapError(err) } -// GetRepoProjectNameByName implements store.RepositoryStore +// GetRepoProjectNameByName implements store.RepositoryStore. func (*repoStore) GetRepoProjectNameByName(ctx context.Context, tx *db.Tx, name string) (string, error) { var pname string name = utils.SanitizeRepo(name) @@ -93,7 +93,7 @@ func (*repoStore) GetRepoProjectNameByName(ctx context.Context, tx *db.Tx, name return pname, db.WrapError(err) } -// SetRepoDescriptionByName implements store.RepositoryStore +// SetRepoDescriptionByName implements store.RepositoryStore. func (*repoStore) SetRepoDescriptionByName(ctx context.Context, tx *db.Tx, name string, description string) error { name = utils.SanitizeRepo(name) query := tx.Rebind("UPDATE repos SET description = ? WHERE name = ?;") @@ -101,7 +101,7 @@ func (*repoStore) SetRepoDescriptionByName(ctx context.Context, tx *db.Tx, name return db.WrapError(err) } -// SetRepoIsHiddenByName implements store.RepositoryStore +// SetRepoIsHiddenByName implements store.RepositoryStore. func (*repoStore) SetRepoIsHiddenByName(ctx context.Context, tx *db.Tx, name string, isHidden bool) error { name = utils.SanitizeRepo(name) query := tx.Rebind("UPDATE repos SET hidden = ? WHERE name = ?;") @@ -109,7 +109,7 @@ func (*repoStore) SetRepoIsHiddenByName(ctx context.Context, tx *db.Tx, name str return db.WrapError(err) } -// SetRepoIsPrivateByName implements store.RepositoryStore +// SetRepoIsPrivateByName implements store.RepositoryStore. func (*repoStore) SetRepoIsPrivateByName(ctx context.Context, tx *db.Tx, name string, isPrivate bool) error { name = utils.SanitizeRepo(name) query := tx.Rebind("UPDATE repos SET private = ? WHERE name = ?;") @@ -117,7 +117,7 @@ func (*repoStore) SetRepoIsPrivateByName(ctx context.Context, tx *db.Tx, name st return db.WrapError(err) } -// SetRepoNameByName implements store.RepositoryStore +// SetRepoNameByName implements store.RepositoryStore. func (*repoStore) SetRepoNameByName(ctx context.Context, tx *db.Tx, name string, newName string) error { name = utils.SanitizeRepo(name) newName = utils.SanitizeRepo(newName) @@ -126,7 +126,7 @@ func (*repoStore) SetRepoNameByName(ctx context.Context, tx *db.Tx, name string, return db.WrapError(err) } -// SetRepoProjectNameByName implements store.RepositoryStore +// SetRepoProjectNameByName implements store.RepositoryStore. func (*repoStore) SetRepoProjectNameByName(ctx context.Context, tx *db.Tx, name string, projectName string) error { name = utils.SanitizeRepo(name) query := tx.Rebind("UPDATE repos SET project_name = ? WHERE name = ?;") diff --git a/server/store/database/user.go b/server/store/database/user.go index 98fe146bd..2e3a70beb 100644 --- a/server/store/database/user.go +++ b/server/store/database/user.go @@ -16,7 +16,7 @@ type userStore struct{} var _ store.UserStore = (*userStore)(nil) -// AddPublicKeyByUsername implements store.UserStore +// AddPublicKeyByUsername implements store.UserStore. func (*userStore) AddPublicKeyByUsername(ctx context.Context, tx *db.Tx, username string, pk ssh.PublicKey) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { @@ -36,7 +36,7 @@ func (*userStore) AddPublicKeyByUsername(ctx context.Context, tx *db.Tx, usernam return err } -// CreateUser implements store.UserStore +// CreateUser implements store.UserStore. func (*userStore) CreateUser(ctx context.Context, tx *db.Tx, username string, isAdmin bool, pks []ssh.PublicKey) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { @@ -68,7 +68,7 @@ func (*userStore) CreateUser(ctx context.Context, tx *db.Tx, username string, is return nil } -// DeleteUserByUsername implements store.UserStore +// DeleteUserByUsername implements store.UserStore. func (*userStore) DeleteUserByUsername(ctx context.Context, tx *db.Tx, username string) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { @@ -80,7 +80,7 @@ func (*userStore) DeleteUserByUsername(ctx context.Context, tx *db.Tx, username return err } -// FindUserByPublicKey implements store.UserStore +// FindUserByPublicKey implements store.UserStore. func (*userStore) FindUserByPublicKey(ctx context.Context, tx *db.Tx, pk ssh.PublicKey) (models.User, error) { var m models.User query := tx.Rebind(`SELECT users.* @@ -91,7 +91,7 @@ func (*userStore) FindUserByPublicKey(ctx context.Context, tx *db.Tx, pk ssh.Pub return m, err } -// FindUserByUsername implements store.UserStore +// FindUserByUsername implements store.UserStore. func (*userStore) FindUserByUsername(ctx context.Context, tx *db.Tx, username string) (models.User, error) { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { @@ -104,7 +104,7 @@ func (*userStore) FindUserByUsername(ctx context.Context, tx *db.Tx, username st return m, err } -// GetAllUsers implements store.UserStore +// GetAllUsers implements store.UserStore. func (*userStore) GetAllUsers(ctx context.Context, tx *db.Tx) ([]models.User, error) { var ms []models.User query := tx.Rebind(`SELECT * FROM users;`) @@ -112,7 +112,7 @@ func (*userStore) GetAllUsers(ctx context.Context, tx *db.Tx) ([]models.User, er return ms, err } -// ListPublicKeysByUserID implements store.UserStore. +// ListPublicKeysByUserID implements store.UserStore.. func (*userStore) ListPublicKeysByUserID(ctx context.Context, tx *db.Tx, id int64) ([]ssh.PublicKey, error) { var aks []string query := tx.Rebind(`SELECT public_key FROM public_keys @@ -164,7 +164,7 @@ func (*userStore) ListPublicKeysByUsername(ctx context.Context, tx *db.Tx, usern return pks, nil } -// RemovePublicKeyByUsername implements store.UserStore +// RemovePublicKeyByUsername implements store.UserStore. func (*userStore) RemovePublicKeyByUsername(ctx context.Context, tx *db.Tx, username string, pk ssh.PublicKey) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { @@ -178,7 +178,7 @@ func (*userStore) RemovePublicKeyByUsername(ctx context.Context, tx *db.Tx, user return err } -// SetAdminByUsername implements store.UserStore +// SetAdminByUsername implements store.UserStore. func (*userStore) SetAdminByUsername(ctx context.Context, tx *db.Tx, username string, isAdmin bool) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { @@ -190,7 +190,7 @@ func (*userStore) SetAdminByUsername(ctx context.Context, tx *db.Tx, username st return err } -// SetUsernameByUsername implements store.UserStore +// SetUsernameByUsername implements store.UserStore. func (*userStore) SetUsernameByUsername(ctx context.Context, tx *db.Tx, username string, newUsername string) error { username = strings.ToLower(username) if err := utils.ValidateUsername(username); err != nil { diff --git a/testscript/script_test.go b/testscript/script_test.go index 911e1f095..5a4b9e61b 100644 --- a/testscript/script_test.go +++ b/testscript/script_test.go @@ -22,7 +22,7 @@ import ( "github.com/charmbracelet/soft-serve/server/test" "github.com/rogpeppe/go-internal/testscript" "golang.org/x/crypto/ssh" - _ "modernc.org/sqlite" + _ "modernc.org/sqlite" // sqlite Driver ) const dbOpts = "?_pragma=busy_timeout(5000)&_pragma=foreign_keys(1)" @@ -118,7 +118,7 @@ func TestScript(t *testing.T) { // from the ssh session instead of the server. // XXX: take another look at this soon lock.Lock() - srv, err := server.NewServer(ctx) + srv, err := server.NewServer(ctx, db) if err != nil { return err } @@ -131,6 +131,7 @@ func TestScript(t *testing.T) { }() e.Defer(func() { + defer db.Close() // nolint: errcheck ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil {