Skip to content

Commit

Permalink
fix(config): use abs paths
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 7498560 commit 2203fe0
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmd/soft/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ func commonInit() (c *gossh.Client, s *gossh.Session, err error) {

func newClient(cfg *config.Config) (*gossh.Client, error) {
// Only accept the server's host key.
pk, err := keygen.New(filepath.Join(cfg.DataPath, cfg.SSH.KeyPath), nil, keygen.Ed25519)
pk, err := keygen.New(cfg.SSH.KeyPath, nil, keygen.Ed25519)
if err != nil {
return nil, err
}
hostKey, err := gossh.ParsePrivateKey(pk.PrivateKeyPEM())
if err != nil {
return nil, err
}
ik, err := keygen.New(filepath.Join(cfg.DataPath, cfg.SSH.InternalKeyPath), nil, keygen.Ed25519)
ik, err := keygen.New(cfg.SSH.InternalKeyPath, nil, keygen.Ed25519)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/soft/migrate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
log.Errorf("failed to copy ssh key: %s", err)
}

cfg.SSH.KeyPath = filepath.Join("ssh", filepath.Base(keyPath))
cfg.SSH.KeyPath = filepath.Join(cfg.DataPath, "ssh", filepath.Base(keyPath))
}

// Read config
Expand Down
2 changes: 1 addition & 1 deletion server/backend/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (d *SqliteBackend) ImportRepository(name string, remote string, opts backen
fmt.Sprintf(`GIT_SSH_COMMAND=ssh -o UserKnownHostsFile="%s" -o StrictHostKeyChecking=no -i "%s"`,
filepath.Join(d.cfg.DataPath, "ssh", "known_hosts"),
// FIXME: upstream keygen appends _ed25519 to the key path.
filepath.Join(d.cfg.DataPath, d.cfg.SSH.ClientKeyPath)+"_ed25519",
d.cfg.SSH.ClientKeyPath+"_ed25519",
),
},
},
Expand Down
3 changes: 1 addition & 2 deletions server/cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bufio"
"fmt"
"path/filepath"
"strings"

"github.com/charmbracelet/keygen"
Expand Down Expand Up @@ -119,7 +118,7 @@ func hookCommand() *cobra.Command {
func checkIfInternal(cmd *cobra.Command, _ []string) error {
cfg, s := fromContext(cmd)
pk := s.PublicKey()
kp, err := keygen.New(filepath.Join(cfg.DataPath, cfg.SSH.InternalKeyPath), nil, keygen.Ed25519)
kp, err := keygen.New(cfg.SSH.InternalKeyPath, nil, keygen.Ed25519)
if err != nil {
logger.Errorf("failed to read internal key: %v", err)
return err
Expand Down
50 changes: 45 additions & 5 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"os"
"path/filepath"
"strings"

"github.com/caarlos0/env/v7"
"github.com/charmbracelet/log"
Expand Down Expand Up @@ -115,6 +116,10 @@ func ParseConfig(path string) (*Config, error) {
return nil, err
}

if err := cfg.init(); err != nil {
return nil, err
}

return cfg, nil
}

Expand All @@ -131,11 +136,6 @@ func DefaultConfig() *Config {
dataPath = "data"
}

dp, _ := filepath.Abs(dataPath)
if dp != "" {
dataPath = dp
}

cfg := &Config{
Name: "Soft Serve",
DataPath: dataPath,
Expand Down Expand Up @@ -181,6 +181,10 @@ func DefaultConfig() *Config {
log.Fatal(err)
}

if err := cfg.init(); err != nil {
log.Fatal(err)
}

return cfg
}

Expand All @@ -189,3 +193,39 @@ func (c *Config) WithBackend(backend backend.Backend) *Config {
c.Backend = backend
return c
}

func (c *Config) init() error {
// Use absolute paths
if !filepath.IsAbs(c.DataPath) {
dp, err := filepath.Abs(c.DataPath)
if err != nil {
return err
}
c.DataPath = dp
}

c.SSH.PublicURL = strings.TrimSuffix(c.SSH.PublicURL, "/")
c.HTTP.PublicURL = strings.TrimSuffix(c.HTTP.PublicURL, "/")

if c.SSH.KeyPath != "" && !filepath.IsAbs(c.SSH.KeyPath) {
c.SSH.KeyPath = filepath.Join(c.DataPath, c.SSH.KeyPath)
}

if c.SSH.ClientKeyPath != "" && !filepath.IsAbs(c.SSH.ClientKeyPath) {
c.SSH.ClientKeyPath = filepath.Join(c.DataPath, c.SSH.ClientKeyPath)
}

if c.SSH.InternalKeyPath != "" && !filepath.IsAbs(c.SSH.InternalKeyPath) {
c.SSH.InternalKeyPath = filepath.Join(c.DataPath, c.SSH.InternalKeyPath)
}

if c.HTTP.TLSKeyPath != "" && !filepath.IsAbs(c.HTTP.TLSKeyPath) {
c.HTTP.TLSKeyPath = filepath.Join(c.DataPath, c.HTTP.TLSKeyPath)
}

if c.HTTP.TLSCertPath != "" && !filepath.IsAbs(c.HTTP.TLSCertPath) {
c.HTTP.TLSCertPath = filepath.Join(c.DataPath, c.HTTP.TLSCertPath)
}

return nil
}
2 changes: 1 addition & 1 deletion server/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func mirrorJob(cfg *config.Config) func() {
fmt.Sprintf(`GIT_SSH_COMMAND=ssh -o UserKnownHostsFile="%s" -o StrictHostKeyChecking=no -i "%s"`,
filepath.Join(cfg.DataPath, "ssh", "known_hosts"),
// FIXME: upstream keygen appends _ed25519 to the key path.
filepath.Join(cfg.DataPath, cfg.SSH.ClientKeyPath)+"_ed25519",
cfg.SSH.ClientKeyPath+"_ed25519",
),
)
if _, err := cmd.RunInDir(r.Path); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"net/http"
"path/filepath"

"github.com/charmbracelet/keygen"
"github.com/charmbracelet/log"
Expand Down Expand Up @@ -54,7 +53,7 @@ func NewServer(ctx context.Context, cfg *config.Config) (*Server, error) {

// Create internal key.
ikp, err := keygen.NewWithWrite(
filepath.Join(cfg.DataPath, cfg.SSH.InternalKeyPath),
cfg.SSH.InternalKeyPath,
nil,
keygen.Ed25519,
)
Expand All @@ -65,7 +64,7 @@ func NewServer(ctx context.Context, cfg *config.Config) (*Server, error) {

// Create client key.
ckp, err := keygen.NewWithWrite(
filepath.Join(cfg.DataPath, cfg.SSH.ClientKeyPath),
cfg.SSH.ClientKeyPath,
nil,
keygen.Ed25519,
)
Expand Down
2 changes: 1 addition & 1 deletion server/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func NewSSHServer(cfg *config.Config, hooks hooks.Hooks) (*SSHServer, error) {
ssh.PublicKeyAuth(s.PublicKeyHandler),
ssh.KeyboardInteractiveAuth(s.KeyboardInteractiveHandler),
wish.WithAddress(cfg.SSH.ListenAddr),
wish.WithHostKeyPath(filepath.Join(cfg.DataPath, cfg.SSH.KeyPath)),
wish.WithHostKeyPath(cfg.SSH.KeyPath),
wish.WithMiddleware(mw...),
)
if err != nil {
Expand Down

0 comments on commit 2203fe0

Please sign in to comment.