3da027a0d5d8aeef105a32d4be40149f38eeb12e

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

refactor: move file backend settings path and set default ssh keypath

Diff

This diff is truncated to protect this page.

  1diff --git a/server/backend/file/file.go b/server/backend/file/file.go
  2index c29bc325833599241debe04b040bcdd383570c9c..cc0281f5e7699686d02ebab61f90e5b8d5d0b6e7 100644
  3--- a/server/backend/file/file.go
  4+++ b/server/backend/file/file.go
  5@@ -49,6 +49,7 @@ const (
  6 	collabs      = "collaborators"
  7 	description  = "description"
  8 	private      = "private"
  9+	settings     = "settings"
 10 )
 11 
 12 var (
 13@@ -80,8 +81,12 @@ func (fb *FileBackend) reposPath() string {
 14 	return filepath.Join(fb.path, repos)
 15 }
 16 
 17+func (fb *FileBackend) settingsPath() string {
 18+	return filepath.Join(fb.path, settings)
 19+}
 20+
 21 func (fb *FileBackend) adminsPath() string {
 22-	return filepath.Join(fb.path, admins)
 23+	return filepath.Join(fb.settingsPath(), admins)
 24 }
 25 
 26 func (fb *FileBackend) collabsPath(repo string) string {
 27@@ -116,13 +121,13 @@ func readAll(path string) (string, error) {
 28 // NewFileBackend creates a new FileBackend.
 29 func NewFileBackend(path string) (*FileBackend, error) {
 30 	fb := &FileBackend{path: path}
 31-	for _, dir := range []string{repos} {
 32+	for _, dir := range []string{repos, settings} {
 33 		if err := os.MkdirAll(filepath.Join(path, dir), 0755); err != nil {
 34 			return nil, err
 35 		}
 36 	}
 37 	for _, file := range []string{admins, anonAccess, allowKeyless, serverHost, serverName, serverPort} {
 38-		fp := filepath.Join(path, file)
 39+		fp := filepath.Join(fb.settingsPath(), file)
 40 		_, err := os.Stat(fp)
 41 		if errors.Is(err, fs.ErrNotExist) {
 42 			f, err := os.Create(fp)
 43@@ -221,7 +226,7 @@ func (fb *FileBackend) AddCollaborator(pk gossh.PublicKey, repo string) error {
 44 //
 45 // It implements backend.Backend.
 46 func (fb *FileBackend) AllowKeyless() bool {
 47-	line, err := readOneLine(filepath.Join(fb.path, allowKeyless))
 48+	line, err := readOneLine(filepath.Join(fb.settingsPath(), allowKeyless))
 49 	if err != nil {
 50 		logger.Debug("failed to read allow-keyless file", "err", err)
 51 		return false
 52@@ -234,7 +239,7 @@ func (fb *FileBackend) AllowKeyless() bool {
 53 //
 54 // It implements backend.Backend.
 55 func (fb *FileBackend) AnonAccess() backend.AccessLevel {
 56-	line, err := readOneLine(filepath.Join(fb.path, anonAccess))
 57+	line, err := readOneLine(filepath.Join(fb.settingsPath(), anonAccess))
 58 	if err != nil {
 59 		logger.Debug("failed to read anon-access file", "err", err)
 60 		return backend.NoAccess
 61@@ -336,7 +341,7 @@ func (fb *FileBackend) IsPrivate(repo string) bool {
 62 //
 63 // It implements backend.Backend.
 64 func (fb *FileBackend) ServerHost() string {
 65-	line, err := readOneLine(filepath.Join(fb.path, serverHost))
 66+	line, err := readOneLine(filepath.Join(fb.settingsPath(), serverHost))
 67 	if err != nil {
 68 		logger.Debug("failed to read server-host file", "err", err)
 69 		return ""
 70@@ -349,7 +354,7 @@ func (fb *FileBackend) ServerHost() string {
 71 //
 72 // It implements backend.Backend.
 73 func (fb *FileBackend) ServerName() string {
 74-	line, err := readOneLine(filepath.Join(fb.path, serverName))
 75+	line, err := readOneLine(filepath.Join(fb.settingsPath(), serverName))
 76 	if err != nil {
 77 		logger.Debug("failed to read server-name file", "err", err)
 78 		return ""
 79@@ -362,7 +367,7 @@ func (fb *FileBackend) ServerName() string {
 80 //
 81 // It implements backend.Backend.
 82 func (fb *FileBackend) ServerPort() string {
 83-	line, err := readOneLine(filepath.Join(fb.path, serverPort))
 84+	line, err := readOneLine(filepath.Join(fb.settingsPath(), serverPort))
 85 	if err != nil {
 86 		logger.Debug("failed to read server-port file", "err", err)
 87 		return ""
 88@@ -380,7 +385,7 @@ func (fb *FileBackend) ServerPort() string {
 89 //
 90 // It implements backend.Backend.
 91 func (fb *FileBackend) SetAllowKeyless(allow bool) error {
 92-	f, err := os.OpenFile(filepath.Join(fb.path, allowKeyless), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 93+	f, err := os.OpenFile(filepath.Join(fb.settingsPath(), allowKeyless), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 94 	if err != nil {
 95 		return fmt.Errorf("failed to open allow-keyless file: %w", err)
 96 	}
 97@@ -394,7 +399,7 @@ func (fb *FileBackend) SetAllowKeyless(allow bool) error {
 98 //
 99 // It implements backend.Backend.
100 func (fb *FileBackend) SetAnonAccess(level backend.AccessLevel) error {
101-	f, err := os.OpenFile(filepath.Join(fb.path, anonAccess), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
102+	f, err := os.OpenFile(filepath.Join(fb.settingsPath(), anonAccess), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
103 	if err != nil {
104 		return fmt.Errorf("failed to open anon-access file: %w", err)
105diff --git a/server/config/config.go b/server/config/config.go
106index a7bc67f9c1aee4d7047ba77c707885878dafac9c..05f008e57ff29a4ecadf20fb5ad411703d040fd7 100644
107--- a/server/config/config.go
108+++ b/server/config/config.go
109@@ -1,6 +1,8 @@
110 package config
111 
112 import (
113+	"path/filepath"
114+
115 	"github.com/caarlos0/env/v6"
116 	"github.com/charmbracelet/log"
117 	"github.com/charmbracelet/soft-serve/server/backend"
118@@ -13,7 +15,7 @@ type SSHConfig struct {
119 	ListenAddr string `env:"LISTEN_ADDR" envDefault:":23231"`
120 
121 	// KeyPath is the path to the SSH server's private key.
122-	KeyPath string `env:"KEY_PATH" envDefault:"soft_serve"`
123+	KeyPath string `env:"KEY_PATH"`
124 
125 	// MaxTimeout is the maximum number of seconds a connection can take.
126 	MaxTimeout int `env:"MAX_TIMEOUT" envDefault:"0"`
127@@ -67,6 +69,9 @@ func DefaultConfig() *Config {
128 	}); err != nil {
129 		log.Fatal(err)
130 	}
131+	if cfg.SSH.KeyPath == "" {
132+		cfg.SSH.KeyPath = filepath.Join(cfg.DataPath, "ssh", "soft_serve")
133+	}
134 	fb, err := file.NewFileBackend(cfg.DataPath)
135 	if err != nil {
136 		log.Fatal(err)