Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"strings"
"sync"

"dnsplane/internal/atomicfile"
)

// PersistedState is stored next to dnsplane.json as cluster_state.json.
Expand Down Expand Up @@ -90,11 +92,7 @@ func (s *stateManager) saveLocked() error {
if err != nil {
return err
}
tmp := s.path + ".tmp"
if err := os.WriteFile(tmp, data, 0o600); err != nil {
return err
}
return os.Rename(tmp, s.path)
return atomicfile.Write(s.path, data, 0o600)
}

func (s *stateManager) NodeID() string {
Expand Down
184 changes: 80 additions & 104 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
)

const (
// FileName is the canonical name of the configuration file.
FileName = "dnsplane.json"
// systemConfigPath is the location checked last when resolving the config.
systemConfigPath = "/etc/" + FileName
// FileName is the canonical name of new configuration files (YAML).
FileName = "dnsplane.yaml"
// FileNameYML is an alternate YAML extension.
FileNameYML = "dnsplane.yml"
// FileNameJSON is the legacy JSON configuration file name.
FileNameJSON = "dnsplane.json"
// systemConfigDir is the packaged system config directory.
systemConfigDir = "/etc/dnsplane"
)

// RecordsSourceType is how the DNS records file is provided: local file, HTTP(S) URL, git repo, or BIND zone directory.
Expand Down Expand Up @@ -230,97 +234,126 @@

// Loaded contains the configuration together with metadata about the source file.
type Loaded struct {
Path string
Created bool
Config Config
Path string // file read for this session
WritePath string // file used for Save (always YAML once present or migrated)
Created bool
Config Config
}

// WriteConfigPath returns the path settings and saves should use.
func (l *Loaded) WriteConfigPath() string {
if l == nil {
return ""
}
if l.WritePath != "" {
return l.WritePath
}
return l.Path
}

// Load resolves the dnsplane configuration file, creating a default one if
// necessary, and returns the parsed configuration alongside metadata.
func Load() (*Loaded, error) {
candidates, err := candidatePaths()
dirs, err := candidateSearchPaths()
if err != nil {
return nil, err
}

for _, path := range candidates {
cfg, err := readConfig(path)
for _, dir := range dirs {
loaded, err := resolveLoadedConfig(dir, "")
if err == nil {
return loaded, nil
}
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
}
if fileExists(legacyEtcConfig()) {
loaded, err := tryLoadFromResolvedPath(legacyEtcConfig())
if err == nil {
cfg.applyDefaults(filepath.Dir(path))
return &Loaded{Path: path, Config: *cfg}, nil
return loaded, nil
}
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("config: failed to read %s: %w", path, err)
return nil, err
}
}

defaultDir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("config: determine working directory: %w", err)
}

defaultPath := filepath.Join(defaultDir, FileName)
cfg := defaultConfig(defaultDir)
if err := writeConfig(defaultPath, cfg); err != nil {
return nil, err
}
cfg.applyDefaults(defaultDir)
return &Loaded{Path: defaultPath, Created: true, Config: *cfg}, nil
return createDefaultInDir(defaultDir)
}

// resolveConfigPath returns the config file path. If path is a directory (ends
// with /, exists as dir, or path has no extension), returns path/FileName;
// otherwise returns path as the config file path.
func resolveConfigPath(path string) (string, error) {
// resolveConfigInput returns the directory or explicit config file path.
func resolveConfigInput(path string) (resolved string, isDir bool, err error) {
path = filepath.Clean(strings.TrimSpace(path))
if path == "" || path == "." {
return "", fmt.Errorf("config: path is empty")
return "", false, fmt.Errorf("config: path is empty")
}
isDir := strings.HasSuffix(path, string(filepath.Separator))
isDir = strings.HasSuffix(path, string(filepath.Separator))
if !isDir {
if info, err := os.Stat(path); err == nil && info.IsDir() {
if info, statErr := os.Stat(path); statErr == nil && info.IsDir() {
isDir = true
} else if !strings.Contains(filepath.Base(path), ".") {
isDir = true
}
}
if isDir {
path = strings.TrimSuffix(path, string(filepath.Separator))
return filepath.Join(path, FileName), nil
return strings.TrimSuffix(path, string(filepath.Separator)), true, nil
}
return path, false, nil
}

func isStandardConfigName(p string) bool {
switch filepath.Base(p) {
case FileName, FileNameYML, FileNameJSON:
return true
default:
return false
}
return path, nil
}

// LoadFromPath loads configuration from the given path, or creates a default
// config at that path if the file does not exist. Path may be a directory
// (then config is path/dnsplane.json) or a file path (then that file is used).
// (then config is path/dnsplane.yaml) or a file path (then that file is used).
func LoadFromPath(path string) (*Loaded, error) {
path = strings.TrimSpace(path)
if path == "" {
return nil, fmt.Errorf("config: path is empty")
}
configPath, err := resolveConfigPath(path)
resolved, isDir, err := resolveConfigInput(path)
if err != nil {
return nil, err
}
cfg, err := readConfig(configPath)
if err == nil {
cfg.applyDefaults(filepath.Dir(configPath))
return &Loaded{Path: configPath, Config: *cfg}, nil
if isDir {
loaded, loadErr := resolveLoadedConfig(resolved, "")
if loadErr == nil {
return loaded, nil
}
if !errors.Is(loadErr, os.ErrNotExist) {
return nil, loadErr
}
return createDefaultInDir(resolved)
}
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("config: failed to read %s: %w", configPath, err)
dir := filepath.Dir(resolved)
if isStandardConfigName(resolved) {
loaded, loadErr := resolveLoadedConfig(dir, resolved)
if loadErr == nil {
return loaded, nil
}
if !errors.Is(loadErr, os.ErrNotExist) {
return nil, loadErr
}
return createDefaultInDir(dir)
}
dir := filepath.Dir(configPath)
if err := os.MkdirAll(dir, 0o750); err != nil {
return nil, fmt.Errorf("config: ensure config directory %s: %w", dir, err)
loaded, err := tryLoadFromResolvedPath(resolved)
if err == nil {
return loaded, nil
}
defaultCfg := defaultConfig(dir)
if err := writeConfig(configPath, defaultCfg); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
defaultCfg.applyDefaults(dir)
return &Loaded{Path: configPath, Created: true, Config: *defaultCfg}, nil
return createDefaultInDir(dir)
}

// Read loads and normalises configuration from the specified path without
Expand Down Expand Up @@ -350,63 +383,6 @@
c.applyDefaults(configDir)
}

func candidatePaths() ([]string, error) {
execPath, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("config: determine executable path: %w", err)
}
execDir := filepath.Dir(execPath)

var paths []string
paths = appendIfMissing(paths, filepath.Join(execDir, FileName))

if userPath, err := userConfigPath(); err == nil && userPath != "" {
paths = appendIfMissing(paths, userPath)
}

paths = appendIfMissing(paths, systemConfigPath)
return paths, nil
}

func userConfigPath() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("config: determine user config dir: %w", err)
}
return filepath.Join(dir, "dnsplane", FileName), nil
}

