2203fe087495ff26b663a00e9b2b69f6afc0c48b

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

Message

fix(config): use abs paths

Diff

  1diff --git a/cmd/soft/hook.go b/cmd/soft/hook.go
  2index ec51aac5010744696e3c03a9c10bd0f9ee763a14..0e3d78fcc9d3b8c4b9c411baae754366e2b7b8d4 100644
  3--- a/cmd/soft/hook.go
  4+++ b/cmd/soft/hook.go
  5@@ -176,7 +176,7 @@ func commonInit() (c *gossh.Client, s *gossh.Session, err error) {
  6 
  7 func newClient(cfg *config.Config) (*gossh.Client, error) {
  8 	// Only accept the server's host key.
  9-	pk, err := keygen.New(filepath.Join(cfg.DataPath, cfg.SSH.KeyPath), nil, keygen.Ed25519)
 10+	pk, err := keygen.New(cfg.SSH.KeyPath, nil, keygen.Ed25519)
 11 	if err != nil {
 12 		return nil, err
 13 	}
 14@@ -184,7 +184,7 @@ func newClient(cfg *config.Config) (*gossh.Client, error) {
 15 	if err != nil {
 16 		return nil, err
 17 	}
 18-	ik, err := keygen.New(filepath.Join(cfg.DataPath, cfg.SSH.InternalKeyPath), nil, keygen.Ed25519)
 19+	ik, err := keygen.New(cfg.SSH.InternalKeyPath, nil, keygen.Ed25519)
 20 	if err != nil {
 21 		return nil, err
 22 	}
 23diff --git a/cmd/soft/migrate_config.go b/cmd/soft/migrate_config.go
 24index ea03f751367c3430b18467f88a6f3ab6175f140a..61e6a2866dc19d04010d7382c011a8c22bd7db14 100644
 25--- a/cmd/soft/migrate_config.go
 26+++ b/cmd/soft/migrate_config.go
 27@@ -57,7 +57,7 @@ var (
 28 					log.Errorf("failed to copy ssh key: %s", err)
 29 				}
 30 
 31-				cfg.SSH.KeyPath = filepath.Join("ssh", filepath.Base(keyPath))
 32+				cfg.SSH.KeyPath = filepath.Join(cfg.DataPath, "ssh", filepath.Base(keyPath))
 33 			}
 34 
 35 			// Read config
 36diff --git a/server/backend/sqlite/sqlite.go b/server/backend/sqlite/sqlite.go
 37index fe73c57238cba7c6c0abdbdb90e6598daaa9e982..0b64c2aa8e3b00eb8d027d22a4d00f6d501e43b2 100644
 38--- a/server/backend/sqlite/sqlite.go
 39+++ b/server/backend/sqlite/sqlite.go
 40@@ -187,7 +187,7 @@ func (d *SqliteBackend) ImportRepository(name string, remote string, opts backen
 41 				fmt.Sprintf(`GIT_SSH_COMMAND=ssh -o UserKnownHostsFile="%s" -o StrictHostKeyChecking=no -i "%s"`,
 42 					filepath.Join(d.cfg.DataPath, "ssh", "known_hosts"),
 43 					// FIXME: upstream keygen appends _ed25519 to the key path.
 44-					filepath.Join(d.cfg.DataPath, d.cfg.SSH.ClientKeyPath)+"_ed25519",
 45+					d.cfg.SSH.ClientKeyPath+"_ed25519",
 46 				),
 47 			},
 48 		},
 49diff --git a/server/cmd/hook.go b/server/cmd/hook.go
 50index afd4ae2f19d5c2ae8f5387a631e51ac4608edb92..ee071f65ab91e3ec70942fccfb63f7e581693880 100644
 51--- a/server/cmd/hook.go
 52+++ b/server/cmd/hook.go
 53@@ -3,7 +3,6 @@ package cmd
 54 import (
 55 	"bufio"
 56 	"fmt"
 57-	"path/filepath"
 58 	"strings"
 59 
 60 	"github.com/charmbracelet/keygen"
 61@@ -119,7 +118,7 @@ func hookCommand() *cobra.Command {
 62 func checkIfInternal(cmd *cobra.Command, _ []string) error {
 63 	cfg, s := fromContext(cmd)
 64 	pk := s.PublicKey()
 65-	kp, err := keygen.New(filepath.Join(cfg.DataPath, cfg.SSH.InternalKeyPath), nil, keygen.Ed25519)
 66+	kp, err := keygen.New(cfg.SSH.InternalKeyPath, nil, keygen.Ed25519)
 67 	if err != nil {
 68 		logger.Errorf("failed to read internal key: %v", err)
 69 		return err
 70diff --git a/server/config/config.go b/server/config/config.go
 71index 032f566ba50155d1d283423616e3a18dd1900a41..d23d29d6cdeb506325da2c4a49a3e096a5f15a81 100644
 72--- a/server/config/config.go
 73+++ b/server/config/config.go
 74@@ -3,6 +3,7 @@ package config
 75 import (
 76 	"os"
 77 	"path/filepath"
 78+	"strings"
 79 
 80 	"github.com/caarlos0/env/v7"
 81 	"github.com/charmbracelet/log"
 82@@ -115,6 +116,10 @@ func ParseConfig(path string) (*Config, error) {
 83 		return nil, err
 84 	}
 85 
 86+	if err := cfg.init(); err != nil {
 87+		return nil, err
 88+	}
 89+
 90 	return cfg, nil
 91 }
 92 
 93@@ -131,11 +136,6 @@ func DefaultConfig() *Config {
 94 		dataPath = "data"
 95 	}
 96 
 97-	dp, _ := filepath.Abs(dataPath)
 98-	if dp != "" {
 99-		dataPath = dp
100-	}
101-
102 	cfg := &Config{
103 		Name:     "Soft Serve",
104 		DataPath: dataPath,
105@@ -181,6 +181,10 @@ func DefaultConfig() *Config {
106 		log.Fatal(err)
107 	}
108 
109+	if err := cfg.init(); err != nil {
110+		log.Fatal(err)
111+	}
112+
113 	return cfg
114 }
115 
116@@ -189,3 +193,39 @@ func (c *Config) WithBackend(backend backend.Backend) *Config {
117 	c.Backend = backend
118 	return c
119 }
120+
121+func (c *Config) init() error {
122+	// Use absolute paths
123+	if !filepath.IsAbs(c.DataPath) {
124+		dp, err := filepath.Abs(c.DataPath)
125+		if err != nil {
126+			return err
127+		}
128+		c.DataPath = dp
129+	}
130+
131+	c.SSH.PublicURL = strings.TrimSuffix(c.SSH.PublicURL, "/")
132+	c.HTTP.PublicURL = strings.TrimSuffix(c.HTTP.PublicURL, "/")
133+
134+	if c.SSH.KeyPath != "" && !filepath.IsAbs(c.SSH.KeyPath) {
135+		c.SSH.KeyPath = filepath.Join(c.DataPath, c.SSH.KeyPath)
136+	}
137+
138+	if c.SSH.ClientKeyPath != "" && !filepath.IsAbs(c.SSH.ClientKeyPath) {
139+		c.SSH.ClientKeyPath = filepath.Join(c.DataPath, c.SSH.ClientKeyPath)
140+	}
141+
142+	if c.SSH.InternalKeyPath != "" && !filepath.IsAbs(c.SSH.InternalKeyPath) {
143+		c.SSH.InternalKeyPath = filepath.Join(c.DataPath, c.SSH.InternalKeyPath)
144+	}
145+
146+	if c.HTTP.TLSKeyPath != "" && !filepath.IsAbs(c.HTTP.TLSKeyPath) {
147+		c.HTTP.TLSKeyPath = filepath.Join(c.DataPath, c.HTTP.TLSKeyPath)
148+	}
149+
150+	if c.HTTP.TLSCertPath != "" && !filepath.IsAbs(c.HTTP.TLSCertPath) {
151+		c.HTTP.TLSCertPath = filepath.Join(c.DataPath, c.HTTP.TLSCertPath)
152+	}
153+
154+	return nil
155+}
156diff --git a/server/jobs.go b/server/jobs.go
157index daf9a1a62b8487a7774f84a7d90d1235794c89a5..e68c7b83c4527462ef89220c3c46a43f0cd66b16 100644
158--- a/server/jobs.go
159+++ b/server/jobs.go
160@@ -39,7 +39,7 @@ func mirrorJob(cfg *config.Config) func() {
161 					fmt.Sprintf(`GIT_SSH_COMMAND=ssh -o UserKnownHostsFile="%s" -o StrictHostKeyChecking=no -i "%s"`,
162 						filepath.Join(cfg.DataPath, "ssh", "known_hosts"),
163 						// FIXME: upstream keygen appends _ed25519 to the key path.
164-						filepath.Join(cfg.DataPath, cfg.SSH.ClientKeyPath)+"_ed25519",
165+						cfg.SSH.ClientKeyPath+"_ed25519",
166 					),
167 				)
168 				if _, err := cmd.RunInDir(r.Path); err != nil {
169diff --git a/server/server.go b/server/server.go
170index d8be8069717ad7b0175b82ed4c8bf37c4797f617..ab34933a0b928d1070353f75613fd83fa154ee6e 100644
171--- a/server/server.go
172+++ b/server/server.go
173@@ -4,7 +4,6 @@ import (
174 	"context"
175 	"errors"
176 	"net/http"
177-	"path/filepath"
178 
179 	"github.com/charmbracelet/keygen"
180 	"github.com/charmbracelet/log"
181@@ -54,7 +53,7 @@ func NewServer(ctx context.Context, cfg *config.Config) (*Server, error) {
182 
183 		// Create internal key.
184 		ikp, err := keygen.NewWithWrite(
185-			filepath.Join(cfg.DataPath, cfg.SSH.InternalKeyPath),
186+			cfg.SSH.InternalKeyPath,
187 			nil,
188 			keygen.Ed25519,
189 		)
190@@ -65,7 +64,7 @@ func NewServer(ctx context.Context, cfg *config.Config) (*Server, error) {
191 
192 		// Create client key.
193 		ckp, err := keygen.NewWithWrite(
194-			filepath.Join(cfg.DataPath, cfg.SSH.ClientKeyPath),
195+			cfg.SSH.ClientKeyPath,
196 			nil,
197 			keygen.Ed25519,
198 		)
199diff --git a/server/ssh/ssh.go b/server/ssh/ssh.go
200index e3156e6e4974746925760be34892e255db37e715..f49ecd183cf2c49a0f8c1afed84bbcb344e43ae3 100644
201--- a/server/ssh/ssh.go
202+++ b/server/ssh/ssh.go
203@@ -103,7 +103,7 @@ func NewSSHServer(cfg *config.Config, hooks hooks.Hooks) (*SSHServer, error) {
204 		ssh.PublicKeyAuth(s.PublicKeyHandler),
205 		ssh.KeyboardInteractiveAuth(s.KeyboardInteractiveHandler),
206 		wish.WithAddress(cfg.SSH.ListenAddr),
207-		wish.WithHostKeyPath(filepath.Join(cfg.DataPath, cfg.SSH.KeyPath)),
208+		wish.WithHostKeyPath(cfg.SSH.KeyPath),
209 		wish.WithMiddleware(mw...),
210 	)
211 	if err != nil {