* feat: add credentials file support for Dolt server passwords Add INI-style credentials file (~/.config/beads/credentials) as a fallback for Dolt server passwords, keyed by [host:port] section. This lets users working with multiple servers avoid juggling env vars per project. Lookup order: BEADS_DOLT_PASSWORD env var > credentials file > empty. Path overridable via BEADS_CREDENTIALS_FILE env var. Warns on overly permissive file permissions (unix only, mirrors ssh). * docs: document credentials file in DOLT.md Add credentials file section to the Dolt backend docs covering file format, default location, permission warnings, and password resolution order. Update the config reference and env var table to reference the new feature. * fix: use resolved runtime port for credentials file lookup The credential file lookup in GetDoltServerPassword() used the port from metadata.json, but the actual connection port comes from doltserver.DefaultConfig() which has its own priority chain (port file > config.yaml > metadata.json). When these diverge — e.g., IAP tunnel on 3308 vs local Dolt on 3307 — the credentials file lookup used the wrong [host:port] section. Add GetDoltServerPasswordForPort() that accepts an explicit port, and use it in main.go where the resolved runtime port is already known. Existing GetDoltServerPassword() delegates to the new method for backward compatibility. * fix: use mysql.Config.FormatDSN for proper password escaping Passwords with special characters (& < > / % etc.) were inserted raw into the DSN string, breaking go-sql-driver/mysql parsing. Passwords like "zId&z,L9P4X,%k4n4rylGV<Ibos9<)/p" would cause "Access denied" errors even though the password was correct. Switch from manual fmt.Sprintf DSN construction to mysql.Config's FormatDSN(), which handles escaping correctly. This also consolidates timeout and TLS configuration into the typed Config struct instead of string concatenation. * test: add DSN special character password roundtrip tests Covers passwords with &, <, >, /, ?, %, @, :, and () — all of which broke the old fmt.Sprintf DSN construction. Verifies that FormatDSN produces a parseable DSN and the password survives the roundtrip.
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package configfile
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// DefaultCredentialsPath returns the platform-appropriate default credentials file path.
|
|
// Linux/macOS: ~/.config/beads/credentials
|
|
// Windows: %APPDATA%\beads\credentials
|
|
func DefaultCredentialsPath() string {
|
|
if runtime.GOOS == "windows" {
|
|
if appdata := os.Getenv("APPDATA"); appdata != "" {
|
|
return filepath.Join(appdata, "beads", "credentials")
|
|
}
|
|
}
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return filepath.Join(home, ".config", "beads", "credentials")
|
|
}
|
|
|
|
// LookupCredentialsPassword reads a password from an INI-style credentials file,
|
|
// keyed by [host:port] section. Returns empty string if not found or on any error.
|
|
//
|
|
// File format:
|
|
//
|
|
// [127.0.0.1:3307]
|
|
// password=localDevPassword
|
|
//
|
|
// [beads.company.com:3307]
|
|
// password=teamServerPassword
|
|
//
|
|
// The file path is determined by:
|
|
// 1. BEADS_CREDENTIALS_FILE env var (if set)
|
|
// 2. Default platform path (see DefaultCredentialsPath)
|
|
func LookupCredentialsPassword(host string, port int) string {
|
|
credFile := os.Getenv("BEADS_CREDENTIALS_FILE")
|
|
if credFile == "" {
|
|
credFile = DefaultCredentialsPath()
|
|
}
|
|
if credFile == "" {
|
|
return ""
|
|
}
|
|
|
|
return readPasswordFromFile(credFile, fmt.Sprintf("%s:%d", host, port))
|
|
}
|
|
|
|
// readPasswordFromFile parses an INI-style credentials file and returns the
|
|
// password for the given [host:port] section. Returns empty string on any error.
|
|
func readPasswordFromFile(path string, sectionKey string) string {
|
|
f, err := os.Open(path) //nolint:gosec // path comes from env var or os.UserHomeDir, not user input
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
defer f.Close()
|
|
|
|
// Warn if file has overly permissive permissions (unix only)
|
|
warnIfInsecurePermissions(path)
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
inSection := false
|
|
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
// Strip inline comments
|
|
if idx := strings.Index(line, "#"); idx >= 0 {
|
|
line = strings.TrimSpace(line[:idx])
|
|
}
|
|
if line == "" {
|
|
continue
|
|
}
|
|
|
|
// Section header: [host:port]
|
|
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
|
|
section := line[1 : len(line)-1]
|
|
if section == sectionKey {
|
|
inSection = true
|
|
} else if inSection {
|
|
// We've left our section without finding a password
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Key=value within our section
|
|
if inSection {
|
|
key, value, found := strings.Cut(line, "=")
|
|
if found && strings.TrimSpace(key) == "password" {
|
|
return strings.TrimSpace(value)
|
|
}
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|