fix(agent): make discovery enrollment compatible and idempotent (#53)

* fix(agent): make discovery enrollment compatible and idempotent

* fix(agent): confirm enrollment persistence and document token aliases
This commit is contained in:
ArthurKoba
2026-11-07 16:29:47 +03:00
committed by GitHub
parent 1ca03df0ef
commit 72d67c80f2
30 changed files with 1324 additions and 71 deletions
+66
View File
@@ -0,0 +1,66 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
// Package localfile persists agent identity and configuration without truncating existing files.
package localfile
import (
"errors"
"os"
"path/filepath"
)
// WriteAtomic syncs the complete contents, replaces path, then syncs its containing directory.
// An error from the final sync means the replacement is visible but its durability is unconfirmed.
func WriteAtomic(path string, data []byte, perm os.FileMode) error {
return writeAtomic(path, data, perm, SyncDirectory)
}
func writeAtomic(path string, data []byte, perm os.FileMode, syncDir func(string) error) error {
file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".tmp-*")
if err != nil {
return err
}
defer func() {
_ = file.Close() // Best effort after an earlier write/close error.
_ = os.Remove(file.Name()) // Best effort; rename normally removes the temporary path.
}()
if err = file.Chmod(perm); err != nil {
return err
}
if _, err = file.Write(data); err != nil {
return err
}
if err = file.Sync(); err != nil {
return err
}
if err = file.Close(); err != nil {
return err
}
if err = os.Rename(file.Name(), path); err != nil {
return err
}
return syncDir(filepath.Dir(path))
}
// SyncDirectory confirms a directory entry update on platforms with directory flushing.
// Windows provides best-effort replacement without a portable directory flush guarantee.
func SyncDirectory(path string) (resultErr error) {
directory, err := os.Open(path) //nolint:gosec // callers provide the directory of a managed local state/config file
if err != nil {
return err
}
defer func() {
if closeErr := directory.Close(); resultErr == nil {
resultErr = closeErr
}
}()
info, err := directory.Stat()
if err != nil {
return err
}
if !info.IsDir() {
return errors.New("cannot sync a non-directory path")
}
return syncDirectory(directory)
}
@@ -0,0 +1,88 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package localfile
import (
"errors"
"os"
"path/filepath"
"testing"
)
func TestWriteAtomicReplacesCompleteFileAndCleansFailure(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "identity.json")
if err := WriteAtomic(path, []byte("old identity"), 0o600); err != nil {
t.Fatal(err)
}
if err := WriteAtomic(path, []byte("new identity"), 0o600); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path)
if err != nil || string(data) != "new identity" {
t.Errorf("WriteAtomic replacement = %q, %v; want complete new identity", data, err)
}
blocked := filepath.Join(dir, "directory")
if err = os.Mkdir(blocked, 0o700); err != nil {
t.Fatal(err)
}
if err = WriteAtomic(blocked, []byte("cannot replace directory"), 0o600); err == nil {
t.Fatal("WriteAtomic(directory) succeeded, want rename failure")
}
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 2 {
t.Errorf("WriteAtomic(after failure) directory entries = %d, want 2 with no temporary file", len(entries))
}
}
func TestWriteAtomicReportsDirectorySyncFailureAfterReplacement(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "identity.json")
if err := os.WriteFile(path, []byte("old identity"), 0o600); err != nil {
t.Fatal(err)
}
syncFailure := errors.New("directory I/O failure")
syncCalls := 0
err := writeAtomic(path, []byte("new identity"), 0o600, func(gotDir string) error {
syncCalls++
if gotDir != dir {
t.Errorf("writeAtomic sync path = %q, want %q", gotDir, dir)
}
data, readErr := os.ReadFile(path)
if readErr != nil || string(data) != "new identity" {
t.Errorf("Directory sync ran before replacement: contents = %q, error %v", data, readErr)
}
return syncFailure
})
if !errors.Is(err, syncFailure) || syncCalls != 1 {
t.Errorf("writeAtomic(directory sync failure) = %v, %d sync calls; want I/O failure, 1", err, syncCalls)
}
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Errorf("writeAtomic(failed directory sync) left %d entries, want only destination", len(entries))
}
}
func TestSyncDirectoryRejectsMissingAndNonDirectoryPaths(t *testing.T) {
dir := t.TempDir()
if err := SyncDirectory(dir); err != nil {
t.Fatalf("SyncDirectory(existing directory) = %v, want nil", err)
}
if err := SyncDirectory(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) {
t.Errorf("SyncDirectory(missing directory) = %v, want not-exist error", err)
}
path := filepath.Join(dir, "file")
if err := os.WriteFile(path, []byte("contents"), 0o600); err != nil {
t.Fatal(err)
}
if err := SyncDirectory(path); err == nil {
t.Error("SyncDirectory(regular file) = nil, want non-directory error")
}
}
@@ -0,0 +1,12 @@
//go:build !windows
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package localfile
import "os"
func syncDirectory(directory *os.File) error {
return directory.Sync()
}
@@ -0,0 +1,12 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package localfile
import "os"
// Windows does not support a portable directory fsync through os.File.Sync.
// The file is flushed before replacement; persisting the directory entry is best effort.
func syncDirectory(_ *os.File) error {
return nil
}