mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 23:04:24 +08:00
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:
@@ -0,0 +1,84 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package state
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/agent/localfile"
|
||||
)
|
||||
|
||||
func TestRegistrationKeySurvivesStaleSnapshotAndRestart(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
store := NewStore(path)
|
||||
stale, err := store.Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key, err := store.EnsureRegistrationKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = store.SetRegisteredNodeID("node-server-assigned"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := hex.DecodeString(key)
|
||||
if err != nil || len(decoded) != 32 {
|
||||
t.Fatalf("EnsureRegistrationKey() length = %d, error %v; want 32, nil", len(decoded), err)
|
||||
}
|
||||
stale.LastError = "background sync failed"
|
||||
if err = store.Save(stale); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
restartedKey, err := NewStore(path).EnsureRegistrationKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if restartedKey != key {
|
||||
t.Error("EnsureRegistrationKey(after stale save and restart) changed enrollment identity")
|
||||
}
|
||||
snapshot, err := NewStore(path).Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if snapshot.NodeID != "node-server-assigned" {
|
||||
t.Errorf("Load(after stale save).NodeID = %q, want server-assigned ID", snapshot.NodeID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistingRegistrationKeyRequiresDirectorySyncBeforeUse(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
store := NewStore(path)
|
||||
key, err := store.EnsureRegistrationKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Model a prior successful rename whose final directory flush was not confirmed.
|
||||
syncFailure := errors.New("directory I/O failure")
|
||||
syncCalls := 0
|
||||
store.syncDirectory = func(dir string) error {
|
||||
syncCalls++
|
||||
if dir != filepath.Dir(path) {
|
||||
t.Errorf("EnsureRegistrationKey sync directory = %q, want state parent", dir)
|
||||
}
|
||||
return syncFailure
|
||||
}
|
||||
for range 2 {
|
||||
got, retryErr := store.EnsureRegistrationKey()
|
||||
if got != "" || !errors.Is(retryErr, syncFailure) {
|
||||
t.Errorf("EnsureRegistrationKey(unconfirmed directory) returned usable key or lost error: %v", retryErr)
|
||||
}
|
||||
}
|
||||
if syncCalls != 2 {
|
||||
t.Errorf("EnsureRegistrationKey(repeated failures) sync calls = %d, want 2", syncCalls)
|
||||
}
|
||||
store.syncDirectory = localfile.SyncDirectory
|
||||
confirmedKey, err := store.EnsureRegistrationKey()
|
||||
if err != nil || confirmedKey != key {
|
||||
t.Errorf("EnsureRegistrationKey(after successful sync) changed identity or failed: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,15 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/agent/localfile"
|
||||
)
|
||||
|
||||
const (
|
||||
stateDirPerm = 0o750
|
||||
stateFilePerm = 0o600
|
||||
nodeIDRandomBytes = 8
|
||||
stateDirPerm = 0o750
|
||||
stateFilePerm = 0o600
|
||||
nodeIDRandomBytes = 8
|
||||
registrationKeyBytes = 32
|
||||
)
|
||||
|
||||
// PagesDeployment records a Pages project tracked by the agent and the last
|
||||
@@ -31,6 +34,7 @@ type PagesDeployment struct {
|
||||
// Snapshot represents the state of the agent at a given point in time.
|
||||
type Snapshot struct {
|
||||
NodeID string `json:"node_id"`
|
||||
RegistrationKey string `json:"registration_key,omitempty"`
|
||||
CurrentVersion string `json:"current_version"`
|
||||
CurrentChecksum string `json:"current_checksum"`
|
||||
PagesDeployments []PagesDeployment `json:"pages_deployments"`
|
||||
@@ -47,15 +51,42 @@ type Snapshot struct {
|
||||
AccessLogOffset int64 `json:"access_log_offset"`
|
||||
}
|
||||
|
||||
// EnsureRegistrationKey persists the private retry credential before enrollment can reach the server.
|
||||
func (s *Store) EnsureRegistrationKey() (string, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
snapshot, err := s.loadUnlocked()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if snapshot.RegistrationKey != "" {
|
||||
// A previous rename may have succeeded even though its directory sync failed.
|
||||
if err = s.syncDirectory(filepath.Dir(s.path)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return snapshot.RegistrationKey, nil
|
||||
}
|
||||
key := make([]byte, registrationKeyBytes)
|
||||
if _, err = rand.Read(key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
snapshot.RegistrationKey = hex.EncodeToString(key)
|
||||
if err = s.saveUnlocked(snapshot); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return snapshot.RegistrationKey, nil
|
||||
}
|
||||
|
||||
// Store manages the storage and retrieval of the agent state snapshot.
|
||||
type Store struct {
|
||||
path string
|
||||
mu sync.Mutex
|
||||
path string
|
||||
mu sync.Mutex
|
||||
syncDirectory func(string) error
|
||||
}
|
||||
|
||||
// NewStore creates a new Store instance at the given path.
|
||||
func NewStore(path string) *Store {
|
||||
return &Store{path: filepath.Clean(path)}
|
||||
return &Store{path: filepath.Clean(path), syncDirectory: localfile.SyncDirectory}
|
||||
}
|
||||
|
||||
// Load loads the snapshot from the store.
|
||||
@@ -91,6 +122,29 @@ func (s *Store) EnsureNodeID() (string, error) {
|
||||
func (s *Store) Save(snapshot *Snapshot) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
current, err := s.loadUnlocked()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updated := *snapshot
|
||||
|
||||
// Other workers can hold a snapshot loaded before enrollment first persisted its key.
|
||||
if current.RegistrationKey != "" {
|
||||
updated.RegistrationKey = current.RegistrationKey
|
||||
updated.NodeID = current.NodeID
|
||||
}
|
||||
return s.saveUnlocked(&updated)
|
||||
}
|
||||
|
||||
// SetRegisteredNodeID commits the server-assigned identity without overwriting concurrent runtime state.
|
||||
func (s *Store) SetRegisteredNodeID(nodeID string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
snapshot, err := s.loadUnlocked()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
snapshot.NodeID = nodeID
|
||||
return s.saveUnlocked(snapshot)
|
||||
}
|
||||
|
||||
@@ -120,7 +174,7 @@ func (s *Store) saveUnlocked(snapshot *Snapshot) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(s.path, data, stateFilePerm)
|
||||
return localfile.WriteAtomic(s.path, data, stateFilePerm)
|
||||
}
|
||||
|
||||
func newNodeID() (string, error) {
|
||||
|
||||
Reference in New Issue
Block a user