feat: import existing smb.conf and /etc/exports on first boot

Adds auto-detection of pre-existing Samba shares and NFS exports when
nasctl is installed on a host that already has these configs.

New package internal/importer parses smb.conf (INI-style) and
/etc/exports (line-based) and imports them into SQLite.

Imported shares/exports are marked dirty so the user must review
and apply manually before any file is overwritten.

Backup: before the first Apply, each module backs up the original
config to <path>.nasctl.bak.<timestamp> (one time only).

New CLI flag --import-on-boot / NASCTL_IMPORT_ON_BOOT env var
(default false, opt-in).

New API endpoints:
  GET  /api/import/status
  POST /api/import/samba
  POST /api/import/nfs

New DB methods ReplaceSambaShares/ReplaceNFSExports (transactional
replace-all), guarded by import.samba.done / import.nfs.done
settings flags.
This commit is contained in:
2026-07-05 21:45:25 -04:00
parent d6b184d19b
commit 4ae7335b31
14 changed files with 1153 additions and 0 deletions
+154
View File
@@ -0,0 +1,154 @@
package importer
import (
"testing"
)
func TestParseExports(t *testing.T) {
tests := []struct {
name string
input string
wantLen int
wantErr bool
}{
{
name: "basic export",
input: `/srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check)
`,
wantLen: 1,
},
{
name: "multiple clients same path",
input: `/srv/nfs/shared *(ro,sync) 192.168.1.0/24(rw,sync,no_root_squash)
`,
wantLen: 1,
},
{
name: "multiple exports",
input: `# This is a comment
/srv/nfs/data 192.168.1.0/24(rw,sync)
/srv/nfs/public *(ro,sync)
# another comment
/srv/nfs/backup 10.0.0.0/8(ro,sync,no_subtree_check)
`,
wantLen: 3,
},
{
name: "wildcard client",
input: `/srv/nfs/public *(ro,sync,no_root_squash)
`,
wantLen: 1,
},
{
name: "wildcard client with parentheses",
input: `/srv/nfs/shared *(rw,sync,no_root_squash)
`,
wantLen: 1,
},
{
name: "empty input",
input: "",
wantLen: 0,
},
{
name: "only comments",
input: `# comment 1
# comment 2
`,
wantLen: 0,
},
{
name: "multiple spaces between entries",
input: `/srv/nfs/shared 192.168.1.0/24(rw,sync) 10.0.0.0/8(ro)
`,
wantLen: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseExports([]byte(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("parseExports() error = %v, wantErr %v", err, tt.wantErr)
return
}
if len(got) != tt.wantLen {
t.Errorf("parseExports() got %d exports, want %d", len(got), tt.wantLen)
for i, e := range got {
t.Logf(" export[%d]: path=%q clients=%v options=%q", i, e.Path, e.Clients, e.Options)
}
}
})
}
}
func TestParseExportLine(t *testing.T) {
tests := []struct {
name string
line string
wantPath string
wantCount int
}{
{
name: "single client with options",
line: `/srv/nfs/shared 192.168.1.100(rw,sync,no_subtree_check)`,
wantPath: `/srv/nfs/shared`,
wantCount: 1,
},
{
name: "wildcard with default options",
line: `/srv/nfs/public *(ro)`,
wantPath: `/srv/nfs/public`,
wantCount: 1,
},
{
name: "multiple clients",
line: `/srv/nfs/shared 192.168.1.0/24(rw) 10.0.0.0/8(ro)`,
wantPath: `/srv/nfs/shared`,
wantCount: 2,
},
{
name: "netgroup",
line: `/srv/nfs/shared @admins(rw,sync)`,
wantPath: `/srv/nfs/shared`,
wantCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := parseExportLine(tt.line)
if !ok {
t.Errorf("parseExportLine(%q) returned false", tt.line)
return
}
if got.Path != tt.wantPath {
t.Errorf("parseExportLine(%q) path = %q, want %q", tt.line, got.Path, tt.wantPath)
}
if len(got.Clients) != tt.wantCount {
t.Errorf("parseExportLine(%q) clients = %v, want %d", tt.line, got.Clients, tt.wantCount)
}
})
}
}
func TestSplitExportsClients(t *testing.T) {
tests := []struct {
input string
expected []string
}{
{`192.168.1.0/24(rw,sync) 10.0.0.0/8(ro)`, []string{`192.168.1.0/24(rw,sync)`, `10.0.0.0/8(ro)`}},
{`*(ro)`, []string{`*(ro)`}},
{`192.168.1.100(rw)`, []string{`192.168.1.100(rw)`}},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := splitExportsClients(tt.input)
if len(got) != len(tt.expected) {
t.Errorf("splitExportsClients(%q) = %v, want %v", tt.input, got, tt.expected)
}
})
}
}