feat(config): add automatic backup before modifying config file (#7)

This commit is contained in:
Adem Baccara
2025-08-31 10:01:37 +01:00
committed by GitHub
parent 6a963974df
commit b41eeecbf7
7 changed files with 105 additions and 50 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ builds:
- 386 - 386
main: ./cmd/main.go main: ./cmd/main.go
ldflags: ldflags:
-X main.version={{.Version}} -X main.gitCommit={{.Commit}} -X main.buildTime={{.Date}} -X main.version={{.Version}} -X main.gitCommit={{.Commit}}
+19 -3
View File
@@ -6,7 +6,7 @@
Lazyssh is a terminal-based, interactive SSH manager inspired by tools like lazydocker and k9s — but built for managing your fleet of servers directly from your terminal. Lazyssh is a terminal-based, interactive SSH manager inspired by tools like lazydocker and k9s — but built for managing your fleet of servers directly from your terminal.
<br/> <br/>
With lazyssh, you can quickly navigate, connect, manage, and transfer files between your local machine and any server defined in your ~/.ssh/config. No more remembering IP addresses or running long scp commands — just a clean, keyboard-driven UI. With lazyssh, you can quickly navigate, connect, manage, and transfer files between your local machine and any server defined in your `~/.ssh/config`. No more remembering IP addresses or running long scp commands — just a clean, keyboard-driven UI.
--- ---
@@ -31,12 +31,28 @@ With lazyssh, you can quickly navigate, connect, manage, and transfer files betw
- 📁 Copy files between local and servers with an easy picker UI. - 📁 Copy files between local and servers with an easy picker UI.
- 📡 Port forwarding (local↔remote) from the UI. - 📡 Port forwarding (local↔remote) from the UI.
- 🔑 Enhanced Key Management: - 🔑 Enhanced Key Management:
- Use default local public key (~/.ssh/id_ed25519.pub or ~/.ssh/id_rsa.pub) - Use default local public key (`~/.ssh/id_ed25519.pub` or `~/.ssh/id_rsa.pub`)
- Paste custom public keys manually - Paste custom public keys manually
- Generate new keypairs and deploy them - Generate new keypairs and deploy them
- Automatically append keys to ~/.ssh/authorized_keys with correct permissions - Automatically append keys to `~/.ssh/authorized_keys` with correct permissions
--- ---
## 🔐 Security Notice
lazyssh does not introduce any new security risks.
It is simply a UI/TUI wrapper around your existing `~/.ssh/config` file.
- All SSH connections are executed through your systems native ssh binary (OpenSSH).
- Private keys, passwords, and credentials are never stored, transmitted, or modified by lazyssh.
- Your existing IdentityFile paths and ssh-agent integrations work exactly as before.
- lazyssh only reads and updates your `~/.ssh/config`. A backup of the file is created automatically before any changes.
- File permissions on your SSH config are preserved to ensure security.
## 📷 Screenshots ## 📷 Screenshots
<div align="center"> <div align="center">
+1 -3
View File
@@ -18,7 +18,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/Adembc/lazyssh/internal/adapters/data/file" "github.com/Adembc/lazyssh/internal/adapters/data/file"
"github.com/Adembc/lazyssh/internal/logger" "github.com/Adembc/lazyssh/internal/logger"
@@ -31,7 +30,6 @@ import (
var ( var (
version = "develop" version = "develop"
gitCommit = "unknown" gitCommit = "unknown"
buildTime = time.Now().Format("2006-01-02 15:04:05")
) )
func main() { func main() {
@@ -55,7 +53,7 @@ func main() {
serverRepo := file.NewServerRepo(log, sshConfigFile, metaDataFile) serverRepo := file.NewServerRepo(log, sshConfigFile, metaDataFile)
serverService := services.NewServerService(log, serverRepo) serverService := services.NewServerService(log, serverRepo)
tui := ui.NewTUI(log, serverService, version, gitCommit, buildTime) tui := ui.NewTUI(log, serverService, version, gitCommit)
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: ui.AppName, Use: ui.AppName,
@@ -16,6 +16,7 @@ package file
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
@@ -56,16 +57,41 @@ func (m *sshConfigManager) writeServers(servers []domain.Server) error {
return err return err
} }
file, err := os.Create(m.filePath) if err := m.backupCurrentConfig(); err != nil {
return err
}
dir := filepath.Dir(m.filePath)
tmp, err := os.CreateTemp(dir, ".lazyssh-tmp-*")
if err != nil { if err != nil {
return err return err
} }
defer func() { defer func() { _ = os.Remove(tmp.Name()) }()
_ = file.Close()
}() if err := os.Chmod(tmp.Name(), 0o600); err != nil {
_ = tmp.Close()
return err
}
writer := &SSHConfigWriter{} writer := &SSHConfigWriter{}
return writer.Write(file, servers) if err := writer.Write(tmp, servers); err != nil {
_ = tmp.Close()
return err
}
if err := tmp.Sync(); err != nil {
_ = tmp.Close()
return err
}
if err := tmp.Close(); err != nil { // close after sync to ensure contents are persisted
return err
}
if err := os.Rename(tmp.Name(), m.filePath); err != nil {
return err
}
return nil
} }
func (m *sshConfigManager) addServer(server domain.Server) error { func (m *sshConfigManager) addServer(server domain.Server) error {
@@ -135,3 +161,49 @@ func (m *sshConfigManager) ensureDirectory() error {
dir := filepath.Dir(m.filePath) dir := filepath.Dir(m.filePath)
return os.MkdirAll(dir, 0o700) return os.MkdirAll(dir, 0o700)
} }
// backupCurrentConfig creates ~/.lazyssh/backups/config.backup with 0600 perms,
// overwriting it each time, but only if the source config exists.
func (m *sshConfigManager) backupCurrentConfig() error {
// If source config does not exist, skip backup
if _, err := os.Stat(m.filePath); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
home, err := os.UserHomeDir()
if err != nil {
return err
}
backupDir := filepath.Join(home, ".lazyssh", "backups")
// Ensure directory with 0700
if err := os.MkdirAll(backupDir, 0o700); err != nil {
return err
}
backupPath := filepath.Join(backupDir, "config.backup")
// Copy file contents
src, err := os.Open(m.filePath)
if err != nil {
return err
}
defer func() { _ = src.Close() }()
// #nosec G304 -- backupPath is generated internally and trusted
dst, err := os.OpenFile(backupPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
return err
}
defer func() { _ = dst.Close() }()
if _, err := io.Copy(dst, src); err != nil {
return err
}
if err := dst.Sync(); err != nil {
return err
}
return nil
}
+1 -29
View File
@@ -25,17 +25,15 @@ import (
type AppHeader struct { type AppHeader struct {
*tview.Flex *tview.Flex
version string version string
buildTime string
gitCommit string gitCommit string
repoURL string repoURL string
} }
func NewAppHeader(version, gitCommit, buildTime, repoURL string) *AppHeader { func NewAppHeader(version, gitCommit, repoURL string) *AppHeader {
header := &AppHeader{ header := &AppHeader{
Flex: tview.NewFlex(), Flex: tview.NewFlex(),
version: version, version: version,
repoURL: repoURL, repoURL: repoURL,
buildTime: buildTime,
gitCommit: gitCommit, gitCommit: gitCommit,
} }
header.build() header.build()
@@ -85,13 +83,11 @@ func (h *AppHeader) buildCenterSection(bg tcell.Color) *tview.TextView {
if commit != "" { if commit != "" {
commitTag = makeTag(commit, "#A78BFA") // violet commitTag = makeTag(commit, "#A78BFA") // violet
} }
timeTag := makeTag(formatBuildTime(h.buildTime), "#3B82F6") // blue
text := versionTag text := versionTag
if commitTag != "" { if commitTag != "" {
text += " " + commitTag text += " " + commitTag
} }
text += " " + timeTag
center.SetText(text) center.SetText(text)
return center return center
@@ -126,30 +122,6 @@ func shortCommit(c string) string {
return c return c
} }
// formatBuildTime tries to parse common time formats and returns a concise human-readable string.
func formatBuildTime(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return "unknown"
}
layouts := []string{
"2006-01-02 15:04:05",
time.RFC3339,
time.RFC1123,
time.RFC1123Z,
}
var t time.Time
var err error
for _, l := range layouts {
t, err = time.Parse(l, s)
if err == nil {
return t.Format("Mon, 02 Jan 2006 15:04")
}
}
return s
}
// makeTag returns a rectangular-looking colored chip for the given text. // makeTag returns a rectangular-looking colored chip for the given text.
func makeTag(text, bg string) string { func makeTag(text, bg string) string {
text = strings.TrimSpace(text) text = strings.TrimSpace(text)
+5 -7
View File
@@ -27,9 +27,8 @@ import (
type tui struct { type tui struct {
logger *zap.SugaredLogger logger *zap.SugaredLogger
version string version string
commit string commit string
buildDate string
app *tview.Application app *tview.Application
serverService ports.ServerService serverService ports.ServerService
@@ -49,14 +48,13 @@ type tui struct {
searchVisible bool searchVisible bool
} }
func NewTUI(logger *zap.SugaredLogger, ss ports.ServerService, version, commit, buildDate string) *tui { func NewTUI(logger *zap.SugaredLogger, ss ports.ServerService, version, commit string) *tui {
return &tui{ return &tui{
logger: logger, logger: logger,
app: tview.NewApplication(), app: tview.NewApplication(),
serverService: ss, serverService: ss,
version: version, version: version,
commit: commit, commit: commit,
buildDate: buildDate,
} }
} }
@@ -68,7 +66,7 @@ func (t *tui) Run() error {
}() }()
t.app.EnableMouse(true) t.app.EnableMouse(true)
t.initializeTheme().buildComponents().buildLayout().bindEvents().loadInitialData().loadSplashScreen() t.initializeTheme().buildComponents().buildLayout().bindEvents().loadInitialData().loadSplashScreen()
t.logger.Infow("starting TUI application", "version", t.version, "commit", t.commit, "buildDate", t.buildDate) t.logger.Infow("starting TUI application", "version", t.version, "commit", t.commit)
if err := t.app.Run(); err != nil { if err := t.app.Run(); err != nil {
t.logger.Errorw("application run error", "error", err) t.logger.Errorw("application run error", "error", err)
return err return err
@@ -89,7 +87,7 @@ func (t *tui) initializeTheme() *tui {
} }
func (t *tui) buildComponents() *tui { func (t *tui) buildComponents() *tui {
t.header = NewAppHeader(t.version, t.commit, t.buildDate, RepoURL) t.header = NewAppHeader(t.version, t.commit, RepoURL)
t.searchBar = NewSearchBar(). t.searchBar = NewSearchBar().
OnSearch(t.handleSearchInput). OnSearch(t.handleSearchInput).
OnEscape(t.hideSearchBar) OnEscape(t.hideSearchBar)
+1 -2
View File
@@ -20,7 +20,6 @@ SHELL = /usr/bin/env bash -o pipefail
# Project variables # Project variables
PROJECT_NAME ?= $(shell basename $(CURDIR)) PROJECT_NAME ?= $(shell basename $(CURDIR))
VERSION ?= v0.1.0 VERSION ?= v0.1.0
BUILD_TIME ?= $(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown") GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Build variables # Build variables
@@ -30,7 +29,7 @@ CMD_DIR ?= ./cmd
PKG_LIST := $(shell go list ./...) PKG_LIST := $(shell go list ./...)
# LDFLAGS for version information # LDFLAGS for version information
LDFLAGS = -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME) -X main.gitCommit=$(GIT_COMMIT)" LDFLAGS = -ldflags "-X main.version=$(VERSION) -X main.gitCommit=$(GIT_COMMIT)"
##@ Dependencies ##@ Dependencies