func readConfig(path string) (*Config, error) {
data, err := os.ReadFile(path) // #nosec G304 -- path from operator (config file)
if err != nil {
return nil, err
}
if len(strings.TrimSpace(string(data))) == 0 {
return nil, fmt.Errorf("config: file %s is empty", path)
}

var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("config: parse %s: %w", path, err)
}

if legacy := extractLegacyRecordSettings(data); legacy != nil {
cfg.DNSRecordSettings = *legacy
}
return &cfg, nil
}

func writeConfig(path string, cfg *Config) error {
payload, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("config: marshal config: %w", err)
}
if err := os.WriteFile(path, payload, 0o600); err != nil {
return fmt.Errorf("config: write %s: %w", path, err)
}
return nil
}

func defaultConfig(baseDir string) *Config {
logDir := "/var/log/dnsplane"
if !isSystemConfigDir(baseDir) {
Expand Down Expand Up @@ -604,7 +580,7 @@
}
}

func appendIfMissing(paths []string, candidate string) []string {

Check failure on line 583 in config/config.go

View workflow job for this annotation

GitHub Actions / golangci-lint (linux)

func `appendIfMissing` is unused (unused)
for _, existing := range paths {
if existing == candidate {
return paths
Expand Down
84 changes: 84 additions & 0 deletions config/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2024-2026 George (earentir) Pantazis (https://earentir.dev)
// SPDX-License-Identifier: GPL-2.0-only

package config

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"dnsplane/internal/atomicfile"

"gopkg.in/yaml.v3"
)

func decodeConfigBytes(path string, data []byte) (*Config, error) {
if len(strings.TrimSpace(string(data))) == 0 {
return nil, fmt.Errorf("config: file %s is empty", path)
}
ext := strings.ToLower(filepath.Ext(path))
jsonData := data
if ext == ".yaml" || ext == ".yml" {
var v any
if err := yaml.Unmarshal(data, &v); err != nil {
return nil, fmt.Errorf("config: parse %s: %w", path, err)
}
var err error
jsonData, err = json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("config: parse %s: %w", path, err)
}
}

var cfg Config
if err := json.Unmarshal(jsonData, &cfg); err != nil {
return nil, fmt.Errorf("config: parse %s: %w", path, err)
}
if legacy := extractLegacyRecordSettings(jsonData); legacy != nil {
cfg.DNSRecordSettings = *legacy
}
return &cfg, nil
}

func readConfig(path string) (*Config, error) {
data, err := os.ReadFile(path) // #nosec G304 -- path from operator (config file)
if err != nil {
return nil, err
}
return decodeConfigBytes(path, data)
}

func writeConfig(path string, cfg *Config) error {
ext := strings.ToLower(filepath.Ext(path))
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o750); err != nil {
return fmt.Errorf("config: ensure config directory %s: %w", dir, err)
}
cfg.applyDefaults(dir)
var payload []byte
var err error
switch ext {
case ".yaml", ".yml":
j, mErr := json.Marshal(cfg)
if mErr != nil {
return fmt.Errorf("config: marshal config: %w", mErr)
}
var v any
if err = json.Unmarshal(j, &v); err != nil {
return fmt.Errorf("config: marshal config: %w", err)
}
payload, err = yaml.Marshal(v)
default:
payload, err = json.MarshalIndent(cfg, "", " ")
}
if err != nil {
return fmt.Errorf("config: marshal config: %w", err)
}
if err := atomicfile.Write(path, payload, 0o600); err != nil {
return fmt.Errorf("config: write %s: %w", path, err)
}
return nil
}
Loading
Loading